/*
 * Global var used for saicoswap
 */
//var selectedColor="";
var rootUrl 	= location.protocol + "//" + location.host;



var selectedColor= new Array(6);
selectedColor[0]="";
selectedColor[1]="";
selectedColor[2]="";
selectedColor[3]="";
selectedColor[4]="";
selectedColor[5]="";
var mainSwatchActive = false;
var mainSwatchContainer = "swatchContainer0";


/*******************
* Browser checking *
********************/
var NS4 = (document.layers)? true:false;
var IE4 = (document.all)? true:false;
var IE5 = false;
var GECKO = false;
if (IE4) {	
	if (navigator.userAgent.indexOf('MSIE 5')>0) {	
		IE5 = true;	
	}
}

if(navigator.userAgent.indexOf("Gecko") != -1)
{
	GECKO = true;
}

/********************************************
* function rollDesc(String desc, int divID) *
*********************************************
 function for writing swatch color 
 description to a layer on rollover
*/

function rollDesc(desc,divID)
{
	var myLyr = "swatchDesc" + divID;
	document.getElementById(myLyr).innerHTML = desc;	
}



/********************************************
* function rollText(String desc, int divID) *
*********************************************
 function for highlighting text if you rollover the image or other text
*/

function rollText(divID,mystyle)
{
	var myLyr = "TextDesc" + divID;
	var myLyr2 = "ItemDesc" + divID;
	if (IE4)
	{
		myLyr=eval(myLyr);
		myLyr2=eval(myLyr2);
		myLyr.style.textDecoration = mystyle; 
		myLyr2.style.textDecoration = mystyle; 
	}
	else if (GECKO)
	{
		document.getElementById(myLyr).style.textDecoration = mystyle;
		document.getElementById(myLyr2).style.textDecoration = mystyle;
	}
}
/**************************************************
* function rollDesc(String imageURL, int imageID) *
***************************************************
 function for swapping images when a swatch is
 clicked on
*/
function swapSwatchImage(imageURL,imageID,prnbr,isSaico,index,blockId,desc, dispMode)
{
	document.images["primaryImage_" + imageID].src = imageURL;
	if(isSaico){
		var tempNumber = prnbr.split("-");
		var itemNumber = tempNumber[1];
		var image1 = new Image();
	}
}


function swapPrimarySwatchImage(imageURL,imageID,prnbr,isSaico,index,blockId,desc, dispMode)
{
	document.images["primaryImage_" + imageID].src = imageURL;
	var myLyr = "primaryDesc";
	if (IE4)
	{
		myLyr=eval(myLyr);
		myLyr.innerHTML = "COLOR:&nbsp;"+ desc; 
	}
	else if (GECKO)
	{
		document.getElementById(myLyr).innerHTML = "COLOR:&nbsp;"+ desc;
	}
	else{
	}
}

function popLargeSwatchWindow(imageURL,imageID,prnbr,isSaico,index,blockId,desc, dispMode)
{
	window.open('/commerce/application?namespace=moreInfo&prnbr='+prnbr+'&moreInfoInd=largeColors&blockId='+blockId+'&index='+index+'&dispMode='+dispMode,'infowin','resizable,height=542,width=500');
}

function saicoSwapImg(groupListIndex, swapFile, color)
{
	if (groupListIndex > 0) {
		swapFile = swapFile.replace("prodpri2","prodpri");
		$("#vsImage_" + groupListIndex).attr('src', swapFile);
	} else {
		jQuery("#altImages a:first").trigger('select');
		VSD.Product.replaceImage("vsImage", swapFile, false);
	}
}

function moveElement(id, x, y)
{
    if (document.getElementById)
    {
        document.getElementById(id).style.left = x + 'px';
        document.getElementById(id).style.top = y + 'px';
    }
}
function showElement(id)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).style.visibility = 'visible';
        }
    }
}
function hideElement(id)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).style.visibility = 'hidden';
        }
    }
}
function swapHtml(id, html)
{
    if (document.getElementById)
    {
        if (document.getElementById(id))
        {
            document.getElementById(id).innerHTML = html;
        }
    }
}

function moveSwatch(index, swatchDiv, colorDesc, img, isSaico,secureProtocol)
{
if(swatchDiv.parentNode.id === mainSwatchContainer)
	{
		mainSwatchActive = true;
	}
	moveSwatchWide(index, swatchDiv, colorDesc, img, isSaico,secureProtocol);
}

