var t;
var wroc = false;
var menuTimer;

function printIt( id, mod ) {
	width = 992;
	height = 600;
	printWindow = window.open('/strony/wersja-do-druku?oid='+ id +'&mod='+ mod,'printEpgWindow','width=' + width + ',height=' + height + ',scrollbars=yes,toolbar=no,menubar=no,status=no,location=no,personalbar=no,directories=no,dependent=yes,resizable=no');
	printWindow.moveTo((screen.width - width)/2,(screen.height - height)/2);
	printWindow.focus();
}

function przewinWideo(){
	clearTimeout(t);
	var next = $("#najnowsze-wideo .paginator li.active").next('li');
	var ostatni = $("#najnowsze-wideo .paginator li:last");

	if(wroc == true){
		$("#najnowsze-wideo .paginator li:first").click();
	}else{
		next.click();  
	}

	if(next.text() == ostatni.text()){
		wroc = true;    
	}else{
		wroc = false;
	}
}

function startRotatePromos(){
	window.setInterval("rotatePromos();", cycleTimeout);
}

function rotatePromos(){
	var last = $('#main-promo ul.buttons li:last').text();
	var el = $('#main-promo ul.buttons .active').text();
	el = eval(parseInt(el)+1);
	if(el > last){
		el = 1;
	}
	$('#main-promo .promo-box').css("display", "none");
	$('#main-promo ul.buttons li').removeClass("active");
	$("#promoB"+el).css("display", "block");
	$('#main-promo ul.buttons li.n'+el).addClass("active");	
}

function wydawniczy_link(link)
{
	window.location = link;
}

function startTwitter(){
	$("#twitter-text").getTwitter({
		userName: "sport_tvppl",
		numTweets: 1,
		loaderText: " ",
		slideIn: false,
		slideDuration: 750,
		showHeading: false,
		headingText: " ",
		showProfileLink: false,
		showTimestamp: false
	});
}

function removeActiveMenu(){
	var sfEls = document.getElementById("page_menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].className=sfEls[i].className.replace(new RegExp(" hover\\b"), "");
	}
}
function przewinWideo(){
	window.clearTimeout(t);
	var next = $("#wideo-trans-najnowsze .paginator li.active").next('li');
	var ostatni = $("#wideo-trans-najnowsze .paginator li:last");
	if(wroc == true){
		$("#wideo-trans-najnowsze .paginator li:first").click();
	}else{
		next.click();  
	}

	if(next.text() == ostatni.text()){
		wroc = true;    
	}else{
		wroc = false;
	}
}

function swap_local(enable, mainNode, containerNode){
	var node = dojo.query(mainNode+' '+containerNode);
	var tab = dojo.query(mainNode+' ul.tabs li');
	var ilosc = node.length;
	for(var t = 0; t < ilosc; t++){
		dojo.style(node[t], "display", "none");
		dojo.removeClass(tab[t],'tactive')
	}
	dojo.addClass(tab[enable],'tactive')
	dojo.style(node[enable], "display", "block");
}

function set_cal_active(obj){
	$('#kalendarium #calendariumBox table tr td a').removeClass('selected');
	$(obj).addClass('selected');

}

