/** 20080919 */
var wrongEmail = false;
var checkedRules = false;
var valid = false;
var axTimeout=29000;
var voted_elements = [];

function createCookie(name,value,days){if(days){var date=new Date();date.setTime(date.getTime()+(days*24*60*60*1000));var expires="; expires="+date.toGMTString();}else{var expires='';}document.cookie=name+"="+value+expires+"; path=/; domain="+document.location.hostname.replace(/([a-zA-Z0-9-_.]+)\.([a-zA-Z0-9-_.]+)(?=\.)/g,"$2");}
function readCookie(name){var nameEQ=name+"=";var ca=document.cookie.split(';');for(var i=0;i < ca.length;i++){var c=ca[i];while(c.charAt(0)==' '){c=c.substring(1,c.length);}if(c.indexOf(nameEQ)==0){return c.substring(nameEQ.length,c.length);}}return null;}
function readCookieDV(name,defVal) {return readCookie(name)!=null ? readCookie(name) : defVal;}
function eraseCookie(name){createCookie(name,'',-1);}

function getPos(obj){var l=t=w=h=0;if(typeof obj=='string'){obj=document.getElementById(obj);}if(obj.offsetParent){w=obj.offsetWidth;h=obj.offsetHeight;do{l+=obj.offsetLeft;t+=obj.offsetTop;}while(obj=obj.offsetParent);}return [l,t,w,h];}

function getPageSize(){
	var xScroll, yScroll;
	if(window.innerHeight && window.scrollMaxY){xScroll=window.innerWidth + window.scrollMaxX;yScroll=window.innerHeight + window.scrollMaxY;}
	else if (document.body.scrollHeight > document.body.offsetHeight){xScroll=document.body.scrollWidth;yScroll=document.body.scrollHeight;}
	else {xScroll=document.body.offsetWidth;yScroll=document.body.offsetHeight;}
	var windowWidth, windowHeight;
	if (self.innerHeight){
		if(document.documentElement.clientWidth){windowWidth=document.documentElement.clientWidth;}
		else {windowWidth=self.innerWidth;}
		windowHeight=self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight){windowWidth=document.documentElement.clientWidth;windowHeight=document.documentElement.clientHeight;}
	else if (document.body){windowWidth=document.body.clientWidth;windowHeight=document.body.clientHeight;}
	if(yScroll < windowHeight){pageHeight=windowHeight;} else {pageHeight=yScroll;}
	if(xScroll < windowWidth){pageWidth=xScroll;} else {pageWidth=windowWidth;}
	return [pageWidth,pageHeight];
}

var selecter=function(obj,id,els){
	var divs=dojo.query('.'+els),tab=obj.parentNode.parentNode.getElementsByTagName('li');
	for(var i=0; i<divs.length;i++){
		divs[i].style.display='none';
	}
	for(var i=0; i<tab.length;i++){
		dojo.removeClass(tab[i],'act');
	}
//	dojo.query('.'+els).style.display='none';
	dojo.byId(id).style.display='block';
	dojo.addClass(obj.parentNode,'act');
}

function changeFontSize(toChange) {
	var obj=dojo.byId('contentNews');
	createCookie('TVPFontSize',toChange,1);
	if(obj){if(toChange==-1){obj.style.fontSize='90%';}else if(toChange==1){obj.style.fontSize='145%';}else{obj.style.fontSize='100%';}}
}

function tfDv(iobj) {if(iobj.value==''){iobj.value=iobj.defaultValue;}else if(iobj.value==iobj.defaultValue){iobj.value='';}else{return;}}

function redir(url) {if(url){document.location.href=''+url;}return false;}

function sDiv(divID) {obj = document.getElementById(divID); if (obj) {obj.style.display = 'block';}}

function hDiv(divID) {obj = document.getElementById(divID); if (obj) {obj.style.display = 'none';}}

function shDiv(divID) {obj = document.getElementById(divID); if (obj) {obj.style.display = obj.style.display=='block' ? 'none' : 'block';}}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}


function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto";
    var docHt = getDocHeight(iframeWin.document);
    if (docHt) iframeEl.style.height = docHt + 30 + "px";
  }
}

var nWindow='';
var oWindow='';

function oWp(url,winName) {
	var a=oWp.arguments;
	url=(a[0]&&a[0]!='')?a[0]:'?';
	winName=(a[1]&&a[1]!='')?a[1]:'iTVPpp';
	var param=(a[2]&&a[2]!='')?a[2]:'width=774,height=710';
	param+=(a[3]&&a[3]!='')?a[3]:',toolbar=no,minimize=no,status=no,memubar=no,location=no,scrollbars=no,resizable=no,scrollbars=no';
	if(!nWindow.closed && nWindow.name==winName){nWindow.close();}
	if (!nWindow.closed && nWindow.location) {nWindow.location.href=url;}
	else {nWindow=window.open(url,winName,param); }// if (!nWindow.opener) nWindow.opener=self;}
	if (window.focus) {nWindow.focus();}
	return false;
}

var showWindow = 1;
var highlightTab = 0;
var cycleTimeout = 10000;

function cycleTabs() {
	var tabs = dojo.query('#wydawca .tabs li');
	if( showWindow == tabs.length ) {showWindow = 1;highlightTab = 0;} 
	else { for( var i=0; i<tabs.length; i++ ) {if( tabs[i].className == 'act' ) {highlightTab += 1;showWindow += 1;}}}
	for( var j=0;j<tabs.length;j++ ) { dojo.removeClass( tabs[j], 'act' ); document.getElementById('wydItem'+(j+1)).style.display = 'none'; }
	dojo.addClass(tabs[highlightTab],'act');document.getElementById('wydItem'+showWindow).style.display = 'block';
	setTimeout('cycleTabs()',cycleTimeout);
}

