var jScrollPane = {};
jScrollPane.horizontalShowed = false;

php.animateSideBar = false;


$(function(){
   $('#credits').hide();

    // Elegant img loader
    if(php.activeKrioImageLoader){
        $("body").krioImageLoader();
    }

});


_log(document)



$(function(){    
    var easing = 'easeOutQuart';
    setTimeout(function(){
        
        if($('#sidebar').size() && php.animateSideBar)
            $('#sidebar').animate({
                left:0
            }, 1100, easing, function(){
                $('#header_opacity').fadeIn(1100, easing);
            });
    }, 500)
    setTimeout(function(){
        if($('footer').size())
            $('footer').fadeIn(1100, 'easeInOutSine');
        
    }, 500)

    setTimeout(function(){
        if($('#noticias_float_box').size())
            $('#noticias_float_box').animate({
                top:'26px'
            }, 1100, easing);
        $('.jspTrack_custom').animate({
            top:'0px'
        }, 1100, easing);
        $('.jspHorizontalBar').animate({
            top:'0px'
        }, 1100, easing, function(){
            jScrollPane.horizontalShowed = true;
        });
        $('.worldButtons').animate({
            top:'0px'
        }, 1100, easing);
        $('#jsAvailableInBtn').animate({
            top:'0px'
        }, 1100, easing);
    }, 2000)    
})






/*******************************************************************************
 * SLIDER PAGES
 ******************************************************************************/
$('#sliderPage').not('.jsNoScrollPane').jScrollPane({
    autoReinitialise: true,
    horizontalDragMinWidth: 200,
    horizontalDragMaxWidth: 300,
    enableKeyboardNavigation: true,
    hijackInternalLinks: true,
    animateScroll:true,
    animateDuration:2000,
    animateEase:'easeOutQuart',
    invertMouseWheelScroll:true,
    mouseWheelSpeed:668,
    horizontalGutter:0,
    verticalGutter:0,
    arrowScrollOnHover:true,
    close_jquery_ui_selectmenu:true,
    externalArrowLeft:$('.jsSliderPageBtnPrev'),
    externalArrowRight:$('.jsSliderPageBtnNext')
});
var jScrollPanel_api = $('#sliderPage').data('jsp');
var scrollToX = 0;

var resituateMapPoints = function($slide){
    $.each($slide.find('.worldPoints > div'), function(){
        var $this = $(this);
        var $img = $slide.find('img.jsSliderBackground');
        var original_h = $img.attr('height');
        var original_w = $img.attr('width');
        var current_h = $img.height();
        var current_w = $img.width();
        var point_top = $this.attr('data_top');
        var point_left = $this.attr('data_left');
        var top = current_h*point_top/original_h-$this.outerHeight()/2;
        var left = current_w*point_left/original_w-$this.outerWidth()/2;
        
        $this.css({
            top:top,
            left:left
        });
    });
};

