/*
 * Author : Cvetozar Ninov
 * Email  : cninov@gmail.com
 * */
var tabs = ["formations","live_comment"];
$(document).ready(function(){
	$(function() {
		$("#slider-ul").slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: 100,
			value: 100,
			slide: handleSlide,
			change : handleChange
		});
	});
	
	$(function() {
		$("#timing_slider").slider({
			orientation: "vertical",
			range: "min",
			min: 0,
			max: 100,
			value: 100,
			slide: handleSlide,
			change : handleChange
		});
	});
	
	function handleChange(e, ui) {
		var element = $(this).next();
		var val = 100-ui.value;
		var maxScroll = element.attr("scrollHeight")-element.height();
		maxScroll = maxScroll/100;
	    element.attr({  scrollTop:val*(maxScroll) });
	  
	}
	
	function handleSlide(e, ui) {
		var element = $(this).next();
		var val = 100-ui.value;
		var maxScroll = element.attr("scrollHeight")-element.height();
		maxScroll = maxScroll/100;
	    element.attr({  scrollTop:val*(maxScroll)   });
	}
	
	$(".main li.expandable").click(function(){
		$("span.expand",this).toggleClass("expanded");
		$('ul',$(this)).toggle();
	});
	
	
	$('.tab_wrapper h2').click(function(){
		var index =$('.tab_wrapper h2').index($(this));		
		$("."+tabs[1-index]).hide();
		$("."+tabs[index]).show();	
		$('.tab_wrapper h2.active').removeClass('active');
		$(this).addClass('active');
		window.location.href=url+"#"+tabs[index];
	});
	
	
	$('.latest_matches h6').click(function(){
		index = $('.latest_matches h6').index(this);
		$('.latest_matches ul').hide();
		$('.latest_matches ul:eq('+index+')').show();
		$('.latest_matches h6.active').removeClass('active');
		$(this).addClass('active');
	});
	
	$('#standings h2').click(function(){
		var index = $('#standings h2').index(this);
		
		if(index==1) {
			$("#formations_links").hide();
		}
		else {
			$("#formations_links").show();
		}
		
		$('#standings table').hide();
		$('#standings table:eq('+index+')').show();
		$('#standings h2.active').removeClass('active');
		$(this).addClass('active');
	});
	
	$("#formations_links a").click(function(){
		$("#formations_links a.active").removeClass("active");
		$(this).addClass("active");
		var act = $(this).attr("id").replace("f_","");
		$.get(_root+'xhr',{method:"stadings_info",id: match_id,act: act},function(data){
			$('.current_team').removeClass('current_team');
			for(n in data)
			{
				var ind=parseInt(n)+1;
				if(data[n].current_team==1) {
					$("#standings table:first tr:eq("+ind+")").addClass('current_team');
				}
				
				$("#standings table:first tr:eq("+ind+") td:eq(1)").html(data[n].team_title);
				$("#standings table:first tr:eq("+ind+") td:eq(2)").html(data[n].matches);
				$("#standings table:first tr:eq("+ind+") td:eq(3)").html(data[n].wins);
				$("#standings table:first tr:eq("+ind+") td:eq(4)").html(data[n].draws);
				$("#standings table:first tr:eq("+ind+") td:eq(5)").html(data[n].loses);
				$("#standings table:first tr:eq("+ind+") td:eq(6)").html(data[n].goal_difference);
				$("#standings table:first tr:eq("+ind+") td:eq(7)").html(data[n].points);
			}
			
			$("tr:odd:not(.current_team)","#standings table").css('background','white');
		},'json');
	});
	
	
	
	$('.expectations em').click(function(){
			$('em',$(this).parent()).removeClass("selected");
			$(this).addClass("selected");
			var em_index = $('.expectations em').index(this);
			$.get(_root+'xhr',{method:"prognose_match", id: match_id , em : em_index},function(data){
				window.location.reload();
			});
			
	});
	
	
	


		
	$(".expand",$("li#match_"+match_id).parent().parent()).trigger("click");
	
	
	
	var last_index = location.href.lastIndexOf("#");
	
	if(last_index!=-1) {
		t = location.href.substr(last_index+1);
		var opn = (t == 'formations') ? 0 : 1; 
		$(".tab_wrapper h2:eq("+opn+")").trigger("click");
	}
	else if(show_live == 1) {
		$(".tab_wrapper .right_tab").trigger("click");
	}
});
