/*
// JavaScript Document
*/
//----------------------------------------------------------------------------------------------------
// Fonction d'apparition de la MEA des listes catégories
// y_positon initale : 198px
// y_position finale : 115px
// y_mouvement		 : 83px
//----------------------------------------------------------------------------------------------------
function getMeaListProductPosition (objectID){
	var Object			= $("#"+objectID);
	var position		= document.getElementById(objectID).style.top;
	/* SetUp de la position initiale si elle n'est pas définie */
	if (position == ""){
		if (isIE6())
			Object.css('top', '201px');
		else
			Object.css('top', '198px');
		/* position est redéfini */
		position		= document.getElementById(objectID).style.top;
	}
	return position;
}

function moveMeaListProductUp (objectID, storedLocation){
	var Object			= $("#"+objectID);
	var position		= getMeaListProductPosition (objectID);
	eval(storedLocation+"=1;");
	if(position == "198px" || position == "201px"){
		Object.removeClass("hide");
		Object.animate({"top": "-=83px"}, "fast");
	}
}

function moveMeaListProductDown (objectID, storedLocation){
	var Object			= $("#"+objectID);
	var position		= getMeaListProductPosition (objectID);
	eval(storedLocation+"=0;");
	if (position == "115px" || position == "118px"){
		Object.animate({"top": "+=83px"}, "fast", function(){
			Object.addClass("hide");
		});
	}
}

function ActivateMeaListProduct(id, objectID, storedLocation) {
    var trackerID;
	eval(storedLocation+"=0;");
	$(id).mouseover(function(){
		if(eval(storedLocation) == 0)
			trackerID = window.setTimeout('moveMeaListProductUp("'+objectID+'", "'+storedLocation+'")',350);
		else
			window.clearTimeout(trackerID);
	});
	$(id).mouseout(function(){
		window.clearTimeout(trackerID);
		trackerID = window.setTimeout('moveMeaListProductDown("'+objectID+'", "'+storedLocation+'")',250);
	});
}
function menuMeaListProduct (){
	var i = 0;
	while (document.getElementById("mea-"+i) && document.getElementById("mea-"+i+"_hover")){
		ActivateMeaListProduct ("#mea-"+i, "mea-"+i+"_hover", "timer_mea_"+i);
		i++;
	}
}

//----------------------------------------------------------------------------------------------------
// Fonction de surbrillance des produits en liste
//----------------------------------------------------------------------------------------------------
function ActivatelistProduct(id) {
	$(id).mouseover(function(){
		$(id).removeClass('article_hover');
		$(id).addClass('article_hover');
	});
	$(id).mouseout(function(){
		$(id).removeClass('article_hover');
	});
}
function listProduct (){
	var i = 0;
	while (document.getElementById("article-"+i)){
		ActivatelistProduct ("#article-"+i);
		i++;
	}
}

//----------------------------------------------------------------------------------------------------
// Fonction de gestion des onglets
//----------------------------------------------------------------------------------------------------
function getTabCount(){
    return $(document).find("div[class^='product-tab-']").length;
}
function Tab (objectID){
	if (document.getElementById('product-tab-'+objectID)
        && document.getElementById('product-content-'+objectID)){
        hideTabs ();
		showTabSubMenu ('product-content-'+objectID+'-submenu-0');
		$("#product-content-"+objectID).removeClass('hide');
		$("#product-tab-"+objectID).removeClass('product-tab-item-off');
		$("#product-tab-"+objectID).removeClass('product-tab-item-on');
		$("#product-tab-"+objectID).addClass('product-tab-item-on');
	}
}
function hideTabs (){
	var i=0;
    var max = getTabCount();
    for( i=0;i<max;i++){
        if( document.getElementById('product-tab-'+i) ){
            if (document.getElementById('product-content-isvalid-'+i)){
                $("#product-content-"+i).removeClass('hide');
                $("#product-content-"+i).addClass('hide');
                $("#product-tab-"+i).removeClass('hide');
                $("#product-tab-"+i).removeClass('product-tab-item-off');
                $("#product-tab-"+i).removeClass('product-tab-item-on');
                $("#product-tab-"+i).addClass('product-tab-item-off');
            }else{
                $("#product-tab-"+i).addClass('hide');
            }
        }
    }
    /*
	while (document.getElementById('product-tab-'+i) && document.getElementById('product-content-'+i)){
		if (document.getElementById('product-content-isvalid-'+i)){
			$("#product-content-"+i).removeClass('hide');
			$("#product-content-"+i).addClass('hide');
			$("#product-tab-"+i).removeClass('hide');
			$("#product-tab-"+i).removeClass('product-tab-item-off');
			$("#product-tab-"+i).removeClass('product-tab-item-on');
			$("#product-tab-"+i).addClass('product-tab-item-off');
		}else{
			$("#product-tab-"+i).addClass('hide');
		}
		i++;
	}
    */
}