var resizeSliderPage = function(){
    var $container = $('#container');
    
    var $sliderPage = $('#sliderPage');
    var $slidePageWpr = $('#sliderPage').find('.wpr');
    var $sliderPageSlides = $sliderPage.find('.jsSlide');
    
    var $resizeToImageWidth = $sliderPageSlides.find('img.jsResizeSlideToImageWidth');
    $.each($resizeToImageWidth, function(){
        var $this = $(this);
        var $jsSlide = $this.parents('.jsSlide');
        $jsSlide.width($this.attr('width'));
    });


    var w = 0;
    $.each($sliderPageSlides, function(){
        var $this = $(this);
        w += parseInt($this.outerWidth(true));
        _log('width: '+w+' de '+$this.outerWidth(true))
    })


    var $slidePageWidth = $sliderPage.find('.width').outerWidth(true);
    $slidePageWpr.width($slidePageWidth+1);
    
    $sliderPage.height($container.height());
    $slidePageWpr.height($container.height());
    $sliderPageSlides.height($container.height());
    if ($slidePageWidth > $sliderPage.width()){
        $sliderPage.find('.jspTrack_custom').show();
        $sliderPage.parent().find('.sliderButtons').show();
        if (jScrollPane.horizontalShowed){
            jScrollPanel_api.reinitialise({
                HorizontalBarTop0:true
            });
        }
    }else{
        $sliderPage.find('.jspTrack_custom').hide();
        $sliderPage.parent().find('.sliderButtons').hide();
    }    
};
var resizeImgSlider  = function(){
    var $slides = $('.jsSlide', '#sliderPage');
    var $wpr = $('.wpr', '#sliderPage');
    
    var resizedParents = false;
    $.each($slides, function(){
        var $this = $(this);
        var $img = $this.find('img.jsSliderBackground');
        if ($img.length<1){
            return;
        }
        var img_w = $img.attr('width');
        var img_h = $img.attr('height');
        var $container_img = $img.parent();        
      
        var height = $container_img.height();
        var width = $container_img.width();        
        var ratio = img_w/img_h;
        
        var h = height;
        var w = height*ratio;
        
        


        if ($img.hasClass('jsResizeParents')){
            $container_img.width(w);
        }else{
            if (w<width){
                w=width;
                h = width/ratio;
            }    
        }
        
        if($img.hasClass('jsResizeFullWidth')){
            //Caso particular company (solo 2)
            var $sliderPage = $('#sliderPage');
            var sidebarWidth = $('#sidebar').width();
            new_w = ($sliderPage.width()-sidebarWidth)/2;
            if ($img.hasClass('jsFirstImage')){
                new_w += sidebarWidth;
            }
            $container_img.width(new_w);
            if (new_w>w){
                w = new_w;
                h = w*(img_h/img_w);
            }
        }
        

        $img.width(w);
        $img.height(h);
        
        var $imgTop = 0;
        var $imgLeft = 0;
        //Centrar
        if ($img.hasClass('jsResizeParents')){
            $imgLeft = 0;
            if (!resizedParents){
                $wpr.width(999999);
                resizedParents = true;
            }
        }else{
            $imgLeft = ($container_img.width() - w)/2;
        }
        
        if ($img.hasClass('jsResizeFullWidth')){                        
            if (h>height){
                $imgTop = ($container_img.height() - h)/2;                
            }
            if (new_w<=w){
                $imgLeft = ($container_img.width() - w)/2;
            }
        }
        $img.css({
            'left':$imgLeft,
            'top':$imgTop
        });
        
        if ($img.hasClass('jsResituateMapPoints')){
            resituateMapPoints($this);            
        }
    });
    if (resizedParents){
        resizeSliderPage();   
    }    
};
$(function(){
    resizeSliderPage();
    resizeImgSlider(); 
});
$(window).resize(function(){
    resizeSliderPage();
    resizeImgSlider();
});


$('.jsSliderPageBtn', '#jsSliderPageButtons').click(function(e){
    var $this = $(this);
    if ($this.hasClass('disable')){
        $this.attr('href', '');
        e.preventDefault();
        return false;
    }
    var $sliderPage = $('#sliderPage');
    var $jspPane = $sliderPage.find('.jspPane');
    var $wpr = $jspPane.find('.wpr');
    var $content = $wpr.find('.jsSlideGoTo'); 

    var left = Math.abs($jspPane.position().left);
    var width = 0;
    //    var widthNext = 0;
    var slideCurrent = 1;
    var slideGoTo = -1;

    $.each($content, function(i){
        var $slide = $(this);
        //        widthNext = $slide.width();
        if (width > left+40){
            return false;
        }

        width += $slide.outerWidth(true);
        slideCurrent = i+1;
        return true;
    });   

    if ($this.hasClass('jsSliderPageBtnPrev')){
        slideGoTo = slideCurrent-1;

    }else if ($this.hasClass('jsSliderPageBtnNext')){
        slideGoTo = slideCurrent+1;
    }       

    var newSliderActiveId = '#sliderPage_slide'+slideGoTo;
    if ($(newSliderActiveId).length>0){
        $this.attr('href', newSliderActiveId);
    }else{
        return false;
    }
    return true;
});



