

var collLoaded = new Array();
var firstLoad = true;
var animDone = true;

$(function () {

	if (flashStep == 'full') { 
		// Address handler
		$.address.init(function(event) {
		    var div = $('#cw_menucollections div.cw_scrollzone'),
		    ul = $('ul.cw_mainscrolllist'),
		    ulPadding = 30;
		    var divWidth = div.width();
		    div.css({
		        overflow: 'hidden'
		    });
		    var lastLi = ul.find('li:last-child');
		
		    div.mousemove(function(e){
		    	
		    	var gap = 70;
		
				var before = div.scrollLeft();
		
		        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
		        var after = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
		        // smooth the mooving effect
	        	if (animDone)  {
			        if ( ( before > after && (before - after) > gap ) || ( before < after && (after - before) > gap ) ) {
			        		animDone = false;
			        		div.animate({ scrollLeft: after }, 1200,false, animationDone);
			        }
			        else div.animate({ scrollLeft: after }, 0);
	        	}

		    });
		    $('div#cw_menucollections div.cw_scrollzone ul a').click(function () {
		        showCollection('coll_swissbank');
		    });
		
		}).change(function(event) {
		    var selection = $('a[rel=address:' + event.value + ']');
		
		    // on affiche une collection
		    if (selection.hasClass('collection_link')) {
		        showCollection(selection.attr("name"),firstLoad);
		        $.address.title('Goldkenn | ' + selection.attr("title"));
		    }
		    // On affiche un produit
		    else if (selection.hasClass('product_link')) {
		        $(selection).closest('.cw_descriptionscollections').show();
		        showProduct(selection.attr("name"),firstLoad);
		        $.address.title('Goldkenn | ' + selection.attr("title"));
		    }
		    // On affiche le menu
		    else showMenu();
		    firstLoad = false;
		});
	}
	if (flashStep == 'collection') {
		showCollection(collectionToShow,firstLoad);
		
	}
	if (flashStep == 'product') {
		showCollection(collectionToShow,firstLoad);
        showProduct(productToShow,firstLoad);	
    }
});

function animationDone() {
	animDone = true;
}

/**
 * Affichage du menu de la page d'accueil
 */
function showMenu() {
    $('.cw_descriptionscollections').fadeOut(1000);
    $.address.value('home');
    $.address.title('Goldkenn');
}

/**
 * Affichage d'une collection
 */
function showCollection(collId,show) {
    $('.div_products').fadeOut(1000);
    if (!show)
        $('#div_'+collId).fadeIn(1000, function () {
            initCollection(collId);
        });
    else {
        $('#div_'+collId).show();
        initCollection(collId);
    }
}

/**
* Affichage d'un détail de produit
*/
function showProduct(id,show) {

    var product = id.split('_');

    // Affiche la Collection, si ce n'est déjà fait
    if($("#div_"+product[0]).css('display') != 'block' || firstLoad) {
        initCollection(product[0]);
        $("#div_"+product[0]).show();
    }

    // On cache les éventuel produits actuellement affichés
    if (!show) $(".cw_detailsproduit").fadeOut(1000);
    // On s'assure que c'est bien l'image du produit et non le slideshow qui est affiché en premier
    $("#div_"+product[1]+" .cw_imageproduitbig").show();
    $("#div_"+product[1]+" .cw_slideshowzone").hide();
    // On affiche la fiche du produit
    if (!show) {
        $('#div_'+product[0] +' .div_products').fadeIn(1000);
        $("#div_"+product[1]).fadeIn(1000);
    }
    else {
        $('#div_'+product[0] +' .div_products').show();
        $("#div_"+product[1]).show();
    }
}


/**
 * Initialise une collection (crée tous les observateurs nécessaires)
 */