//----------------------------------------------------------------------------------------------------
// Fonction d'initialisaton de la navigation pour la Tab en cours
//----------------------------------------------------------------------------------------------------
function showTabSubMenu (objectID){
	if (document.getElementById(objectID)){
		var index	= objectID.substr(0, ('product-content-x').length);
		var nbTabs	= hideTabSubMenus (index);
		if (nbTabs==1)
			$("#"+objectID).addClass('hide');
		else
			$("#"+objectID).addClass('product-submenu-item-on');
		$("#"+objectID+'-content').removeClass('hide');
	}
}
function hideTabSubMenus (objectID){
    var i=0;
	while (document.getElementById(objectID+'-submenu-'+i) && document.getElementById(objectID+'-submenu-'+i+'-content')){
		$("#"+objectID+'-submenu-'+i).removeClass('product-submenu-item-on');
		$("#"+objectID+'-submenu-'+i+'-content').removeClass('hide');
		$("#"+objectID+'-submenu-'+i+'-content').addClass('hide');
		i++;
	}
	return i;
}

//----------------------------------------------------------------------------------------------------
// Fonction d'affichage des illustrations
//----------------------------------------------------------------------------------------------------
function viewFocus (objectID){
	if (document.getElementById('focus-'+objectID)){
		hideFocus ();
		autoShowHide ('focus-'+objectID, 'show', true);
	}
}
function hideFocus (objectID){
	var i=0;
	while (document.getElementById('focus-'+i)){
		if (document.getElementById('focus-'+objectID) != document.getElementById('focus-'+i))
		autoShowHide ('focus-'+i, 'hide', true);
		i++;
	}
}

//----------------------------------------------------------------------------------------------------
// Fonction pour la pagination des testimoniaux
//----------------------------------------------------------------------------------------------------
function viewTestimonial (objectID){
	hideTestimonials ();
	$("#"+objectID).removeClass('pagination-off');
	$("#"+objectID).addClass('pagination-on');
	$("#"+objectID+"-content").removeClass('hide');
}
function hideTestimonials (){
	var i=1;
	while (document.getElementById('testimonial-pagination-'+i)){
		$("#testimonial-pagination-"+i).removeClass('pagination-on');
		$("#testimonial-pagination-"+i).removeClass('pagination-off');
		$("#testimonial-pagination-"+i).addClass('pagination-off');
		if (document.getElementById('testimonial-pagination-'+i+'-content')){
			$("#testimonial-pagination-"+i+"-content").removeClass('hide');
			$("#testimonial-pagination-"+i+"-content").addClass('hide');
		}
		i++;
	}
}

//----------------------------------------------------------------------------------------------------
// Fonction pour les testimoniaux
//----------------------------------------------------------------------------------------------------
function dejatemoigne ( ){
	if (document.getElementById('action_ok')){
		$("#testimonial-depot").addClass('hide');
		$("#testimonial-depot-done").removeClass('hide');
	}
}
function controlertemoignage (){
	window.setTimeout('dejatemoigne()', 1500);
}

