
/*
	Carousel
*/

var carousel; // for ease of debugging; globals generally not a good idea
var slideNumber = 1;
var sections=0;
var slotsLeft=0;
var doinc=true;
var page_qty = Array();
var firstTime = true;
var startSlot=0;
var dochange=false;
// Quantity images by page
page_qty[0] = 9;
page_qty[1] = 12;
page_qty[2] = 7;
page_qty[3] = 5;

// the first column of the lists below identify if the campaign will direct the user to view the link in a new window or in the same window
// 0 = same window
// 1 = new window

firstStyle = function(ob) {
	ob.moveTo(startSlot);
	YAHOO.util.Dom.setStyle('mainContent-item-'+startSlot,'margin-left','0');
	YAHOO.util.Dom.setStyle('navDots','visibility','visible');
}


//Uncomment the next lines to randomize the image order
//shuffle = function(o){ //v1.0
//	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
//	return o;
//};

//imageList = shuffle(imageList);



var lastRan = -1;


var loadInitialItems = function(type, args) {	
    /*var start = args[0];*/
    //var last = args[1];
	
	var last=containerList[page_id].imageList.length;
	var start=1;	
    load(this, start, last);
    // Attachs the Click Handlers for the new Carousel content.
    attachOmnitureHandlers();
};

var loadNextItems = function(type, args) {    
    var start = args[0];
    var last = args[1];		
    var alreadyCached = args[2];
	/*if(dochange)
		if(last==containerList[page_id].imageList.length-(slotsLeft+1)){		
			carousel.cfg.setProperty("scrollInc",3,false);	
		}	
		else{		
			carousel.cfg.setProperty("scrollInc",slotsLeft,false);
		}
	else{
		dochange=true;
	}*/
	if(doinc){
		if(slideNumber<sections){
			slideNumber++;							
		}
		else{
			slideNumber=1;
			doinc=false;
		}		
	}
	else
		doinc=true;		
	document.getElementById("navDots").innerHTML="";
	for(var i=1;i<=sections;i++){
		if(i==slideNumber)
			document.getElementById("navDots").innerHTML+="<div id='dotOn'/>";
		else
			document.getElementById("navDots").innerHTML+="<div id='dotOff'/>";		
	}
	if (typeof document.body.style.maxHeight != "undefined") {
		
		 //ie7  
		
	} else {		
		fixIt(document.getElementById("navDots"));		
	}	
    if(!alreadyCached) {			
        load(this, start, last);		
        // Attachs the Click Handlers for the new Carousel content.
        attachOmnitureHandlers();
    }
	
};

var loadPrevItems = function(type, args) {		
    var start = args[0];
    var last = args[1];	
    var alreadyCached = args[2];	
	/*if(dochange)
		if(last==containerList[page_id].imageList.length-(slotsLeft+3)){		
			carousel.cfg.setProperty("scrollInc",slotsLeft,false);	
		}	
		else{		
			carousel.cfg.setProperty("scrollInc",3,false);
		}
	else{
		dochange=true;
	}*/
	if(doinc)
		if(slideNumber>1){
			slideNumber--;
		}
		else{
			slideNumber=sections;
			doinc=false;
		}
	else
		doinc=true;
	
	document.getElementById("navDots").innerHTML="";
	for(var i=1;i<=sections;i++){
		if(i==slideNumber)
			document.getElementById("navDots").innerHTML+="<div id='dotOn'/>";
		else
			document.getElementById("navDots").innerHTML+="<div id='dotOff'/>";		
	}	
	if (typeof document.body.style.maxHeight != "undefined") {
		
		 //ie7  
		
	} else {		
		fixIt(document.getElementById("navDots"));		
	}	
    if(!alreadyCached) {
        load(this, start, last);		
        // Attachs the Click Handlers for the new Carousel content.
        attachOmnitureHandlers();
    }	
	
	
};

function choosePage() {
	return 0;
}







