var LastRPId;
var HasOpened = false;
var LastSwatchId ;
var HasShownSwatch = false;
var hideSelect = true;
var IsIe = false;
var LastEnlargeId = "";
var extraIndex = -1 ;
var ppShown = false;

//navigator.userAgent.toLowerCase().indexOf('safari')
//alert(navigator.userAgent.toLowerCase());
if(navigator.userAgent.toLowerCase().indexOf('msie')>-1){
IsIe  = true;
}


function InitShop(){
	var aAnchors = mElement("Images").getElementsByTagName("A");
		for(var i=0;i<aAnchors.length;i++){
			t=i+1
			EnlargeId = aAnchors[i].id.replace('RP','SEA');
			addEvent(mElement(EnlargeId),'click',ShowEnlarge);	
			addEvent(mElement("SelCloser"+t),'click',SelClose);	
			addEvent(aAnchors[i],'click',ShowSelector);	
			
		}

	var aAnchors = mElement("ProductEnlarges").getElementsByTagName("A");
		for(var i=0;i<aAnchors.length;i++){

			if(aAnchors[i].id == "PPLIC"){
				addEvent(aAnchors[i],'click',HidePPEnlargeClick);	
			}else{
			addEvent(aAnchors[i],'click',HideEnlarge);	
			}

		}
		

	var aSelects = mElement("ProductSelectors").getElementsByTagName("Select");	
	for(var i=0;i<aSelects.length;i++){
		if(aSelects[i].id.substr(0,8) == "ColorDD_"){
		addEvent(aSelects[i],'change',CheckSize);	
		}
	
	}
	//document.
	//alert(existTest(document.getElementById("InnerShopLinksL4")));
	//alert(existTest(document.getElementById("InnerShopLinksL4")));
	if (existTest(mElement("InnerShopLinksL4"))){
		var aAnchors = mElement("InnerShopLinksL4").getElementsByTagName("A");
		for(var i=0;i<aAnchors.length;i++){
			if(aAnchors[i].className == "ppp"){
				addEvent(aAnchors[i],'click',ShowPPEnlargeClick);	
			}
		}
	}

	var aAnchors = mElement("ProductSelectors").getElementsByTagName("A");
		for(var i=0;i<aAnchors.length;i++){
		//alert(aAnchors[i].id);
			if(aAnchors[i].className == "SIEA"){
				addEvent(aAnchors[i],'click',ShowEnlarge);	
			}
			
			
			if(aAnchors[i].id.substr(0,3) == "SWA"){
				addEvent(aAnchors[i],'mouseover',ShowSwatch);	
				addEvent(aAnchors[i],'mouseout',HideSwatch);	
			}
			if(aAnchors[i].className == "vsc"){
				addEvent(aAnchors[i],'click',ShowSizeChart);	
			
			}
			if(aAnchors[i].id.substr(0,14) == "SizeChartLink_"){
				addEvent(aAnchors[i],'click',CloseSizeChart);				
			}			
			
			
		}		


}

function CheckSize(tEvent){

	var ProductId = this.id.replace('ColorDD_','');
	SetSizes(ProductId);
	//alert(ProductId);
	/*
	
	*/
}
function SetSizes(productId){
	if (ColorSizeObj["P" + productId].HasMultiple){
		var SizeSelector = mElement("SizeDD_" + productId);
		var ColorSelector = mElement("ColorDD_" + productId);
		SizeSelector.options.length = 0;
		for (var x=0;x<ColorSizeObj["P" + productId][ColorSelector.options[ColorSelector.selectedIndex].value].length;x++){
			var thisOptionValue = ColorSizeObj["P" + productId][ColorSelector.options[ColorSelector.selectedIndex].value][x];
			SizeSelector.options[SizeSelector.options.length] = new Option (thisOptionValue,thisOptionValue);

			//SizeSelector.options.add(new Option(thisOptionValue,thisOptionValue));
		}
	}
}

function ShowSizeChart(tEvent){
var posIndex = this.id.replace('vsca_','');
var SCWId = 'SizeChartLink_' + posIndex;
var SCImgId = 'SizeChartImg_' + posIndex;
mElement(SCImgId).src = this.href;
mElement(SCImgId).style.display = "block";
CancelClick(tEvent,this);
}

function CloseSizeChart(tEvent){
var posIndex = this.id.replace('SizeChartLink_','');
var SCImgId = 'SizeChartImg_' + posIndex;
mElement(SCImgId).style.display = "none";
CancelClick(tEvent,this);
}

function HidePPEnlargeClick(tEvent){
	HidePPEnlarge();
	CancelClick(tEvent,this);
}

