/*----------------------------------------------------
// js_functions.js
//
// written by Kevin Eyer (keyer@frymulti.com)
// Fry Multimedia
//--------------------------------------------------*/

// define menu attributes here
menuColor="#FFFFFF"
menuColorOn="#CCCCCC"
fontColorOn="#414141"
fontColorOff="#414141"
lineColor = "#999999"

sOldImageOver = ""

separator="<img src=/assets/images/spacer.gif height=1 width=1 border=0>"
separator2="<img src=/assets/images/separator2.gif height=2 width=122 border=0>"
line_separator="<img src=/assets/images/js_line.gif height=3 width=122 border=0>"
//*---------------------------------------------------
// shows the drop down menu 
// expected arg is active div
//--------------------------------------------------*/
function showDDNav(){

	
	var args
	var theObj
	
	args = showDDNav.arguments;
		
	
	if(br=="N")
	{
		theObj = eval(args[1]);
	}
	else if(br=="IE")
	{
		theObj = eval(args[0]);
	}
	else 
	{
		return;
	}
	
	
	
	//*******************************************************
	//CODE ADDED TO KEEP MOUSED OVER MENU ITEM HIGHLIGHTED 
	//WHEN A USER LEAVES THE MENU ITEM TO CHOOSE A SUB ITEM
	//GF 02/27/2002
	
	//NEED TO TURN OFF PREVIOUS HIGHLIGHTED ITEM

	if (theObj)
	{
		//alert("SHOULD HIGHLIGHT")
		sObjID = theObj.id
		nObjLen = sObjID.length
		
		//ALL ITEMS ARE PREFIXED WITH 'menu'
		nMenuItem = sObjID.substring(4,nObjLen);
		
		sImageNmOn = "image" + nMenuItem + "on";
		sImageID = "image" + nMenuItem;
		
		//FIRST MAKE SURE THE LAST ITEM IS OFF
		if (sOldImageOver.length>0)
		{
			sOldImageOverOff = sOldImageOver + "off"
			
			changeImages(sOldImageOver, sOldImageOverOff)
		}
		
		//TURN THE NEW ONE ON
		//NOTE: ONE EXCEPTION -image8 is actually image27 in furniture
		if (sImageID=='image8')
		{
			sImageID='image27'
			sImageNmOn='image27on'
		}
		changeImages(sImageID, sImageNmOn);
		
		sOldImageOver = sImageID
		//*******************************************************
	}
	else
	{
		return;	
	}
	
	if(br=="N"){
		
		
  		if (theObj)
  		{
  			if(theObj.visibility=='hide')
  			{
  				
  				theObj.visibility = 'show';
  			}
  		}
	}
	else if(br=="IE"){
		
		if (theObj){ 			
			if(theObj.style.visibility=='hidden'){
			
				// activate hideElement to hide <select> boxes
				//hideElement('SELECT');
				theObj.style.visibility = 'visible';
				
			}
		}
	}
	else{return}
}

//*---------------------------------------------------
// hides the drop down menu 
// expected arg is active div 
//--------------------------------------------------*/
function hideDDNav(){

	var args, theObj;
	args = hideDDNav.arguments;
	
	if(br=="N")
	{
		theObj = eval(args[1]);
	}
	else if(br=="IE")
	{
		theObj=eval(args[0]);
	}
	else 
	{
		return;
	}

	
	if(br=="N"){

		theObj = eval(args[1]);
		if (theObj) if(theObj.visibility=='show'){theObj.visibility = 'hide';}
	}
	else if(br=="IE"){
		
		if (theObj) 
			{
				if(theObj.style.visibility=='visible')
				{
					showElement('SELECT');theObj.style.visibility = 'hidden';
									
				}
				
			}
		}
	else{return}
}

//*---------------------------------------------------
// hides drop down menu when menu is active
// expected arg is active menu
//--------------------------------------------------*/
function hideDiv(){
	args = hideDiv.arguments;
	if(br=="N"){}
	else if(br=="IE"){
		theObj=eval(args[0]);
		// calculate active size of menu
		leftDiv=theObj.offsetLeft +2
		rightDiv=theObj.offsetLeft + theObj.clientWidth -2
		topDiv=theObj.offsetTop +2
		bottomDiv=theObj.offsetTop + theObj.clientHeight -2		
		if(window.event.clientY > bottomDiv || 
			window.event.clientY < topDiv || 
			window.event.clientX < leftDiv || 
			window.event.clientX > rightDiv)
			{
			theObj.style.visibility = 'hidden';
			
			//*******************************************************
			//CODE ADDED TO KEEP MOUSED OVER MENU ITEM HIGHLIGHTED 
			//WHEN A USER LEAVES THE MENU ITEM TO CHOOSE A SUB ITEM
			//!!AND TURN IT OFF WHEN THEY MOUSEOFF THE DHTML MENU!!
			sObjID = theObj.id
			nObjLen = sObjID.length

			//ALL ITEMS ARE PREFIXED WITH 'menu'
			nMenuItem = sObjID.substring(4,nObjLen);
				
			sImageNmOff = "image" + nMenuItem + "off";
			sImageID = "image" + nMenuItem;
			//alert("TURNING OFF: " + sImageNmOff);
			changeImages(sImageID, sImageNmOff);
			//*******************************************************	
			
			 // activate showElement to show <select> menu's
			 //showElement('SELECT');
			}				
		}
	else{return}
}

