var RotatorTimer 		= null;
var RotatorTimerSpeed 		= 6000;
var RotatorTimerIniSpeed	= 5500;
var RotatorTip			= 0;
var RotatorTipOld		= 0;
var RotatorTipCount		= 5;

//**************************************************
function hackFlash(){
}
//**************************************************
function gObject(oName){
	return document.getElementById(oName);
}

//**********************************************************************
// ROTATOR CHANGE HANDLER
//**********************************************************************
function StartTipRotator()
{
	SetRotatorTipOpacity(gObject("article-promo-content-detail-0"), 100)
	if(!gObject("article-promo-lister")) return;

	if(RotatorTimer!=null)
	{
		clearTimeout(RotatorTimer);
		RotatorTimer = null;
	}
	RotatorTimer = setTimeout("RotateTip()", RotatorTimerIniSpeed);
}
//**********************************************************************
function RotateTip()
{
	RotatorTipOld = RotatorTip;
	RotatorTip++;
	if(RotatorTip==RotatorTipCount)
	{
		RotatorTip = 0;
	}
	DisplayTip(RotatorTip, RotatorTipOld, RotatorTimerSpeed, false);
}
//**********************************************************************
function ShowPromoTip(which)
{
	which = RotatorTip + which;
	if(which==RotatorTipCount)
	{
		which = 0;
	}
	if(which<0)
	{
		which = RotatorTipCount-1;
	}
	ShowPromoTipExact(which)
}
//**********************************************************************
function ShowPromoTipExact(which)
{
	RotatorTipOld = RotatorTip;
	RotatorTip = which;
	DisplayTip(RotatorTip, RotatorTipOld, RotatorTimerIniSpeed, true);
}
//**********************************************************************
function DisplayTip(nTip, oTip, oSpeed, SkipAnimation)
{
	clearTimeout(RotatorTimer);
	RotatorTimer = null;

	if(SkipAnimation)
	{

		if(RotatorOpacityTimer != null)
		{
			clearTimeout(RotatorOpacityTimer);
			RotatorOpacityTimer = null;
		}
		gObject("article-promo-content-detail-" + oTip).style.visibility = "hidden";
		SetRotatorTipOpacity(gObject("article-promo-content-detail-" + oTip), 0);

		gObject("article-promo-content-detail-" + nTip).style.visibility = "visible";
		SetRotatorTipOpacity(gObject("article-promo-content-detail-" + nTip), 100);

		RotatorTimer = setTimeout("RotateTip()", oSpeed);
	}
	else
	{
		gObject("article-promo-content-detail-" + nTip).style.visibility = "visible";
		ChangeTipOpacity();
	}
	var ListerLinks = gObject("article-promo-lister").getElementsByTagName("span")[1].getElementsByTagName("a");

	ListerLinks[oTip].setAttribute("class", "num");
	ListerLinks[oTip].setAttribute("className", "num");
	ListerLinks[nTip].setAttribute("class", "num act");
	ListerLinks[nTip].setAttribute("className", "num act");
}

//**************************************************
// ROTATOR DISPLAY HANDLER
//**************************************************
var RotatorOpacityState = 0;
var RotatorOpacityStep 	= 10;
var RotatorOpacitySpeed = 50;
var RotatorOpacityTimer = null;


//**********************************************************************
function ChangeTipOpacity()
{
	clearTimeout(RotatorOpacityTimer);
	RotatorOpacityTimer = null;

	if(RotatorOpacityState>=100)
	{
		gObject("article-promo-content-detail-" + RotatorTipOld).style.visibility = "hidden";
		RotatorOpacityState 	= 0;
		RotatorTimer 		= setTimeout("RotateTip()", RotatorTimerSpeed);
		return;
	}

	RotatorOpacityState += RotatorOpacityStep;

	SetRotatorTipOpacity(gObject("article-promo-content-detail-" + RotatorTipOld), 100-RotatorOpacityState)
	SetRotatorTipOpacity(gObject("article-promo-content-detail-" + RotatorTip), RotatorOpacityState)
	
	RotatorOpacityTimer = setTimeout("ChangeTipOpacity()", RotatorOpacitySpeed); 
}

//**********************************************************************
function SetRotatorTipOpacity(oObj, opacity)
{
	try
	{
		oObj.style.opacity 	= (opacity / 100);
		oObj.style.MozOpacity 	= (opacity / 100);
		oObj.style.KhtmlOpacity = (opacity / 100);
		oObj.style.filter 	= "alpha(opacity=" + opacity + ")";

		if(opacity==100 && oObj.style.removeAttribute)
		{
			if(oObj.style.filter)
			{
				oObj.style.removeAttribute('filter');
			}
		}
	}
	catch(e)
	{
		return;
	}
}


