
window.addEventListener ? window.addEventListener("load",xFadeInit,false) : window.attachEvent("onload",xFadeInit);
var d=document, imgs = new Array();
var fadeTimer = 2000, items = 0;
var now = new Date();

function xFadeInit() {
	if(!d.getElementById || !d.createElement)return;
		
	imgs = d.getElementById("imageContainer").getElementsByTagName("img");
	for(i=1;i<imgs.length;i++) {
		imgs[i].xOpacity = 0;		
	}
	imgs[0].style.display = "block";
	imgs[0].xOpacity = .99;
	items = imgs.length-1;
	setButtons(0);
	setAltInfo(0);	
	setLinkStyle(current+1,goingTo);
	setTimeout(autoScroller,5000);
}

current=0;
goingTo=1;
scrollMe = true;
	
function setLinkStyle(c,g) {
	var e1 = document.getElementById('imgLink'+c);
	var e2 = document.getElementById('imgLink'+g);
	e1.src = 'http://static.invitationbox.com/images/homepage/off-'+c+'.png?'+now;
	e2.src = 'http://static.invitationbox.com/images/homepage/on-'+g+'.png?'+now;
}	

function goToLink(x) {
	goingTo = x-1;
	xfade();
	setButtons(goingTo);
}

function goToLinkClick(x) {
	goToLink(x);
	scrollMe = false;
}

function goBack() { 
	goingTo = current-1;
	xfade();
	setButtons(goingTo);
}

function goForward() {
	goingTo = current+1;
	xfade();
	setButtons(goingTo);
}

function goForwardClick() {
	goForward();
}

function goBackClick() {
	goBack();
	scrollMe = false;
}

function setButtons(x) {
	if (x <= 0) {
		dimElem(document.getElementById('prev'));
	} else {
		fadeIn(document.getElementById('prev'));
	}
	if (x > (items-1)) {
		dimElem(document.getElementById('next'));
	} else {
		fadeIn(document.getElementById('next'));
	}
}

function xfade() {
	//alert('c=' + current + '    gt=' + goingTo);
	cOpacity = imgs[current].xOpacity;

	if ( goingTo <= items && goingTo > -1 ) {	
		nIndex = goingTo;
	} else {
		return;
	}
	setLinkStyle(current+1,goingTo+1);
	
	nOpacity = imgs[nIndex].xOpacity;
	
	cOpacity-=.05; 
	nOpacity+=.05;
	
	imgs[nIndex].style.display = "block";
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;
	
	setOpacity(imgs[current]); 
	setOpacity(imgs[nIndex]);
	
	if(cOpacity<=0) {
		imgs[current].style.display = "none";
		current = nIndex;
	} else {
		setTimeout(xfade,50);
	}		
	setAltInfo(current);
		
	function setOpacity(obj) {
		if(obj.xOpacity>.99) {
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}
}	

function dimElem(elem) {	
	if (elem) {
		for(i = 1; i > .25; i -= .05) {
			elem.style.opacity = i;
			elem.style.MozOpacity = i;
			elem.style.filter = "alpha(opacity=" + (i*100) + ")";
		}
	}
}		

function fadeIn(elem) {
	if (elem) {
		for(i = .2; i <= 1; i+=.05) {
			elem.style.opacity = i;
			elem.style.MozOpacity = i;
			elem.style.filter = "alpha(opacity=" + (i*100) + ")";
		}
	}
}

function visitCards() {
	switch (current) {
		case 0:
			window.location.href='http://www.invitationbox.com/beachpool.html';
			break;
		case 1:
			window.location.href='http://www.invitationbox.com/barbeque.html';
			break;
		case 2:
			window.location.href='http://www.invitationbox.com/wedding.html';
			break;
		case 3:
			window.location.href='http://www.invitationbox.com/bridalshower.html';
			break;
		case 4:
			window.location.href='http://www.invitationbox.com/birthdayinvitations.html';
			break;
		}				
}

function setAltInfo(x) {
	var el = document.getElementById('fulldivClick');
	if (el) {
		switch(x) {
			case 0:
				el.title = 'Summer, Beach & Pool Party Invitations';
				el.alt = 'Summer, Beach & Pool Party Invitations at InvitationBox.com';
				break;
			case 1:
				el.title = 'Barbeque, BBQ, Barbecue Party Invitations';
				el.alt = 'Barbeque Party Invitations, BBQ Party Invitations, Barbecue Party Invitations at InvitationBox.com';
				break;
			case 2:
				el.title = 'Wedding Invitations';
				el.alt = 'Wedding Collection of Invitations and Stationery at InvitationBox.com';
				break;
			case 3:
				el.title = 'Bridal Shower Invitations';
				el.alt = 'Bridal Shower Invitations at InvitationBox.com';
				break;
			case 4:
				el.title = 'Birthday Party Invitations';
				el.alt = 'Birthday Party Invitations at InvitationBox.com';
				break;
		}
	}		
}

function autoScroller() {
	if (scrollMe) {
		if (current >= items) {
			goToLink(1);
		} else {
			goForward();
		}
		setTimeout(autoScroller,5000);
	}
}
