var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var flashDescription;

//
// This will likely fail on versions before flash 6, but since we don't 
// support versions before 6.0r65 it is ok.
//
function isFlashCurrent(versionMajorReq, versionMinorReq, versionRevisionReq) {
    var versionMajor;
    var versionMinor;
    var versionRevision;

    // NS/Opera version >= 3 check for Flash plugin in plugin array
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash"]) {
            // parse the description to get the version
            flashDescription = navigator.plugins["Shockwave Flash"].description;
            var descArray = flashDescription.split(" ");
            var tempArrayMajor = descArray[2].split(".");
            versionMajor = tempArrayMajor[0];
            versionMinor = tempArrayMajor[1];
            if (descArray[3] != "") {
                tempArrayMinor = descArray[3].split("r");
            } else {
                tempArrayMinor = descArray[4].split("r");
            }
            versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;

        }
    } else if (isIE && isWin && !isOpera) {
        try {
            var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash." + versionMajorReq);

            // GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
            // so be careful.  Of course we don't run those versions.
            axo.AllowScriptAccess = "always";
            // required for v6.x?
            var version = axo.GetVariable("$version");
            // safe to call for 6.0r47 or greater
            flashDescription = version;

            // parse the version data
            tempArray = version.split(" ");
            // ["WIN", "2,0,0,11"]
            tempString = tempArray[1];
            // "2,0,0,11"
            versionArray = tempString.split(",");
            // ['2', '0', '0', '11']

            versionMajor = versionArray[0];
            versionMinor = versionArray[1];
            versionRevision = versionArray[2];
            // should be 3???
        } catch (e) {
        }
    }

    // test version
    if (versionMajor > versionMajorReq) {
        return true;
    }
    if (versionMajor == versionMajorReq
            && versionMinor > versionMinorReq) {
        return true;
    }
    if (versionMajor == versionMajorReq
            && versionMinor == versionMinorReq
            && versionRevision >= versionRevisionReq) {
        return true;
    }
    return false;
}


/**
Adds Scene7 flash viewer to HTML page in the place this methods was called. Should not be executed after the document is ready.
@param inURL:String flash viewer URL
@param inWidth:String flash object width
@param inHeight:String flash object height
@param inID:String flash object id (ID= attribute of OBJECT tag and NAME= attribute of EMBED tag)
@param inBgColor:String flash object background color, in #RRGGBB format
@param inWmode:String (optional) wmode attribute value, usually is "window", "opaque" or transparent. Default is "window"
*/
function s7loadViewer(inURL, inWidth, inHeight, inID, inBgColor, inWmode) {
	var html = s7GetViewerHTML(inURL, inWidth, inHeight, inID, inBgColor, inWmode);
	document.writeln(html);
}

/**
Returns HTML markup suitable for embedding Scene7 flash viewer into the page.
@param inURL:String flash viewer URL
@param inWidth:String flash object width
@param inHeight:String flash object height
@param inID:String flash object id (ID= attribute of OBJECT tag and NAME= attribute of EMBED tag)
@param inBgColor:String flash object background color, in #RRGGBB format
@param inWmode:String (optional) wmode attribute value, usually is "window", "opaque" or transparent. Default is "window"
@return :String HTML code for embedding the viewer
*/
function s7GetViewerHTML(inURL, inWidth, inHeight, inID, inBgColor, inWmode) {
	if (!inWmode) {
		inWmode = 'window';
	}
	var html = '';
	html += "<object ";
	html += "	classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' ";
	html += "	codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0' ";
	html += "	width='" + inWidth + "' ";
	html += "	height='" + inHeight + "' ";
	html += "	id='" + inID + "' ";
	html += "	align=''>";
	html += "	<param name='movie' value='" + inURL + "'>";
	html += "	<param name='quality' value='high'>";
	html += "	<param name='bgcolor' value='" + inBgColor + "'>";
	html += "	<param name='AllowScriptAccess' value='always'>";
	html += "	<param name='allowFullScreen' value='true'>";
	html += "	<param name='wmode' value='" + inWmode + "'>";
	html += "	<embed ";
	html += "		src='" + inURL + "' ";
	html += "		quality='high' ";
	html += "		bgcolor='" + inBgColor + "'  ";
	html += "		wmode='" + inWmode + "' ";
	html += "		width='" + inWidth + "' ";
	html += "		height='" + inHeight + "' ";
	html += "		name='" + inID + "' ";
	html += "		AllowScriptAccess='always' ";
	html += "		allowFullScreen='true' ";
	html += "		align='' ";
	html += "		type='application/x-shockwave-flash' ";
//	html += "		pluginspage='http://www.macromedia.com/go/getflashplayer'>";
	html += "	</embed>";
	html += "</object>";
	return html;
}


