(function() {// Easing equation, borrowed from jQuery easing plugin
// http://gsgd.co.uk/sandbox/jquery/easing/
jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

function shown(elt, child) {
    return child.offset().left + child.width() < elt.offset().left + elt.width();
};

// Utility method used to bind mouse hover
var scroller_mouse_stop = function(interval) {
    var delayed_interval;
    
    return [
        function() {
            if ( delayed_interval ) {
                window.clearInterval(delayed_interval);
                delete delayed_interval;
            }

            $(this).trigger('stop');
        },
        function() {
            // Just in case ...
            if ( delayed_interval ) {
                window.clearInterval(delayed_interval);
                delete delayed_interval;
            }

            var elt = $(this);
            var restart = function() { elt.trigger('start') };
            delayed_interval = window.setTimeout(restart, interval);
        }
    ]
};

var start_scroller = function() {
    var interval = 6000;
    $('#buttons .prev').hide();
    if ($('#slideshow div.slideshow-content li').length <= 4) {
        $('#buttons .navigation').hide();
    }

    $('#slideshow div.slideshow-content').serialScroll({
        items: 'li',
        prev: '#box_news_scroller img.prev',
        next: '#box_news_scroller img.next',
        offset: 0,
        start: 0,
        duration: 1000,
        interval: interval,
        force: true,
        stop: true,
        lock: false,
        cycle: false, //don't pull back once you reach the end
        easing: 'easeOutQuart', //use this easing equation for a funny effect
        jump: false, //click on the images to scroll to them
        exclude: 2,
        
        onBefore: function(e, elem, $pane, $items, pos){
            /**
         * 'this' is the triggered element
         * e is the event object
         * elem is the element we'll be scrolling to
         * $pane is the element being scrolled
         * $items is the items collection at this moment
         * pos is the position of elem in the collection
         * if it returns false, the event will be ignored
         */
            //those arguments with a $ are jqueryfied, elem isn't.
            e.preventDefault();
            if (this.blur) 
                this.blur();
            // Cacher les boutons si au début ou à la fin
            if (pos == 0) {
                $('#buttons .prev').fadeOut(500);
            } else if (pos > 0) {
                $('#buttons .prev').fadeIn(500);
            }
            if ($items.length - pos <= 4) {
                $('#buttons .next').fadeOut(500);
            } else if ($items.length - pos > 4) {
                $('#buttons .next').fadeIn(500);
            }
        },
        onAfter: function(elem){
            //'this' is the element being scrolled ($pane) not jqueryfied
        }
    }); 


    // Stop scrolling on hover
    // Since start starts imediately, wait interval to start again
    var stop_scroll = scroller_mouse_stop(interval);
    $('#slideshow div.slideshow-content').hover(stop_scroll[0], stop_scroll[1]);
    delete stop_scroll; // will be used later

    /* Links on all the surface of the box */
    $('#slideshow div.slideshow-content li.article').click(function(e) {
        e.preventDefault();
        var a = $(this).find('a');
        if (a.hasClass('external')) {
            window.open(a.attr('href'));
        }
        else {
            document.location = a.attr('href');
        }
    });
   
    /* Toggle images on hover */
    $('img.navigation').hover(function() {
        $(this).attr('src', $(this).attr('src').replace(/on/, 'off'));
    }, function() {
        $(this).attr('src', $(this).attr('src').replace(/off/, 'on'));
    });

}

var toggle_position = function(_current_position) {
    var current_position = _current_position;
    var new_position = current_position == 'up' ? 'down' : 'up';
    
    var container_current = $('div.container_box_news.' + current_position);
    var container_new = $('div.container_box_news.' + new_position);
    
    /* IE7 my love... */
    var width = container_current.width();
    container_current.width(width);
    
    container_current.fadeOut('fast', function() {
        $$ = $(this);
        
        /* Change the direction of the arrow */
        $arrow = $$.find('#block_extra_arrow');
        var src = $arrow.attr('src').replace(current_position, new_position);
        $arrow.attr('src', src);
        
        var html = $$.html();
        $$.empty();
        
        /* Set marging up or down according to the position of the block */
        if(new_position == 'down') {
            container_new.css('margin', '7px 0 0 0');
        }
        if(new_position == 'up') {
            container_new.css('margin', '7px 0 8px 0');
        }
        
        container_new
            .html(html)
            .removeClass(current_position)
            .addClass(new_position)
            .fadeIn('fast');
            
        start_scroller();
    
        $('#block_extra_arrow').click(function() {
            current_position = new_position;
            toggle_position(current_position);
        });
    });
    
    /* Save position */
    $.ajax({
        data: {'position' : new_position},
        type: "POST",
        url: g_url_save_cms_block_position
    });
}

window.bind_block_cms = function(_position) {
	var block_cms_current_position = _position;
    $('#block_extra_arrow').click(function() {
        toggle_position(block_cms_current_position);
    });
    
    start_scroller();
}

$(document).ready(function() {
    /* Header news */ 
    var news_interval = 3000;
    $('#latest-news-container').serialScroll({
        items: 'p',
        offset: 0,
        start: 0,
        duration: 1000,
        interval: news_interval,
        force: true,
        stop: true,
        lock: false,
        cycle: false, //don't pull back once you reach the end
        // easing: 'easeOutQuart', //use this easing equation for a funny effect
        jump: false, //click on the images to scroll to them
        axis: 'y'
    });

    var stop_scroll = scroller_mouse_stop(news_interval);
    $('#latest-news-container').hover(stop_scroll[0], stop_scroll[1]);

});

})();
