var currentchartitem = 0;
var chartdata = {};
var previousPoint = null;
$(window).hashchange( function(){
	
    var url = (location.hash.replace("#",""));
    var url = url.split("/");

    if(url[1]){
     
     	if(url[1] == "list"){
     		
     		var id = url[2]

     		showChartItemMore(id);
     		window.location.href = location.hash;
     	}
    }
    else{
    
    	closeChartItemMore(currentchartitem);
    }
   
})
    
$(document).ready(function(){
    
    $(window).hashchange();
     
    $(".chart_item_header").click(function(){
    	
       	window.location.href = $(this).attr("href");
    });    
}); 


function showChartItemMore(id){
	

	
	if(currentchartitem > 0){
        	
        closeChartItemMore(currentchartitem);		
    }
			
	currentchartitem = id;	
	
	$("#chart_item_"+id+" .chart").addClass("active");
		
	if($("#chart_item_"+id+" .chart_item_content").html() == ""){
		
		$("#chart_item_"+id+" .chart_item_content").load("/list/info/"+id+"/"+chartpage,{},function(){
			
			$("#chart_item_"+id+" .chart_item_content").slideDown(300,function(){$('body').scrollTo("#chart_item_"+id, 300 );});
		})
	}
	else{
		$("#chart_item_"+id+" .chart_item_content").slideDown(300,function(){$('body').scrollTo("#chart_item_"+id, 300 );});
	}
	
	$("#chart_item_"+id+" .chart_item_more").attr("href","javascript:closeChartItemMore("+id+")").html("SLUIT");
	$("#chart_item_"+id+" .chart_item_header").attr("href","javascript:closeChartItemMore("+id+")");
}


function closeChartItemMore(id){
	
	$("#chart_item_"+id+" .chart_item_more").attr("href","javascript:closeChartItemMore("+id+")").html("MEER");
	$("#chart_item_"+id+" .chart_item_header").attr("href","javascript:showChartItemMore("+id+")");
	
	$("#tooltip").remove();
	previousPoint = null;   
	
	//$("#chart_item_"+id+" .chart_item_content").slideUp('fast');
	$("#chart_item_"+id+" .chart_item_content").html('').hide();
	
	$("#chart_item_"+id+" .chart").removeClass("active");
	
}


function showChartSongInfo(id){
	$("#tooltip").remove();
	previousPoint = null;
	$("#chart_item_"+id+" .chart_item_content .buttons a").removeClass("active");
	$("#chart_item_"+id+" .chart_item_content .buttons .song_info_button").addClass("active");
	$("#chart_item_"+id+" .chart_item_content .song_info").show();
	$("#chart_item_"+id+" .chart_item_content .artist_info").hide();
	$("#chart_item_"+id+" .chart_item_content .chart_info").hide();
	$("#chart_item_"+id+" .chart_item_content .reaction_info").hide();
}

function showChartArtistInfo(id){
	$("#tooltip").remove();
	previousPoint = null;
	$("#chart_item_"+id+" .chart_item_content .buttons a").removeClass("active");
	$("#chart_item_"+id+" .chart_item_content .buttons .artist_info_button").addClass("active");
	$("#chart_item_"+id+" .chart_item_content .song_info").hide();
	$("#chart_item_"+id+" .chart_item_content .artist_info").show();
	$("#chart_item_"+id+" .chart_item_content .chart_info").hide();
	$("#chart_item_"+id+" .chart_item_content .reaction_info").hide();
}

