// SundaySky javascript

// This function opens the player with the URL of the video. 
// Naming convention are for SundaySky's video are determined by the project's scenario
function openSundayskyPlayer(productSku) {	
    var mediaLookupURL = "http://shoebuyfe.sundayskylb1.com/MediaLookup?accountId=ShoeBuy%26videoletId=ProductShowcase%26key=";			
    var mediaLookupSuffix = ":1.0:default:overlaid_video";
    sundaySky.create(mediaLookupURL + productSku + mediaLookupSuffix, { onObjectClick: sundaySkyInteractivityClicked });
}

// This function will be called when a user clicks on the video
function sundaySkyInteractivityClicked(clickedObject) {
    if (clickedObject.actionName == "general_click") {
        alert(clickedObject.productId); // the product ID is of the product being presented in the specific video.
    }
}

function showLayer(o) {
	var layer = document.all[o];
	var one = null;
	if (layer != null) one = layer.style;
	if (one != null) {
		one.visibility = 'visible';
		one.display = 'block';
	}
}

function hideLayer(o) {
	var layer = document.all[o];
	var one = null;
	if (layer != null) one = layer.style;
	if (one != null) {
		one.visibility = 'hidden';
		one.display = 'none';
	}
}

function switchColorImage(cid) {
    colorId = cid;
    if (colorId == "") {
         return;
    } else {
		var multiCnt = multiCntMap[colorId];
		if (showThumb && multiCnt > 0) {
			if (multiCnt < selected) {
				switchThumb(1);
			}
			hideLayer('nonMulti');
			showLayer('multi');
			document.viewImage.src = fileroot + colorId + imgType + selected + ".jpg";
			for (var i = 1; i <= multiCnt; i++) {
				eval("document.thumb" + i + ".src='" + fileroot + colorId + "_dt" + i + ".jpg';");
				eval("document.thumb" + i + ".width='30';");
				eval("document.thumb" + i + ".height='30';");
			}
			for (var i = multiCnt + 1; i <= maxImg; i++) {
				eval("document.thumb" + i + ".src='';");
				eval("document.thumb" + i + ".width='0';");
				eval("document.thumb" + i + ".height='0';");
				eval("document.thumb" + i + ".border='0';");
			}
		}
		else {
			if (multiCnt > 0) {
				hideLayer('nonMulti');
				showLayer('multi');
			}
			else {
				hideLayer('multi');
				showLayer('nonMulti');
			}

			for (var i = 1; i <= maxImg; i++) {
				eval("document.thumb" + i + ".src='';");
				eval("document.thumb" + i + ".width='0';");
				eval("document.thumb" + i + ".height='0';");
				eval("document.thumb" + i + ".border='0';");
			}
			if (imageMaps[colorId] == null) {
				document.viewImage.src = '/pi/'+imageMaps['DEFAULT'];
			} else {
				document.viewImage.src = '/pi/'+imageMaps[colorId];
			}
		}
    }
    var zoomlink = document.getElementById("zoomlink");
	if (zoomlink != null) {
		var zoomMapKey = colorId;
		if (zoomMapKey == "") {
			zoomMapKey = 'DEFAULT';
		}
		var thisobj = zoomlink.firstChild;
		if (zoomImageMaps[zoomMapKey] == null) {
			if (thisobj != null) zoomlink.firstChild.nodeValue = '';
		} else {
			if (thisobj != null) zoomlink.firstChild.nodeValue = 'View Larger Image';
			else {
				var txt = document.createTextNode('View Larger Image');
				zoomlink.appendChild(txt);
			}
		}
	}
}

function switchThumb(o) {
	 eval("document.thumb" + selected + ".border=0;");
	 selected = o;
	 eval("document.thumb" + selected + ".border=1;");
	 document.viewImage.src = fileroot + colorId + imgType + selected + ".jpg";
}

function showDetailImage(o) {
	var layer = document.getElementById(o); 
	var one = null;
	if (layer != null) one = layer.style;
	if (one != null) {
		one.visibility = 'visible';
		one.display = 'block';
	}
}

function hideDetailImage(o) {
	var layer = document.getElementById(o); 
	var one = null;
	if (layer != null) one = layer.style;
	if (one != null) {
		one.visibility = 'hidden';
		one.display = 'none';
	}
}

function switchDetailImageFromSelect(root, element, colorIds, shouldEnableZoom, shouldSwitchAssociated) {
    return switchDetailImage(root, colorIds[element.selectedIndex], colorIds, shouldEnableZoom, shouldSwitchAssociated);
}

function switchDetailImage(root, colorId, colorIds, shouldEnableZoom, shouldSwitchAssociated) {
    if (colorId == null || colorId == "") {
         return;
    }

    var wasFound = false;
    for (cid in colorIds) {
        if (colorIds[cid] == colorId) {
            showDetailImage(root + colorId);
            wasFound = true;
        } else {
            hideDetailImage(root + colorIds[cid]);
        }
    }

    if (shouldEnableZoom && wasFound) {
        var jblink = 'a#jblink' + colorId;
        enableZoom(jblink);
        var $j = jQuery;
        if ($j(jblink).next().text() != "Hover above to zoom") {
            $j(jblink).after('<div style="margin-top:4px;"><img src="/images/icon_zoom.gif" style="vertical-align:middle;" />Hover above to zoom</div>');
        }
    }

    if (shouldSwitchAssociated) {
        switchAssociatedPanels(colorId, colorIds);
    }
}

function selectElement(form, rootElemId, selectedIndex) {
    var elem = form[rootElemId];
    if (elem.type == "radio") {
        elem.checked = "1";
    } else {
        elem[selectedIndex].selected = "1";
    }
}

function getSelected(rootElemId, type) {
    var elem = document.forms.handleBuy[rootElemId];
    if (type == 'radio' || type == 'table') {
        if (elem.length == undefined) {
            return [0, elem.value];
        }

        var len = elem.length;
        for (var i = 0; i < len; i++) {
            if (elem[i].checked) {
                return [i, elem[i].value];
            }
        }
        return [0, ""];
    } else if (type == 'dropdown') {
        return [elem.selectedIndex, elem.options[elem.selectedIndex].value];
    }
    return [0, ""];
}

