function TTQuery(){
	var that = this;
	this.q = new GAQuery();
	this.q.callback = function(results){that.callback(results);};
	this.q.errorHandler = function(e){that.errorHandler(e);};
	this.q.setOptions(['dimensions=ga:pagePath,ga:date','sort=ga:date','metrics=ga:pageviews','filters=ga:pagePath=@ref=tre','start-date=2010-04-01','end-date=2012-04-26','max-results=10000']);
	var s = 'http://www.etsy.com/treasury/search/' + userInfo.getUserName();
	controller.printMsg('Etsy has now launched their Treasury Search.  You may view the Treasuries you are in here:<br/><a href="' + s + '" target="_blank">' + s + '<a/>');
	controller.main.innerHTML += '<br />'
}


TTQuery.prototype.createDate = function(date){
	return new Date(date.substring(0,4),date.substring(4,6)-1,date.substring(6,8));
	
};

TTQuery.prototype.getTresId = function(url){
	var pos = url.indexOf('ref=tre-');
	var start,end;
	if (pos != -1)
	{
		start = pos+8;
		end = url.indexOf('-',start);
		if (end == -1){
			end = url.length;
		}
		return url.substring(start,end);
	}
	return '';
};


TTQuery.prototype.drawTable = function(){
	
	var str = '<div class="box">';
	str += '<h1  style="text-align:center;font-weight:bold;font-size:18px">' + userInfo.getUserName() + ' has tracks leading to ' + this.treasuries.length + ' treasuries</h1>'; 
	str += '<p style="margin:0px;text-align:left;font-weight:bold;font-size:10px">Click on a header to sort by that column</p>';
	str += '<div style="margin-left:auto;margin-right:auto;width:570px" id="table_div"></div></div>';
	
	controller.main.innerHTML += str;
	
	
	
	var data = new google.visualization.DataTable(); 
        data.addColumn('date', 'First Clicked');
		data.addColumn('date', 'Last Clicked');
        data.addColumn('string', 'Item and Treasury ID'); 
        data.addColumn('number', 'Total Clicks');
        data.addRows(this.treasuries.length); 
	
	for (var i = 0; i < this.treasuries.length; i++){
		var title = this.listingIds[this.treasuries[i].listingId].title;
		var url = this.listingIds[this.treasuries[i].listingId].url;
		if (title.length > 25){
			title = title.substr(0,24) + '...';
		}
		data.setCell(i, 0, this.createDate(this.treasuries[i].firstClick));
		data.setCell(i, 1, this.createDate(this.treasuries[i].lastClick));
        data.setCell(i, 2, '<img alt="' + title + '" style="float:left;width:50px;height:50px" src="' + url + '"/><p style="text-align:left;font-weight:bold;font-size:10px">' + title + '</p><p style="margin:0px;text-align:left;font-size:8px" >Treasury:</p><p style="margin:0px;text-align:left;font-size:10px"><a style="padding:0px" target="_blank" href="http://www.etsy.com/treasury/' + this.treasuries[i].id + '">' + this.treasuries[i].id + '</a></p>'); 
        data.setCell(i, 3, this.treasuries[i].pageviews, null, {className: 'clicks cell'});
	}
	
	var table = new google.visualization.Table(document.getElementById('table_div')); 
	
    table.draw(data, {showRowNumber: false,allowHtml:true,width:'570px',cssClassNames:{headerRow: 'headerCells',headerCell:'headerCells',tableCell:'cell'}});
	
	supA.print();

};

TTQuery.prototype.etsyCallback = function(data){
	this.count++;
	
	if (data.ok) {
		var results = data.results;
		var id = "" + results[0].listing_id;
		this.listingIds[id].title = results[0].title;
		this.listingIds[id].url = results[0].image_url_75x75;
	}
	
	if (this.count >= this.treasuries.length){
		this.drawTable();
	}
};

TTQuery.prototype.callback = function(results){
	 
	var ids = {};
	this.listingIds = {};
	var listingId;
	this.treasuries = [];
	
	var entries = results.feed.getEntries();
	
	if (entries.length <= 0){
			var str = 'Treasury East has no tracks leading to ' + userInfo.getUserName() + ' at this time.';
			controller.printMsg(str);
			supA.print();
			return;
		}
	
	var that = this;
	var f = function(data){
			that.etsyCallback(data);
			};
	
	this.count = 0;
	for (var i = 0; i < entries.length; i++){
			var pagePath = entries[i].getValueOf('ga:pagePath');
			var id = this.getTresId(pagePath);
			
			if (ids[id]){
				ids[id].pageviews += entries[i].getValueOf('ga:pageviews');
				ids[id].lastClick = entries[i].getValueOf('ga:date');
				continue;
			}
			
			//listingIds.push(SLQuery.getItemId(pagePath));
			listingId = SLQuery.getItemId(pagePath);
			
			var t = {'listingId':listingId,'firstClick':entries[i].getValueOf('ga:date'),'lastClick':entries[i].getValueOf('ga:date'),'id':id,'pageviews':entries[i].getValueOf('ga:pageviews')};
			ids[id] = t;
			this.listingIds[listingId] = {'title':'unkown','url':'images/blank_item.gif'};
			this.treasuries.push(t);
			
			this.q2 = new ETSYQuery(1,'/listings/',listingId,'detail_level=low');
			
			this.q2.callback = f;
			this.q2.query();
			
			
	}
			
	
};

TTQuery.prototype.errorHandler = function(e){
	etsyGoogle.handleError(e);
};