//*---------------------------------------------------
// highlight menuitem
// expected arg is active menuitem
//--------------------------------------------------*/
function divOver(){
	args = divOver.arguments;
	if(br=="N"){}
	else if(br=="IE"){
		theObj=eval(args[0]);
		theObj.style.backgroundColor=menuColorOn;
	}
	else{return}
}

//*---------------------------------------------------
// highlight off menuitem
// expected arg is active menuitem
//--------------------------------------------------*/
function divOut(){
	args = divOut.arguments;
	if(br=="N"){}
	else if(br=="IE"){
	theObj=eval(args[0]);
		theObj.style.backgroundColor=menuColor;
	}
	else{return}
}

//*---------------------------------------------------
// highlight on menuitem in netscape
// expected arg is active menuitem
//--------------------------------------------------*/
function menuOver(){
	var args, theMenu;
	args = menuOver.arguments;
	theMenu = eval(args[0]);
	theMenu.bgColor=menuColorOn 
}

//*---------------------------------------------------
// highlight off menuitem in netscape
// expected arg is active menuitem
//--------------------------------------------------*/
function menuOut(){
	var args, theMenu;
	args = menuOut.arguments;
	theMenu = eval(args[0]);
	theMenu.bgColor=menuColor;
	
}

//*---------------------------------------------------
// goes to specified url
// expected arg is url 
//--------------------------------------------------*/

// the code below is used to resolve the issu of the dhtml link 
// showing up secure when changing from a secure to insecure environment
// the " linkn" value builds and absolute  url from the http for all the options in the data.js file
// removing the  linkn" value will make all the links relative again.

	url= window.location.href
	
	dev=url.indexOf("cbdev.frymulti.com")
	dev2=url.indexOf("chicbdev.frymulti.com")
	qa=url.indexOf("fryqa.crateandbarrel.com")
	qa2=url.indexOf("chifryqa.crateandbarrel.com")
	qa3=url.indexOf("qa.crateandbarrel.com")
	qa5=url.indexOf("chitest.crateandbarrel.com")
	
	//Added this link to for local Crate testing
	qa4=url.indexOf("appdev2")
	
	sand=url.indexOf("sandbox.crateandbarrel.com")
	stage=url.indexOf("staging.crateandbarrel.com")
	
if (dev2!="-1"){
		linkn="http://chicbdev.frymulti.com"}
else if (dev!="-1"){
		linkn="http://cbdev.frymulti.com"}
else if (qa!="-1"){
		linkn="http://fryqa.crateandbarrel.com"}
else if (qa2!="-1"){
		linkn="http://chifryqa.crateandbarrel.com"}
else if (qa3!="-1"){
		linkn="http://qa.crateandbarrel.com"}
else if (qa5!="-1"){
		linkn="http://chitest.crateandbarrel.com"}
else if (sand!="-1"){
		linkn="http://sandbox.crateandbarrel.com"}
else if (stage!="-1"){
		linkn="http://staging.crateandbarrel.com"}
		
//Added this link to for local Crate testing
else if (qa4!="-1"){
		linkn="http://appdev2"}
		
else {
		linkn=""}
		//linkn=sURL;


function gotoUrl(){
	args = gotoUrl.arguments;
	var CM_RefferingURL, CM_Location, CM_Exists;
	CM_ReferringURL = window.location.href;
	CM_Exists = args[0].indexOf('?');
	
	if (CM_Exists == -1) {
		CM_Location = args[0] 
	}
	else {
		CM_Location = args[0] 
	}
	location.href =(linkn)+ (CM_Location)
	
}

//*---------------------------------------------------
// hides specified tag
//--------------------------------------------------*/
function hideElement(HTMLtag)
{
	for (i = 0; i < document.all.tags(HTMLtag).length; i++)
	{
		obj = document.all.tags(HTMLtag)[i];
		obj.style.visibility = "hidden";
	}
}

//*---------------------------------------------------
// shows specified tag
//--------------------------------------------------*/
function showElement(HTMLtag)
{
	for (i = 1; i < document.all.tags(HTMLtag).length; i++)
	{
		obj = document.all.tags(HTMLtag)[i];
		obj.style.visibility = "visible";
	}
}

