/**
 * JS methods used on product detail page.
 */


/* JAVASCRIPT FOR IMAGES */
function selectImageMediumImpl(pIndex, pDisplay, displayMediumImage) {
	hideVideo();
	hideImages3D();
	hideVii();
	
	if (displayMediumImage) {
		var linkedMediumImage = _linkImages[pIndex].medium;
	}
	else {
		var linkedMediumImage = _linkImages[pIndex].large;
	}
	
	if (pDisplay && linkedMediumImage!="") {
		document.getElementById("fiche_zoom_produit").src=linkedMediumImage;
		_indexImageSelected=pIndex;
		

	} else {
		_indexImageSelected=0;
		document.getElementById("fiche_zoom_produit").src=_linkImages[_indexImageSelected].large;
	}
}

/* END JAVASCRIPT FOR IMAGES */


/** set the selected link between photos, video, 3D and vii */
var linkTab = new Array();
linkTab[0] = "button-photos";
linkTab[1] = "button-videos";
linkTab[2] = "button-3d";
linkTab[3] = "button-vii";
function setSelectedButtonLink(linkId) {
	for (var i=0 ; i<4 ; i++) {
		var currentLinkId = linkTab[i];
		if (document.getElementById(currentLinkId)) {
			if (currentLinkId == linkId) {
				jQuery("#" + currentLinkId).addClass("boutons-on");
			}
			else {
				jQuery("#" + currentLinkId).removeClass("boutons-on");
			}
		}
	}
}


/* JAVASCRIPT VIDEO */
function showVideo(index) {
	
	document.getElementById('detail-zoom').style.display='none';
	// show the videos miniatures, hide the photos miniatures
	if (document.getElementById('miniatures-videos')!=null) document.getElementById('miniatures-videos').style.display='block';
	if (document.getElementById('miniatures-photos')!=null) document.getElementById('miniatures-photos').style.display='none';
	
	if (document.getElementById('product-video-screen')!=null) document.getElementById('product-video-screen').style.display='block';
	if (document.getElementById('product-images3d-screen')!=null) document.getElementById('product-images3d-screen').style.display='none';
	if (document.getElementById('product-vii-screen')!=null) document.getElementById('product-vii-screen').style.display='none';
	document.getElementById('fiche_zoom_produit').style.display='none';
	// hide main tab (countries)
	if (document.getElementById('description_produit')) {
		document.getElementById('description_produit').style.display='none';
	}
	// hide sticker
	if (document.getElementById('sticker')) {
		document.getElementById('sticker').style.display='none';
	}
	
	// displays good video in function of index (BTFR-455)
	if (index != undefined && videosTab != undefined) {
		var videoToShow = videosTab[index];
		if (videoToShow != undefined) {
			s1.addVariable("file",videoToShow);
			s1.write("video-container");
		}
	}
	
	// class of links
	setSelectedButtonLink("button-videos");
}

/** Show the photos miniatures - hide videos miniatures, and displayed video */
function showPhotos() {
	document.getElementById('detail-zoom').style.display='block';
	if (document.getElementById('miniatures-photos')!=null) document.getElementById('miniatures-photos').style.display='block';
	if (document.getElementById('miniatures-videos')!=null) document.getElementById('miniatures-videos').style.display='none';
	hideVideo();
	hideImages3D();
	hideVii();
	setSelectedButtonLink("button-photos");
}

/** mouse over on videos miniatures -- set "selected" class */
function setSelectedVideo(videoMiniatureIndex) {
	if (_indexVideoMiniatureSelected!=null) {
        document.getElementById("mini_vid_"+_indexVideoMiniatureSelected).className="miniature";
    }
	document.getElementById("mini_vid_"+videoMiniatureIndex).className="miniature selected";
	_indexVideoMiniatureSelected=videoMiniatureIndex;
}

function hideVideo() {
	if (document.getElementById('product-video-screen')!=null) document.getElementById('product-video-screen').style.display='none';
	document.getElementById('fiche_zoom_produit').style.display='inline';
	// show main tab (countries)
	if (document.getElementById('description_produit')) {
		document.getElementById('description_produit').style.display='block';
	}
	// show sticker
	if (document.getElementById('sticker')) {
		document.getElementById('sticker').style.display='inline';
	}
}
/* END JAVASCRIPT VIDEO */

/* JAVASCRIPT IMAGES 3D */