/* Elegant image loading, thanks to paul irish */
$(function(){
    var i = 1;
    var $imgsSlides = $('.jsSlide img', '#sliderPage');    
    if ($imgsSlides.length>0){
        $imgsSlides.hide();
        $.each($imgsSlides, function(){
            var $this = $(this);
            var $slide = $this.parents('.jsSlide');
            $slide.find('.jsContent').hide();
            $slide.find('.jsLoading').fadeIn(2000);
        })

        $imgsSlides.bind('imgpreload', function (e) {
            _log('pepe');
            var $ele = $(this);            
            setTimeout(function(){
                    (function($ele){
                        $slide = $ele.parents('.jsSlide');
                        $slide.find('.jsLoading').stop(true, true).hide();
                        $slide.find('.jsContent').show();
                        $ele.fadeIn(500);
                    }($ele))
            }, i*500/*(simulacion en local)(i*1000)*/)
            i++;
        });   
    }
});









var mouseDown = false;
var mouseUp = false;
var mouseMove = false;
var startX = 0;
var endX = 0;
var currentX = 0;
var pos1, pos2;
var antes = new Date().getTime();


$('#sliderPage').not('.jsNoScrollPane').mousedown(function(e){
    e.flags = {
        dragging:false
    }
    e.preventDefault();
    $('#sliderPage').removeClass('cursor_openhand').addClass('cursor_closehand');
    mouseDown = true;
    mouseUp = false;
    startX = e.pageX;
    currentX = Math.abs($('.jspPane', '#sliderPage').position().left);
    jScrollPanel_api.stop();
//quero parar el panel al clicar encima, pero no lo consigo.
//$(jScrollPanel_api.getContentPane()).stop(true,true).css({left:$('.jspPane', '#sliderPage').position().left})

});

$(document).mouseup(function(e){
    e.preventDefault();
    $('#sliderPage').not('.jsNoScrollPane').removeClass('cursor_closehand').addClass('cursor_openhand');
    mouseDown = false;
    mouseUp = true;
    pos1 = e.pageX;
//    jScrollPanel_api.scrollByX(-(pos1-pos2)*1.65, true);
//esto esta cometnado
});
$(document).mousemove(function(e){
    e.flags = {
        dragging:false
    }
    e.preventDefault();
    mouseMove = true;



    if (mouseDown) {
        savePos2(e.pageX);
        jScrollPanel_api.scrollToX(currentX - (e.pageX - startX), false);
    }
    return false;
});


var savePos2 = function(pos){
    var now = new Date().getTime();
    if((now-antes) >= 200){
        antes = now;
        pos2 = pos;
    }    
}


$('.jsSlideOverToggle').click(function(e){
    var $target = $(e.target);
    if ($target.hasClass('jsLink') || $target.parents('.jsLink').length>0){
        return;
    }
    var $this = $(this);
    var minheight = $this.css('min-height');
    var maxheight = $this.css('max-height');
    if ($this.hasClass('jsOpened')){
        $this.stop().animate({height:minheight}, 800);
        $this.removeClass('jsOpened').addClass('jsClosed');
    }else{
        $this.stop().animate({height:maxheight}, 800);
        $this.removeClass('jsClosed').addClass('jsOpened');
    }
});


$(function(){

    $('#idiomas_toggle_btn').click(function(e){
        e.preventDefault();
        var $idiomas = $('#idiomas');
        $('#jsSliderPageButtons').fadeToggle();
        $idiomas.slideToggle();
    });
    $(document).click(function(e){
        var $idiomas = $('#idiomas');
        var $idiomas_toggle_btn = $('#idiomas_toggle_btn');
        var $target = $(e.target);
        if ($target.is($idiomas_toggle_btn) || $target.parents('#idiomas').length>0){
            return false;
        }
        $('#jsSliderPageButtons').fadeIn();
        $idiomas.slideUp();
    });



});



$('a', '#sidebar_nav').hover(function(){
    var $this = $(this);
    if ($this.hasClass('s')){
        return false;
    }
    $this.stop().animate({
        'padding-left':'37px'
    });
}, function(){
    var $this = $(this);
    if ($this.hasClass('s')){
        return false;
    }
    $this.stop().animate({
        'padding-left':'32px'
    });
});



