var flashMovie;
var imageIDold;
var data = new Array();
parent.mydata = new Array(false);
parent.colorID = false
parent.swatchID = false
parent.imageID = false
parent.preload='';
parent.keywords='';
var divactive;
var divnext;
var divflash;


function loadImages()
{
MM_preloadImages('../../assets/images/returnhitpage2.gif','../../assets/images/cartadd2.gif','../../assets/images/cartview2.gif'
				,'../../assets/images/navhome2.gif','../../assets/images/navfitting2.gif','../../assets/images/navscience2.gif','../../assets/images/navapp2.gif','../../assets/images/navbrands2.gif','../../assets/images/navlogin2.gif','../../assets/images/navcart2.gif','../../assets/images/navaccount2.gif','../../assets/images/navcontact2.gif','../../assets/images/navabout2.gif'
				,'../../assets/images/lock0.gif','../../assets/images/lock1.gif'
				)
i = parent.preload.split(',')
for (j=0;j<i.length-1;j++)
{	
	//alert(i[j])
	MM_preloadImages(i[j])
}
}

function placeelements()
{
	//divflash.style.visibility='visible'
	divactive.style.left = '194px'
	divflash.style.left = (getleft(document.getElementById('flashspace'))-0) +'px'
	//alert('left='+divflash.style.left)
	divflash.style.top = (gettop(document.getElementById('flashspace'))-0) +'px'
	parent.divtop=divflash.style.top
	//alert('top='+divflash.style.top)
	wrapperheight()
	//h=divactive.clientHeight
	//if (h < 433) h = 433
	//document.getElementById('blockleft').style.height=(h+20)+'px'	
	//document.getElementById('blockleft2').style.height=(h+19)+'px'	
	//h=135+39+h+20
	//document.getElementById('wrapper').style.height = h+'px'
	//document.getElementById('bottomnav').style.top=(h+10)+'px'
	//document.getElementById('blockbottom').style.top='-1000px'
	divnext.style.left = '-1000px'
	document.getElementById('blockright').style.top='-1000px'
	document.getElementById('blockright').style.left='-1000px'
	clearInterval(parent.place)
	divflash.style.visibility='visible'
	
	for (j=0; j < parent.sorted.length; j++)
	{
		obj=document.getElementById('size_lb'+parent.sorted[j])
		if (obj) obj.style.visibility='visible'
	}
	//obj=document.getElementById('size_lb$partID')
	obj=document.getElementById('size_lb'+partID)
	if (obj) obj.style.visibility='visible'
}

function wrapperheight()
{
	d=divactive.clientHeight
	s=document.getElementById('selector').clientHeight
	h = d > s ? d : s;
	if (h < 433) h = 433
	//alert('d= '+d+' s='+s+' h='+h)
	document.getElementById('blockleft').style.height=(h+20)+'px'	
	document.getElementById('blockleft2').style.height=(h+19)+'px'	
	h=135+39+h+20
	document.getElementById('wrapper').style.height = h+'px'
	document.getElementById('bottomnav').style.top=(h+10)+'px'
	document.getElementById('blockbottom').style.top='-1000px'
}