function initCollection(collId) {

    // Initialisation du carousel de miniatures si ce n'est déjà fait
    if (collLoaded[collId] != true) {
        $('#div_'+collId+' .cw_jcarousel').jcarousel({
            scroll : 10
        });
        collLoaded[collId] = true;
    }

    /** EVENEMENTS **/

    // Bouton : Afficher les trois images des produits (slides)
    $('#div_'+collId+" .cw_imageproduitbig .cw_slideshow_btn div").click(function () {
            $("#div_"+collId+" .cw_imageproduitbig").hide();
            $("#div_"+collId+" .cw_slideshowzone").fadeIn(1000);
            // Effet de slideshow
            $("#div_"+collId+" .jimgMenu ul").kwicks({
                max: 450,
                duration: 1000,
                easing: 'easeOutQuad',
                complete: 'callback'
            });
        });
	
	$('#div_'+collId+" .cw_imageproduitbig img").click(function () {
			$("#div_"+collId+" .cw_imageproduitbig").hide();
			$("#div_"+collId+" .cw_slideshowzone").fadeIn(1000);
			// Effet de slideshow
			$("#div_"+collId+" .jimgMenu ul").kwicks({
				max: 450,
				duration: 1000,
				easing: 'easeOutQuad',
				complete: 'callback'
			});
		});

    // Bouton : Afficher la grande image du produit
    $('#div_'+collId+" .cw_slideshowzone .cw_slideshow_btn div").click(function () {
            $("#div_"+collId+" .cw_slideshowzone").hide();
            $("#div_"+collId+" .cw_imageproduitbig").fadeIn(1000);
        });

    // Bouton : Fermer un produit et revenir à la collection
    $('#div_'+collId+" .cw_close div a").click(function () {
        // On cache les éventuel produits actuellement affichés
        $("#div_"+collId+" .cw_detailsproduit").fadeOut(1000, function() {
            // On s'assure que c'est bien l'image du produit et non le slideshow qui est affiché en premier
            $("#div_"+collId+" .cw_imageproduitbig").show();
            $("#div_"+collId+" .cw_slideshowzone").hide();
        });

        $.address.value($(this).attr('rel').split(':')[1]);
        $.address.title('Goldkenn | '+$(this).attr('title'));
    }
    );

    // Bouton : Fermer la collection en cours
    $('#div_'+collId+" .cw_backCollections").click(function () {
        showMenu();
    });

    /** EFFETS VISUELS **/

    // Mouseover sur les miniatures (mini zoom)
    $('#div_'+collId+" ul.cw_iconscrolllist li img").hover(
        function () {
            $(this).css({
                'width': '75px',
                'height' : '75px',
                'margin-top': '-7px',
                'margin-left': '-7px'
            });
        },
        function () {
            $(this).css({
                'width': '60px',
                'height' : '60px',
                'margin-top': '0px',
                'margin-left': '0px'
            });
        });

    // Mouseover sur le bouton slideshow
    $('#div_'+collId+" .cw_imageproduitbig .cw_slideshow_btn div").hover(
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/slideshow_on.gif)'
            });
        },
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/slideshow.gif)'
            });
        });

    // Mouseover sur le bouton back
    $('#div_'+collId+" .cw_slideshowzone .cw_slideshow_btn div").hover(
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/back_on.gif)'
            });
        },
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/back.gif)'
            });
        });

    // Mouseover sur le bouton close
    $('.cw_lang_fr #div_'+collId+" .cw_close div a").hover(
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/fermer_on.gif)'
            });
        },
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/fermer.gif)'
            });
        }
        );

    // Mouseover sur le bouton close
    $('.cw_lang_en #div_'+collId+" .cw_close div a").hover(
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/close_on.gif)'
            });
        },
        function () {
            $(this).css({
                'background-image': 'url(/fileadmin/templates/goldkenn/images/close.gif)'
            });
        }
        );

    // Afficher initialement la collection (grande image du produit)
    $("#div_"+collId+" .cw_detailsproduit").fadeOut(1000);
    $("#div_"+collId+" .cw_imageproduitbig").fadeIn(1000);
    $("#div_"+collId+" .cw_slideshowzone").hide();
}

