var viewerInstanceNameMap = {}

function insertInitialViewer(
							sap_product_code,
							viewerInstanceName, 
							width, 
							height,
							zoomProfile,
							viewerSWF,
							imageURL,
							infoURL,
							swfURL,
							skins,
							swatch_bg,
							swatch_active_bg,
							waitIcon,
							hotspotSkin,
							hotspotSkinFG,
							hotspotIcon,
							selected_material_image,
							enable360,
							codeRootPath
							) {
	var imageList = getListOfMaterialImages(sap_product_code);
	var imageListArray = new Array();
	var skinArray = new Array();
	var initialFrame = util_getPositionInList(imageList, "Underarmour/" + selected_material_image);
	var imageListArray = new Array();
	var viewerUrl = swfURL + viewerSWF;
	viewerInstanceNameMap[sap_product_code] = viewerInstanceName;
	
	// process comma delimited image list
	imageListArray = imageList.split(",");
	for(var k=0; k<imageListArray.length; k++)
		imageListArray[k] = imageListArray[k] + '_ss';
	spinImageList = imageListArray.join(",");
	
	// process skin comma delimited skin list
	skinArray = skins.split(",");

	// declare objects to be passed to swfobject
	var flashvars = {};
	var params = {};
	var attributes = {};

	//SERVER VARS
	flashvars.serverURL = imageURL;
	flashvars.infoServerUrl =  infoURL;
	
	//IMAGE ARRAY VAR
	flashvars.image =  imageList;
	
	//ID & INSTANCE VARS
	flashvars.id =  viewerInstanceName;
	flashvars.instanceName =  viewerInstanceName;
	
	//ZOOM VARS
	flashvars.zoomStep = "1.5";
	
	//SKIN VARS
	flashvars.skin =  skins; 
	flashvars.codeRoot = codeRootPath;
	flashvars.contentRoot = swfURL;
	
	//TIMING VARS
	flashvars.turnTime = "0.25";
	flashvars.transitionTime =  "0.1";
	flashvars.fadeTime =  "0";
	
	//SWATCH VARS
	flashvars.swPadding = "0,0"; 
	flashvars.swatchBackGround =  swfURL + swatch_bg; 
	flashvars.activeSwatchBackGround =  swfURL + swatch_active_bg; 	

	//HOTSPOT VARS
	flashvars.infoPopupSkin =  swfURL + hotspotSkin;
	flashvars.infoPopupSkinFG =  swfURL + hotspotSkinFG;
	flashvars.ztgHotspotIcon =  swfURL + hotspotIcon;
	flashvars.ztgLayout = "hotspot,0,0";
	flashvars.infoShowDelay = "0.2";

	//SPIN VIEWER VARS
	flashvars.allowSpin = enable360;
	flashvars.spinSets = spinImageList;
	
	//WAIT VARS 
	flashvars.waitIcon =  waitIcon;
	flashvars.waitIconTimer =  "2.0";

	//DEFAULT RESET VARS
	flashvars.tooltips = "false";
	flashvars.labelKey = "label"; 
	flashvars.persistence = "0"; 
	flashvars.scrollBars = "false";
	flashvars.popupShowFriendly =  "false";
	flashvars.useTiles = "false";

	//SWF DISPLAY PARAMS
	params.wmode = "transparent";
	params.allowScriptAccess = "always";
	params.instanceName = viewerInstanceName;
	//params.SWLIVECONNECT = "true";

	// id of DOM element to write to
	attributes.id = viewerInstanceName;

	// embed the swf
	swfobject.embedSWF(
		viewerUrl + "?voidparam=void" + zoomProfile,
		"flashcontent_" + viewerInstanceName,
		width,
		height,
		"6",
		"/js/expressInstall.swf",
		flashvars,
		params,
		attributes
	);
}