function HidePPEnlarge(){
	mElement("PPI").style.display='none';
	SetSelectState('ProductSelectors','visible',true)	
	ppShown = false;

}
function ShowPPEnlargeClick(tEvent){
	ShowPPEnlarge();
	CancelClick(tEvent,this)	
}

function ShowPPEnlarge(){
	mElement("PPI").style.display='block';
	SetSelectState('ProductSelectors','hidden',true)	
	ppShown = true;
}

function ShowEnlarge(tEvent){
//'PELI
//alert("here");
	var enlargRootId = this.id.replace('SEA','').replace('SIEA','');
	EnlargeImageId = 'PEL' + enlargRootId;
	mElement(EnlargeImageId).style.display='block';
	if (extraIndex > -1){
	//mElement('PELI' + enlargRootId).src = ExtraSwatchObj.Data[extraIndex].fullsrc;
	}
	ActivatorId = this.id.split("_")[0].replace('SEA','P').replace('SIEA','P');	
	SetSelectState(ActivatorId,'hidden',true);
	CancelClick(tEvent,this)
	LastEnlargeId = EnlargeImageId;
}

function HideEnlarge(tEvent){
	EnlargeImageId = this.id.replace('PELC','PEL').replace('PRELC','PEL') 
	HideEnlarge2(EnlargeImageId);
	ActivatorId = this.id.split("_")[0].replace('PELC','P').replace('PRELC','P');	
	SetSelectState(ActivatorId,'visible',true);	
	CancelClick(tEvent,this)
}
function HideEnlarge2(EnlargeImageId){
	mElement(EnlargeImageId).style.display='none';
}
function SelClose(tEvent){
	LastRPId.style.display = 'none';
		if (LastEnlargeId != ''){
		HideEnlarge2(LastEnlargeId);
	}

	CancelClick(tEvent,this)
}
function ShowSwatch(tEvent){
	var rootId = this.id.split("_")[0].replace("SWA","")
	ActivatorId = "P" + rootId;
	HasShownSwatch = true;
	swatchId = this.id.replace("SWA","SW");
	LastSwatchId = swatchId;
	mElement(swatchId).style.display = 'block';
	mElement("ColorDD_" + rootId).selectedIndex = mElement(this.id).getAttribute('colorIndex');	
	if (existTest(SwIndex[swatchId])){
		extraIndex = SwIndex[swatchId];
		mElement("SelImage" + ExtraSwatchObj.Data[extraIndex].PImgId).src = ExtraSwatchObj.Data[extraIndex].src;
		mElement("PELI" + ExtraSwatchObj.Data[extraIndex].PImgId).src = ExtraSwatchObj.Data[extraIndex].fullsrc;
	}
		SetSizes(rootId);
}


function HideSwatch(tEvent){
	swatchId = this.id.replace("SWA","SW");
	mElement(swatchId).style.display = 'none';
	ActivatorId = this.id.split("_")[0].replace("SWA","P");	
	//SetSelectState(ActivatorId,'visible',false);
	
}

function SetSelectState(Id,State,All){
	if (IsIe){	
		var aSelectors = mElement(Id).getElementsByTagName("Select");
		if (All){
			for(var i=0;i<aSelectors.length;i++){
				aSelectors[i].style.visibility=State;
			}
		}else{
			aSelectors[0].style.visibility=State;
		}
	}
}

function ShowSelector(tEvent){
	CancelClick(tEvent,this);
	extraIndex = -1;
	if (HasOpened){
		LastRPId.style.display = 'none';
	}
	if (LastEnlargeId != ''){
		HideEnlarge2(LastEnlargeId);
	}
	if (ppShown){
		HidePPEnlarge();
	}
	
	displaySelector(this.id);
	SetSizes(this.id.replace("RP",""));
}

function displaySelector(pSelId){
	thisRPID = pSelId;
	thisDetailId = thisRPID.replace("RP","P");
	mElement(thisDetailId).style.display = 'block';
	LastRPId = mElement(thisDetailId);
	HasOpened = true;
}

function existTest(thisObj){
	if((navigator.userAgent.toLowerCase().indexOf('macintosh')!=-1) || (navigator.userAgent.toLowerCase().indexOf('firefox')!=-1)) {
		if (thisObj != null){
		return true;
		}
	}else if (typeof thisObj != 'undefined'){
		return true
	}
	return false;
}
function CancelClick(tEvent,tThis){
if(tEvent.preventDefault)tEvent.preventDefault();
		tEvent.returnValue = false;
		tEvent.cancel = true;

    if(navigator.userAgent.toLowerCase().indexOf('safari')!=-1) {
      window.templink = tThis;
      window.temphref = tThis.href;
      tThis.href='javascript:void(0);';
      setTimeout('templink.href=temphref;temphref=null;templink=null;',100);
    }		

}

if (mElement("Images")){
InitShop();

}