var load = function(carousel, start, last) {
	id_page = choosePage();
	//document.getElementById('carouselCounter').innerHTML = page_qty[id_page];
    page = choosePage();		
    for(var i=start-1;i<last;i++) {		
        //var element = "<div class=\"mainContentImageBox elementBox\" title=\"" + containerList[page].imageList[i].header + "\"  onclick=\"javascript:window.location = '" + containerList[page].imageList[i].url + "';\" >"
        var element ;
		if( containerList[page].imageList[i].targetBlank == 1){

			element = "<div class=\"mainContentImageBox elementBox \" title=\"" + containerList[page].imageList[i].header + "\" >"
				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<a class=\"carouselLink " + containerList[page].imageList[i].omnitureClasses + "\" href=\"" + containerList[page].imageList[i].url + "\" ><img class=\"carouselLink\" alt=\"" + containerList[page].imageList[i].header + "\" class=\"thumbnail\"  src=\"" + containerList[page].imageList[i].imageUrl
				//+ "' />"//comment this line and uncomment the next one to add url to the image
				+ "\" /></a>"
				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<h2><a class=\"carouselLink " + containerList[page].imageList[i].omnitureClasses + "\" href=\"" + containerList[page].imageList[i].url + "\" >" + containerList[page].imageList[i].header + "</a></h2>"
				//+ "</a>"
				//+ "<a class='carouselLink' href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<p>" + containerList[page].imageList[i].body + "</p>"
				//+ "</a>"
				+ "</div>";
		}else{

			element = "<div class=\"mainContentImageBox elementBox \" title=\"" + containerList[page].imageList[i].header + "\" >"
				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<a class=\"carouselLink " + containerList[page].imageList[i].omnitureClasses + "\" target=\"_blank\" href=\"" + containerList[page].imageList[i].url + "\" ><img class=\"carouselLink\" alt=\"" + containerList[page].imageList[i].header + "\" class=\"thumbnail\"  src=\"" + containerList[page].imageList[i].imageUrl
				//+ "' />"//comment this line and uncomment the next one to add url to the image
				+ "\" /></a>"
				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<h2><a class=\"carouselLink " + containerList[page].imageList[i].omnitureClasses + "\" target=\"_blank\" href=\"" + containerList[page].imageList[i].url + "\" >" + containerList[page].imageList[i].header + "</a></h2>"
				//+ "</a>"
				//+ "<a class='carouselLink' href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
				+ "<p>" + containerList[page].imageList[i].body + "</p>"
				//+ "</a>"
				+ "</div>";
//
//			element = "<div class=\"mainContentImageBox elementBox carouselLink\" title=\"" + containerList[page].imageList[i].header + "\" >"
//				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
//				+ "<img  onclick=\"javascript:window.location = '" + containerList[page].imageList[i].url + "';\" class=\"carouselLink\" alt=\"" + containerList[page].imageList[i].header + "\" class=\"thumbnail\"  src=\"" + containerList[page].imageList[i].imageUrl
//				//+ "' />"//comment this line and uncomment the next one to add url to the image
//				+ "\" />"
//				//+ "<a href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
//				+ "<h2 onclick=\"javascript:window.location = '" + containerList[page].imageList[i].url + "';\" ><a href='#'>" + containerList[page].imageList[i].header + "</a></h2>"
//				//+ "</a>"
//				//+ "<a class='carouselLink' href='" + containerList[page].imageList[i].url + "'>"//uncomment this line to add url to the image
//				+ "<p>" + containerList[page].imageList[i].body + "</p>"
//				//+ "</a>"
//				+ "</div>";

		}		
        carousel.addItem(i + 1, element);
		
    }	
}
/**
 * Custom button state handler for enabling/disabling button state.
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {
    var enabling = args[0];    
	var leftImage = args[1];	
    if(enabling) {
        //leftImage.src = "/assets/images/btn_arrow.gif";
	document.getElementById("prev-arrow").style.cursor = "pointer";
	//document.getElementById("prev-arrow").style.opacity = "100";
    } else {
		//leftImage.src = "/assets/images/btn_arrow.gif";
	document.getElementById("prev-arrow").style.cursor = "default";
	//document.getElementById("prev-arrow").style.opacity = "0.4";
	var diff=5;
	if(slotsLeft>0)
		diff=slotsLeft
	else
		diff=3;
	var endc=containerList[page_id].imageList.length-(2+diff);		
	setTimeout('moveTo('+endc+')',2500);
    }
};
var moveTo=function(value){	
	carousel.moveTo(value);	
}
var handleNextButtonState = function(type, args) {
    var enabling = args[0];
    var leftImage = args[1];	
    if(enabling) {        
		document.getElementById("next-arrow").style.cursor = "pointer";
		//document.getElementById("next-arrow").style.opacity = "100";
    } else {		
		document.getElementById("next-arrow").style.cursor = "default";
		//document.getElementById("next-arrow").style.opacity = "0.4";
		setTimeout('moveTo('+startSlot+')',2500);		
    }
};



var attachOmnitureHandlers = function() {
    var carouselRootElement =  YAHOO.util.Dom.get("mainContent");
    var omnitureLinks = YAHOO.util.Dom.getElementsByClassName('omniture', "*", carouselRootElement);

    // Purge the Click Handlers for each carousel element
	
    for (var i = 0; i < omnitureLinks.length; i++) {
		//do not purge the existing click event handlers for left and right arrows
		if(omnitureLinks[i].id != 'prev-arrow' && omnitureLinks[i].id != 'next-arrow'){ 
          YAHOO.util.Event.purgeElement(omnitureLinks[i], false, 'click');
		}
    }

    // Attachs the Click Handlers for the new Carousel content.
    omniture.attachClickHandlers(carouselRootElement);
}

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 */



