/*
    Document   : js
    Created on : 17/03/2010, 15:41:05
    Author     : Miguel Saurí <miguelsauri@picantedesign.com>
    Description:
        Global js- scripts
*/

$(document).ready(function() {
    //* clone images putting grayscale-copy above original
    $('.desatImg img').each(function(index,domEle){
        var height;
        var width;
        switch($(domEle).parent().attr('class')){
            case 'desatImg logo':
                width = 65;
                height = 70;
                break;
            case 'desatImg destac':
                width = 240;
                height = 90;
                break;
            case 'desatImg galerias':
                width = 120;
                height = 360;
                break;
        }
        $(domEle).parent().css('height', height);
        $(domEle).parent().css('width', width);
        //$(domEle).parent().css('height', $(domEle).attr('height')/2);
        ////$(domEle).parent().css('width', $(domEle).attr('width'));
        var org = $(domEle).clone();
        org.addClass('over');
        //org.css('top','-'+$(domEle).parent().css('height'));
        org.css('top',height*-1);
        org.attr('rel', height);
        org.appendTo($(domEle).parent());
    });

    //* hover effect. fading in and Out
    $(".desatImg").hover(function(e) {
        $(this).children('.over').fadeOut('slow');
    },
    function() {
        $(this).children('.over').fadeIn('fast');
    });

    //* Slide show
    // ancho de cada imagen o de la "Ventana"
    // var imageWidth = $("#slideshow").width(); // a veces no funciona. Depende del Navegador
    var imageWidth = 750;
    // cantidad de imagenes
    var imageSum = $("#slider img").size();
    // ancho calculado de la tira de imagenes que se mueve
    var imageReelWidth = imageWidth * imageSum;

    // Generar botones para paginas


    //Adjust the image reel (div#slider) to its new size
    $("#slider").css({
        'width' : imageReelWidth
    });

    //Paging  and Slider Function
    rotate = function(){
        var triggerID = $active.attr("rel") - 1; //Get number of times to slide
        var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

        $(".paging a").removeClass('active'); //Remove all active class
        $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

        //Slider Animation
        $("#slider").animate({
            left: -image_reelPosition
        }, 1000 );

    };

    //Rotation  and Timing Event
    rotateSwitch = function(){
        play = setInterval(function(){ //Set timer
            $active = $('.paging a.active').next(); //Move to the next paging
            if ( $active.length === 0) { //If paging reaches the end...
                $active = $('.paging a:first'); //go back to first
            }
            rotate(); //Trigger the paging and slider function
        }, 4000); //milliseconds
    };

    rotateSwitch(); //Run function on launch

    $("#slider img").hover(function() {
        clearInterval(play); //Stop the rotation
    }, function() {
        rotateSwitch(); //Resume rotation timer
    });

    $('.eitem a.gal').fancybox({
        'titlePosition':'inside'
    });
    $('#galeria .galeria a').fancybox({
        'titleposition':'inside'
    });
    $('#galeria .galeria a.vimeo').fancybox({
        'padding'       : 0,
        'autoScale'     : false,
        'transitionIn'  : 'none',
        'transitionOut' : 'none',
        'width'         : 320,
        'height'        : 240,
        'href'          : this.href,
        'type'          : 'swf',
        'swf'           : {
            'wmode'             : 'opaque',
            'allowfullscreen'   : 'false'
        }
    });
    $('.drafttools .publish').fancybox({
        ajax : {
            type    : "POST"
        }
    });
    
    $('.eitem a.gal').css({
        'opacity':0
    });

    //$('#nov ol').makeacolumnlists({cols:3,colWidth:0,equalHeight:'li',startN:1});
    $('#nov ol').columnizeList({
        cols:3,
        constrainWidth:1
    });
    //    $('ol.calEventos').each(function(i){
    //        $(this).columnizeList({cols:3, constrainWidth:1});
    //    });

    // CALENDARIO
    $('ol.calEventos').fadeOut('fast',function(){
        $('ol.esteMes').fadeIn('normal');
    });
    $('a.verFechas').click(function(event){
        event.preventDefault();
        $(this).parent('h2').parent('li').children('ol.calEventos').toggle();
    });

});