function essence(move,partID)
{
	if (parent.sorted.length==0) return
	//if (document.body.clientWidth > 1040)
		//window.resizeTo(1040,900)
	ID = partID
	match=''
	for (j=0;j<parent.sorted.length;j++)
	{
		if (parent.sorted[j] == partID)
		{
			if (move == 'right')
			{	
				if (parent.sorted[j-1])
				{
					ID = parent.sorted[j-1]
					match = parent.matches[j-1]
				}	
				else 
				{
					alert('You are already at the beginning of your search set.')
					return
				}
			}
			else
			{
				if (parent.sorted[j+1])
				{
					ID = parent.sorted[j+1]
					match = parent.matches[j+1]
				}	
				else
				{
					alert('You are already at the end of your search set.')
					return
				}
			}
			break
		}
	}
	if (ID == partID) return
	clearTimeout(parent.normal[partID])
	parent.colors = new Array();
	document.getElementById('flashspace').src='../../images/socks'+imageIDold+'-med.jpg'
	parent.move=move
	//alert('socks-detail.js keywords='+parent.keywords)
	xmlreqGET('socks-detail.pl?partID='+ID+'&match='+match+'&essence=1&keyword='+parent.keywords) 
	
	document.getElementById('blockright').style.left='997px'
	document.getElementById('blockright').style.top='0px'


	h=divactive.clientHeight
	if (h < 433) h = 433
	document.getElementById('blockleft').style.height=(h+20)+'px'	
	h=135+39+h+20+25
	document.getElementById('blockbottom').style.top=h+'px'

	
	parent.step=3
	clearInterval(parent.moveit)
	
	setTimeout("divflash.style.visibility='hidden'",1)
	//el=document.getElementById('size_lb')
	//if (el)
	//setTimeout("document.getElementById('size_lb').style.visibility='hidden'",1)
	for (j=0; j < parent.sorted.length; j++)
	{
		obj=document.getElementById('size_lb'+parent.sorted[j])
		if (obj) obj.style.visibility='hidden'
	}
		
	if (parent.move=='left')
	{
		//setTimeout("divflash.style.top='-1000px'",1)
		parent.left=194+825
		divnext.style.left=194+825
		parent.moveit = setInterval('moveLeft()',1)
	}
	else if (parent.move=='right')
	{
		//setTimeout("divflash.style.top='-1000px'",1)
		//setTimeout("divflash.style.visibility='hidden'",1000)
		parent.left= 194-825
		divnext.style.left= 194-825
		parent.moveit = setInterval('moveRight()',1)
	}
}

function xmlhttpDone(url,req)
{
	if (req.status == 200 || req.status == 304)
	{
		parent.window.status='Done '+url
		
		parent.mydata=req.responseText.split(String.fromCharCode(5))
		if (url.indexOf('selector-core') > -1)
		{
			//test = window.open("","_blank")
			//test.document.writeln(parent.mydata[1])
			parent.window.status='Done '+url
			if (parent.loaded)
				parent.selectorint = setInterval("loadselector()", 100)
		}
		else
		{
			if (imageIDold != parent.mydata[1])
			{
				imageIDold=parent.mydata[1]
				setTimeout("loadXML('../../cgi-bin/socks-xml.pl?imageID='+parent.mydata[1])",1)
			}	
			if (parent.move=='none')
			{
				divactive.innerHTML = parent.mydata[2]
				loadsecondnav(parseInt(parent.mydata[0]))
				//divflash.style.top = parent.divtop
				placeelements()
			}
			else
			{
				divnext.innerHTML = parent.mydata[2]
			}	
		}
		//test=window.open()
		//test.document.writeln(mydata[2])
		//test.document.close()
		setTimeout("runfast()",1)
	}
	else
	{
		alert('Server communication error, please try again.')
		parent.window.status='ERROR: '+url
	}
}

function runfast()
{
	parent.step=12
	return
	clearInterval(parent.moveit1)
	if (parent.move=='left')
	{
		parent.moveit2 = setInterval('moveLeft()',1)
	}
	else if (parent.move=='right')
	{
		parent.moveit2 = setInterval('moveRight()',1)
	}
}

