// JavaScript Document
var imageList = ["1.jpg",
                 "1.jpg",
                 "2.jpg",
                 "3.jpg",
                 "4.jpg",
                 "5.jpg",
				 "6.jpg",
                 "7.jpg",
                 "8.jpg",
                 "9.jpg",
				 "10.jpg",
				 "11.jpg",
				 "12.jpg",
                 "13.jpg",
                 "14.jpg",
				 "15.jpg",
				 "16.jpg",
                 "17.jpg",
				 "18.jpg",
				 "19.jpg"
				 
                 ];
var urlList = [
                 "1.gif",
				 "1.gif",
                 "2.jpg",
                 "3.jpg",
                 "4.jpg",
                 "5.jpg",
				 "6.jpg",
                 "7.jpg",
                 "8.jpg",
                 "9.jpg",
				 "10.jpg",
				 "11.jpg",
				 "12.jpg",
                 "13.jpg",
                 "14.jpg",
				 "15.jpg",
				 "16.jpg",
                 "17.jpg",
				 "18.jpg",
				 "19.jpg"
                 ];

var lastRan = -1;

/**
 * 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) {

      var innerHTML = 
          '<a style="cursor:pointer;" onclick="abrir(\'' + 
          url + 
          '\');"><img style="border:1px solid #333; margin-right:2px;" src=\"img/pequenos/' + 
          imgUrl +
        '\" width="' +
        124 +
        '" height="' +
        92+
        '"/>';
  
    return innerHTML;
    
};
function abrir(a){
document.getElementById('img_pro').innerHTML = '<img src="img/grandes/'+a+'" style="cursor:auto" />';
}
/**
 * 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]; 
    var alreadyCached = args[2]; 
    var reveal = args[3]; // whether the carousel is revealing additional items
//console.log(reveal);
    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++) {
        carousel.addItem(i, fmtItem(imageList[i], urlList[i], "Number " + i));
/*
        // Example of an alternate way to add an item (passing an element instead of html string)
        var p = document.createElement("P");
        var t = document.createTextNode("Item"+i);
        p.appendChild(t);
        carousel.addItem(i, p );
*/
    }
};

/**
 * 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 = "img/left-enabled.jpg";        
    } else {
        leftImage.src = "img/left-disabled.gif";    
    }
};

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the next button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: nextButtonStateHandler
 **/
var handleNextButtonState = function(type, args) {
    var enabling = args[0];
    var rightImage = args[1];
    if(enabling) {
        rightImage.src = "img/right-enabled.jpg";    
    } else {
        rightImage.src = "img/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 pageLoad = function() 
{
    var carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
        {
            numVisible:        5,
            animationSpeed:    .8,
            animationMethod:   YAHOO.util.Easing.easeBoth,
            scrollInc:         3,
            navMargin:         40,
            size:              19,
            loadInitHandler:   loadInitialItems,
            prevElement:     "prev-arrow",
            nextElement:     "next-arrow",
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState,
            nextButtonStateHandler:   handleNextButtonState
        }
    );
}

YAHOO.util.Event.addListener(window, 'load', pageLoad);

