// JavaScript Document

function init() {
	Effect.Appear('normal_title', {duration:0.5});
	Effect.Appear('timepieces_title', {duration:0.5, queue:'end', afterFinish:startFade});
}

function startFade() {
	fadelistA = $('swapA').getElementsByTagName('img');
	fadelistB = $('swapB').getElementsByTagName('img');
	nowFadeList = 'A';
	fader = new PeriodicalExecuter(fadeOut, 2);
}

function fadeOut() {
	nowFadeList = (nowFadeList == 'A') ? 'B' : 'A';
	for (var i=0; i<eval('fadelist'+nowFadeList).length; i++) {
		if (Element.visible(eval('fadelist'+nowFadeList)[i])) {
			//alert("can now see "+i);
			if (nowFadeList == 'A') {
				lastSplashA = i;
				nextSplashA = i + 1;
				if (nextSplashA >= fadelistA.length) nextSplashA = 0;
			} else {
				lastSplashB = i;
				nextSplashB = i + 1;
				if (nextSplashB >= fadelistB.length) nextSplashB = 0;
			}
			
			Element.setStyle(eval('fadelist'+nowFadeList)[eval('lastSplash'+nowFadeList)],{ 'z-index':'10'});
			Element.setStyle(eval('fadelist'+nowFadeList)[eval('nextSplash'+nowFadeList)],{ 'z-index':'9'});
			Element.show(eval('fadelist'+nowFadeList)[eval('nextSplash'+nowFadeList)]);
			Element.setOpacity(eval('fadelist'+nowFadeList)[eval('nextSplash'+nowFadeList)],1);
			Effect.Fade(eval('fadelist'+nowFadeList)[i], { duration:0.5,afterFinish: fadedHide });
			break;
		}
	}
}
function fadedHide() {
	Element.setStyle(eval('fadelist'+nowFadeList)[eval('lastSplash'+nowFadeList)],{ 'z-index':'1'});
	Element.hide(eval('fadelist'+nowFadeList)[eval('lastSplash'+nowFadeList)]);
}

function initSlideLinks() {
	//alert('start links mods');
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	//alert("found "+anchors.length+" links");
	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "slide")){
			anchor.onclick = function () { slideLoad(this); return false;}
		}
	}

}

function slideLoad(objLink) {
	document.objLink = objLink;
	Element.addClassName(document.objLink.rev,'sliding');
	//alert('slide load with '+document.objLink.rev);
	new Effect.Scale(document.objLink.rev, 200, { scaleY:'false', scaleX:'true', afterFinish:loadPart });
}

function loadPart() {
	window.location = document.objLink.href;
	//alert('load '+document.objLink.href);
}

//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}
addLoadEvent(init);
addLoadEvent(initSlideLinks);