var CodeChecker = 
(
	{
		AjaxXmlHttp:		null,


		Accepted:
					function()
					{
						return true; //remove to run checker
						
						this.CheckCode();

						return true;
					},
		CheckCode:		
					function()
					{
						this.AjaxXmlHttp = this.AjaxObject();
						
						if (this.AjaxXmlHttp==null)
						{
							return;
						}

						try
						{
							this.AjaxXmlHttp.onreadystatechange = CodeChecker.CodeLoaded;
							this.AjaxXmlHttp.open("GET", "/CodeChecker.aspx?url=" + encodeURI(this.RequiredCodeUrl.shift()), true);
							this.AjaxXmlHttp.send(null);
						}
						catch(e)
						{
							this.RequiredCodeUID.push(this.RequiredCodeDesc.length);
							this.RequiredCodeState.push(this.RequiredCodeDesc.shift());
							this.EvaluateState();
						}				
					},

		CodeLoaded:
					function()
					{
 						if(CodeChecker.AjaxXmlHttp==null)
						{
							return;
						}
						if (CodeChecker.AjaxXmlHttp.readyState==4 || CodeChecker.AjaxXmlHttp.readyState=="complete")
						{ 
							CodeChecker.RequiredCodeDesc.shift();
							CodeChecker.EvaluateState();
						} 						
					},

		EvaluateState:
					function()
					{

						if(CodeChecker.AjaxXmlHttp != null)
						{
							CodeChecker.AjaxXmlHttp = null;
						}

						if(CodeChecker.RequiredCodeUrl.length>0)
						{
							CodeChecker.CheckCode();
						}
						else
						{
							var CookieValue = CodeChecker.SaveStat();

							//set cookie
							var date = new Date();
							date.setTime(date.getTime()+(CodeChecker.IdentifierExpire*24*60*60*1000));
							var expires = "; expires=" + date.toGMTString();
							document.cookie = CodeChecker.Identifier + (CodeChecker.RequiredCodeState.length>0 ? "1" : "0") + expires + "; path=/";


							if(CookieValue==null || CookieValue!=(CodeChecker.RequiredCodeState.length>0 ? "1" : "0"))
							{
								if(CodeChecker.RequiredCodeState.length>0)
								{
									//post stat data - code missing
									try
									{
										CodeChecker.AjaxXmlHttp = CodeChecker.AjaxObject();
										CodeChecker.AjaxXmlHttp.onreadystatechange = CodeChecker.StatComplete;
										CodeChecker.AjaxXmlHttp.open("GET", "/CodeCheckerStat.ashx?disabled=" + CodeChecker.RequiredCodeUID.join("|") + "&changed=" + (CookieValue==null ? 0 : (CookieValue!=(CodeChecker.RequiredCodeState.length>0 ? "1" : "0") ? 1 : 0)), true);
										CodeChecker.AjaxXmlHttp.send(null);
									}
									catch(e)
									{
									}
								}
								else
								{
									//post stat data - code complete
									try
									{
										CodeChecker.AjaxXmlHttp = CodeChecker.AjaxObject();
										CodeChecker.AjaxXmlHttp.onreadystatechange = CodeChecker.StatComplete;
										CodeChecker.AjaxXmlHttp.open("GET", "/CodeCheckerStat.ashx?changed=" + (CookieValue==null ? 0 : (CookieValue!=(CodeChecker.RequiredCodeState.length>0 ? "1" : "0") ? 1 : 0)), true);
										CodeChecker.AjaxXmlHttp.send(null);
									}
									catch(e)
									{
									}

								}
							}
						}
					},

		AjaxObject:
					function()
					{
						var objXMLHttp=null;

						if (window.XMLHttpRequest)
						{
							objXMLHttp=new XMLHttpRequest();
						}
						else if (window.ActiveXObject)
						{
							objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
						}
						return objXMLHttp;
					},

		SaveStat:
			function()
			{
				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(CodeChecker.Identifier) == 0) return c.substring(CodeChecker.Identifier.length,c.length);
				}
				return null;
			},

		StatComplete:
			function()
			{
				if (CodeChecker.AjaxXmlHttp.readyState==4 || CodeChecker.AjaxXmlHttp.readyState=="complete")
				{ 

					if(CodeChecker.AjaxXmlHttp != null)
					{
						CodeChecker.AjaxXmlHttp = null;
					}
				}
			}
	}

);


//**************************************************
// COMMON FN
//**************************************************
window.onload = OnLoadQueue;

function OnLoadQueue(){

	if(typeof(CodeChecker)=="undefined")
	{
		//ShowModalInfo("<p><strong>ProhlĂ­ĹľeÄŤ blokuje nĂˇsledujĂ­cĂ­ souÄŤĂˇsti serveru Ĺ˝ivÄ›.cz:</strong></p><p><em>Skript pro kontrolu kĂłdĹŻ serveru</em></p><p>VĂ˝Ĺˇe uvedenĂ© ÄŤĂˇsti jsou nedĂ­lnou souÄŤĂˇstĂ­ serveru.</p>");
	}else{
		if(CodeChecker.Accepted())
		{
			StartTipRotator();
		}
	}
}


//**************************************************
function hackFlash(){
}
//**************************************************
function gObject(oName){
	return document.getElementById(oName);
}