var pageLoad = function()
{
	page_id = choosePage();	
	sections=Math.ceil(containerList[page_id].imageList.length/3);	
	slotsLeft=containerList[page_id].imageList.length-Math.floor(containerList[page_id].imageList.length/3)*3;	
	
	if(slotsLeft){
		startSlot=slotsLeft+1;
		for(var i=1;i<=slotsLeft;i++){				
			containerList[page_id].imageList.splice(0,0,containerList[page_id].imageList[containerList[page_id].imageList.length-i]);

		}	
	}
	else{
		startSlot=4;
		containerList[page_id].imageList.splice(0,0,containerList[page_id].imageList[containerList[page_id].imageList.length-1])
		containerList[page_id].imageList.splice(0,0,containerList[page_id].imageList[containerList[page_id].imageList.length-2])
 	    containerList[page_id].imageList.splice(0,0,containerList[page_id].imageList[containerList[page_id].imageList.length-3])																																															
	}
	var temp=slotsLeft;
	if(slotsLeft==0)
		temp=3;
	containerList[page_id].imageList[containerList[page_id].imageList.length]=containerList[page_id].imageList[temp];
	containerList[page_id].imageList[containerList[page_id].imageList.length]=containerList[page_id].imageList[temp+1];
	containerList[page_id].imageList[containerList[page_id].imageList.length]=containerList[page_id].imageList[temp+2];	
    carousel = new YAHOO.extension.Carousel("mainContent",
    {
        numVisible:        3,
        animationSpeed:   2,
        scrollInc:         3,
        navMargin:         10,
        firstVisible:       startSlot,
        prevElement:     "prev-arrow",
        nextElement:     "next-arrow",
        loadInitHandler:   loadInitialItems,
        loadNextHandler:   loadNextItems,
        loadPrevHandler:   loadPrevItems,
        prevButtonStateHandler:   handlePrevButtonState,
        nextButtonStateHandler:   handleNextButtonState,
        autoPlay: 1,
        size: containerList[page_id].imageList.length,
        wrap:false
    });
	/*if (page_qty[page_id] <= 3){
		document.getElementById("carouselPageDetail").innerHTML = "<div style='width:22px;height:24px;'>&nbsp;</div>";		
	}*/
	//carousel.cfg.setProperty("scrollInc",1,false);	
	//carousel.moveTo(4);	
	for(var i=0;i<sections;i++){
		if(i==0)
			document.getElementById("navDots").innerHTML+="<div id='dotOn'/>";
		else
			document.getElementById("navDots").innerHTML+="<div id='dotOff'/>";		
	}	
	
	if (typeof document.body.style.maxHeight != "undefined") {
		
		 //ie7  
		
	} else {		
		fixIt(document.getElementById("navDots"));		
	}		
			
};

