/*
Lealtamedia.com homepage carousel
Lealta Media
v1
*/

// Set up global variables that track the frames to fade from/to
var previous; 
var current = 0;


// Set up the page when the window loads
function promoInit() {
var slides = document.getElementById("promo").getElementsByTagName("li");
slides[0].style.display = "block"; // Display the first slide
document.getElementById("promoLoader").style.display = "none"; // Hide the loading screen
document.getElementById("promo").style.display = "block"; // Show the carousel
promoResize();
}


// The width of the carousel requires a script to keep it centered in the page
function promoResize() {
var browserWidth = document.body.clientWidth;
var slideList = document.getElementById("promo").getElementsByTagName("li");

for (i=0;i<slideList.length;i++) {
	
	var promoImage = document.getElementById("slide_"+(i+1)).getElementsByTagName("img")[0];
	var promoContent = document.getElementById("slide_"+(i+1)).getElementsByTagName("div")[1];
	var promoWidth = promoImage.offsetWidth;
	var currentLeft = (browserWidth-promoWidth)/2;
	
	promoImage.style.left = currentLeft+"px";
	promoContent.style.left = currentLeft+"px";
	}
}


// Navigate to/from slides in the carousel
function slide(obj) {

var slideList = document.getElementById("promo").getElementsByTagName("li");
var navList = document.getElementById("promoNav").getElementsByTagName("li");

// Configure all of the slides
for (i=0;i<slideList.length;i++) {
	slideList[i].style.display = "none"; // Hide all slides
	slideList[i].xOpacity = 1; // Set opacity to 100%;
	
	// Cross-browser opacity method handles first/last slides
	setOpacity(slideList[i]); 
	if (i-1<0) setOpacity(slideList[0]);
	else setOpacity(slideList[i-1]);	
	
	// Determine previous slide (the slide to start the cross-fade)
	if (navList[i].getElementsByTagName("a")[0].className == "active") {
		previous = i;
		}
	navList[i].getElementsByTagName("a")[0].className = "";
	}

// Determine next slide (the slide to end the cross-fade)
for (i=0;i<navList.length;i++) {
	if (obj.parentNode==navList[i]) {
		current = i;
		}
	}

// Set the style of the carousel button
navList[current].getElementsByTagName("a")[0].className = "active";

// Run the cross-fade transition between the previous slide and current slide
crossfade();

}


/* ##### run the cross-fade ##### */
function crossfade() {

var slideList = document.getElementById("promo").getElementsByTagName("li");
var navList = document.getElementById("promoNav").getElementsByTagName("li");

// Display the navigation pointer

//alert(navList[previous].offsetWidth);

var thePointer = document.getElementById("promoNavPointer").getElementsByTagName("div")[0];
var pointerWidth = 30; // For pointer positioning -- js can't get this value because it's a background image
var pointerOffset = (navList[current].offsetWidth/2)-(pointerWidth/2);

// Determine position of active button - for IE
if (navList[current].offsetParent.offsetLeft>0) {
	var pointerBackgroundPosition = navList[current].offsetLeft+pointerOffset;
	}
// Determine position of active button - for all other browsers
else {
	var pointerBackgroundPosition = (navList[current].offsetLeft-navList[current].parentNode.offsetLeft)+pointerOffset;	
	}
	
	
thePointer.style.backgroundPosition = pointerBackgroundPosition+"px bottom";

// Display the two slides displayed in the cross-fade, and set their display order
slideList[previous].style.display = "block";
slideList[previous].style.zIndex = 2;
slideList[current].style.display = "block";
slideList[current].style.zIndex = 1;

// Increment the opacity effect in the cross-fade animation
previousOpacity = slideList[previous].xOpacity;
currentOpacity = slideList[current].xOpacity;
previousOpacity-=.05; 
currentOpacity+=.05;
slideList[previous].xOpacity = previousOpacity;
slideList[current].xOpacity = currentOpacity;
setOpacity(slideList[previous]); // cross-browser method to apply opacity effect to the slides
setOpacity(slideList[current]);

// If the cross-fade is at the end, stop the crossfade animation
if(previousOpacity<=0) {
	slideList[previous].style.display = "none";
	slideList[current].xOpacity = 1;
	setOpacity(slideList[current]);
	clearTimeout(timer);

// If not at the end, increment through the crossfade animation
} else {
	timer = setTimeout(crossfade,50);
	}

// Center the new slide in the carousel
promoResize();

}


/* #### Cross-browser method to apply opacity #####  */	
function setOpacity(obj) {
if (obj.xOpacity>1) {
	obj.xOpacity = 1;
	return;
	}
obj.style.opacity = obj.xOpacity;
obj.style.MozOpacity = obj.xOpacity;
obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
}


// Set up the carousel when the window loads
window.onload = promoInit;
window.onresize = promoResize;
