/**
 * Since carousel.addItem uses an HTML string to create the interface
 * for each carousel item, this method formats the HTML for an LI.
 **/
var fmtItem = function(imgUrl, url, title, titletag) {

      var innerHTML = 
          '<a href="' + 
          url + 
          '"><img src="'+ 
          imgUrl +
        '" width="' +
        75 +
        '" height="' +
        75+
        '" title="'+titletag+'" alt="'+titletag+'"/><br />' + 
          title + 
          '<\/a>';
  
    return innerHTML;
};

var fmtItem2 = function(imgUrl, url, title, titletag) {

      var innerHTML = 
          '<a href="' + 
          url + 
          '"><img src="'+ 
          imgUrl +
        '" width="' +
        100+
        '" height="' +
        75+
        '" title="'+titletag+'" alt="'+titletag+'"/><br /><div id="vidtext">' + 
          title + 
          '</div><\/a>';
  
    return innerHTML;
};

/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 

    load(this, start, last);    
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
var loadNextItems = function(type, args) {    

    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
};

var load = function(carousel, start, last) {
    for(var i=start;i<=last;i++) {  
		if(carousel._carouselElemID == "dhtml-carousel")
			carousel.addItem(i, fmtItem(imageList[i-1],urlList[i-1],'',imageTitle[i-1]));
		else
			carousel.addItem(i, fmtItem2(videoList[i-1],videoUrlList[i-1],videoTitleList[i-1],''));
	}
		/*
        var randomIndex = getRandom(18, lastRan);
        lastRan = randomIndex;
        carousel.addItem(i, fmtItem(imageList[randomIndex], urlList[randomIndex], ''));
		*/
};

var getRandom = function(max, last) {
    var randomIndex;
    do {
        randomIndex = Math.floor(Math.random()*max);
    } while(randomIndex == last);
    
    return randomIndex;
};

/**
 * 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 = "./style/left-enabled.gif";    
    } else {
        leftImage.src = "./style/left-disabled.gif";
    }
    
};

var handleNextButtonState = function(type, args) {
    var enabling = args[0];
    var rightImage = args[1];

    if(enabling) {
        rightImage.src = "./style/right-enabled.gif";    
    } else {
        rightImage.src = "./style/right-disabled.gif";
    }
    
};

/**
 * 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 carousel; // for ease of debugging; globals generally not a good idea
var carousel2; 

var pageLoad = function() 
{
	if(undefined !== window.imgsize)
	{
	    carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
	        {
	            numVisible:        4, 
	            animationSpeed:    0.25,
	            scrollInc:         4,
				size:			   imgsize,
				wrap:			   imgwrap,
	            navMargin:         35,
				revealAmount:      22,
	            prevElement:       "prev-arrow",
	            nextElement:       "next-arrow",
	            loadInitHandler:   loadInitialItems,
	            loadNextHandler:   loadNextItems,
	            loadPrevHandler:   loadPrevItems,
	            prevButtonStateHandler: handlePrevButtonState,
	            nextButtonStateHandler: handleNextButtonState
	        }
	    );
    }
   	if(undefined !== window.vidsize)
    {
	    carousel2 = new YAHOO.extension.Carousel("dhtml-carousel2", 
		    {
		        numVisible:        3, 
		        animationSpeed:    0.25,
		        scrollInc:         3,
		        size:			   vidsize,
				wrap:			   vidwrap,
		        navMargin:         40,
				revealAmount:      22,
		        prevElement:       "prev-arrow2",
		        nextElement:       "next-arrow2",
		        loadInitHandler:   loadInitialItems,
		        loadNextHandler:   loadNextItems,
		        loadPrevHandler:   loadPrevItems,
		        prevButtonStateHandler: handlePrevButtonState,
		        nextButtonStateHandler: handleNextButtonState
		    }
	    );
    }					
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);
