// product.js
function RMG_play()
{
	var url ="http://www.dillards.com/html/promos/reactor/rmg_playlist.html";
	var name = "RMG_play";
	var features ="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=600,height=400,top=20,left=20";
window.open(url, name, features);
}

function selectBoxChanged(attribute, attrValue) {
	//alert("SelectBoxChanged called");
	//alert("attribute>>" + attribute); 
	//alert("attrValue>>" + attrValue); 
	var thisValueMap = attributeMap[attribute][attrValue];
	for(var assocAttr in thisValueMap) {
		updateSelectBox(assocAttr+"AttrValue", thisValueMap[assocAttr]);
	}
	
	fixSwatches();
}

function selectBoxChangedBundle(attribute, attrValue, count) {
	//alert("selectBoxChangedBundle");
	//alert("attribute>>" + attribute); 
	//alert("attrValue>>" + attrValue); 
	//alert("count>>" + count); 
	var num = count - 1;
	var valueMap = eval("attributeMap_" + count);
	var thisValueMap = valueMap[attribute][attrValue];
	for(var assocAttr in thisValueMap) {
		updateSelectBox(assocAttr+"_"+count, thisValueMap[assocAttr]);
	}
	fixSwatchesBundle(s7lst[num].imageList,num);
}

function updateSelectBox(id, values) {
	//alert("UpdateSelectBox called");
	//alert("id>>" + id);
	var select = document.getElementById(id);
	if(!select) return;
	if (!select.options) return;
	var prevValue = select.value;
	select.options.length = 0;
	select.options[0] = new Option("", "");
	for(var i = 0; i < values.length; i++) {
		select.options[i+1] = new Option(values[i], values[i]);
		if(values[i] === prevValue) {
			select.selectedIndex = i+1;
		}
	}
}

function resetSelectBox() {
	//alert("resetSelectBox called");
	for(var attribute in attributeMap) {
		var select = document.getElementById(attribute + "AttrValue");
		select.options.length = 0;
		select.options[0] = new Option("", "");
		var attrValMap = attributeMap[attribute];
		for(var attrVal in attrValMap) {
			select.options[select.options.length] = new Option(attrVal, attrVal);
		}
	}
	fixSwatches();
}

/**
 * Adapts a select box color name to the convention used for Scene7 images.
 */
function normalizeColorName(color) {
	//alert("normalizeColorName called");
	if(color && typeof(color) === 'string') {
		color = color.toLowerCase().replace(/ /g, "_");
		color = color.toLowerCase().replace(/[/]/g, "_");
	}
	return color;
}


/**
 * Gets the Scene7 color name from a URL string.
 * the Url is of the form DillardsZoom/PARTNUM_zi_color_name
 */
function extractColor(url) {
    //alert("extractColor called");
	var colorRegEx = /[^_]+_zi_([^?.]+).*$/;
    colorRegEx.test(url);
    return RegExp.$1;
}


/**
 * Updates the selected swatch due to changes in non-scene7 elements
 * (select box changes)
 */
function changeSwatch(color) {
    //alert("changeSwatch called");	
    //var thisSwatchName = 'DillardsZoom/' + normalizeColorName(color);
    var myColor = normalizeColorName(color);
   	myColor = "zi_" + myColor + "\\?";
   	colorRegEx = new RegExp(myColor);
   	//find in main image list
    var swatchIndex = -1;
    
    for(var i in s7swatchMgr.mainImageList) {
        //if(s7swatchMgr.mainImageList[i]['swatchList'][0]['name'] === thisSwatchName) {
       if(colorRegEx.test(s7swatchMgr.mainImageList[i]['name'])) {
            swatchIndex = i;
            break;
        }
    }
    if(swatchIndex >= 0) {
    	s7swatchMgr.selectSwatch('swatchGrid', s7swatchMgr.mainImageList, [], 0, swatchIndex);
    }
}

/**
 * Updates the selected swatch due to changes in non-scene7 elements
 * (select box changes)
 */
