var startCounter=0;
var divideby = new Array(24,60,60,1);
var modby    = new Array(365,24,60,60);
var oldtop="400px";
var adTimer=0;
var boxOffset = 0;
var lastBoxID = 0;
var skipDir=0;
var persons = new Array(0,300);
var directions = new Array(1,1);
var peoplewidth = new Array(176,151);

// Animate single digit
function animateDigit(amount,div_id) {
	// Step 1: Ensure the Previous and Next ones are the same
	var offdigit = document.getElementById(div_id);
	var ondigit = document.getElementById("on"+div_id);
	var slidecont = document.getElementById("slide"+div_id);
	
	if (offdigit&&ondigit&&slidecont) {
		if (ondigit.innerHTML!=amount) {		
			slidecont.style.top = "0px";
			offdigit.innerHTML = ondigit.innerHTML;
			ondigit.innerHTML = amount;
			
			new Effect.Morph(slidecont, {style: 'top: -31px',duration: 0.8});
		}
	}
}


// Ticker counter
function tickerCounter() {
	var foo = new Date(); 
	var t = startCounter-parseInt(foo.getTime()/1000);
	if (t<1) t=0;
	
	// Updane and trigger animations where required
	for (a=3; a>=0; a--) {
		t=parseInt(t/divideby[a]);
		dv=t%modby[a];
		animateDigit(parseInt(dv/100),"digith"+a);
		animateDigit(parseInt((dv/10)%10),"digitt"+a);
		animateDigit(dv%10,"digitu"+a);	
	}


	if (t>0) setTimeout("tickerCounter()",1000);
}


function changePhoto(index) {
	var p1 = document.getElementById("photoBoxPhoto1");
	var p2 = document.getElementById("photoBoxPhoto2");
	if (!p1) return true;
	if (!p2) return true;
	var fp1 = document.getElementById("photoBoxPhoto1flip");
	var fp2 = document.getElementById("photoBoxPhoto2flip");
	if (!fp1) return true;
	if (!fp2) return true;
	
	p1.style.backgroundImage = "url(/images/loading.gif)";
	p2.style.backgroundImage = "url(/images/loading.gif)";
	fp1.style.backgroundImage = "";
	fp2.style.backgroundImage = "";
	
	var dd=0;
	do {
		mmm = document.getElementById("selectedLinkHeading"+dd);
		if (mmm) mmm.className = (dd==index)?"lastYearPhotosSelected":"";
		dd++;
	} while (mmm);
	
		
	
	return false;
}



// dynamicphoto'.($a+1)

function isOverPhoto(boxnumber) {
	
}

function isOutPhoto(boxnumber) {
}


function MoveBoxesToNewLocation() {
	for (var a=-1; a<=6; a++) {
		var d = document.getElementById("dynamicphoto"+(a+1));
		if (d) {
			realpos = a + boxOffset;
			if (realpos<-1) realpos+=8;
			if (realpos>6) realpos-=8;
			x = -110+((realpos)*160);
			
			if ((realpos==0)||(realpos==5)) alpha=25; else
			if ((realpos==1)||(realpos==4)) alpha=75; else
			if ((realpos==2)||(realpos==3)) alpha=99; else alpha=0;
		
			new Effect.Morph(d, {style: 'left: '+x+'px; opacity:'+(alpha/100.0),duration: 1.0});
		}
	}
}


function changePic(boxNumber) {
	lastBoxID = boxNumber-boxOffset;
	if (lastBoxID<-1) lastBoxID+=8;
	if (lastBoxID>6) lastBoxID-=8;
	
	new Ajax.Request('/common/getnewpic.php?index='+skipDir, {method:'get',
		onComplete: function(transport) {
			if (200 == transport.status) {
				var d = document.getElementById("dynamicphoto"+(lastBoxID+1));
				if (d) d.style.backgroundImage = "url("+transport.responseText+")";
			}
		},
		onFailure :function(transport) {}
	} );		
}

function advanceLEFT() {
	boxOffset++;
	skipDir++;
	if (boxOffset>7) boxOffset=0;
	MoveBoxesToNewLocation();
	changePic(7);
}


function advanceRIGHT() {
	boxOffset--;
	skipDir--;
	if (boxOffset<0) boxOffset=7;
	MoveBoxesToNewLocation();
	changePic(-2);
}

function isClickPhoto(boxnumber) {
	clearTimeout(adTimer); adTimer=0;
	boxnumber+=boxOffset;
	if (boxnumber<-1) boxnumber+=8;
	if (boxnumber>7) boxnumber-=8;
	
	if ((boxnumber==3)||(boxnumber==4)) {
		// just reset the timeout
	} else
	if (boxnumber<3) advanceLEFT(); else advanceRIGHT();
	adTimer=setTimeout("autoAdvance()",3000);
	return false;
}