function loadsecondnav(partID,as)
{
	//loc=0
	obj = document.getElementById('secondnav');
	if (!obj)
	{
		setTimeout("loadsecondnav('"+partID+"','"+as+"')",100)
		return
	}
	//prior=partID-1
	//next=partID+1
	parent.colors = new Array();
	//html = '<table cellpadding=0 cellspacing=0 border=1 height=39 width=986>'
	//html += '<tr valign=bottom><td><span class=greenlink style="margin:20px 0 10px 180px; width: 300;"><a href="javascript:essence(\\'right\\','+prior+')"><< Previous model</a></span></td>'
	//html += '<td><div class=greenlink style="float:left; margin:20px 0 10px 10px; width: 300; height:20px;"><a href="javascript:essence(\\'left\\','+next+')">Next search model >></a></div></td>'
	//html += '<td align=right><span class=detailtext style="margin:20px 0 10px 10px;">0 pairs</span> <a href=socks-edit.pl><img src=../../assets/images/cartview.gif width=23 height=28 border=0 onMouseover="this.src=//'../../assets/images/cartview2.gif//'" onMouseout="this.src=//'../../assets/images/cartview.gif//'" style="padding: 0 15px 0 0;"></a></td>'
	//html += '</tr></table>'
	
//	if (parent.sorted.length > 0)
//	{
//		previousclass='goodnav'
//		nextclass='goodnav'
//		notFound=true
//		for (j=0;j<parent.sorted.length;j++)
//		{
//			if (parent.sorted[j]==partID)
//			{
//				loc=(j+1);
//				if (j==0) previousclass='badnav'
//				if (j==parent.sorted.length-1) nextclass='badnav'
//				notFound=false
//				break
//			}
//		}
//		if (notFound)
//		{
//			previousclass='badnav'
//			nextclass='badnav'
//		}
//	}
//	else
//	{
//		previousclass='badnav'
//		nextclass='badnav'
//	}
	
//	html = '<div class=detailtext style="float:right; margin:8px 15px 0 2px;"><a href=socks-edit.pl?ni><img src=../../assets/images/cartview.gif width=23 height=28 border=0 onMouseover="this.src=\'../../assets/images/cartview2.gif\'" onMouseout="this.src=\'../../assets/images/cartview.gif\'"></a></div>'
//	if (pairs > 1)
//		html += '<div class=detailtext style="float:right; margin:20px 0 0 0;" id=incart><b>'+pairs+' pairs in your cart</b></div>'
//	else
//		html += '<div class=detailtext style="float:right; margin:20px 0 0 0;" id=incart><b>'+pairs+' pair in your cart</b></div>'
	
//	html += '<div class=greenlink style="position:relative; left:190px; top:20px;"><a href="javascript:essence(\'right\','+partID+')" class='+previousclass+' id=previousid>&#171; PRIOR MODEL</a>'
//	html += '<a href="javascript:essence(\'left\','+partID+')" style="margin:0 0 0 20px" class='+nextclass+' id=nextid>NEXT MODEL &#187;</a>'
//	if (loc > 0)
//		html += ' &nbsp; &nbsp; Showing #'+ loc + ' of '+ parent.sorted.length + ' models'
//	html += '</div>';
	
	loc=-1
	if (parent.sorted.length > 0)
	{
		for (j=0;j<parent.sorted.length;j++)
		{
			if (parent.sorted[j]==partID)
			{
				loc=j;
				break
			}
			else if (parent.sorted[j]==as)
			{
				loc=j;
				parent.sorted[j]=partID
				break
			}
		}
	}
	html = '<div class=detailtext style="float:right; margin:8px 15px 0 0px;"><a href=socks-edit.pl?ni><img src=../assets/images/cart.jpg width=23 height=22 border=0 onMouseover="this.src=\'../assets/images/cart.jpg\'" onMouseout="this.src=\'../assets/images/cart.jpg\'" title="Shopping cart"></a></div>'
	if (pairs > 1)
		html += '<div class=secondarynav style="float:right; margin:15px 0 0 0;"><b>'+pairs+' pairs in your cart</b></div>'
	else
		html += '<div class=secondarynav style="float:right; margin:15px 0 0 0;"><b>'+pairs+' pair in your cart</b></div>'
	
	if (loc > -1)
	{
		html += '<table border=0 height=23 cellpadding=0 cellspacing=0 style="position:relative; left:190px; top:10px;"><tr>'
		if (loc > 0)
		{
			html += '<td width=25 align=right valign=top><a href="javascript:essence(\'right\',\''+parent.sorted[loc]+'\')" id=previousid><img src=../assets/images/prior_btn.gif width=19 height=19 border=0 hspace=1></a></td>'
			html += '<td><a href="javascript:essence(\'right\',\''+parent.sorted[loc]+'\')" class=goodnav id=previousid>Prior</a></td>'
		}	
		else
		{
			html += '<td width=25><a style="visibility: hidden" href="javascript:essence(\'right\',\''+parent.sorted[loc]+'\')" id=previousid><img src=../assets/images/prior_btn.gif width=19 height=19 border=0 hspace=1></a></td>'
			html += '<td><a style="visibility: hidden" href="javascript:essence(\'right\',\''+parent.sorted[loc]+'\')" class=goodnav id=previousid>Prior</a></td>'
		}
		
		if (loc < parent.sorted.length-1)
		{
			next = '<td><a href="javascript:essence(\'left\',\''+parent.sorted[loc]+'\')" class=goodnav id=nextid>Next</a></td>'
			next += '<td valign=top><a href="javascript:essence(\'left\',\''+parent.sorted[loc]+'\')" class=goodnav id=nextid><img src=../assets/images/next_btn.gif width=19 height=19 border=0 hspace=3></a></td>'
		}
		else
			next = ''
		
		html += '<td align=center width=185 nowrap class=secondarynav>Showing model '+ (loc+1) + ' of '+ (parent.sorted.length) + '</td>'
		
		html += next+'</tr></table>'
	}
	
	
	
	
	
	if (document.location.href.indexOf('.htm') == -1)
	{
		obj.innerHTML=html
	}	
	
}

