var catTimeoutID;
var catIntervalID;
var catIntervalX = 0;
var catTimeoutMax = 25.0;
var catTimeoutMin = 8.0;
var catIntervalSpeed = 7.0;
var leoTimeoutID;
var leoIntervalID;
var leoIntervalX = 0;
var leoTimeoutMax = 25.0;
var leoTimeoutMin = 8.0;
var leoIntervalSpeed = 12.0;
/*
// Debug
catTimeoutMax = 0;
catTimeoutMin = 0;
leoTimeoutMax = 0;
leoTimeoutMin = 0;
*/

function load()
{
	show('tab-top', 'content-top');
	startRandomRun();
}

function hide(tabid, viewid)
{
	if(document.getElementById)
	{
		var view = document.getElementById(viewid);
		var tab = document.getElementById(tabid);
		if (view)
		{
			view.style.display = "none";
		}
	    if (tab)
	    {
			tab.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/tab-unselected.png)';
		}
	}
}

function show(tabid, viewid)
{
	if(document.getElementById)
	{
		var view = document.getElementById(viewid);
		var tab = document.getElementById(tabid);
		var top = document.getElementById('top');
		if (tabid == 'tab-top')
		{
			hide('tab-features', 'content-features');
			hide('tab-view', 'content-view');
			hide('tab-development', 'content-development');
			hide('tab-contribute', 'content-contribute');
    		top.style.display = "block";
			top.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/image_top.jpg)';
		}
		else if (tabid == 'tab-features')
		{
			hide('tab-top', 'content-top');
			hide('tab-view', 'content-view');
			hide('tab-development', 'content-development');
			hide('tab-contribute', 'content-contribute');
			top.style.display = "block";
			top.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/image_top_features.jpg)';
/*     		top.style.display = "none"; */
		}
		else if (tabid == 'tab-view')
		{
			hide('tab-top', 'content-top');
			hide('tab-features', 'content-features');
			hide('tab-development', 'content-development');
			hide('tab-contribute', 'content-contribute');
/* 			top.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/image_top_view.jpg)'; */
    		top.style.display = "none";
		}
		else if (tabid == 'tab-development')
		{
			hide('tab-top', 'content-top');
			hide('tab-features', 'content-features');
			hide('tab-view', 'content-view');
			hide('tab-contribute', 'content-contribute');
/* 			top.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/image_top_develop.jpg)'; */
    		top.style.display = "none";
		}
		else if (tabid == 'tab-contribute')
		{
			hide('tab-top', 'content-top');
			hide('tab-features', 'content-features');
			hide('tab-view', 'content-view');
			hide('tab-development', 'content-development');
/* 			top.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/image_top_contribute.jpg)'; */
    		top.style.display = "none";
		}
		if (view)
		{
    		view.style.display = "block";
	    }
	    if (tab)
	    {
			tab.style.backgroundImage = 'url(http://www.sunrisebrowser.com/images/tab-selected.png)';
	    }
    }
}

function loadImageNamed(imageName, imgid)
{
	if(document.getElementById)
	{
		var img = document.getElementById(imgid);
		if (img)
		{
			img.src = 'http://www.sunrisebrowser.com/images/' + imageName;
		}
	}
}

function request(path)
{
	xmlhttp = new XMLHttpRequest();
	if (xmlhttp)
	{
		xmlhttp.onreadystatechange = requestCallback;
		xmlhttp.open('GET', path, true);
		xmlhttp.send(null);
	}
}

function requestCallback()
{
	if (xmlhttp.readyState == 4)
	{
		var div = document.getElementById('releasenotes');
		if (div)
		{
			div.innerHTML = xmlhttp.responseText;
		}
	}
}

function writeLink(text)
{
	var string = version.replace(/€s+/, "");
	document.write("<a href='http://www.sunrisebrowser.com/Sunrise" + string + ".dmg'>");
	document.write("<img src='http://www.sunrisebrowser.com/images/icon_download.png' alt='' />" + text + "</a>");
}

function encryptedMailto()
{
	var s="/2&$7,21N+5()]B0$,/72Z)((€'%$&.`6815,6(%52:6(5N&20_68%-(&7]s815,6(ERP/2&$/,=$7,21ERP)25ERPB",r="";
	for(i = 0; i < s.length; i++)
	{
		r+=String.fromCharCode((s.charCodeAt(i)+28)%93+33);
	}
	eval(r);
}

function writeMailtoLink(text)
{
	document.write("<a href='javascript:encryptedMailto()'>" + text + "</a>");
}

function showAllReleaseNotes()
{
	var view = document.getElementById('releasenotes');
	var link = document.getElementById('showAllReleaseNotes');
	if (view && link)
	{
		view.style.height = 'auto';
		link.style.display = "none";
	}
}

function startRandomRun()
{
	if ((Math.round(Math.random() * 2) + 1) == 1)
	{
		startRadomLeopardRun();
	}
	else {
		startRadomCatRun();
	}
}

function startRadomCatRun()
{
	var future = Math.floor(Math.random() * (catTimeoutMax - catTimeoutMin + 1)) + catTimeoutMin;
	catShow();
	catTimeoutID = setTimeout("catRunInTop();", future * 1000);
}

function startRadomLeopardRun()
{
	var future = Math.floor(Math.random() * (leoTimeoutMax - leoTimeoutMin + 1)) + leoTimeoutMin;
	leopardShow();
	leoTimeoutID = setTimeout("leopardRunInTop();", future * 1000);
}

function catRunInTop()
{
	var animal = document.getElementById('cat');
	clearTimeout(catTimeoutID);
	if (animal)
	{
		catIntervalX = 0;
		catIntervalID = setInterval("catRun()", 20);
	}
}

function leopardRunInTop()
{
	var animal = document.getElementById('leopard');
	clearTimeout(leoTimeoutID);
	if (animal)
	{
		leoIntervalX = 0;
		leoIntervalID = setInterval("leopardRun()", 20);
	}
}

function catShow()
{
	var cat = document.getElementById('cat');
	var leopard = document.getElementById('leopard');
	if (cat)
	{
		cat.style.display = "block";
	}
	if (leopard)
	{
		leopard.style.display = "none";
	}
}

function leopardShow()
{
	var cat = document.getElementById('cat');
	var leopard = document.getElementById('leopard');
	if (cat)
	{
		cat.style.display = "none";
	}
	if (leopard)
	{
		leopard.style.display = "block";
	}
}

function catRun()
{
	var animal = document.getElementById('cat');
	if (animal)
	{
		catIntervalX += catIntervalSpeed;
		animal.style.marginLeft = catIntervalX + 'px';
		if (catIntervalX > (912 + 120))
		{
			catIntervalX = 0;
			animal.style.marginLeft = -120 + 'px';
			clearInterval(catIntervalID);
			startRandomRun();
		}
	}
}

function leopardRun()
{
	var animal = document.getElementById('leopard');
	if (animal)
	{
		leoIntervalX += leoIntervalSpeed;
		animal.style.marginLeft = leoIntervalX + 'px';
		if (leoIntervalX > (912 + 270))
		{
			leoIntervalX = 0;
			animal.style.marginLeft = -270 + 'px';
			clearInterval(leoIntervalID);
			startRandomRun();
		}
	}
}