function moveSwatchWide(index, swatchDiv, colorDesc, img, isSaico,secureProtocol)
{
	sCont=document.getElementById("swatchContainer" + index);
	sBCont=document.getElementById("content");
	var bodyPos = getPosition(sBCont);
	var divPos = getPosition(swatchDiv); // this logic is in config-img.js
	var contPos = getPosition(sCont);
	sContTop = sCont.offsetTop + sBCont.offsetTop;
	sContLeft = sCont.offsetLeft + sBCont.offsetLeft;
	sContWidth = sCont.offsetWidth;
	sContRight = sContLeft+sContWidth;	
		
	sBox=document.getElementById("SwatchBox");
	sBoxTop = sBox.offsetTop;
	sBoxLeft = sBox.offsetLeft - 1;
	sBoxWidth = sBox.offsetWidth;
	sBoxHeight = sBox.offsetHeight + 3; // 5px above swatch
	
	sBoxNewRight = divPos.x + sBoxWidth;
	
	sBoxNewLeft = sCont.offsetLeft - sBCont.offsetLeft + (swatchDiv.offsetLeft + (swatchDiv.offsetWidth / 2) ) - (sBoxWidth/2);

	if (divPos.x != contPos.x) {
		divPos.x = contPos.x + 11;
	}

if(secureProtocol=="true"){
urlString="https://secure-media.victoriassecret.com/product/swatchlg/";
}else{
urlString="http://media.victoriassecret.com/product/swatchlg/";
}
	imgTag='<img src="'+urlString + img + '.jpg">';
	imgURL=urlString + img + '.jpg';
	showSwatch(index);

	var IfrRef = document.getElementById('DivShim');
	IfrRef.style.width = sBox.offsetWidth;
    IfrRef.style.height = sBox.offsetHeight;
    IfrRef.style.top = sBox.style.top;
    IfrRef.style.left = sBox.style.left;
    IfrRef.style.zIndex = 999;
	
	moveElement('DivShim', divPos.x, sCont.offsetTop-sBoxHeight);
	moveElement('SwatchBox', divPos.x, sCont.offsetTop-sBoxHeight);
	
	swapHtml('SwatchBoxColorDesc', colorDesc);
	if (isSaico === true){
		swapHtml('SwatchBoxAction', "click to change color in photo.");
	} else if(isSaico === false){
		swapHtml('SwatchBoxAction', "");
	}
	// new not available for click
	if(isSaico == 'n/a')
	{
		swapHtml('SwatchBoxAction', "photo unavailable for this color.");
	}
	
	swapHtml('SwatchBoxImg', imgTag);
}

dispSwatch=false;
function hideSwatch(index,swatchDiv){
	dispSwatch=false;
	mainSwatchActive = false;
	swatchBorder = swatchDiv;
	setTimeout('doHideSwatch()',100);
}
function doHideSwatch(index,swatchBorder){
	
	if(!dispSwatch){
		var IfrRef = document.getElementById('DivShim');
		IfrRef.style.display = "none";
		hideElement('SwatchBox');
	}
}
function showSwatch(index){
	dispSwatch=true;
	var IfrRef = document.getElementById('DivShim');
	IfrRef.style.display = "block";
	showElement('SwatchBox');
}

{
	document.write("<div class=\"SwatchBox\" id=\"SwatchBox\">");
	document.write(	"<div class=\"SwatchBoxImg\" id=\"SwatchBoxImg\"></div>");
	document.write(	"<div class=\"SwatchBoxText\">");
	document.write(		"<div class=\"SwatchBoxColorDesc\" id=\"SwatchBoxColorDesc\"></div>");
	document.write(		"<div class=\"SwatchBoxAction\" id=\"SwatchBoxAction\"></div>");
	document.write(	"</div>");
	document.write("</div>");
	document.write("<iframe class=\"DivShim\" id=\"DivShim\" src=\"javascript:false;\" scrolling=\"no\" frameborder=\"0\" style=\"position:absolute; top:0px; left:0px; display:none;\"></iframe>");
}

function toggleSWFSwatch(swatchXML)
{
	document.getElementById("vsImage").toggleSWFSwatch(swatchXML);
}

function onBlur()
{
	vsImage.onWindowBlur();
};
function onFocus()
{
	vsImage.onWindowFocus();
};
function fixFocus()
{
	if(document.body.className == 'blurred')
		window.focus();
};

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

$(document).ready(function(){
	$(".swatch").hover(function () {
		$(this).children(":first").css("display","block");
	},
	function () {
		$(this).children(":first").css("display","none");
	});
});
