function l(foo) {if(window.console) {console.log(foo);}}

function Lecole() {
    var selected;
    var container_width = 0;
    var DEFAULT_ITEM = $('#gallery-list .productGroup').eq(0);
    var FADE_START = 0.5;
    var FADE_STOP = 0.25;
    var FADE_FINISH = 0;
    var SCROLLSPEED = 350
    var FADESPEED = 650
    var GALLERYSPEED = 450
    
    var w = 0;
    var move = 0;
    


    var init = function() {
        $('#gallery-list li.productGroup')
            .mouseenter(function() {
                var c = $(this);
                if(c.data('selected') == undefined) {
                    c.find('.overlay').stop().fadeTo(150,FADE_STOP);
                }
            })
            .mouseleave(function() {
                var c = $(this);
                if(c.data('selected') == undefined) {
                    c.find('.overlay').stop().fadeTo(150,FADE_START);
                }
            })       
            .each(function() {
                container_width += $(this).width();
                $('#gallery-list').width(container_width);
            })
            .click(function() {
                set_item($(this));
            });
            
		/*
        $(window).resize(function() {
            // always keeps the selected item centered
            //reset_screen();
        });
        */
    };
    
    var reset_screen = function() {
        set_item(selected, 0);    
    }
    
    var set_item = function(elem, speed) {
	    if(!selected || selected.index() != elem.index()) {

        if(!elem) return;
        if(!speed && speed != 0) speed = GALLERYSPEED;
        var c = elem;

        w = ( $(window).width() < $("#gallery").width() ) ? $(window).width() : $("#gallery").width();
        
        if(selected) {
            if(c.index() != selected.index() || speed != 0) {
                selected.data('selected', null);
                selected.find('.overlay').stop().fadeTo(speed == 0 ? 0 : 100, FADE_START);
                selected.find('.productGroupLinks').stop().fadeTo(speed == 0 ? 0 : 100, FADE_FINISH);
            }
        }
        
        // make the current image marked as selected
        c.data('selected', true);
        $('li.productGroup.active').removeClass('active');
        c.addClass('active');
        
        var container = $('#gallery-container');
        var list = $('#gallery-list');
        var root = $('#gallery-list li.productGroup');
        var length = root.length;
    	var first = root.eq(0);
    	var last = root.eq(length - 1);
     
        // carousel
        if(c.index() <= 1) { // prepend the last item in the list
            last.prependTo(list);
			container.scrollLeft( last.width() + container.scrollLeft() );
        }
        if(c.index() >= length-3) { // append the first item in the list
            first.appendTo(list);
			container.scrollLeft( container.scrollLeft() - first.width() );
        }
        
        // animate the gallery to the selected item
        $('#gallery-container').animate({
            'scrollLeft': (c.position().left + $(this).scrollLeft()) - ((w / 2) - (c.width() / 2))
        }, speed,'easeInOutQuad');
        
        c.find('.overlay').fadeTo(speed == 0 ? 0 : 100, FADE_FINISH);
        c.find('.productGroupLinks').fadeTo(speed == 0 ? 0 : 100, 1);
        
        selected = c;
        }
    }
    
    init();
    set_item(DEFAULT_ITEM, 0);

};


$(function() {
	if($('#gallery-container').length > 0) {
		var lecole = new Lecole();
	}
});