function showChartChart(id){
	
	
	
	$("#tooltip").remove();
	previousPoint = null;
	$(".buttons a").removeClass("active");
	$("#chart_item_"+id+" .chart_item_content .buttons .chart_info_button").addClass("active");
	$("#chart_item_"+id+" .chart_item_content .song_info").hide();
	$("#chart_item_"+id+" .chart_item_content .artist_info").hide();
	$("#chart_item_"+id+" .chart_item_content .chart_info").show();
	$("#chart_item_"+id+" .chart_item_content .reaction_info").hide();
	
	if($("#chart_item_"+id+" .chart_item_content .chart_info").attr("graph") == "false" && chartdata[id].length > 0){
		
		$("#chart_item_"+id+" .chart_item_content .chart_info").attr("graph") == "true"
		
		var items = chartdata[id];
		var data = [];
		var ticksx = [];
		var ticksy = [];
		
		//var ticksy = [[0,"50"],[10,"40"],[20,"30"],[30,"20"],[40,"10"],[50,"1"]]
		
		//fills array with y data
		
		
		
		for(var i=0;i<listcount;i = i+10){

			ticksy.push([i,(listcount - i)+""]);
		}
		

		
		ticksy.push([listcount,"1"]);
		

		var ymin = 0;
		
		//fill array with data
		for(var i=0;i<items.length;i++){
			
			var ditem = [];
			var itemname = "";
			var di = data.length;
			
			if(items[i-1]){
				if(items[i].week - items[i-1].week != 1 && items[i].week != 1 && !(items[i].week == 52 && items[i].year == 2010)){
					
					ditem = ([di,"","week"]);
					ticksx.push([di,"",""]);
					data.push(ditem);
				}
			}
			
			di = data.length;
			
			if(items[i].year){
				
				ditem = [];
				itemname = "'" + (items[i].year).substring(2,4) + "<br />w"+ items[i].week;
				
				ditem = ([di,(listcount +1) - parseInt(items[i].thisweek),"<strong>"+(items[i].thisweek) + "</strong> ("+ "'" + (items[i].year).substring(2,4) + " w"+ items[i].week +")"]);
				
				if(ditem[1] < 0){
					
					ymin = ditem[1];
				}
				
				data.push(ditem);
			}
			
			
			
			
			if(items.length < 15){
				
				ticksx.push([di,itemname]);
			}
			else if(i%2 == 0){
				
				ticksx.push([di,itemname]);
			}
		}
		
		if(ymin < 0){
			
			var dy = Math.ceil(-1*ymin / 10) *10;
			listcount =  dy + listcount;
			
			
			ticksy = [];
			
			for(var i=0;i<listcount;i = i+10){

				ticksy.push([i,(listcount - i)+""]);
			}
			
			for(var i=0;i<data.length;i++){
				
				data[i][1] = data[i][1] + dy;	
			}
			
		}
		
		
		
		var plot = $.plot("#chart_"+id,[{data:data,bars:{lineWidth:1,margin:1,show:true,fill:true,align:'center',fillColor:{ colors: ["rgba(255,128,128,1)","rgba(255,12,12,0.9)"] }},lines:{show:false},points:{show:false},color:'rgba(255,0,0,0.8)'}],{yaxis:{max:listcount + 5,min:0,ticks:ticksy},xaxis:{ticks:ticksx },grid: {hoverable: true, clickable: false,backgroundColor: { colors: ["#000", "#999"] }}});

		
		    		
		    		$("#chart_"+id).bind("plothover", function (event, pos, item) {
		    			
						if (item) {
		                	if (previousPoint != item.datapoint[0]) {
		                    	previousPoint = item.datapoint[0];
		                    
		                    	$("#tooltip").remove();
		                    	var x = item.datapoint[0].toFixed(2);
		                        var y = item.datapoint[1].toFixed(2);
		                    
		                    	showTooltip(item.pageX, item.pageY,item.series.data[item.dataIndex][2]);
		                    	
		                    	
		                	}
		
		       		 	}
						else {
		                	$("#tooltip").remove();
		                	previousPoint = null;            
		            	}
		    		});
	}
	
}

function showChartReaction(id){
	
	$("#tooltip").remove();
	previousPoint = null;  
	
	$(".buttons a").removeClass("active");
	$("#chart_item_"+id+" .chart_item_content .buttons .reaction_info_button").addClass("active");
	$("#chart_item_"+id+" .chart_item_content .song_info").hide();
	$("#chart_item_"+id+" .chart_item_content .artist_info").hide();
	$("#chart_item_"+id+" .chart_item_content .chart_info").hide();
	$("#chart_item_"+id+" .chart_item_content .reaction_info").show();
}

function showTooltip(x, y, contents) {
	$('<div id="tooltip">' + contents + '</div>').css( {
		            		textAlign: 'center',
		            		position: 'absolute',
		            		display: 'none',
		            		width: '80px',
		            		top: (y - 30)+"px",
		            		left: (x -40)+"px",
		            		border: '1px solid #fdd',
		            		padding: '2px',
		            		'background-color': '#fee',
		            		opacity: 0.80
		        		}).appendTo("body").fadeIn(200);
		    		}
		    		
		    		
		    		
/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

		    		
		    		
