var startCounter=0;
var divideby = new Array(24,60,60,1);
var modby    = new Array(365,24,60,60);

// 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);
	
	new Ajax.Request('/common/getnewpic.php?index='+index, {method:'get',
		onComplete: function(transport) {
			if (200 == transport.status) {				
				txt = transport.responseText;
    				var d = txt.split("\n");
    				if (d.length>=2) {
					p1.style.backgroundImage = "url("+d[0]+")";
					p2.style.backgroundImage = "url("+d[1]+")";
					fp1.style.backgroundImage = "url("+d[0]+")";
					fp2.style.backgroundImage = "url("+d[1]+")";
    				}
			}
		},
		onFailure :function(transport) {alert("An error occured. Please try again later.");}
	} );		
	
	return false;
}


// Change amazon code
function changeAmazon(band) {
	var d = document.getElementById("_btn"+band);
	if (!d) return;
	d.innerHTML = "<img src=\"/images/loading.gif\" alt=\"Please Wait...\">";
	prefix = "field_"+bands[band].id+"_";		
	e = document.getElementById(prefix+"name");
		
	
}



// Start ticker on start
function countdownTimer() {
	var foo = new Date();
	startCounter+=parseInt(foo.getTime()/1000);

	setTimeout("tickerCounter()",100);	
}