jQuery.noConflict();
(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;
            }

            jQuery(this).trigger('stop');
        },
        function() {
            // Just in case ...
            if ( delayed_interval ) {
                window.clearInterval(delayed_interval);
                delete delayed_interval;
            }

            var elt = jQuery(this);
            var restart = function() { elt.trigger('start') };
            delayed_interval = window.setTimeout(restart, interval);
        }
    ]
};

var start_scroller = function() {
    var interval = 10000;
    jQuery('#buttons .prev').hide();
    if (jQuery('#slideshow div.slideshow-content li').length <= 3) {
        jQuery('#buttons .navigation').hide();
    }

    jQuery('#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: true, //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, jQuerypane, jQueryitems, pos){
            /**
         * 'this' is the triggered element
         * e is the event object
         * elem is the element we'll be scrolling to
         * jQuerypane is the element being scrolled
         * jQueryitems 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 jQuery 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) {
                jQuery('#buttons .prev').fadeOut(500);
            } else if (pos > 0) {
                jQuery('#buttons .prev').fadeIn(500);
            }
            if (jQueryitems.length - pos <= 3) {
                jQuery('#buttons .next').fadeOut(500);
            } else if (jQueryitems.length - pos > 3) {
                jQuery('#buttons .next').fadeIn(500);
            }
        },
        onAfter: function(elem){
            //'this' is the element being scrolled (jQuerypane) not jqueryfied
        }
    }); 


    // Stop scrolling on hover
    // Since start starts imediately, wait interval to start again
    var stop_scroll = scroller_mouse_stop(interval);
    jQuery('#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 */
    jQuery('#slideshow div.slideshow-content li.article').click(function(e) {
        e.preventDefault();
        var a = jQuery(this).find('a');
        if (a.hasClass('external')) {
            window.open(a.attr('href'));
        }
        else {
            document.location = a.attr('href');
        }
    });
   
    /* Toggle images on hover */
    jQuery('img.navigation').hover(function() {
        jQuery(this).attr('src', jQuery(this).attr('src').replace(/on/, 'off'));
    }, function() {
        jQuery(this).attr('src', jQuery(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 = jQuery('div.container_box_news.' + current_position);
    var container_new = jQuery('div.container_box_news.' + new_position);
    
    /* IE7 my love... */
    var width = container_current.width();
    container_current.width(width);
    
    container_current.fadeOut('fast', function() {
        jQueryjQuery = jQuery(this);
        
        /* Change the direction of the arrow */
        jQueryarrow = jQueryjQuery.find('#block_extra_arrow');
        var src = jQueryarrow.attr('src').replace(current_position, new_position);
        jQueryarrow.attr('src', src);
        
        var html = jQueryjQuery.html();
        jQueryjQuery.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();
    
        jQuery('#block_extra_arrow').click(function() {
            current_position = new_position;
            toggle_position(current_position);
        });
    });
    
    /* Save position */
    jQuery.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;
    jQuery('#block_extra_arrow').click(function() {
        toggle_position(block_cms_current_position);
    });
    
    start_scroller();
}

jQuery(document).ready(function() {
    /* Header news */ 
    var news_interval = 10000;
    jQuery('#latest-news-container').serialScroll({
        items: 'p',
        offset: 0,
        start: 0,
        duration: 1000,
        interval: news_interval,
        force: true,
        stop: true,
        lock: false,
        cycle: true, //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);
    jQuery('#latest-news-container').hover(stop_scroll[0], stop_scroll[1]);

});

})();
