var data_sets

$(document).ready(function(){
	this_page_load();
});

function this_page_load(){

	if($('#match_dates select').length > 0){
		$('#match_dates select').selectToUISlider({
			labels: 2, sliderOptions: {
				stop: function(e, ui){
				update_league_table(ui.value)
			}}
		});
	}

	$("#table_view a").click(function(){
		$("#league_table table").animate({"marginLeft": "-470px"}, "slow");
		$(this).addClass('selected');
		$("#graph_view a").removeClass('selected');
		$("div#graph_result").hide();
	});
	
	$("#graph_view a").click(function(){
		if(!data_sets){
			$.getJSON(location.href+"/team_positions.json", {}, function(data){
				data_sets = data
				plot_team_positions();
			});
		}
		$("#league_table table").animate({"marginLeft": "0px"}, "slow");
		$(this).addClass('selected');
		$("#table_view a").removeClass('selected');
		$("div#graph_result").fadeTo("slow", 0.8).show();
		;
	});	
	
}

function update_league_table(date){
	selected = $("td.tick").find("input:checked");
	$("#league_table").load($("form#match_dates").attr("action") + '?' +$("form#match_dates").serialize(), function(){
		$.each(selected, function(key, val){
			$("input[name="+$(val).attr("name")+"]").attr('checked', true);
		});
		if(data_sets){
			plot_team_positions();
		}
		if($("#graph_view a").hasClass("selected")){
			$("#league_table table").css("marginLeft", "0px");
		}
	});
}

function plot_team_positions(){
	
    var i = 0;
    $.each(data_sets, function(key, val) {
			val.color = i;
	    ++i;
	  });
	 	
		$("#graph").css('height', $("#league_table table tbody").height());
		$("td.tick").find("input").click(plot_selected);
	
		function plot_selected() {
   		
			var data = [];
      $("td.tick").find("input:checked").each(function () {
	    	var key = $(this).attr("name");
	      if(key && data_sets[key]){
	      	data.push(data_sets[key]);
				}
	    });
			
			$.plot($("#graph"), data, {
     		yaxis: { min: 0, max: ($("td.tick").length - 1) },
       	xaxis: { tickDecimals: 0, ticks: null, max: ($("a#handle_date").attr("aria-valuenow")) },
				points: { show: true },
				lines: { show: true },
				grid: { clickable: true, 
								hoverable: true,  
								tickColor: "#FFF",
								labelMargin: 1,
					    	borderWidth: 0 },
				legend: { show: false }
     	});

	  }
	
		$("#graph").bind("plotclick", function (event, pos, item) {
			if(item) {
				$(".box:first").position('left');
				var team_id = item.series.label;
				var date = $("select#date option").eq(item.dataIndex).attr("value");
				$.getJSON(location.href+"/get_result?team_id="+team_id+"&date="+date, function(data){
					if(data){
						var match = data.match;
						$("div#graph_result").html(
							match.home_team.name + " <strong>" +
							match.home_team_score + "</strong> - <strong>" + 
							match.away_team_score + "</strong> " + 
							match.away_team.name
						);
					}
					else{
						$("div#graph_result").html("Team did not play on this date");					
					}
				});
				
		  }
		});
	
		plot_selected();

}