/**
 * 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 */


/* JAVASCRIPT VIDEO */
function showVideo() {
	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';
	}
}

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='block';
	}
}
/* 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';
    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';
    }
    
} 

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='block';
    }    
}
/* 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';
    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';
    }
    
} 

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='block';
    }    
}
/* 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();
        });
    }
}
/* END JAVASCRIPT EXPAND/COLLAPSE PRODUCT CONTENT */
 

function verticalAlignAddToCartButtonText(){
	
	//addItemButton
    addToCartText = jQuery("div#addItemButton > a > span");
    if(addToCartText.height() <= 15){
    	addToCartText.css("position", "static");
    	addToCartText.css("bottom", "0");
    }
    
    //addEbookingButton
    addToEbookingCartText = jQuery("div#addEbookingButton > a > span");
    if(addToEbookingCartText.height() <= 15){
    	addToEbookingCartText.css("position", "static");
    	addToEbookingCartText.css("bottom", "0");
    }
}

/* BEGIN JIRA CORE-4495 */
function showEBookingConfirmPopup(){
	jQuery("#ebookingaddform").submit(function() {
		s = jQuery(this).serialize();
		jQuery.ajax({
		    type: "POST",
		    data: s,
		    url: jQuery(this).attr("action"),
		    success: function(retour){
				var url = jQuery("#urlPopup").val();
				var quantity = jQuery("select.quantity-select").val();
				url = url+'/~quantity='+quantity;
				$ShowPopup(url);
		    }
		});
		return false;
	});
	jQuery("#ebookingaddform").submit();
}
/* END JIRA CORE-4495 */