$(document).ready(function(){
	/*
	$('#kalendarium #calendariumBox table tr td a').hover(
		function(){
			$(this).addClass('activehover');
			return false;
		},
		function(){                
			$(this).removeClass('activehover');
			return false;
		}
	);
	*/	
	
	$('#wideo-trans-najnowsze .paginator li').hover(
		function(){
			$(this).addClass('activehover');
			return false;
		},
		function(){                
			$(this).removeClass('activehover');
			return false;
		}
	);			  
	$('#wideo-trans-najnowsze .paginator li').click(function(){
		t = window.setTimeout('przewinWideo()', 10000);
		var infoid = $(this).text();
		$('#wideo-trans-najnowsze .paginator li').removeClass('active');
		$(this).addClass('active');
		$('.wideo-tresc').hide();
		$('#popWideo' + infoid).show();
	});
	
	t = window.setTimeout('przewinWideo()', 10000);
						   
    if($("#galerie ul").children().size() > 2)
    {
        $("#galerie").jCarouselLite({   
        btnNext: "#galerie-next",
        btnPrev: "#galerie-prev"
        }); 
    }
 	if($("#twitter-bar").length){
		startTwitter();
		setInterval("startTwitter();", 30000);
	}
	if($("#page_menu").length){
		var sfEl = document.getElementById("page_menu");
		var sfEls = document.getElementById("page_menu").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				for (var ij=0; ij<sfEls.length; ij++) {
					sfEls[ij].className=sfEls[ij].className.replace(new RegExp(" hover\\b"), "");
				}
				this.className+=" hover";
			}
		}
		sfEl.onmouseout = function(){
			if(typeof(menuTimer != 'undefined')){
				clearTimeout(menuTimer);
			}
			menuTimer = setTimeout("removeActiveMenu()", 6000);
		}
	}
		
	if($("#page_menu_2").length){
		var sfEls2 = document.getElementById("page_menu_2").getElementsByTagName("LI");
		for (var i=0; i<sfEls2.length; i++) {
			sfEls2[i].onmouseover=function() {
				this.className+=" hover";
			}
			sfEls2[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" hover\\b"), "");
			}
		}
	}

	t = setTimeout(function() {przewinWideo()}, 10000);

	
	function normalALL(){
	  $("#aaa li").each( function() {
		  $(this).css({'text-decoration':'none'});
      });
	}
	
	$('#aaa #pierw').click(
	    function()	
	    {
	      normalALL();
	      $(this).css({'text-decoration':'underline'});
	      $('#wideo-news > *').removeClass('m2').removeClass('m0').addClass('m1');
	      $('#ser_mag_contener > *').removeClass('m2').removeClass('m0').addClass('m1');  
	      $('#ser_mag_tytul').removeClass('m2').removeClass('m0').addClass('m1');                  
	      return false;
	    }
	  );
	
	  $('#aaa #drugi').click(
	    function()
	    {
	      normalALL();
	      $(this).css({'text-decoration':'underline'});
	      $('#wideo-news > *').removeClass('m2').removeClass('m1').addClass('m0');
	      $('#ser_mag_contener > *').removeClass('m2').removeClass('m1').addClass('m0');        
	      $('#ser_mag_tytul').removeClass('m2').removeClass('m1').addClass('m0');            
	      return false;
	    }
	  );
	  $('#aaa #trzec').click(
	    function()
	    {
	      normalALL();
	      $(this).css({'text-decoration':'underline'});
	      $('#wideo-news > *').removeClass('m1').removeClass('m0').addClass('m2');
	      $('#ser_mag_contener > *').removeClass('m1').removeClass('m0').addClass('m2');
	      $('#ser_mag_tytul').removeClass('m1').removeClass('m0').addClass('m2');                       
	      return false;
	    }
	  );
	  
	$('#opcje-kom #posi a').click(function(){
		$('#podziel-sie').toggle();
		return false;
	});


            
	$('.info-menu li a').click(function(){
		var infoid = $(this).attr('infoid');
		$('.info-menu li a').removeClass('aktualny');
		$(this).addClass('aktualny');
		$('.informacje-tresc').hide();
		$('.informacje-wczesniejsze').hide();
		$('#popInfo' + infoid).show();
		$('#wczesniejsze-' + infoid).show();
		return false;
	});
      
	$("#kat-menu select").change(function(){
		var action = $('#sort-select').attr('action');
		action = action + '&pokaz=' + $(this).val();
		$('#sort-select').attr('action', action);
		$('#sort-select').submit();
	});

	$('#najnowsze-wideo .paginator li').hover(
		function(){
			$(this).addClass('activehover');
			return false;
		},
		function(){                
			$(this).removeClass('activehover');
			return false;
		}
	);
      
	$('#najnowsze-wideo .paginator li').click(function(){
		t = setTimeout(function() {przewinWideo()}, 10000);
		var infoid = $(this).text();
		$('#najnowsze-wideo .paginator li').removeClass('active');
		$(this).addClass('active');
		$('.wideo-tresc').hide();
		$('#popWideo' + infoid).show();
		return false;
	});
	
	$('#crawl #przesun').cycle({
		fx:     'scrollUp', 
		timeout: 6000, 
		delay:  -2000,
		cleartypeNoBg:  true
	});
	
	$('#naj_wideo_gora a').click(function(){
		$('.tabs a').removeClass('aktualny');
		$(this).addClass('aktualny');
		/*$('.naj_wideo_tresc').hide();*/
		$('#popWid0').hide();
		$('#popWid1').hide();
		$('#popWid2').hide();
		$('#popWid3').hide();
		var tab_id = $(this).attr('id').split('_');
		$('#popWid' + tab_id[1]).show();
		return false;
	});

	$('#usForm').submit(function(){
		$.ajax({
			type: "GET",
			url: "/pub/sess/usersurvey",
			data: $(this).serialize(),
				success: function(msg){
				$('#usForm').html(msg);
			}
		});
		return false;
	});

	$('#usForm .zobacz-wyniki').click(function(){
		$.ajax({
			type: "GET",
			url: $(this).attr('href'),
			success: function(msg){
				$('#usForm').html(msg);
			}
		});
		return false;
	});
  
	$('#w-spis a').click(function(){
		$('#w-spis a').removeClass('aktualny');
		$(this).addClass('aktualny');
		var katid = $(this).attr('id');
		$('.wydawniczy-news').hide();
		$('#Wydawniczy' + katid.replace(/katid_/i,"")).show();
		return false;
	});  
	
	if($("#main-promo").length){
		setTimeout("startRotatePromos();", cycleTimeout);
		$('#main-promo .buttons li').click(function(e){
			var el = this.innerHTML;
			$('.promo-box').css("display", "none");
			$('#main-promo .buttons li').removeClass("active");
			$("#promoB"+el).css("display", "block");
			$('#main-promo .buttons li.n'+el).addClass("active");
		});
		$('#main-promo .buttons li').hover(
		function(e){
				$('#main-promo .buttons li').css("cursor", "pointer");
		},
		function(f){
			$('#main-promo .buttons li').css("cursor", "default");
		});
	}
});