function changeSwatchBundle(color, num) {
    //alert("changeSwatchBundle");
    //var num = count - 1;
    //alert("num>>" + num);  
	
    var myColor = normalizeColorName(color);
   	myColor = "zi_" + myColor + "\\?";
   	colorRegEx = new RegExp(myColor);
   	//alert("colorRegEx>>" + colorRegEx);
   	
   	var swatchIndex = -1;
   	
   	s7swatchMgr = s7swatchMgrArray[num];
   	//alert("size of list>>" + s7swatchMgr.mainImageList.length);
   	for(var i in s7swatchMgr.mainImageList) { 
   		//alert("s7swatchMgr.mainImageList>>" + s7swatchMgr.mainImageList[i]); 
   		//alert("s7swatchMgr.mainImageList.name>>" + s7swatchMgr.mainImageList[i]['name']); 		
		if(colorRegEx.test(s7swatchMgr.mainImageList[i]['name'])) {
        	swatchIndex = i;
            break;
		}
    }
    if(swatchIndex >= 0) {
    	s7swatchMgr.selectSwatch('swatchGrid'+ num, s7swatchMgr.mainImageList, [], 0, swatchIndex);
    }
}

/**
 * Synchronizes the color swatch image list in the Scene7 data structure with
 * the select box.
 */
function filterColorList(select,mainList) {
    //alert("filterColorList called");
    var validColors = {};
    for(var i = 0; i < select.options.length; i++) {
        var color = normalizeColorName(select.options[i].value);
        validColors[color] = true;
    }
    var swatchList = [];
    for(var swatch in mainList) {
        var swatchColor = extractColor(mainList[swatch]['name']);
        if(validColors[swatchColor]) {
            swatchList.push(mainList[swatch]);
        }
    }
    s7swatchMgr.mainImageList = swatchList;
}

/**
 * Updates the selected swatch to match the select box
 */
function fixSwatchesBundle(list,imgNum) {
	//alert("fixSwatchesBundle called");
	var colorNum = imgNum + 1;
	s7swatchMgr = s7swatchMgrArray[imgNum];
	var select = document.getElementById("Color_"+colorNum);   	
   	if(!select) return;
   	filterColorList(select,list);
	if(select.selectedIndex > 0) {
    	var color = normalizeColorName(select.options[select.selectedIndex].value);
        changeSwatchBundle(color,imgNum);
    }
    else if(list.length >= 1){
    	var num = imgNum-1;
    	s7swatchMgr.selectSwatch('swatchGrid'+ imgNum, s7swatchMgr.mainImageList, [], 0, 0);
    }
}
/**
 * Updates the selected swatch to match the select box
*/
function fixSwatches() {
	//alert("fixSwatches called");
	var select = document.getElementById("ColorAttrValue");
    if(!select) return;
	filterColorList(select,s7lst[0].imageList);
	//alert("selectedIndex>>" + select.selectedIndex);
	if(select.selectedIndex > 0) {
    	var color = normalizeColorName(select.options[select.selectedIndex].value);
        changeSwatch(color);
    }
    else if(s7swatchMgr.mainImageList.length >= 1){
    	//alert("true");
    	s7swatchMgr.selectSwatch('swatchGrid', s7swatchMgr.mainImageList, [], 0, 0);
    }
    
} 

function showSizeChart(imgUrl)
{
	myImage = new Image();
	myImage.src =(imgUrl);

	var features = "scrollbars=no,screenX=20,screenY=20,top=20,left=20";
	showWindow(imgUrl, "SizeChart", features);
	
}

function showWindow(img, title, features){
  if((myImage.width!=0)&&(myImage.height!=0)){
    var width = myImage.width+20;
	var height = myImage.height+20;
	features="width="+width+",height="+height+","+features;
	window.open(img,title,features);
  }
  else{
    funzione="showWindow('"+img+"', '"+title+"', '"+features+"')";
    intervallo=setTimeout(funzione,20);
  }
}