jQuery(document).ready( function() {
	TwitterGraph.init();
	jQuery(".graph-statics-table").css("opacity","100");
});

var TwitterGraph = {
	init : function() {
		
		jQuery(".graph-statics-table").hide();
		
		jQuery(".graph-statics-table").css("opacity","0");

		var _self = this;
		
		this.dailyGraph = jQuery('#daily-graph');
		this.hourlyGraph = jQuery('#hourly-graph');
		this.weeklyGraph = jQuery('#weekly-graph');
		
		this.dailyLink = jQuery('#daily-trend-link');
		this.hourlyLink = jQuery('#hourly-trend-link');
		this.weeklyLink = jQuery('#weekly-trend-link');
		
		this.isDailyRendered = false;
		this.isHourlyRendered = false;
		this.isWeeklyRendered = false;
		
		
		if(simbolo!=''){
			this.initialShow = this.weeklyLink;
		} else {
			this.initialShow = this.hourlyLink;
		}

		//hourly graph
		if(_self.hourlyGraph.length){
			if(_self.hourlyGraph.is(':visible')){
				_self.graph(Properties.HOURLY_GRAPH_URL, _self.hourlyGraph, 60);
				_self.isHourlyRendered = true
			}
			if(_self.hourlyLink.length){
				_self.dailyLink.css('color','#2D2D2D');
				_self.dailyLink.css('font-weight','bold');
			}
		}
		
		//daily graph
		if(_self.dailyGraph.length){
			if(_self.dailyGraph.is(':visible')){
				this.graph(Properties.DAILY_GRAPH_URL, this.dailyGraph, 24);
				this.isDailyRendered = true;
			}
			if(_self.dailyLink.length){
				_self.dailyLink.css('color','#2D2D2D');
				_self.dailyLink.css('font-weight','bold');
			}
		}
		
		//weekly graph
		if(_self.weeklyGraph.length){
			var symbolParameter = Parameter.get('search');
			if (typeof symbolParameter != 'undefined' && symbolParameter!='') {
				symbolParameter = '&symbol='+symbolParameter;
			} else {
				symbolParameter='';
			}
			if(_self.weeklyGraph.is(':visible')){
				_self.graph(Properties.WEEKLY_BIS_GRAPH_URL + symbolParameter, _self.weeklyGraph, 7);
				this.isWeeklyRendered = true;
			}
		}
		
		//hourly link
		if (_self.hourlyGraph.length && _self.hourlyLink.length) {
			_self.hourlyLink.click(
					function() {
						
						jQuery(".graph-statics-table").hide();
						jQuery(".graph-statics-links a").css('color','#4871A8').css('font-weight','normal');
						
						_self.hourlyLink.css('color','#2D2D2D');
						_self.hourlyLink.css('font-weight','bold');
						
						_self.hourlyGraph.show();
						
						//lazy rendering only first time when requested
						if(!_self.isHourlyRendered){
							_self.graph(Properties.HOURLY_GRAPH_URL, _self.hourlyGraph, 60);
							_self.isHourlyRendered = true;
						}
					});
		}		
		
		//daily link
		if (_self.dailyGraph.length && _self.dailyLink.length) {
			_self.dailyLink.click(
					function() {
						
						jQuery(".graph-statics-table").hide();
						jQuery(".graph-statics-links a").css('color','#4871A8').css('font-weight','normal');
						
						_self.dailyLink.css('color','#2D2D2D');
						_self.dailyLink.css('font-weight','bold');
												
						_self.dailyGraph.show();
						
						if(!_self.isDailyRendered){
							_self.graph(Properties.DAILY_GRAPH_URL, _self.dailyGraph, 60);
							_self.isDailyRendered = true;
						}
												
					});
		}
		
		//weekly link
		if (_self.weeklyGraph.length && _self.weeklyLink.length) {
			_self.weeklyLink.click(
					function() {
						
						jQuery(".graph-statics-table").hide();
						jQuery(".graph-statics-links a").css('color','#4871A8').css('font-weight','normal');
						
						_self.weeklyLink.css('color','#2D2D2D');
						_self.weeklyLink.css('font-weight','bold');
						
						_self.weeklyGraph.show();
						
						//lazy rendering only first time when requested
						if(!_self.isWeeklyRendered){
							_self.graph(Properties.WEEKLY_BIS_GRAPH_URL, _self.weeklyGraph, 60);
							_self.isWeeklyRendered = true;
						}
						//_self.isHourlyRendered = true;						
						
					});
		}
		
		//jQuery(".graph-statics-table").hide();
		jQuery(".graph-statics-links a").css('color','#4871A8').css('font-weight','normal');
		
		_self.initialShow.click();
		
		

		
	},
	graph : function(url, table, timeline) {
		if(simbolo!=''){
			url=url.replace("?","?symbol="+simbolo);
			url=url.replace("callback","&callback");
		}
		jQuery
				.getJSON(
						url,
						function(data) {

							if (data.DATA && data.DATA.length) {
								for ( var row = 0; row < data.DATA.length; row++) {

									var symbol = data.DATA[row][0];
									var usageData = new Array();

									for ( var hour = row; hour < data.DATA.length; hour++) {
										var nextSymbol = data.DATA[hour][0];
										if (symbol == nextSymbol) {
											var min = data.DATA[hour][2];
											var minValue = data.DATA[hour][1];
											
											if(min < timeline)
												usageData[min] = minValue;
											
										} else {
											break;
										}
										row = hour;
									}

									var twittsCount = 0;
									for ( var hour = 0; hour < timeline; hour++) {
										if (usageData[hour] == undefined) {
											usageData[hour] = 0;
										}
										twittsCount = twittsCount
												+ usageData[hour];
									}

									var text = jQuery('<td class="graph-statics-text"></td>');
									var span = jQuery('<td class="graph-statics-view"></td>');
									var count = jQuery('<td class="graph-statics-count"></td>');
									count.text(twittsCount);
									
									var href = jQuery('<a href="#"></a>');
									href.attr('class','graph-symbol-a');
									href.text(symbol);
									
									text.append(href);

									var div = jQuery('<tr class="graph-statics"></tr>');
									div.append(text);
									div.append(span);
									div.append(count);

									table.append(div);

									span.sparkline(usageData, {
										type : 'line',
										barColor : '#0066cc',
										height : '15px',
										width : 150,
										lineColor : '#00f',
										spotRadius : 0
									});

									usageData = null;
								}
								
								jQuery('.graph-symbol-a').click(function(){
									Search.symbolPage(jQuery(this).text());
								});
							}
						});
	}
};