function insertInitialViewer2(
							sap_product_code,
							viewerInstanceName, 
							width, 
							height,
							zoomProfile,
							viewerSWF,
							imageURL,
							infoURL,
							swfURL,
							skins,
							swatch_bg,
							swatch_active_bg,
							waitIcon,
							hotspotSkin,
							hotspotSkinFG,
							hotspotIcon,
							selected_material_image,
							enable360,
							codeRootPath
							) {

	var imageList = getListOfMaterialImages(sap_product_code);
	var imageListArray = new Array();
	var skinArray = new Array();
	var initialFrame = util_getPositionInList(imageList, "Underarmour/" + selected_material_image);
	var imageListArray = new Array();
	var viewerUrl = swfURL + viewerSWF;

	viewerInstanceNameMap[sap_product_code] = viewerInstanceName;
	
	// process comma delimited image list
	imageListArray = imageList.split(",");
	for(var k=0; k<imageListArray.length; k++)
		imageListArray[k] = imageListArray[k] + '_ss';
	spinImageList = imageListArray.join(",");


	var viewerWidth = width;
	var viewerHeight = height;
	var instanceName = viewerInstanceName;
	var bgColor = '#aaaaaa';
	// image parameter for the zoom viewer 
	var image = imageList;
	//a list of spin sets
	var spinSets = spinImageList;
	var allowSpin = true;
	// should be changed to appropreate server URL e.g. 'http://s7d2.scene7.com/' 
	var baseServerUrl = viewerUrl;
	var codeRootUrl = codeRootPath;
	var contentRoot = swfURL;
	var serverUrl = imageURL;
	var url = baseServerUrl + '?codeRoot=' + codeRootUrl + '&contentRoot=' + contentRoot
	+ '&serverURL=' + serverUrl 
	+ '&image=' + image
	+ '&spinSets=' + spinSets
	+ '&buildTarget=debug&fadeTime=0&imagePadding=25&labelKey=label&persistence=0&scrollBars=false&skin=' + skins 
	+ '&activeSwatchBackGround=' + swatch_active_bg
	+ '&swatchBackGround=' + swatch_bg
	+ '&swHighlightThickness=2&swHighlightColor=0xffffff&swCellSpacing=20,20&instanceName='+instanceName 
	+ '&swPadding=10,5&modifier=op_sharpen%3d1&swRetainSelection=false&allowSpin='+allowSpin
	+ '&ztgHotspotIcon=' + hotspotIcon
	+ '&ztgLayout=hotspot,0,0&infoShowDelay=0.2&infoPopupSkin=' + hotspotSkin
	+ '&infoPopupSkinFG=' + hotspotSkinFG
	+ '&waitIcon=' + waitIcon
	+ '&waitIconTimer=2.0&tooltips=false&labelKey=label&persistence=0&scrollBars=false&useTiles=false&infoServerUrl=' + infoURL;

	s7loadViewer(url, width, height, instanceName, bgColor, 'opaque');

}

function getListOfMaterialImages(sap_product_code) {
	var ar = arVs[sap_product_code.toLowerCase()];
	var result = new Array();
	var group = null;
	var s;
	
	for (var i = 0; i < ar.length; i++) {
		if (ar[i].product_image != group && ar[i].product_image != "") {
			result.push("Underarmour/" + ar[i].product_image);
			group = ar[i].product_image;
		}
	}
	return result.join(",");
}


function util_getPositionInList(list, str) {
	var ar = list.split(",");
	var index = -1;
	for (var i = 0; i < ar.length; i++) {
		if (ar[i] == str) {
			index = i;
			break;
		}
	}
	return index;
}

function changeImageSet(sap_product_code, sap_material_code) {
	var imageList = getListOfMaterialImages(sap_product_code);
	var initialFrame = util_getPositionInList(imageList, "Underarmour/" + sap_material_code);
	var viewerInstanceName = viewerInstanceNameMap[sap_product_code];
	if(viewerInstanceName != null) {
		if (document.embeds[viewerInstanceName])
			document.embeds[viewerInstanceName].changeImageSet(initialFrame);
		else
			document.getElementById(viewerInstanceName).changeImageSet(initialFrame);
	}
};



/**
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);
	$("#zoom_viewer_container").html(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='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 += "		align='' ";
	html += "		type='application/x-shockwave-flash' ";
	html += "		pluginspage='http://www.macromedia.com/go/getflashplayer'>";
	html += "	</embed>";
	html += "</object>";
	return html;
}


function s_ProductToolsZoom(v) {
	var s = 0;
}