//----------------------------------------------------------------------------------------------------
// Fonction pour la pagination des testimoniaux
//----------------------------------------------------------------------------------------------------
function viewGestuelle (objectID){
	hideGestuelles ();
	$("#"+objectID).removeClass('pagination-off');
	$("#"+objectID).addClass('pagination-on');
	$("#"+objectID+"-content").removeClass('hide');
}
function hideGestuelles (){
	var i=1;
	while (document.getElementById('gestuelle-pagination-'+i)){
		$("#gestuelle-pagination-"+i).removeClass('pagination-on');
		$("#gestuelle-pagination-"+i).removeClass('pagination-off');
		$("#gestuelle-pagination-"+i).addClass('pagination-off');
		if (document.getElementById('gestuelle-pagination-'+i+'-content')){
			$("#gestuelle-pagination-"+i+"-content").removeClass('hide');
			$("#gestuelle-pagination-"+i+"-content").addClass('hide');
		}
		i++;
	}
}

//----------------------------------------------------------------------------------------------------
// Fonction pour carrousel Gamme
//----------------------------------------------------------------------------------------------------
function CarrousselGamme (orientation){
	var movable	 = 0;
	var mouv	 = 288;
	if (orientation=="right" && current_position<(max_position-max_bloc)){
		movable = (max_position-max_bloc)-current_position;
		if (movable>max_bloc)
			movable = max_bloc;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "-="+mouv+"px"}, "quick");
		current_position=current_position+movable;
	}
	if (orientation=="left" && current_position>0){
		movable = max_bloc-current_position;
		if (movable<=0)
			movable = max_bloc;
		else
			movable = max_bloc-movable;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "+="+mouv+"px"}, "quick");
		current_position=current_position-movable;
	}
	if (!isIE6())
		console.log(current_position);
}
function initialiserCarroussel (){
    objectID	= "gamme-carrousel-content";
    elementID 	= document.getElementById(objectID);
    key			= 0;
    if (elementID){
        var nbNodes = document.getElementById(objectID).childNodes.length;
        for (var i=0; i<nbNodes; i++){
            var node = document.getElementById(objectID).childNodes[i];
            if ( node.nodeName.toLowerCase() == "div" && node.className.match("article") )
				key++;
        }
        max_position = key;
    }
	return max_position;
}

//----------------------------------------------------------------------------------------------------
// Fonction pour carrousel Espace Privé
//----------------------------------------------------------------------------------------------------
function CarrousselEspacePrive (orientation){
	var movable	 = 0;
	var mouv	 = 718;
	var max_bloc = 1;
	if (orientation=="right" && current_position<(max_position-max_bloc)){
		movable = (max_position-max_bloc)-current_position;
		if (movable>max_bloc)
			movable = max_bloc;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "-="+mouv+"px"}, "quick");
		current_position=current_position+movable;
	}
	if (orientation=="left" && current_position>0){
		movable = max_bloc-current_position;
		if (movable<=0)
			movable = max_bloc;
		else
			movable = max_bloc-movable;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "+="+mouv+"px"}, "quick");
		current_position=current_position-movable;
	}
	if (!isIE6())
		console.log(current_position);
}

//----------------------------------------------------------------------------------------------------
// Fonction pour carrousel ListeProduit
//----------------------------------------------------------------------------------------------------
function CarrousselListeProduit (orientation){
	var movable	 = 0;
	var mouv	 = 112;
	var max_bloc = 4;
	if (orientation=="right" && current_position<(max_position-max_bloc)){
		movable = (max_position-max_bloc)-current_position;
		if (movable>max_bloc)
			movable = max_bloc;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "-="+mouv+"px"}, "quick");
		current_position=current_position+movable;
	}
	if (orientation=="left" && current_position>0){
		movable = max_bloc-current_position;
		if (movable<=0)
			movable = max_bloc;
		else
			movable = max_bloc-movable;
		mouv = mouv*movable;
		$("#gamme-carrousel-content").animate({"left": "+="+mouv+"px"}, "quick");
		current_position=current_position-movable;
	}
	if (!isIE6())
		console.log(current_position);
	return true;
}
function DirectAccessForCarrousselListeProduit (position){
	var max_bloc 	= 4;
	var mouvement 	= 0;
	for (var i=0; i<position; i++){
		var movable	 = 0;
		var mouv	 = 112;
		if (current_position<(max_position-max_bloc)){
			movable = (max_position-max_bloc)-current_position;
			if (movable>max_bloc)
				movable = max_bloc;
			mouv = mouv*movable;
			current_position=current_position+movable;
			mouvement = mouvement+mouv
		}
	}
	$("#gamme-carrousel-content").animate({"left": "-="+mouvement+"px"}, "quick");
	return true;
}
function voirProduitListe ( ObjectID, Url ){
	initialiserProduitListe();
	var AjaxFile			= Url;
	if (document.getElementById('carousel-illustration-'+ObjectID)){
		ProduitListeReset ( );
		$("#carousel-illustration-"+ObjectID).addClass("illustration-on");
		$("#carousel_product").load (Url+"?ViewMode=viewLessMode", function() {
			$("#carousel_product").removeClass("loader");
			if (initialiser != null)
				initialiser();
		});
	}
}
function initialiserProduitListe (){
	var i=0;
	while ( document.getElementById('carousel-illustration-'+i) ){
		$("#carousel-illustration-"+i).removeClass("illustration-on");
		i++;
	}
}
function ProduitListeReset ( ){
    if (document.getElementById('carousel_product')){
        document.getElementById('carousel_product').innerHTML = "<br /><br />";
        $("#carousel_product").addClass('loader');
	}
}

