var api = '';
var scroller = '';
var thumbWidth = 90;
var thumbHeight = 90;
var thumbMargin = 12;

$(document).ready(function(){
    
    
    $('input.ph').placeholder();    
    $('#globalnav > li').subnavMagique();
    
    /* Youtube wmode fix */
    $('iframe.youtube').each(function() {
        var url = $(this).attr("src");
        $(this).attr("src",url+"?wmode=transparent");
    });
    
    /* Home buttons */
        $('#home_buttons .bt').hover(
            function(){
                $(this).addClass('hover');
            },
            function(){
                $(this).removeClass('hover');    
            });
        $('#home_buttons .bt').click(function(){
           var link = $(this).find('h2 a').attr('href');
           window.location = link;
        });
    
    /* Tooltip */
        $('#main .tt').not('.print.tt').tipTip({
            defaultPosition: 'top',
            edgeOffset: 5,
            delay: 0
        });
        $('.print.tt').tipTip({
            defaultPosition: 'top',
            edgeOffset: 5,
            delay: 0
        });
        $('#header .tt').tipTip({ delay: 0 });
        
    // Slideshow HOME
         $('#home #slideshow #medias').cycle({
            fx:     'fade', 
            speed:   2000,
            timeout: 5000,
            easing: 'easeOutExpo',
            next:   '#home #slideshow .next',
            prev:   '#home #slideshow .prev'
        });
    
    // Slideshow SHOWROOM
         $('#showroom #slideshow #medias').cycle({
            fx:     'scrollHorz', 
            speed:  600,
            timeout: 0, /* @oli si tu veux mettre un scrolling chronomètré, c'est ici en ms */
            easing: 'easeOutExpo',
            next:   '#media_container .next', 
            prev:   '#media_container .prev',
            pager:  '#slideshow_thumbs_content',
            pagerAnchorBuilder: function(idx, slide) {
                thumb = $(slide).data('cycle-thumb');
                return '<div><a href="#"><img src="' + thumb + '" width="'+ thumbWidth +'" height="'+ thumbHeight +'" /></a></div>'; 
            }
        });
            
    scroller = $('#slideshow_thumbs_panel');
    scroller.jScrollPane({
        clickOnTrack: true,
        mouseWheelSpeed : 500,
        showArrows: false,
        animateScroll: true,
        animateDuration: 400,
        trackClickSpeed: 20,
        hideFocus: true,
        animateEase: 'easeOutQuad'/*,
        horizontalDragMinWidth: 80,
        horizontalDragMaxnWidth: 80*/
    });

    $('.li_overflow_hidden li').each(function() {
        if ($(this).index() <= 1) {
            $(this).css('border-top', '0').css('padding-top', '0');
        }
    });
    
});

// redefine Cycle's updateActivePagerLink function 
$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
    $(pager).find('div').removeClass('active')
        .filter('div:eq('+currSlideIndex+')').addClass('active');
        var api = getJScrollApi();
        if(api != undefined) {
            targetY = currSlideIndex * (thumbHeight + thumbMargin);
            api.scrollToY(targetY);
        }
};

function getJScrollApi() {
    return $('#slideshow_thumbs_panel').data('jsp');
}


$.fn.subnavMagique = function() {
    $(this).each(function(i) {
        var $subnav = $(this).find('.subnav');
        $(this).hover(
            function() {
                $subnav.slideDown('normal', 'easeOutQuad');
                },
            function() {
                $subnav.hide();
            }
        ); 
    });
}


/* Scroll page offset fix */
/*
 * (c)2011 John Pezzetti
 *
 * addDomLoadEvent is (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
 *
 * For more info, see:
 * http://www.johnpezzetti.com/?p=141
 * http://www.thefutureoftheweb.com/blog/adddomloadevent
 * http://www.alexandre-gomes.com/?p=115
 * http://stackoverflow.com/questions/681087/how-can-i-detect-a-scrollbar-presence-using-javascript-in-html-iframe
 *
 */
 
addDOMLoadEvent = (function(){
    // create event function stack
    var load_events = [],
        load_timer,
        script,
        done,
        exec,
        old_onload,
        init = function () {
            done = true;
 
            // kill the timer
            clearInterval(load_timer);
 
            // execute each function in the stack in the order they were added
            while (exec = load_events.shift())
                exec();
 
            if (script) script.onreadystatechange = '';
        };
 
    return function (func) {
        // if the init function was already ran, just run this function now and stop
        if (done) return func();
 
        if (!load_events[0]) {
            // for Mozilla/Opera9
            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", init, false);
 
            // for Internet Explorer
            /*@cc_on @*/
            /*@if (@_win32)
                document.write("<script id=__ie_onload defer src=//0><\/scr"+"ipt>");
                script = document.getElementById("__ie_onload");
                script.onreadystatechange = function() {
                    if (this.readyState == "complete")
                        init(); // call the onload handler
                };
            /*@end @*/
 
            // for Safari
            if (/WebKit/i.test(navigator.userAgent)) { // sniff
                load_timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState))
                        init(); // call the onload handler
                }, 10);
            }
 
            // for other browsers set the window.onload, but also execute the old window.onload
            old_onload = window.onload;
            window.onload = function() {
                init();
                if (old_onload) old_onload();
            };
        }
 
        load_events.push(func);
    }
})();
