var flashHostURI = "/wcsstore/ConsumerDirectStorefrontAssetStore/";

var FlashObject = function(prd, cat, ver,zoom, extensionImg,zoomLienPlie) {
	this.prd = prd;
	this.cat = cat;
	this.version = ver;
	this.params = new Object();
	this.addParam('quality', 'high'); // default to high
	this.doDetect = getQueryParamValue('detectflash');
	this.flashPath = flashHostURI + "flash/product/"+this.cat+"/";
	this.imagePath = flashHostURI + "images/catalog/product/";	
	this.zoomLienPlie = zoomLienPlie;
	
	this.extensionImg = ".jpg";
	if(typeof(extensionImg) != "undefined" && extensionImg != null){
		this.extensionImg = extensionImg;
	}
}

var FOP = FlashObject.prototype;
FOP.addParam = function(name, value) { this.params[name] = value; }
FOP.getParams = function() { return this.params; }
FOP.getParam = function(name) { return this.params[name]; }

FOP.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    return (paramTags == "") ? false:paramTags;
}

FOP.getHTML = function() {
	var flashHTML = "";
	flashHTML += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="534" height="476" id="newmodule" align="middle">';
	flashHTML += '<param name="allowScriptAccess" value="sameDomain" />';
	flashHTML += '<param name="scale" value="noscale" />';
	flashHTML += '<param name="quality" value="high" />';
	flashHTML += '<param name="wmode" value="transparent" />';
	flashHTML += '<param name="SWLIVECONNECT" value="true" />';
	flashHTML += '<param name="bgcolor" value="#ffffff" />';
	
	if(typeof(this.zoomLienPlie) != "undefined" && this.zoomLienPlie != null){			
		flashHTML += '<param name="FlashVars" value="xmlProduct=<product id=\'' + this.prd + '\' masque=\'' + flashHostURI + 'flash/product/zoom/masque/' + this.cat + '/lemasque.swf\' img=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/tied/' + this.prd + this.extensionImg + '\' imgzoom=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/tied_zoom/' + this.prd + this.extensionImg + '\' />" />';
	} else {
		flashHTML += '<param name="FlashVars" value="xmlProduct=<product id=\'' + this.prd + '\' masque=\'' + flashHostURI + 'flash/product/zoom/masque/' + this.cat + '/lemasque.swf\' img=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/default/' + this.prd + this.extensionImg + '\' imgzoom=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/zoom/' + this.prd + this.extensionImg + '\' />" />';	
	}
	
	flashHTML += '<param name="movie" value="' + flashHostURI + 'flash/product/zoom/main.swf" />';
	flashHTML += '<embed src="' + flashHostURI + 'flash/product/zoom/main.swf" quality="high" bgcolor="#ffffff" width="534" height="476" name="newmodule" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" scale="noscale" wmode="transparent" SWLIVECONNECT="true" FlashVars="xmlProduct=<product id=\'' + this.prd + '\' masque=\'' + flashHostURI + 'flash/product/zoom/masque/' + this.cat + '/lemasque.swf\' img=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/default/' + this.prd + this.extensionImg + '\' imgzoom=\'' + flashHostURI + 'images/catalog/product/' + this.cat + '/item/zoom/' + this.prd + this.extensionImg + '\' />"/>';
	flashHTML += '</object>';
	return flashHTML;
}

FOP.write = function(elementId) {
	if(detectFlash(this.version) || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (document.images["visuprd"]){
			document.images["visuprd"].src = flashHostURI + 'images/catalog/product/' + this.cat + '/item/default/' + this.prd + ".jpg";					
		}else{
			document.write("<img src=" + flashHostURI + "images/catalog/product/" + this.cat + "/item/default/" + this.prd +".jpg id=visuprd>");
			document.getElementById("noflash_text").style.display ="inline";
		}
	}
}

/* ---- detection functions ---- */
function getFlashVersion() {
	var flashversion = 0;
	if (navigator.plugins && navigator.mimeTypes.length) {
		var x = navigator.plugins["Shockwave Flash"];
		if(x && x.description) {
			var y = x.description;
   			flashversion = y.charAt(y.indexOf('.')-1);
		}
	} else {
		result = false;
	    for(var i = 15; i >= 3 && result != true; i--){
   			execScript('on error resume next: result = IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.'+i+'"))','VBScript');
   			flashversion = i;
   		}
	}
	return flashversion;
}

function detectFlash(ver) {
 	return (getFlashVersion() >= ver) ? true:false;
	}

// get value of query string param
function getQueryParamValue(param) {
	var q = document.location.search || document.location.href.split("#")[1];
	if (q) {
		var detectIndex = q.indexOf(param +"=");
		var endIndex = (q.indexOf("&", detectIndex) > -1) ? q.indexOf("&", detectIndex) : q.length;
		if (q.length > 1 && detectIndex > -1) {
			return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
		} else {
			return "";
		}
	}
}

function changeFlash(prd,cat){
	var myFlashObject = new FlashObject(prd, cat, 6);
	myFlashObject.write("GrandVisuel");
}