// JavaScript Buxixo Document

var fmtItem = function(imgUrl, url, title, i) {
      var innerHTML = 
          '<img id="carousel-image-' + i + '" src="' + 
          imgUrl +
        '" width="' +
        94+
        '" height="' +
        74+
        '"/><input type="hidden" id="carousel-anchor-' + i + '" value="' + 
          url + 
          '"/>';
  
    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);
    spotlight(this);    
    preview(this);
};

/**
 * 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) {
    // get the last middle item and turn off spotlight
    var li = this.getItem(priorCenterItem);
    
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];

    if(!alreadyCached) {
        load(this, start, last);
    }
    spotlight(this);
    preview(this);
};

/**
 * 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) {
    // get the last middle item and turn off spotlight
    var li = this.getItem(priorCenterItem);

    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
    spotlight(this);
    preview(this);
};

var load = function(carousel, start, last) {

    for(var i=start;i<=last;i++) {
        var randomIndex = i ;//getRandom(18, lastRan);
        lastRan = randomIndex;
        carousel.addItem(i, fmtItem(imageList[randomIndex], urlList[randomIndex], "Number " + i, i), 'non-spotlight');
        
        // Image click will scroll to the corresponding carousel item.
        YAHOO.util.Event.addListener('carousel-image-'+i, 'click', function(evt) {
            this.carousel.scrollTo(this.index-2);
			document.getElementById("txt_foto").innerHTML = txts[this.index];
        }, {carousel:carousel,index:i}, true);
    }
};

var getRandom = function(max, last) {
    var randomIndex;
    do {
        randomIndex = Math.floor(Math.random()*max);
    } while(randomIndex == last);
    
    return randomIndex;
};

var handlePrevButtonState = function(type, args) {

    var enabling = args[0];
    var leftImage = args[1];
    if(enabling) {
        leftImage.src = "/imagens/seta_left.png";    
    } else {
        leftImage.src = "/imagens/seta_left.png";
    }
    
};

var handleNextButtonState = function(type, args) {

    var enabling = args[0];
    var rightImage = args[1];
    if(enabling) {
        rightImage.src = "/imagens/seta.png";
    } else {
        rightImage.src = "/imagens/seta.png";
    }
    
};

function completeHandler(type, args) {

}
function preview(carousel) {
    var firstVisible = carousel.getProperty("firstVisible");
    var middle = firstVisible + 2;
    
    var anchor = YAHOO.util.Dom.get('carousel-anchor-' + middle);
    YAHOO.util.Dom.get('preview').src = anchor.value;
    document.getElementById("txt_foto").innerHTML = txts[middle];
	//alert(middle)
}

function spotlight(carousel) {
    var firstVisible = carousel.getProperty("firstVisible");
    var start = firstVisible;
    var revealAmount = carousel.getProperty("revealAmount");
    var size = carousel.getProperty("size");
    
    if(revealAmount && firstVisible > 1) {
        start = firstVisible - 1;
    }
    var lastVisible = firstVisible + carousel.getProperty("numVisible") - 1;
    var end = lastVisible;
    if(revealAmount && lastVisible < size) {
        end = lastVisible + 1;
    }
    
    var middle = firstVisible + 2;
    
    for(var i=start; i<=end; i++) {
        var li = carousel.getItem(i);
        
        if(i == middle) {
            YAHOO.util.Dom.replaceClass(li, 'non-spotlight', 'spotlight');
            priorCenterItem = i;
        } else {
            YAHOO.util.Dom.replaceClass(li, 'spotlight', 'non-spotlight');
        }
    }
}
function esconde_galeria(){
    if(document.getElementById("esq_galeria"))
     document.getElementById("esq_galeria").style.display="none"; 
 }