//----------------------------------------------------------------------------------------------------
// Gestion des variants
//----------------------------------------------------------------------------------------------------
function effacerColoris ( ){
	var container		= "touches";
	var el				= "touche-";
    var elementID		= document.getElementById(container);
    if (elementID){
        var nbNodes		= elementID.childNodes.length;
        for (i=0; i<nbNodes; i++){
            var node = elementID.childNodes[i];
            if ( node.nodeName.toLowerCase() == "div" && node.id.match(el) ){
                if (document.getElementById(node.id)){
                    $("#"+node.id).removeClass("touche touche-on");
                    $("#"+node.id).removeClass("touche touche-off");
                    $("#"+node.id).addClass("touche touche-off");
                }
            }
        }
    }
}
function effacerMatieres ( ){
	var container		= "matieres";
	var el				= "matiere-";
    var elementID		= document.getElementById(container);
    if (elementID){
        var nbNodes		= elementID.childNodes.length;
        for (i=0; i<nbNodes; i++){
            var node = elementID.childNodes[i];
            if ( node.nodeName.toLowerCase() == "div" && node.id.match(el) ){
                if (document.getElementById(node.id)){
                    $("#"+node.id).removeClass("hide");
                    $("#"+node.id).addClass("hide");
                }
            }
        }
    }
}
function ChangerVariant ( sku ){
	/* désactivation des variants */
	effacerColoris ( );
	effacerMatieres ( );
	/* activation du variant sélectionné */
	$("#touche-"+sku).removeClass("touche touche-off");
	$("#touche-"+sku).addClass("touche touche-on");
	$("#matiere-"+sku).removeClass("hide");
	/* affichage de la référence du variant */
	if (document.getElementById("sku"))
		document.getElementById("sku").innerHTML = sku;
    /* activation de la select box */
    if (document.f_basket.reference.nodeName.toLowerCase()=="select"){
        var select = document.f_basket.reference;
        for (i=0;i<select.options.length;i++){
            if (select.options[i].value==sku)
                select.options[i].selected = true;
        }
    }
}

//----------------------------------------------------------------------------------------------------
// Variables générales
//----------------------------------------------------------------------------------------------------
var current_position 	= 0;
var max_position		= 0;
var max_bloc			= 3;

function initialiserPagination (){
    viewGestuelle	('gestuelle-pagination-1');
    viewTestimonial ('testimonial-pagination-1');
}

//----------------------------------------------------------------------------------------------------
// Exécution automatique des fonctions
//----------------------------------------------------------------------------------------------------
function initialiser (){
	if (Tab != null){
		if (document.getElementById('f_highlight'))
			Tab(document.f_highlight.highlight.value);
		else
			Tab(0);
	}
	if (viewFocus != null)
		viewFocus(0);
	if (initialiserCarroussel != null)
		initialiserCarroussel();
	if (initialiserPagination != null)
		initialiserPagination();
	if (menuMeaListProduct != null)
		menuMeaListProduct();
	if (listProduct != null)
		listProduct();
}
$(document).ready(function () {
	if (initialiser != null)
		initialiser();
});