/* Begin Menu Fix : add wmode parameter - opaque or transparent */
function addWModeImages3D(pIdIframe) {
	/* URL of the image 3d must be in the same domain than the website */
	if (document.getElementById('product-images3d-screen')!=null) {
		var myf = document.getElementById(pIdIframe);

		if (myf != null) {

			try {

				myf = myf.contentWindow.document || myf.contentDocument;

				var embed = myf.body.getElementsByTagName('embed')[0];
				if (embed != null) {
					var new_embed;
					// everything but Firefox & Konqueror
					if(embed.outerHTML) {
						var html = embed.outerHTML;
						// replace an existing wmode parameter
						if(html.match(/wmode\s*=\s*('|")[a-zA-Z]+('|")/i))
							new_embed = html.replace(/wmode\s*=\s*('|")window('|")/i,"wmode='transparent'");
						// add a new wmode parameter
						else
							new_embed = html.replace(/<embed\s/i,"<embed wmode='transparent' ");
						// replace the old embed object with the fixed version
						embed.insertAdjacentHTML('beforeBegin',new_embed);
						embed.parentNode.removeChild(embed);
					} else {
						// cloneNode is buggy in some versions of Safari & Opera, but works fine in FF
						new_embed = embed.cloneNode(true);
						if(!new_embed.getAttribute('wmode') || new_embed.getAttribute('wmode').toLowerCase()=='window')
							new_embed.setAttribute('wmode','transparent');
						embed.parentNode.replaceChild(new_embed,embed);
					}
				}

				var object = myf.body.getElementsByTagName('object')[0];
				if (object != null) {
					var new_object;
					// object is an IE specific tag so we can use outerHTML here
					if(object.outerHTML) {
						var html = object.outerHTML;
						// replace an existing wmode parameter
						if(html.match(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")[a-zA-Z]+('|")\s*\/?\>/i))
							new_object = html.replace(/<param\s+name\s*=\s*('|")wmode('|")\s+value\s*=\s*('|")window('|")\s*\/?\>/i,"<param name='wmode' value='transparent' />");
						// add a new wmode parameter
						else
							new_object = html.replace(/<\/object\>/i,"<param name='wmode' value='transparent' />\n</object>");
						// loop through each of the param tags
						var children = object.childNodes;
						for(j=0; j<children.length; j++) {
							if(children[j].getAttribute('name').match(/flashvars/i)) {
								new_object = new_object.replace(/<param\s+name\s*=\s*('|")flashvars('|")\s+value\s*=\s*('|")[^'"]*('|")\s*\/?\>/i,"<param name='flashvars' value='"+children[j].getAttribute('value')+"' />");
							}
						}
						// replace the old embed object with the fixed versiony
						object.insertAdjacentHTML('beforeBegin',new_object);
						object.parentNode.removeChild(object);
					}
				}

			} catch (err) { } // cross-domain forbidden
		}
	}
}
/* End Menu Fix */ 

function showImages3D() {
	addWModeImages3D('iframeProductUrlImages3D');
	
    if (document.getElementById('product-images3d-screen')!=null) document.getElementById('product-images3d-screen').style.display='block';
    if (document.getElementById('product-video-screen')!=null) document.getElementById('product-video-screen').style.display='none';
    if (document.getElementById('product-vii-screen')!=null) document.getElementById('product-vii-screen').style.display='none';
    
    if (document.getElementById('miniatures-photos')!=null) document.getElementById('miniatures-photos').style.display='none';
	if (document.getElementById('miniatures-videos')!=null) document.getElementById('miniatures-videos').style.display='none';
    
    document.getElementById('fiche_zoom_produit').style.display='none';
    //document.getElementById('product-video-screen').sendEvent('playpause');
    // hide main tab (countries)
    if (document.getElementById('description_produit')) {
        document.getElementById('description_produit').style.display='none';
    }
    // hide sticker
    if (document.getElementById('sticker')) {
        document.getElementById('sticker').style.display='none';
    }
    
    setSelectedButtonLink("button-3d");
} 

function hideImages3D() {
    if (document.getElementById('product-images3d-screen')!=null) document.getElementById('product-images3d-screen').style.display='none';
    document.getElementById('fiche_zoom_produit').style.display='inline';//"block"
    // show main tab (countries)
    if (document.getElementById('description_produit')) {
        document.getElementById('description_produit').style.display='block';
    }
    // show sticker
    if (document.getElementById('sticker')) {
        document.getElementById('sticker').style.display='inline';
    }    
}
/* END JAVASCRIPT IMAGES 3D */

/* JAVASCRIPT VIDEO INTERACTIVE */
function showVii() {
	addWModeImages3D('iframeProductUrlVideoInteractive');
	
	if (document.getElementById('product-vii-screen')!=null) document.getElementById('product-vii-screen').style.display='block';
	if (document.getElementById('product-images3d-screen')!=null) document.getElementById('product-images3d-screen').style.display='none';
    if (document.getElementById('product-video-screen')!=null) document.getElementById('product-video-screen').style.display='none';
    
    if (document.getElementById('miniatures-photos')!=null) document.getElementById('miniatures-photos').style.display='none';
	if (document.getElementById('miniatures-videos')!=null) document.getElementById('miniatures-videos').style.display='none';
    
    document.getElementById('fiche_zoom_produit').style.display='none';
    //document.getElementById('product-video-screen').sendEvent('playpause');
    // hide main tab (countries)
    if (document.getElementById('description_produit')) {
        document.getElementById('description_produit').style.display='none';
    }
    // hide sticker
    if (document.getElementById('sticker')) {
        document.getElementById('sticker').style.display='none';
    }
    
    setSelectedButtonLink("button-vii");
} 

function hideVii() {
    if (document.getElementById('product-vii-screen')!=null) document.getElementById('product-vii-screen').style.display='none';
    document.getElementById('fiche_zoom_produit').style.display='inline';//"block"
    // show main tab (countries)
    if (document.getElementById('description_produit')) {
        document.getElementById('description_produit').style.display='block';
    }
    // show sticker
    if (document.getElementById('sticker')) {
        document.getElementById('sticker').style.display='inline';
    }    
}
/* END JAVASCRIPT IMAGES 3D */

/* JAVASCRIPT EXPAND/COLLAPSE PRODUCT CONTENT */
function expandProductContent(pThis, pExpand) {
    var expandIt=false;
    if (expandProductContent.arguments.length==2) {
        expandIt=pExpand;
    }
    var eltProductContent=jQuery(pThis);
    var spanProductContent=jQuery("span:first" , eltProductContent);
    var tableProductContent=jQuery("~ table" , eltProductContent)[0];
    var backTopProductContent=jQuery("~ div.backTop" , eltProductContent)[0];
    var paragraphProductContent=jQuery("~ p" , eltProductContent);
    
    if (!expandIt && spanProductContent.hasClass("icon-expand")) {
        spanProductContent.removeClass("icon-expand");
        spanProductContent.addClass("icon-collapse");
        jQuery(tableProductContent).slideUp(300, function () {
            jQuery(backTopProductContent).hide();
            jQuery(paragraphProductContent).hide();
        });
    } else {
        spanProductContent.removeClass("icon-collapse");
        spanProductContent.addClass("icon-expand");
        jQuery(tableProductContent).slideDown(300, function () {
            jQuery(backTopProductContent).removeClass("top-collapse");
            jQuery(backTopProductContent).show();
            jQuery(paragraphProductContent).show();
        });
    }
}


/*
 * 
 */

function expandProductContentbtfr(pThis, pExpand) {
    var expandIt=false;
    if (expandProductContentbtfr.arguments.length==2) {
        expandIt=pExpand;
    }
    var eltProductContent=jQuery(pThis);
    var spanProductContent=jQuery("span:first" , eltProductContent);
    //var tableProductContent=jQuery("~ table" , eltProductContent)[0];
    //var backTopProductContent=jQuery("~ div.backTop" , eltProductContent)[0];
    var idTable = pThis.id;
    
    //selectionner la table correspondante
    if(idTable == 'productDetailSizeColors') {
		var tableProductContent=document.getElementById("productDetailSizeColorsTab");
		var backTopProductContent=document.getElementById("productDetailSizeColorsBackTop");
    }
    
    if(idTable == 'productDetailInfosDetailled') {
		var tableProductContent=document.getElementById("productDetailInfosDetailledTab");
		var backTopProductContent=document.getElementById("productDetailInfosDetailledBackTop");
    }
    
    if(idTable == 'productDetailInfosMore') {
		var tableProductContent=document.getElementById("productDetailInfosMoreTab");
		var paragraphProductContent1=document.getElementById("productDetailInfosMorePar1");
		var paragraphProductContent2=document.getElementById("productDetailInfosMorePar2");
		var backTopProductContent=document.getElementById("productDetailInfosMoreBackTop");
    }
    
    if(idTable == 'productDetailTechnicalBrands') {
		var tableProductContent=table=document.getElementById("productDetailTechnicalBrandsTab");
		var backTopProductContent=document.getElementById("productDetailTechnicalBrandsBackTop");
    }
    
    if(idTable == 'productDetailInfosTechnical') {
		var tableProductContent=document.getElementById("productDetailInfosTechnicalTab");
		var backTopProductContent=document.getElementById("productDetailInfosTechnicalBackTop");
    }

	
    //var paragraphProductContent=jQuery("~ p" , eltProductContent);
    
    if (!expandIt && spanProductContent.hasClass("icon-expand")) {
        spanProductContent.removeClass("icon-expand");
        spanProductContent.addClass("icon-collapse");
        jQuery(tableProductContent).slideUp(300, function () {
            jQuery(backTopProductContent).hide();
            jQuery(paragraphProductContent1).hide();
            jQuery(paragraphProductContent2).hide();
        });
    } else {
        spanProductContent.removeClass("icon-collapse");
        spanProductContent.addClass("icon-expand");
        jQuery(tableProductContent).slideDown(300, function () {
            jQuery(backTopProductContent).removeClass("top-collapse");
            jQuery(backTopProductContent).show();
            jQuery(paragraphProductContent1).show();
            jQuery(paragraphProductContent2).show();
        });
    }
}

/* END JAVASCRIPT EXPAND/COLLAPSE PRODUCT CONTENT */
