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;
}

var countdown_images;

function open_window(href,name,param) {
 if(!name) {name='w_'+parseInt(Math.random()*1000000);}
 if(!param) {param='status=yes,toolbar=yes,location=yes,resizable=yes,scrollbars=yes,menubar=yes';}
 window.open(href,name,param);
}

function externalLinks() { 
 if(!document.getElementsByTagName) {return;} 
 var anchors = document.getElementsByTagName("a"); 
 for(var i=0; i<anchors.length; i++) { 
  var anchor=anchors[i]; 
  if(anchor.getAttribute("href") && anchor.getAttribute("rel")=="external") {
   anchor.target='w_'+parseInt(Math.random()*1000000);
	}
 } 
} 

function make_countdown(end,id,path,ext,time) {
 //time=parseInt($time()/1000);
 timeleft=end-time;
 digits='';
 if(timeleft>8640000) {
	html='<b class="counterText">Aikaa j&auml;ljell&auml; kilpailun alkuun:</b><br />';
	html+='<br />';
	html+='<div style="font-size: 2em; font-weight: bold"> '+parseInt(timeleft/86400+1)+' p&auml;iv&auml;&auml;!</div>';
	document.getElementById(id).innerHTML=html;
	document.getElementById(id).style.display='block';
 }
 else if(timeleft>0) {
	if(!countdown_images) {
	 countdownimg=new Asset.images([path+'0'+ext,path+'1'+ext,path+'2'+ext,path+'3'+ext,path+'4'+ext,path+'5'+ext,path+'6'+ext,path+'7'+ext,path+'8'+ext,path+'9'+ext,path+'separator'+ext]);
	 countdown_images=1;
	}
	seconds=timeleft%60;
	minutes=((timeleft-seconds)/60)%60;
	hours=((timeleft-minutes*60-seconds)/3600)%24;
	days=(timeleft-hours*3600-minutes*60-seconds)/86400;
	days='00'+days+'';
	days=days.substr(days.length-2,2);
	hours='00'+hours+'';
	hours=hours.substr(hours.length-2,2);
	minutes='00'+minutes+'';
	minutes=minutes.substr(minutes.length-2,2);
	seconds='00'+seconds+'';
	seconds=seconds.substr(seconds.length-2,2);
  digits=days+hours+minutes+seconds+'';
	html='<b class="counterText">Aikaa j&auml;ljell&auml; kilpailun alkuun:</b><br />';
	for(i=0; i<8; i++) {
	 if(i!=0 && (i%2)==0) {
	  html+='<img src="'+path+'separator'+ext+'" alt="" />';
	 }
	 html+='<img src="'+path+digits.substr(i,1)+ext+'" alt="" id="count_'+id+'_'+i+'" />';
	}
	document.getElementById(id).innerHTML=html;
	document.getElementById(id).style.display='block';
	do_countdown(digits,id,path,ext);
 }
}

function do_countdown(d,id,path,ext) {
 date1=new Date();
 digits='00000000'+d+'';
 digits=digits.substr(digits.length-8,8);
 c=[];
 d=[];
 for(i=0; i<8; i++) {
  c[i]=parseInt(digits.substr(i,1));
  d[i]=c[i];
 }
 if(d[7]==0) {
  d[7]=9;
	if(d[6]==0) {
	 d[6]=5;
	 if(d[5]==0) {
	  d[5]=9;
		if(d[4]==0) {
		 d[4]=5;
		 if(d[3]==0) {
		  if(d[0]==2) {
			 d[3]=3;
			}
			else {
			 d[3]=9;
			}
			if(d[2]==0) {
			 d[2]=2;
			 if(d[1]==0) {
			  d[1]=9;
				if(d[0]==0) {
				 d[0]=9;
				}
				else {
				 d[0]--;
				}
			 }
			 else {
			  d[1]--;
			 }
			}
			else {
			 d[2]--;
			}
		 }
		 else {
		  d[3]--;
		 }
		}
		else {
		 d[4]--;
		}
	 }
	 else {
	  d[5]--;
	 }
	}
	else {
	 d[6]--;
	}
 }
 else {
  d[7]--;
 }
 digits='';
 numsum=0;
 html='';
 for(i=0; i<8; i++) {
  digits+=d[i]+'';
	numsum+=d[i];
  if(d[i]!=c[i]) {
	 document.getElementById('count_'+id+'_'+i).src=path+d[i]+ext;
	}
 }
 if(numsum>0) {
  date2=new Date();
  setTimeout('do_countdown("'+digits+'","'+id+'","'+path+'","'+ext+'")',1000+date1.getTime()-date2.getTime());
 }
 else {
	document.getElementById(id).innerHTML='<br /><b style="color: #000000">Kilpailu on jo alkanut!</b><br />';
 }
}


var sponsor_anim=readCookie('spanim');
if(sponsor_anim==null) {sponsor_anim=1;} // jos ei asetettu ==> animoi

function sponsorFade2() {
 var sponsoritFX_in=new Fx.Tween($('sponsorit_img'),{
 		 'duration': 250,
		 'link': 'chain',
		 'onComplete': function() {
		   sponsoritFX_show.start('opacity', 1);
		 }
 });
 var sponsoritFX_show=new Fx.Tween($('sponsorit_img'),{
 		 'duration': 5000,
		 'link': 'chain',
		 'onComplete': function() {
		   sponsoritFX_out.start('opacity', 0);
		 }
 });
 var sponsoritFX_out=new Fx.Tween($('sponsorit_img'),{
 		 'duration': 250,
		 'link': 'chain',
		 'onComplete': function() {
		   sponsor_current=(sponsor_current+1)%sponsor_total;
			 $('sponsorit_img').src=sponsorit[sponsor_current][0];
		   $('sponsorit_a').href=sponsorit[sponsor_current][1];
			 sponsoritFX_in.start('opacity', 1);
		 }
 });
 //$('sponsorit_img').setStyle('opacity',1);
 //$('sponsorit_img').setStyle('width',350);
 sponsoritFX_show.start('opacity', 1);
}

function sponsorFade() {
 var sponsoritFX_in=new Fx.Morph($('sponsorit_img'),{
 		 'duration': 250,
		 'link': 'chain',
		 'onComplete': function() {
		   sponsoritFX_show.start({'opacity': [1,1]});
		 }
 });
 var sponsoritFX_show=new Fx.Morph($('sponsorit_img'),{
 		 'duration': 5000,
		 'link': 'chain',
		 'onComplete': function() {
		   if(sponsor_anim){
			   sponsoritFX_out.start({'opacity': [1,0], 'width': [175,375], 'height': [45,0]});
			 }
		 }
 });
 var sponsoritFX_out=new Fx.Morph($('sponsorit_img'),{
 		 'duration': 250,
		 'link': 'chain',
		 'onComplete': function() {
		   sponsor_current=(sponsor_current+1)%sponsor_total;
			 $('sponsorit_img').src=sponsorit[sponsor_current][0];
		   $('sponsorit_a').href=sponsorit[sponsor_current][1];
			 sponsoritFX_in.start({'opacity': [0,1], 'width': [375,175], 'height': [90,45]});
		 }
 });
 sponsoritFX_show.start({'opacity': [1,1]});
}

function sponsor_stop_start(x) {
 sponsor_anim=(sponsor_anim+1)%2;
 alert(sponsor_anim);
 document.cookie="spanim="+sponsor_anim+"; path=/";
 $('sp_anim').src='gfx/'+(sponsor_anim==0?'sp_stop.png':'sp_play.png');
 
}