//*---------------------------------------------------
// build the divs for IE
// expected args are 'div name','x in menudata aray',
// 'menu left coord','top in pixels' 
//--------------------------------------------------*/
var tdID = 0
function doDiv(){
	args = doDiv.arguments;
	var divID = args[0];
	var x = args[1];
	var divLeft = args[2];
	var divTop = args[3];

	objDiv = "<div id=\"" + divID + "\" style=\"position:absolute; width:150px; z-index:2; top:"+ divTop +"px; left: " + divLeft + "; background-color: #FFFFFF; border:1 solid #999999; visibility: hidden;\" onMouseover=\"showDDNav('document.all[\\'"+ divID +"\\']')\"  onMouseout=\"hideDiv('document.all[\\'" + divID + "\\']')\">"
	objDiv += "<div id=\"spacer\" style=\"position:relative; height:6px; width:140px; z-index:3; left: 5; font-size:1;\"></div>"
	
	for (y = 0; y < 15; y++){
	    if(menuData[x][y][0]!=null){
		objDiv += "<div id=\"bar\" style=\"position:relative; height:1; width:140px; z-index:3; left: 5; background-color:" + lineColor + ";\">" + separator + "</div>"
		objDiv += "<div id=\"x" + tdID + "\" style=\"position:relative; background-color:"+ menuColor +"; width:140px; z-index:3; left: 5;\" onMouseover=\"divOver('document.all[\\'x" + tdID + "\\']')\" onMouseout=\"divOut('document.all[\\'x" + tdID + "\\']')\" class=\"dhtmlNav\" onClick=\"gotoUrl('"+menuData[x][y][1]+"')\">&nbsp;"+ menuData[x][y][0] +"&nbsp;</div>"
		tdID = tdID +1
		}
	}
	objDiv += "<div id=\"bar\" style=\"position:relative; height:1; width:140px; z-index:3; left: 5; background-color:" + lineColor + ";\">" + separator + "</div>"
	objDiv += "<div id=\"spacer\" style=\"position:relative; height:6px; width:140px; z-index:3; left: 5; font-size:1;\"></div></div>"
	
	document.write(objDiv);
}


//*---------------------------------------------------
// build the layers for netscape
// expected args are 'div name','x in menudata aray',
// 'menu left coord','top in pixels' 
//--------------------------------------------------*/
function doLayer(){
	args = doLayer.arguments;
	var nestLayerID=0
	var layerID = args[0];
	var x = args[1];
	var layerLeft = args[2];
	var layerTop = args[3];

	if(navigator.platform.indexOf("Win") < 0){layerInc=16;layerStyle="netMenuMac";}
	else{layerInc=15;layerStyle="netMenu";}	
		
	objDiv = "<layer id=\""+ layerID +"\" Z-INDEX=1 BGCOLOR=#999999 WIDTH=125 LEFT="+ layerLeft +" TOP="+ layerTop +" VISIBILITY=HIDE onmouseover=\"showDDNav('','document.layers[\\'"+ layerID +"\\']')\" onmouseout=\"hideDDNav('','document.layers[\\'"+ layerID +"\\']')\">"
	
	var nestTop=2
			for (y = 0; y < 15; y++){
		    if(menuData[x][y][0]!=null){
			objDiv += "<layer id=\"line2\" BGCOLOR=\""+ lineColor +"\" WIDTH=122 HEIGHT=3 Z-INDEX=8 LEFT=2 TOP="+ nestTop +">"+ separator2+ line_separator +"</layer>"
			objDiv += "<layer id=\"x"+nestLayerID+"\" class=\""+ layerStyle +"\" Z-INDEX=7 BGCOLOR=\""+ menuColor +"\" HEIGHT=19 WIDTH=122 LEFT=2 TOP="+ nestTop +" onmouseover=\"menuOver('document.layers[\\'"+ layerID +"\\'].document.layers[\\'x"+nestLayerID+"\\']')\" onmouseout=\"menuOut('document.layers[\\'"+ layerID +"\\'].document.layers[\\'x"+nestLayerID+"\\']')\" onClick=\"gotoUrl('"+menuData[x][y][1]+"')\"><a id=navstyle  href=javascript:gotoUrl('"+menuData[x][y][1]+"')><font color=" + fontColorOff + ">"+ menuData[x][y][0]
			objDiv +="</font></a></layer>"		
			nestLayerID=nestLayerID+1
			nestTop=nestTop+layerInc+1
			}
		}
		objDiv += "<layer id=\"line2\" BGCOLOR=\""+ lineColor +"\" WIDTH=122 LEFT=2 HEIGHT=6 Z-INDEX=8 TOP="+ nestTop +">"+ line_separator +separator2+"</layer>"
		objDiv +="<layer id=\"lineEnd\" BGCOLOR=#999999 WIDTH=122 HEIGHT=3 Z-INDEX=7 TOP="+ nestTop +"></layer></layer>"

	document.write(objDiv)
}