function redir(url) {if(url){document.location.href=''+url;}return false;}


/* Glosowanie */
function ratingDesc(rating) {
	if( document.getElementById('your_vote_desc')) {
		document.getElementById('your_vote_desc').innerHTML = rating;
	}
}

function ratingPurge() {
	if( document.getElementById('your_vote_desc')) {
		document.getElementById('your_vote_desc').innerHTML = '';
	}
}

function castVote(object_id, layer_id, vote_value) {
	var already_voted = false;
	var bindArgs={
		content:{
			action:'add',
			object_id:object_id,
			poutput:'xhtml'
		},
		timeout:axTimeout,
		url:'/pub/sess/vote?object_id='+object_id+'&vote_value='+vote_value,
		load:function(response,ioArgs) {
			
			var div=dojo.byId('voteID_'+layer_id);
			var t = response.indexOf('avg_vote');
			var d = response.indexOf('vote_value');
			if( t != -1 ) {				
			
				var vote_count = response.substring( t + 10, t + 12);
				var casted_vote = response.substring( d + 11, d + 12 );
			
				if ( vote_count < 10 ) { vote_count = 0; }
				if ( vote_count > 9 &&vote_count < 20 ) { vote_count = 10; }
				if ( vote_count > 19 &&vote_count < 30 ) { vote_count = 20; }
				if ( vote_count > 29 &&vote_count < 40 ) { vote_count = 30; }
				if ( vote_count > 39 && vote_count < 50 ) { vote_count = 40; }
				if ( vote_count > 49 ) { vote_count = 50; }
				
				if( dojo.query('#userVotesCount_' + layer_id + ' li.actual') ) {
					
					var s = dojo.query('#userVotesCount_' + layer_id + ' li.actual');
				
					s.removeClass('w10');
					s.removeClass('w20');
					s.removeClass('w30');
					s.removeClass('w40');
					s.removeClass('w50');
					s.addClass('w'+vote_count);
					
					var f = dojo.query('#voteID_' + layer_id + ' li.actual');
				
					f.removeClass('w10');
					f.removeClass('w20');
					f.removeClass('w30');
					f.removeClass('w40');
					f.removeClass('w50');
					f.addClass('w'+casted_vote+'0');
					
				} else {
					var s = dojo.query('#voteID_' + layer_id + ' li.actual');
				
					s.removeClass('w10');
					s.removeClass('w20');
					s.removeClass('w30');
					s.removeClass('w40');
					s.removeClass('w50');
					s.addClass('w'+vote_count);

				}
				
				voted_elements.push(object_id);
				
			}else {
				var st = response.indexOf('already_voted');
				if( st != -1) {
					div.innerHTML='';
				} else {
					div.style.color = "#ff0000";
					div.style.backgroundColor = "#ffffff";
					div.style.top = 0;
					div.style.paddingLeft = '3px';
					div.style.paddingRight = '3px';
					div.innerHTML = 'G�os ju� by� oddany';
				}
			}			
		},
		error:function(response,ioArgs){
			//console.error('HTTP status code: ',ioArgs.xhr.status);
		}
	};
	
	for(var i=0;i<voted_elements.length;i++) {
		if(voted_elements[i] == object_id) { already_voted = true; }
	}
	
	if( already_voted == false ) {
		dojo.xhrGet( bindArgs );
	}
	return false;
}


function checkTransmissionTime(){
	var actualDate = new Date();
	var switchDate = new Date();
	var switchDate2 = new Date();
	switchDate.setHours(12,00,00);
	switchDate2.setHours(20,00,00);
	var startTransmissionDate = new Date(2010, 7, 1, 12, 0, 0, 0);
	var stopTransmissionDate = 	new Date(2010, 8, 7, 20, 0, 0, 0);
	//$("#wideo-tab .buttons .tab").removeClass("active");
	//$("#wideo-tab .content").removeClass("active");
	if((actualDate.getTime() <= switchDate2.getTime() 
			&& actualDate.getTime() >= switchDate.getTime()) 
			&& actualDate.getTime() >= startTransmissionDate.getTime() 
			&& actualDate.getTime() <= stopTransmissionDate.getTime() ){
		return 0; //tab 0
	}else{
		return 1; //tab 1 		
	}
}