S7ZoomViewer = function(companyName) {
	this.viewerWidth    ;
	this.viewerHeight   ;
	this.viewerType 	= '/genericzoom';
	this.bgColor        ;
	this.instanceName  	= this.setInstanceName() ;
	this.codeRootURL    ;
	this.contentRootUrl ;
	this.serverUrl      ;
	this.sku            ;
	this.noFlashPreset  ;
	this.companyName 	= companyName    ;
	this.config;
	this.container;
};
	
S7ZoomViewer.prototype.setInstanceName = function() {
	var curDateTime = new Date();
	var curTime = 'ZoomMX' + curDateTime.getHours()+ curDateTime.getMinutes() + curDateTime.getSeconds();
	return curTime;
}

S7ZoomViewer.prototype.setFlashParam = function(inName, inVal) 
{

		var divcontainer = "flash_setvariables_" + this.instanceName;

		if (!document.getElementById(divcontainer))
		{
			var divholder = document.createElement("div");
			divholder.id = divcontainer;
			document.body.appendChild(divholder);
		}

			
		document.getElementById(divcontainer).innerHTML = "";
		var divinfo = "<embed src='http://s7d5.scene7.com/is-viewers-4.1/flash/gateway.swf' FlashVars='lc=" + this.instanceName + "&fq="+escape(inName + "=" + inVal)+"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
		document.getElementById(divcontainer).innerHTML = divinfo;

};


S7ZoomViewer.prototype.setCodeRoot = function(inCodeRootURL) {
	this.codeRootURL = inCodeRootURL;
};

S7ZoomViewer.prototype.setContentRoot = function(inContentRootUrl) {
	this.contentRootUrl = inContentRootUrl;
};

S7ZoomViewer.prototype.setServerUrl= function(inServerUrl) {
	this.serverUrl = inServerUrl;
};


S7ZoomViewer.prototype.setViewerWidth = function(inViewerWidth) {
	this.viewerWidth = inViewerWidth;
};

S7ZoomViewer.prototype.setViewerHeight = function(inViewerHeight) {
	this.viewerHeight = inViewerHeight;
};

S7ZoomViewer.prototype.setBGColor = function(inBGColor) {
	this.bgColor = inBGColor;
};

S7ZoomViewer.prototype.setNoFlashPreset = function(imgPreset) {
	this.noFlashPreset = imgPreset;
};
S7ZoomViewer.prototype.setDiv = function(inContainer) {
	if (inContainer)
	{
		this.container = inContainer;
	} else {
		alert('ERROR: [S7ZoomViewer], the container [' + inContainer + '] not available on this page');
	}
};


S7ZoomViewer.prototype.setSKU = function(inSKU) {
	this.sku = inSKU;
};

S7ZoomViewer.prototype.setCompanyName = function(inCompanyName) {
	this.companyName = inCompanyName;
};

S7ZoomViewer.prototype.setConfig = function(inConfig) {
	this.config = inConfig;
};