function moveLeft(){
	parent.left = parent.left - parent.step
	divactive.style.left = (parent.left - 825) + 'px'
//	if (parent.left < 530)
//	{
//		divflash.style.left = (parent.left + 215) + 'px'
//		divflash.style.top = parent.divtop
		//divflash.style.visibility='visible'
//	}
//	else
//	{
		//194-825+4=-631
		//divflash.style.left = (parent.left - 625) + 'px'
//	}
	divnext.style.left = parent.left + 'px'
	if (parent.left <= 204) //194
	{
		clearInterval(parent.moveit)
		//finishedmove()
		parent.place = setInterval("finishedmove()", 10)
	}
}

function moveRight(){
	parent.left = parent.left + parent.step
	divactive.style.left = (parent.left + 825) + 'px'
//	if (parent.left > 0)
//	{
//		divflash.style.left = (parent.left + 215) + 'px'
//		divflash.style.top = parent.divtop
		//divflash.style.visibility='visible'
//	}
//	else
//		divflash.style.left = (parent.left + 1025) + 'px'
	
	divnext.style.left = parent.left + 'px'
	if (parent.left >= 184) //194
	{
		clearInterval(parent.moveit)
		//finishedmove()
		parent.place = setInterval("finishedmove()", 10)
	}
}

function finishedmove()
{
	if (divnext.innerHTML=='.' || parent.mydata[0] == false) return
	clearInterval(parent.place)
	loadsecondnav(parseInt(parent.mydata[0]))
	divactive.innerHTML = '.'
	var c=divactive
	divactive=divnext
	divnext=c
	parent.place = setInterval("placeelements()", 10)
	parent.mydata[0] == false
}

parent.colorID = false
parent.swatchID = false
parent.imageID = false
parent.partID = false
parent.colors = new Array();
parent.normal = new Array();
parent.normalint = new Array();

function swatchoff(partID,colorID,swatchID,imageID)
{
	//document.images['swatch'+partID+'-'+swatchID].src='../../images/socks'+imageID+'-tn.jpg'
	document.images['swatch'+partID+'-'].src='../../images/socks'+imageID+'-tn.jpg'
	if (colorID)
	{
		obj = document.getElementById('row'+partID+'-'+colorID)
		//obj = document.getElementById('row'+colorID)
		obj.style.backgroundColor='#ffffff';
	}
}

function swatchon(type,partID,colorID,swatchID,imageID)
{
	//alert('type='+type+' partID='+partID+' colorID='+colorID+' swatchID='+swatchID+' imageID='+imageID)
	clearTimeout(parent.normal[partID])
	if (parent.partID && partID+'-'+swatchID != parent.partID+'-'+parent.swatchID)
		//setTimeout("swatchoff("+parent.partID+","+parent.colorID+","+parent.swatchID+","+parent.imageID+")",500)
	
	parent.partID = partID
	parent.imageID = imageID
	parent.swatchID = swatchID
	//document.images['swatch'+partID+'-'+swatchID].src='../../swatches/swatch'+swatchID+'-big.jpg'
	document.images['swatch'+partID+'-'].src='../../swatches/swatch'+swatchID+'-big.jpg'
	if (colorID)
	{
		parent.colors[colorID]=colorID;
		for (j in parent.colors)
		{
			if (j == colorID) continue
			obj = document.getElementById('row'+partID+'-'+parent.colors[j])
			if (obj)
				obj.style.backgroundColor='#ffffff';
		}
		
		obj = document.getElementById('row'+partID+'-'+colorID)
		obj.style.backgroundColor='#d4edea';
	}
	if (type == 'timeout')
	{
		parent.normal[partID] = setTimeout("swatchoff("+partID+","+colorID+","+swatchID+","+imageID+")",5000)
		if (parent.colorID)
		{
			clearInterval(parent.normalint[parent.partID])
			parent.normalint[parent.partID] = setInterval("swatchon('',"+parent.partID+","+parent.colorID+","+parent.swatchID+","+parent.imageID+")",3000)
		}
	}
	else if (type == 'interval')
	{
		clearInterval(parent.normalint[parent.partID])
		parent.colorID = colorID
		parent.swatchID = swatchID
		parent.imageID = imageID
		parent.partID = partID
		parent.normalint[parent.partID] = setInterval("swatchon('',"+parent.partID+","+colorID+","+swatchID+","+imageID+")",3000)
	}
	else if (type == 'clear')
	{
		parent.colorID = false
		parent.swatchID = false
		parent.imageID = false
		parent.partID = false
		parent.normal[partID] = setTimeout("swatchoff("+partID+","+colorID+","+swatchID+","+imageID+")",5000)
		clearInterval(parent.normalint[partID])
	}	
}