// Auto advance to the next photo
function autoAdvance() {
	advanceRIGHT();
	adTimer=setTimeout("autoAdvance()",2000);
}



// Start ticker on start
function countdownTimer() {
	var foo = new Date();
	startCounter+=parseInt(foo.getTime()/1000);

	setTimeout("tickerCounter()",100);
	adTimer=setTimeout("autoAdvance()",2000);
}

function refreshSelection() {
	var simpleBox = document.getElementById("simpleContact");
	var advancedBox = document.getElementById("advancedContactChosen");
	var advancedBoxList = document.getElementById("advancedContact");
	if (!simpleBox) return;
	if (!advancedBoxList) return;
	if (!advancedBox) return;
	
	arrowFloat = "<div id=\"arrowFloat\"></div>";
	
	// Step 1: Get selected item
	if (simpleBox.selectedIndex<0) advancedBox.innerHTML = arrowFloat; else {
		var box = simpleBox.selectedIndex;
		var contentBox = document.getElementById("personid_"+simpleBox.options[box].value);
		if (contentBox) {
			advancedBox.innerHTML = arrowFloat+contentBox.innerHTML;
			advancedBox.style.backgroundImage = contentBox.style.backgroundImage;
		}
	}
}

function prepareAdvancedForm() {
	var simpleBox = document.getElementById("simpleContact");
	var advancedBoxOuter = document.getElementById("advancedChosenOuter");
	var advancedBox = document.getElementById("advancedContactChosen");
	var advancedBoxList = document.getElementById("advancedContact");
	var spacerGo = document.getElementById("spacerGo");	
	if (!spacerGo) return;
	if (!simpleBox) return;
	if (!advancedBoxList) return;
	if (!advancedBox) return;
	if (!advancedBoxOuter) return;
	refreshSelection();
	spacerGo.style.display = "none";
	simpleBox.style.display = "none";
	advancedBoxOuter.style.display = "block";
}

function overOverPerson(idover,hover) {
	var d = document.getElementById("personid_"+idover);
	var simpleBox = document.getElementById("simpleContact");
	if (!simpleBox) return;
	if (!d) return;
	if (hover) d.className="activePerson"; else {
		var box = simpleBox.selectedIndex;
		d.className=(simpleBox.options[box].value==idover)?"selectedPerson":"";		
	}
	return false;
}


function toggleAdvancedPopup() {
	var advancedBoxList = document.getElementById("advancedContact");
	var simpleBox = document.getElementById("simpleContact");
	if (!simpleBox) return;
	if (!advancedBoxList) return;
	if (advancedBoxList.style.display=="block") advancedBoxList.style.display="none"; else {
		// Scroll the selection into view
		advancedBoxList.style.display="block";
		var box = simpleBox.selectedIndex;
		if (box>=0) {
			var contentBox = document.getElementById("personid_"+simpleBox.options[box].value);
			if (contentBox) contentBox.scrollIntoView(false);
		}
	}
	return false;
}

function setSelectedPerson(idover) {
	var simpleBox = document.getElementById("simpleContact");
	if (!simpleBox) return;
	for (var a=0; a<simpleBox.options.length; a++)
		if (simpleBox.options[a].value==idover) {
			simpleBox.selectedIndex = a;
			refreshSelection();
			toggleAdvancedPopup();
			return false;
		}
	refreshSelection();
	toggleAdvancedPopup();
	return false;
}


function goNextMovement() {
	for (var a=0; a<persons.length; a++) {
		var p = document.getElementById("person"+(a+1));
		if (p) {			
			p.style.backgroundPosition = (directions[a]==1)?"0px":"-"+peoplewidth[a]+"px";
			persons[a]+=((60+Math.floor(Math.random()*25))*directions[a]);
			if (persons[a]>550-peoplewidth[a]) {
				directions[a]=-1;
				persons[a] = 550-peoplewidth[a];
			}
			if (persons[a]<0) {
				directions[a]=1;
				persons[a]=0;
			}
			
			if (a==0) new Effect.Morph("person"+(a+1),{style: "left: "+persons[a]+"px;", duration: 1.0, afterFinish: function() {goNextMovement();}});
			      else new Effect.Morph("person"+(a+1),{style: "left: "+persons[a]+"px;", duration: 1.0});
		}
	}
}

function goFlag() {
	var d = document.getElementById("totalizerflag");
	if (d) {
		d.style.top = "402px";
		new Effect.Appear("totalizerflag", {duration: 1.5});
		new Effect.Morph("totalizerflag",{style: "top: "+oldtop, duration: 1.7});
	}
	var d = document.getElementById("peopleContainer");
	if (d) {
		new Effect.Appear("peopleContainer",{duration: 1.5, afterFinish: function() {goNextMovement();}});
	}

}

function initAdvancedContactForm() {
	Event.observe(window, "load", prepareAdvancedForm);
}

Event.observe(window, "load", goFlag);

