/*thanks to Soh Tanaka we really apreciate
http://designm.ag/tutorials/image-rotator-css-jquery/
*/

//comprueba si dentro de una url existe uno de los dominios definidos en el array y retorna external
function getDomainExternal(url)
{
    ret='_blank';
    url = url.match(/:\/\/(.[^/]+)/)[1];
    var domaisToCheck = new Array('racc.es','racc.cat', 'racctravel.cat' , 'racctravel.com', 'racctravel.es');
    for(domain in domaisToCheck){
        if(url.indexOf(domaisToCheck[domain],0)>0){
            return '_top';
        }
    }
    return ret;
}

var intId;
// esta función pasa utomaticamente slides a partir del indice del parámetro start
var slideNoAccesibe = '';
var main_image = '';
function animateRotator(start)
{
    numItem = $(".image_thumb ul li").size();
    var n = start;
    tiempo = 10000;
    if(n==-1){setInterval(function(){n=0},tiempo)}
    intId = setInterval(function(){
        if (n < numItem) {
            $(".image_thumb ul li:eq("+n+")").trigger('click');
        } else {
            //si el primer slide no es accesible
            if(imageRotatorSlideIni==-1){
                $('.main_image').html(main_image);
                botonExternal(-1);
                $(".image_thumb ul li:eq("+(n-1)+")").removeClass('active');
                clearInterval(intId);
                animateRotator(imageRotatorSlideIni);
            }else{
                $(".image_thumb ul li:eq(0)").trigger('click');
            }
        }
    }
    ,tiempo);
}

function botonExternal(indice){
    indicePlus = 1;
    botonInnerRotator = "#botonInnerRotator_" + parseInt(indice + indicePlus);
    if(($(botonInnerRotator).length) > 0){
        newInnerRotator = $(botonInnerRotator).clone();
        $(newInnerRotator).attr("id",botonInnerRotator + '_n');
        //console.log('hay uno '  + botonInnerRotator);
        $(newInnerRotator).insertAfter('#urlItemMainImage .desc h3');
        $(newInnerRotator).show();
    }
}

/*Show Image Description and Set Transparency on Block*/
$(document).ready(function() {
    //transparencia
    transparencia = 1;
    if($.browser.msie){transparencia=0.8};
    browser = $.browser;
    $(".main_image .desc").show();
    $(".main_image .block").animate({ opacity: transparencia }, 1 ); //Set Opacity
    //si el rotator tiene una slide que no es accesible se guarda su contenido en una variable
    if(imageRotatorSlideIni==-1){

        main_image = $('.main_image').html();
        var onclickValue = $(this).find('h3').html();  //Get HTML of the "block" container
        onclickValue = onclickValue.replace(/<[^>]+>/g,'');
        $(".main_image a").click(function() { _gaq.push(['_trackEvent', 'clic_home', 'Cajon_Rotator_Central', onclickValue ]);});

    }
    botonExternal(0);

    /*Click and Hover Events for Image List*/
    // comentamos esta linea para que la primera imagen no corresponda con ninguna de las seleccionable con el menu

    $(".image_thumb ul li").click(function(){
        //Set Variables
        clearInterval(intId);
        var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
        //var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
        var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
        var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
        if ($(this).is(".active")) {  //If the list item is active/selected, then...
            //return false; // Don't click through - Prevents repetitive animations on active/selected list-item
        } else { //If not active then...
            //Animate the Description
            var onclickValue = $(this).find('h3').html();  //Get HTML of the "block" container
            onclickValue = onclickValue.replace(/<[^>]+>/g,'');

            $(".main_image .block").animate({ opacity: transparencia, marginBottom: "-300px" }, 250 , function() { //Pull the block down (negative bottom margin of its own height)
            $(".main_image .block").html(imgDesc).animate({ opacity: transparencia,  marginBottom: "0" }, 250 ); //swap the html of the block, then pull the block container back up and set opacity
            $(".main_image img").attr({ src: imgTitle , alt: imgAlt}); //Switch the main image (URL + alt tag)
            //incrustamos el boton
            indiceExt = indice;
            if(imageRotatorSlideIni==-1){
                indiceExt = parseInt(indiceExt +1);
            }
            botonExternal(indiceExt);

            $(".main_image a").unbind('click');
            $(".main_image a").click(function() { _gaq.push(['_trackEvent', 'clic_home', 'Cajon_Rotator_Central', onclickValue ]);});
            });
        }
        //Show active list-item
        $(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all list-items
        $(this).addClass('active');  //Add class of 'active' on the selected list
        indice = $('.image_thumb ul li').index(this);
        indiceLink = indice;
        if(imageRotatorSlideIni==-1){
            indiceLink=indiceLink+1;
        }
        //se actualiza la url de la info
        relext = getDomainExternal(urlsImageRotator[indiceLink]);
        $(".main_image a").attr({target: relext , href: urlsImageRotator[indiceLink]});

        animateRotator(indice+1);
        return false;
    }) .hover(function(){ //Hover effects on list-item
        $(this).addClass('hover'); //Add class "hover" on hover
    }, function() {
        $(this).removeClass('hover'); //Remove class "hover" on hover out
    });
    /*Toggle the Hide/Show Option*/
    $("a.collapse").click(function(){
        $(".main_banner .block").slideToggle(); //Toggle the description (slide up and down)
        $("a.collapse").toggleClass("show"); //Toggle the class name of "show" (the hide/show tab)
    });
    animateRotator(imageRotatorSlideIni);
});//Close Function