S7ZoomViewer.prototype.draw = function() {
	var flashVars = this.getFlahVars();

	if (this.codeRootURL == null) {
		alert('ERROR: [S7ZoomViewer], the parameter [codeRoot] is not specified.');
		return;
	}
	var embeddedViewerUrl = this.codeRootURL + '/genericzoom.swf';
	var args = '';
	for (var a in flashVars)	{
		if (args == '')	{
			args += a + '=' + flashVars[a];
		} else {
			args += '&' + a + '=' + flashVars[a];
		}
	}
		
	var html;
	if (!isFlashCurrent(7, 0, 14)) {
            html = '<img src="' + this.serverUrl + '/' + this.companyName + '/' + this.sku + '?$' + this.noFlashPreset + '$">'
    } else {
		html = s7GetViewerHTML(embeddedViewerUrl + '?logo2=false&' + args, this.viewerWidth, this.viewerHeight, this.instanceName, this.bgColor, 'opaque');
	
	}
	if (this.container == null) {
		alert('ERROR: [S7ZoomViewer], the viewer container element is not specified.');
		return;
	}
	

		var flashvars = {
		}; 
		
		var params = {     //Setup param tag elements for the flash object to embed in the page 
			allowScriptAccess:"Always",
			menu:"false",
			quality:"high",
			scale:"noscale",
			salign:"LT",
			bgcolor:"#FFFFCC",
			wmode:"transparent"
		};  
		
		var attributes = {  //Setup object tag elements for the flash object to embed in the page
			id: this.instanceName,
			name: this.instanceName
		};
	
		var viewerUrl = "";  //Build Viewer Path to Viewer swf
		viewerUrl += this.codeRootURL;
		viewerUrl += this.viewerType + ".swf?";
		viewerUrl += "serverUrl=" + this.serverUrl;
		viewerUrl += "&contentRoot=" + this.contentRootUrl;
		viewerUrl += "&instanceName=" + this.instanceName;
		viewerUrl += "&image=" + this.companyName + "/" + this.sku;
		viewerUrl += "&config=" + this.config;
		viewerUrl += "&modifier=op_sharpen=1";		
		
	 swfobject.embedSWF(viewerUrl, this.container.id, this.viewerWidth, this.viewerHeight, "7.0.14.0", false, flashvars, params, attributes);  //Add Viewer to page into the myContent div element, at 600 x 600 pixels, checking for flash version 6,0,65 before allowing initialization to proceed, otherwise show the default div content
	
};

S7ZoomViewer.prototype.getFlahVars = function() {
	var fv  = new Object();
	if (this.serverUrl == null) {
		alert('ERROR: [S7ZoomViewer], the parameter [serverUrl] is not specified.')
	} else {
		fv.serverUrl = this.serverUrl;
	}
	if (this.contentRootUrl == null) {
		alert('ERROR: [S7ZoomViewer], the parameter [contentRoot] is not specified.')
	} else {
		fv.contentRoot = this.contentRootUrl;
	}
	if (this.config != null) {
		fv.config = this.config;
	}
	if (this.sku == null) {
		alert('ERROR: [S7ZoomViewer], the [SKU] is not specified.');
	} else if (this.companyName == null) {
		alert('ERROR: [S7ZoomViewer], the [companyName] is not specified.');
	} else {
		fv.image = this.companyName + '/' + this.sku;
	}
	if (this.instanceName != null) {
		fv.instanceName = this.instanceName;
	}

	return fv;
};

function sjGetDeviceType(){
	var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
	var isIPhone  = (navigator.userAgent.match(/iPhone/i));
	var isIPod  = (navigator.userAgent.match(/iPod/i));
	var isIPad  = (navigator.userAgent.match(/iPad/i));
	var isAndroid  = (navigator.userAgent.match(/Android/i));

	function isFlash() {
		if (navigator.plugins != null && navigator.plugins.length > 0) {
			if (navigator.plugins["Shockwave Flash"]) {
				return true;
			}
		} else if (isIE && isWin && !isOpera) {
			try {
				var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
				if (axo) return true;
			} catch (e) {
			}
		}
		return false;
	}

	if (isFlash()){
			return 2;
	}
 
	if (isIPhone || isIPod || isIPad){
			return 1;
	}
	return 0;
};