function displayErrorMessage( where ) {
	switch( where ) {
		case 'required':
			document.getElementById('rules_required').innerHTML = "To pole jest wymagane";
			document.getElementById('rules_required').style.display = 'block';
			break;
		case 'email':
			document.getElementById('wrong_email').innerHTML = "Wpisz poprawny adres e-mail";
			document.getElementById('wrong_email').style.display = 'block';
			break;
		case 'empty_email':
			document.getElementById('wrong_email').innerHTML = "Musisz podać adres e-mail";
			document.getElementById('wrong_email').style.display = 'block';
			break;
	}
}

function removeErrorMessage( where ) {
	switch( where ) {
		case 'required':
			document.getElementById('rules_required').innerHTML = "";
			document.getElementById('rules_required').style.display = 'none';
			break;
		case 'email':
			document.getElementById('wrong_email').innerHTML = "";
			document.getElementById('wrong_email').style.display = 'none';
			break;
	}
}

var timer=false;


var newsletter = {

	checkForm: function(f) {
		newsletter.validateEmail( f.email.value );
		newsletter.validateRules(f);
		if( !wrongEmail && checkedRules ) {
			return true;
		} else {
			return false;
		}
	},

	validateEmail: function( email ) {
		var emailRegex = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if( email != '' ) {
			if ( !emailRegex.test( email ) ) {
				displayErrorMessage('email');
				wrongEmail = true;
			} else {
				removeErrorMessage('email');
				wrongEmail = false;
			}
		} else {
			displayErrorMessage('empty_email');
			wrongEmail = true;
		}
	},
	
	validateRules: function(fi) {
		checkedRules = fi.regulamin.checked;
		if( !checkedRules ){
			displayErrorMessage('required');
			checkedRules = false;
		} else {
			removeErrorMessage( 'required');
			checkedRules = true;
		}
	},
	
	boxMail: function(el) {
		var s = document.getElementById( 'newsletter_box_email' );
		if( s.value == 'Podaj swój adres email:' ) {
			s.value = '';
		}
	}
}

function showContactDetails(element) {
	element.nextSibling.style.display=(element.nextSibling.style.display == 'none')?'block':'none';
	var elementHTML = element.innerHTML;
	var sig = elementHTML.substring(elementHTML.length - 6, elementHTML.length - 5);
	(sig == '+')?element.innerHTML = elementHTML.replace('+','-'):element.innerHTML = elementHTML.replace('-','+');
	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 printIt( id, mod ) {
	width = 992;
	height = 600;
	printWindow = window.open('http://sport.tvp.pl/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 enableEditorial() {document.getElementById('redakcja').disabled = false;}
function disableEditorial() {document.getElementById('redakcja').disabled = true;}
function NPB(){return;}
function NJB(){return false;}

function init(){
	dojo.query('.item').onmouseover(function(e){dojo.addClass(this,'hover')});
	dojo.query('.item').onmouseout(function(e){dojo.removeClass(this,'hover')});
	dojo.query('#formSearch').onmouseover(function(e){dojo.addClass(this,'hover');});
	dojo.query('#formSearch').onmouseout(function(e){dojo.removeClass(this,'hover')});
	var result=dojo.query('#wydawca .tabs li');
	if(result.length>0){setTimeout( 'cycleTabs()', cycleTimeout);}
	if(document.getElementById('katalogPop')){
		var pos=getPos(dojo.byId('mtKatalog'));
		dojo.style('katalogPop','left',(pos[0] - 127) +'px');
		dojo.style('katalogPop','top',pos[1]+'px');
		dojo.query('#mtKatalog').onmouseover(function(e){if(timer){window.clearTimeout(timer)} dojo.style('katalogPop','display','block')});
		dojo.query('#katalogPop').onmouseover(function(e){if(timer){window.clearTimeout(timer)}});
		dojo.query('#mtKatalog').onmouseout(function(e){timer=window.setTimeout("dojo.style('katalogPop','display','none')",250)});
		dojo.query('#katalogPop').onmouseout(function(e){timer=window.setTimeout("dojo.style('katalogPop','display','none')",250)});
	}
	if(document.getElementById('newsletter')){
		newsletter.validateEmail(document.getElementById('email').value);
	}
	if(document.getElementById('refUrlSSO')){
		var refUrlSSO = document.getElementById('refUrlSSO');
		var refUrlSSOTarget = document.location.href;
		refUrlSSOTarget = refUrlSSOTarget.replace(/^http:\/\//i, "");
		refUrlSSO.value = refUrlSSOTarget;
	}
//	executeOnLoadEvents();
}

function process_live_transmission() {
	dojo.xhrGet({
			url: "http://sport.tvp.pl/pub/stat/details?object_id=4876131&xslt=tvp-sport/strony/tdp2011.ajax.xslt",
			load: function(result) {
				var str = "<p class=\"live-transmission-paragraph\">" + result.replace(/^$/gm, "</p><p class=\"live-transmission-paragraph\">") + "</p>";
				dojo.byId('TourDePologne2011LiveContener').innerHTML = str;
				var reg = /^\s*$/;
				dojo.query('#TourDePologne2011LiveContener p').forEach(function(node, index, array){
					if(reg.test(node.innerHTML)) {
						dojo.destroy(node);
					}
				});
			}
	});
}

dojo.addOnLoad(init);