function sizechange(obj,oldID)
{
	newID = obj.options[obj.selectedIndex].value
	partID=newID;
	for (j=0;j<parent.sorted.length;j++)
	{
		if (parent.sorted[j] == oldID)
		{
			parent.sorted[j] = newID
		}
	}
	parent.move='none'
	xmlreqGET('socks-detail.pl?partID='+newID+'&essence=1') 
}

function sockdetail_DoFSCommand(command, args) { 
	switch (command)
	{
		case 'writezone':
			writezone(args)
			break;
		case 'boldzone':
			boldzone(args)
			break;
		case 'regularzone':
			regularzone(args)
			break
		default:
			window.status='ERROR: '+command+'----'+args
	}
}

function PassFlash(){
	window.document.sockDetail.SetVariable("closeupText", document.data.mytext.value);
}

function readVariable(mvar)
{
	loadRefresh(flashMovie.GetVariable('/:'+mvar));
}

function loadRefresh(mfile)
{
	mdate = new Date()
	document.location.href=mfile+'?cache='+mdate.getTime()
}

function loadXML(mfile)
{
	flashMovie=getFlashMovieObject("sockdetail");
	flashMovie.SetVariable("runFunction", "loadXML~"+mfile)
	flashMovie.GotoFrame(2);
	flashMovie.Play()
}

function flash_onRollOver(args)
{
	boldzone(args)
	data = args.split('~')
	//if (flashMovie==null)
	flashMovie=getFlashMovieObject("sockdetail");
	flashMovie.SetVariable("runFunction", "onRollOver~"+data[0])
	//flashMovie.SetVariable("closeupText", "onRollOver~"+j)
	
	//flashMovie.GotoFrame(2);
	flashMovie.GotoFrame('2');
	//alert('The current Frame is: '+flashMovie.TGetProperty("/", 4));
	for (j=0; j<50; j++)
   {
	//alert(j+'= '+flashMovie.TGetProperty("_root", j));
   }

	
	flashMovie.Play()
	//alert(flashMovie.TotalFrames())
	//alert(flashMovie.GetVariable("runFunction"))
	//flashMovie.Zoom(50)
	//play()
	//stop()
}

function flash_onRollOut(args)
{
	regularzone(args)
	data = args.split('~')
	flashMovie=getFlashMovieObject("sockdetail");
	flashMovie.SetVariable("runFunction", "onRollOut~"+data[0])
	flashMovie.GotoFrame(2);
	flashMovie.Play()
}

function writezone(args)
{
var mdata = args.split('~')
var obj=document.getElementById('zone_'+mdata[0])
if (mdata[1])	obj.innerHTML=mdata[1]
}

function boldzone(args){
window.status=args
data = args.split('~')
obj=document.getElementById('zone_'+data[0])
//obj=getElementById('zone_e')
obj.style.color='#000000'
obj.style.backgroundColor='#d4edea';
//obj.style.fontWeight='bold'
}

function regularzone(args){
//alert(hotspot)
data = args.split('~')
obj=document.getElementById('zone_'+data[0])
obj.style.backgroundColor='#ffffff';
obj.style.color='#090644'
//obj.style.fontWeight='normal'
}

//the getFlashMovieObject() function has moved to selector.js
