var currentActiveMenu = null;
var nextActiveMenu = null;
var imageSpecifier = "";
var imageSel = "";
var selReady = 0;
var surfaceNames = [];
var surfaceNamesXMLObj = null;


//////////////////////////////////////////////////////////////////////////////////////////////////////


function updateImage(specifier, sel)
{
	if(imageSpecifier != specifier)
	{
		updateSurfaces(specifier);
	}
	imageSpecifier = specifier;
	imageSel = sel;
	selReady = 0;
	AJAXReplace("renderedimagescreen", "showimage.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
	//AJAXReplace("mainroommenu", "mainmenu.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
	deactivateMenu(currentActiveMenu);
}

////////////////////////////////////////////////////////////////////////////////////////////////////

var xmlHttp

function updateTileSeries(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="cvTile.cfm";
url=url+"?series="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("cvTileSeries").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////
function updateTileSeries2()
{
	
	AJAXReplace("cvTileSeries", "placeholder.cfm?series=" + series );

	//AJAXReplace("cvTileSeries", "cvTile.cfm?series=" + series );
	//AJAXReplace("mainroommenu", "mainmenu.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
	deactivateMenu(currentActiveMenu);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


// this is to update the image on the workspace not the main large window

function updateImage2(specifier2, sel2)
{
	if(imageSpecifier != specifier)
	{
		updateSurfaces(specifier2);
	}
	imageSpecifier2 = specifier2;
	imageSel2 = sel2;
	selReady = 0;
	AJAXReplace("renderedimagescreen", "FlooringSelect_dev1c.cfm?specifier=" + imageSpecifier2 + "&sel=" + imageSel2);
//	AJAXReplace("mainroommenu", "mainmenu.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
	//deactivateMenu(currentActiveMenu);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////











function flipChamImageLoad(filename)
{
	document.getElementById('load' + filename).style.display = 'none'; document.getElementById('image' + filename).style.display = 'inline';
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function showEnlargeAndPrint(specifier, sel)
{
	var windowHandle = window.open("enlarge.cfm?specifier=" + specifier + "&sel=" + sel, "enlargeandprintwindow", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=790,height=550,top=5,left=5");
	windowHandle.focus();
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function GetXmlHttpObject(handler)
{ 
	var objXMLHttp = null;
	if(window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}	


//////////////////////////////////////////////////////////////////////////////////////////////////////

	
function AJAXReplace(elementid, url)
{
	var xmlHttp = GetXmlHttpObject();
	var baseElement = document.getElementById(elementid);
	if(xmlHttp == null || baseElement == null)
	{
		return;
	}
	baseElement.innerHTML = "Loading, please wait...";
	baseElement.xmlHttpObject = xmlHttp;
	eval("baseElement.xmlHttpFunction = function() { baseElement = document.getElementById(\"" + elementid + "\"); if(baseElement.xmlHttpObject.readyState==4 || baseElement.xmlHttpObject.readyState==\"complete\")	{ 			baseElement.innerHTML = baseElement.xmlHttpObject.responseText;	} }");
	xmlHttp.onreadystatechange = baseElement.xmlHttpFunction;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////

	
function updateSurfaces(passedSpecifier)
{
	surfaceNamesXMLObj = GetXmlHttpObject();
	if(surfaceNamesXMLObj == null)
	{
		return;
	}
	surfaceNamesXMLObj.onreadystatechange = function() 
	{ 
		if(surfaceNamesXMLObj.readyState==4 || surfaceNamesXMLObj.readyState=="complete") 
		{ 			
			surfaceNames = surfaceNamesXMLObj.responseText.split(",");	
			var stoneStyle = "none";
			var brickStyle = "none";
			var trimStyle = "none";
			for(i=0;i<surfaceNames.length;i++)
			{
				surfaceNames[i] = surfaceNames[i].toLowerCase();
				if(surfaceNames[i] == "stone") { stoneStyle = "inline"; }
				if(surfaceNames[i] == "brick" || surfaceNames[i] == "wall" || surfaceNames[i] == "wall body" || surfaceNames[i] == 'siding') { brickStyle = "inline"; }
				if(surfaceNames[i] == "trim") { trimStyle = "inline"; }
			}
			document.getElementById("gafbuttonstone").style.display = stoneStyle;
			document.getElementById("gafbuttonbrick").style.display = brickStyle;
			document.getElementById("gafbuttontrim").style.display = trimStyle;
			selReady = 1;
		} 
	};
	surfaceNamesXMLObj.open("GET", "getsurfaces.cfm?specifier=" + passedSpecifier, true);
	surfaceNamesXMLObj.send(null);
}


//////////////////////////////////////////////////////////////////////////////////////////////////////

	
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function prepMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
	if(menuElement)
	{
		menuElement.destinationY = 515;
		menuElement.currentY = 515;
		menuElement.speedY = 30;
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function changeSel(productid, whichSurface)
{
	var newSelString = "";
	var selArray = imageSel.split(",");
	if(selArray.length < whichSurface)
	{
		for(i=selArray.length;i<whichSurface;i++)
		{
			selArray[i] = "-1";
		}
	}
	for(i=0;i<selArray.length;i++)
	{
		if(i > 0)
		{
			newSelString = newSelString + ",";
		}
		if(i == (whichSurface - 1))
		{
			newSelString = newSelString + productid.toString();
		}	
		else
		{
			newSelString = newSelString + selArray[i];
		}
	}
	return newSelString;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function moveMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
//	var bgElement = document.getElementById("transparentmenubg");
	if(menuElement)
	{
		if(menuElement.destinationY < (menuElement.currentY - menuElement.speedY))
		{
			menuElement.currentY -= menuElement.speedY;
			menuElement.style.top = menuElement.currentY + "px";		
//			bgElement.style.top = menuElement.currentY + "px";		
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
		else if(menuElement.destinationY > (menuElement.currentY + menuElement.speedY))
		{
			menuElement.currentY += menuElement.speedY;
			menuElement.style.top = menuElement.currentY + "px";	
//			bgElement.style.top = menuElement.currentY + "px";		
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
		else
		{
			menuElement.currentY = menuElement.destinationY;
			menuElement.style.top = menuElement.currentY + "px";		
//			bgElement.style.top = menuElement.currentY + "px";		
			if(currentActiveMenu == elementid && menuElement.destinationY > 0)
			{
				currentActiveMenu = null;
				unhighlightAllButtons();
			}
			if(nextActiveMenu)
			{
				activateMenu(nextActiveMenu);
				nextActiveMenu = null;
			}
		}
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////



function activateMenu(elementid)
{
	if(currentActiveMenu)
	{
		nextActiveMenu = elementid;
		deactivateMenu(currentActiveMenu);
	}
	else {
		var menuElement = document.getElementById(elementid);
		if(menuElement)
		{
			if(!(menuElement.currentY))
			{
				prepMenu(elementid);
			}
			menuElement.destinationY = 0;
			currentActiveMenu = elementid;
			setTimeout("moveMenu('" + elementid + "');", 50);
		}
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function deactivateMenu(elementid)
{
	var menuElement = document.getElementById(elementid);
	if(menuElement)
	{
		if(!menuElement.currentY)
		{
			prepMenu(menuElement);
		}
		menuElement.destinationY = 515;
		setTimeout("moveMenu('" + elementid + "');", 50);
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function showRoomSelect()
{
	if(currentActiveMenu == "newhouseselectscreen")
	{
		deactivateMenu("newhouseselectscreen")
	}
	else
	{
//AJAXReplace("newhouseselectscreeninner", "roomselect_dev.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
AJAXReplace("newhouseselectscreeninner", "roomselect_dev2.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
		activateMenu("newhouseselectscreen");
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function showFlooringSelect()
{
	if(currentActiveMenu == "roofingselectscreen")
	{
		deactivateMenu("roofingselectscreen");
	}
	else
	{
	//AJAXReplace("roofingselectscreeninner", "meh.html?specifier=" + imageSpecifier + "&sel=" + imageSel);
AJAXReplace("roofingselectscreeninner", "Flooringselect_dev1c.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
		activateMenu("roofingselectscreen");
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////function showWallSelect()
function showWallSelect()
{
	if(currentActiveMenu == "wallselectscreen")
	{
		deactivateMenu("wallselectscreen");
	}
	else
	{
		AJAXReplace("wallselectscreeninner", "WallSelect1.cfm?specifier=" + imageSpecifier + "&sel=" + imageSel);
		activateMenu("wallselectscreen");
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function showProductSelect(whichProduct)
{
	if(currentActiveMenu == whichProduct + "selectscreen")
	{
		deactivateMenu(whichProduct + "selectscreen");
	}
	else
	{
		var whichSurface = 2;
		for(i=0;i<surfaceNames.length;i++)
		{
			if(whichProduct == "stone" && surfaceNames[i] == "stone")	{ whichSurface = i + 1; }
			if(whichProduct == "brick" && (surfaceNames[i] == "brick" || surfaceNames[i] == "wall" || surfaceNames[i] == "wall body" || surfaceNames[i] == "siding"))	{ whichSurface = i + 1; }
			if(whichProduct == "trim" && surfaceNames[i] == "trim")	{ whichSurface = i + 1; }
		}
		AJAXReplace(whichProduct + "selectscreeninner", "getpalette.cfm?specifier=" + imageSpecifier + "&surface=" + whichSurface);
		activateMenu(whichProduct + "selectscreen");
	}
}



//////////////////////////////////////////////////////////////////////////////////////////////////////


function highlightButton(whichButton)
{
	var buttonElement = document.getElementById("gafbutton" + whichButton);
	if(buttonElement)
	{
		buttonElement.src = "images/sidebar_button_" + whichButton + "_on.gif";
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function unhighlightButton(whichButton)
{
	var buttonElement = document.getElementById("gafbutton" + whichButton);
	if(buttonElement && currentActiveMenu != whichButton + "selectscreen")
	{
		buttonElement.src = "images/sidebar_button_" + whichButton + ".gif";
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function unhighlightAllButtons()
{
	unhighlightButton("newhouse");
	unhighlightButton("roofing");
	unhighlightButton("stone");
	unhighlightButton("brick");
	unhighlightButton("siding");
	unhighlightButton("trim");
}


//////////////////////////////////////////////////////////////////////////////////////////////////////


function setupPage()
{
}
	

//////////////////////////////////////////////////////////////////////////////////////////////////////


window.onload = setupPage;

