var sResults, pageLength = 1000, pages,pagesBC, currentPage,currentPageBC, prevImage, bcArray,rangeLength = 20,x,y,heading;

topLevelChanged = function()
{
	//clearImage();
	if (typeof ddlTopLevel == "object")
	{
		if (ddlTopLevel.options[ddlTopLevel.selectedIndex].value != "")
		{
			bc.innerHTML = "";
			ddlBotLevel.style.display = "none";
			Public.ProductFinder.GetMidLevel(ddlTopLevel.options[ddlTopLevel.selectedIndex].value, populateMidLevel);
			document.TopIndex = ddlTopLevel.selectedIndex;
		}
		else
		{
			ddlMidLevel.style.display = "none";
			ddlBotLevel.style.display = "none";
			ddlMidLevel.options.length = 0;
			ddlBotLevel.options.length = 0;
			ddlMidLevel.options[0] = new Option("Please select a category to search by", "");
			ddlBotLevel.options[0] = new Option("Please select a category to search by", "");
			document.TopIndex = null;
		}
		clearBC();
		clearSR();
	}
	
	return true;
}

midLevelChanged = function()
{
	//clearImage();
	if (typeof ddlTopLevel == "object" && typeof ddlMidLevel == "object")
	{
		if (ddlMidLevel.options[ddlMidLevel.selectedIndex].value != "")
		{
			switch(ddlTopLevel.options[ddlTopLevel.selectedIndex].value)
			{
				case "Category":
					Public.ProductFinder.GetChildFurnitureTypes(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, populateChildFurnitureTypes);
					break;
				case "Designer":
					Public.ProductFinder.GetRangesByDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, populateRangesD);
					break;
				case "Brand":
					Public.ProductFinder.GetRangesByBrand(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, populateRangesB);
					break;
			}
			document.MiddleIndex = ddlMidLevel.selectedIndex;
		}
		
		ddlBotLevel.options.length = 0;
		ddlBotLevel.style.display = "none";
		clearBC();
		clearSR();
	}
	return true;
}

botLevelChanged = function()
{
	//clearImage();
	if (typeof ddlTopLevel == "object" && typeof ddlMidLevel == "object" && typeof ddlBotLevel == "object")
	{
		clearBC();
		clearSR();
		
		if (ddlBotLevel.options[ddlBotLevel.selectedIndex].value != "")
		{		   
			switch(ddlTopLevel.options[ddlTopLevel.selectedIndex].value)
			{
				case "Category":					
					Public.ProductFinder.GetRangesByFurnitureType(ddlBotLevel.options[ddlBotLevel.selectedIndex].value, populateRangesC);
					break;
					
				case "Brand":
					Public.ProductFinder.GetFurnitureTypesByRange(ddlBotLevel.options[ddlBotLevel.selectedIndex].value, populateFurnitureTypesBrands);
					break;
			}
			document.BottomIndex = ddlBotLevel.selectedIndex;
		}
	}
	return true;
}

populateMidLevel = function(array)
{
	var content = array.value;
	
	if (typeof ddlMidLevel == "object")
	{
		ddlMidLevel.options.length = 0;
		ddlBotLevel.options.length = 0;
		if (content != null)
		{
			ddlMidLevel.style.display = "inline";
			ddlMidLevel.options[0] = new Option("-- Select " + ddlTopLevel.options[ddlTopLevel.selectedIndex].value + " --","");
			for (var i = 0; i < content.length; i++)
			{
				ddlMidLevel.options[(i+1)] = new Option(content[i][0], content[i][1]);
			}
		}
		else
		{
			ddlMidLevel.options[0] = new Option("Please select a category to search by", "");
			ddlMidLevel.style.display = "none";
		}
	}
	
	if (document.MiddleIndex != null)
	{
		var tempMiddleIndex = document.MiddleIndex;
		ddlMidLevel.options[document.MiddleIndex].selected = true;
		document.MiddleIndex = null;
		midLevelChanged();
		document.MiddleIndex = tempMiddleIndex;
	}
}

populateChildFurnitureTypes = function(array)
{
	var content = array.value;
	
	if (typeof content == "object" && typeof ddlBotLevel == "object" && content.length > 0)
	{
		ddlBotLevel.options.length = 0;
		ddlBotLevel.options[0] = new Option("-- Select Sub-Category --","");
		for (var i = 0; i < content.length; i++)
		{
			ddlBotLevel.options[(i+1)] = new Option(content[i][0], content[i][1]);
		}
		ddlBotLevel.style.display = "inline";
	}
	else if (typeof ddlMidLevel == "object")
	{
		Public.ProductFinder.GetRangesByFurnitureType(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, populateRangesC);
		ddlBotLevel.style.display = "none";
	}
	
	
	if (document.BottomIndex != null)
	{
		var tempBottomIndex = document.BottomIndex;
		ddlBotLevel.options[document.BottomIndex].selected = true;
		document.BottomIndex = null;
		botLevelChanged();
		document.BottomIndex = tempBottomIndex;
	}
	
}

populateRangesC = function(array)
{
    heading = "Ranges";
    content = array.value;
	//alert(content);
	fillRangeArray();	
	//Public.ProductFinder.GetImageCategory((ddlBotLevel.options.length > 0 ? ddlBotLevel.options[ddlBotLevel.selectedIndex].value : ddlMidLevel.options[ddlMidLevel.selectedIndex].value), setImage);
	
}

fillRangeArray = function()
{
    
    populateBC(1);  
}

populateRangesD = function(array)
{
    heading = "Ranges";
    content = array.value;
	fillRangeArray();	

	Public.ProductFinder.GetImageDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, setImage);

	
	if (document.BottomIndex != null)
	{
		var tempBottomIndex = document.BottomIndex;
		ddlBotLevel.options[document.BottomIndex].selected = true;
		document.BottomIndex = null;
		botLevelChanged();
		document.BottomIndex = tempBottomIndex;
	}
}

/*
populateRangesB = function(array)
{
    heading = "Ranges";
    content = array.value;

	//alert(content);

	fillRangeArray();	

	Public.ProductFinder.GetImageDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, setImage);

	
	if (document.BottomIndex != null)
	{
		var tempBottomIndex = document.BottomIndex;
		ddlBotLevel.options[document.BottomIndex].selected = true;
		document.BottomIndex = null;
		botLevelChanged();
		document.BottomIndex = tempBottomIndex;
	}
}
*/


populateRangesB = function(array)
{
    heading = "Ranges";
    content = array.value;

	//alert(content);

	fillRangeArray();	

	//alert (ddlMidLevel.options[ddlMidLevel.selectedIndex].value);

	Public.ProductFinder.GetImageDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, setImage);
    document.BottomIndex = 1;

	
/*	
    if (document.BottomIndex != null)
	{
		var tempBottomIndex = document.BottomIndex;
		ddlBotLevel.options[document.BottomIndex].selected = true;
		document.BottomIndex = null;
		botLevelChanged();
		document.BottomIndex = tempBottomIndex;
	}
*/
}



/*
populateRangesB = function(array)
{
	var content = array.value;
		//alert(content);
	if (typeof content == "object" && content.length > 0 && typeof ddlBotLevel == "object")
	{
		ddlBotLevel.options.length = 0;
		ddlBotLevel.options[0] = new Option("-- Select Range --","");
		for (var i = 0; i < content.length; i++)
		{
			ddlBotLevel.options[(i+1)] = new Option(content[i][0], content[i][1]);
		}
		
		ddlBotLevel.style.display = "inline";
	}
	
	//alert (document.BottomIndex);
	//alert (content.length);
	//alert (document.BottomIndex > content.length)

    var tempBottomIndex = 0;

    if (document.BottomIndex > content.length) 
    {
	    if (document.BottomIndex != null)
	    {	  
	        if (document.BottomIndex > content.length) 
	        {
	            document.BottomIndex = 1;
		        tempBottomIndex = 1;
		        ddlBotLevel.options[document.BottomIndex].selected = true;
		        document.BottomIndex = null;
		        botLevelChanged();
		        document.BottomIndex = tempBottomIndex;
	        }
	        else
	        {
		        tempBottomIndex = document.BottomIndex;
		        ddlBotLevel.options[document.BottomIndex].selected = true;
		        document.BottomIndex = null;
		        botLevelChanged();
		        document.BottomIndex = tempBottomIndex;
		    }
	    }
	}
	else
	{	  
        document.BottomIndex = 1;
		tempBottomIndex = 1;
		ddlBotLevel.options[document.BottomIndex].selected = true;
		document.BottomIndex = null;
		botLevelChanged();
		document.BottomIndex = tempBottomIndex;
	}
}
*/

populateFurnitureTypes = function(array)
{
    heading = "Categories";     
    content = array.value;
	fillRangeArray();
	
	//populateBC("Categories", array.value);
	Public.ProductFinder.GetImageRange(ddlBotLevel.options[ddlBotLevel.selectedIndex].value, setImage);
}


populateFurnitureTypesBrands = function(array)
{
    heading = "Ranges";     
    content = array.value;
	fillRangeArray();
	
	//populateBC("Categories", array.value);
	Public.ProductFinder.GetImageRange(ddlBotLevel.options[ddlBotLevel.selectedIndex].value, setImage);
}


//populateBC = function(heading, content)
populateBC = function(pNumber)
{   	
	if (typeof content == "object" && typeof bc == "object" && content != null && content.length > 0)
	{	
	    ////bc.innerHTML = "<div style=\"width:560px; height:45px;\"><div style=\"float:left;\"><h2>" + heading + "</h2></div><div style=\"float:right;\"><span id=\"PagingBC\"></span></div></div>";		
	    bc.innerHTML = "<div style=\"width:560px; height:45px;\"><div style=\"float:left;\"><h2>" + heading + "</h2></div><div style=\"float:right;\"></div></div>";		
		bcArray = content;
		currentPageBC = pNumber;
		generatePagingBottomContent();		
		
		switch(pNumber)
			{
				case 1:
					x = 0;
					if(content.length < 20){ y = content.length;}else {y = 20;}					
					break;
				case 2:
				    x = 21;
				    if(content.length < 40){ y = content.length;}else {y = 40;}
					break;
				case 3:
				    x = 41;
				    if(content.length < 60){ y = content.length;}else {y = 60;}
					break;
				case 4:
				    x = 61;
				    if(content.length < 80){ y = content.length;}else {y = 80;}
					break;
				case 5:
				    x = 81;
				    if(content.length < 100){ y = content.length;}else {y = 100;}
					break;
			}
		
		
	    y = content.length;
		
		for (var i = x; i < y; i++)
		{
		  var a = document.createElement("a");
		//  a.innerHTML = "<a href=\"javascript:loadProductsByRange(" + content[i][1] + ")\"><img src=\"" + content[i][2] + "\" />" + "<br />" + "<div class=\"fProductName\">" + content[i][0] + "</div>" +"</a>";
		  a.innerHTML = "<img src=\"" + content[i][2] + "\" onclick=\"javascript:jumpToAnchor();\" />"
		  a.innerHTML += "<div class=\"fProductName\">"+ content[i][0]+ "</div>";			
		 // a.href = "javascript:loadProductsByRange(" + content[i][1] + ");";
		  a.id = content[i][1];
				a.onmouseover = function()
				{
					var links = this.parentNode.getElementsByTagName("A");
					for (var i = 0; i < links.length; i++)
					{
						if (ddlTopLevel.options[ddlTopLevel.selectedIndex].value == "Designer")
						{
							if (this === links[i]) { displayImage(bcArray[i][2]); };
						}
						else
						{
							if (this === links[i]) { displayImage(bcArray[i][3]); };
						}  
					}
				}
			
			a.onmouseout = function()
			{
				resetImage();
			}
			
			a.onclick = function()
			{				
				BCLinkClicked(this.id);
				//BCLinkClicked(this.id);
			}
			
			bc.appendChild(a);
			
			if (content[i][2] != null)
			{
				AddImageToPreload(content[i][2]);
			}
		}
		
		if (document.LinkClicked != null)
		{
			BCLinkClicked(document.LinkClicked, true);
		}
		sIFRChange();
		
		
	}
}

function BCLinkClicked(objid, autoClick)
{		
	var link = document.getElementById(objid);
	var sfEls = bc.getElementsByTagName("A");
		
	if (link != null && sfEls != null)
	{
	    document.LinkClicked = objid;
		for (var i = 0; i < sfEls.length; i++)
		{			
			sfEls[i].className=link.className.replace(new RegExp(" selected\\b"), "");
			
			if (link === sfEls[i])
			{			   
				if (ddlTopLevel.options[ddlTopLevel.selectedIndex].value != "Brand")
				{
					//loadProductsByRange(bcArray[i][1]);
					loadProductsByRange(objid);
				}
				else
				{
					//loadProductsByCategory(bcArray[i][1]);
					//loadProductsByCategory(objid);
					loadProductsByRange(objid);
					
				}
				
				if (autoClick || ddlTopLevel.options[ddlTopLevel.selectedIndex].value != "Category")
					displayImage(bcArray[i][2]);
					
				setPrevImage();
			}
		}
		link.className+=" selected";
	}
}


generatePagingBottomContent = function()
{
	
	var paging = document.getElementById("PagingBC");
	pagesBC = Math.ceil(content.length/rangeLength);
	
	if (typeof paging == "object" && paging != null)
	{
		paging.innerHTML = "";
		if (currentPageBC > 2) { paging.innerHTML += "<a href=\"javascript:populateBC(1)\"><img src=\"images/icn_first.gif\" alt=\"First\" /></a>"; }
		if (currentPageBC != 1) { paging.innerHTML += "<a href=\"javascript:populateBC(" + (currentPageBC-1) + ")\"><img src=\"images/icn_prev.gif\" alt=\"Previous\" /></a>"; }
		for (var i = 1; i < pagesBC+1; i++)
		{
			paging.innerHTML += "<a href=\"javascript:populateBC(" + i + ")\"" + (currentPageBC == i ? "class=\"selected\"" : "") + ">" + i + "</a>";
			//if (i != pagesBC) { paging.innerHTML += "<span class=\"sep\">|</span>"; }
		}
		if (currentPageBC != pages) { paging.innerHTML += "<a href=\"javascript:populateBC(" + (currentPageBC + 1) + ")\"><img src=\"images/icn_next.gif\" alt=\"Next\" /></a>"; }
		if (currentPageBC < (pages-1)) { paging.innerHTML += "<a href=\"javascript:populateBC(" + pagesBC + ")\"><img src=\"images/icn_last.gif\" alt=\"Last\" /></a>"; }
	}
}


clearBC = function()
{
	bc.innerHTML = "";
}



setPrevImage = function()
{
	var images = imageLoc.getElementsByTagName("img");
	
	prevImage = images[0].src;
}

setImage = function(data)
{
	if (data.value != null)
	{
		prevImage = data.value;
	}
	else
	{
		prevImage = "images/img_notAvailMED.gif";
	}
	displayImage(data.value);
}

displayImage = function(imageSrc)
{
	if (typeof imageLoc == "object" && imageLoc != null)
	{
		if (imageSrc == null || imageSrc.length == 0) { imageSrc = "images/img_notAvailMED.gif"; }
		
		imageLoc.innerHTML = "<img src=\"" + imageSrc + "\" />";
		//imageLoc.style.display = "block";
		imageLoc.style.display = "none";
	}
	else
	{
		clearImage();
	}
}

resetImage = function()
{
	if (prevImage != null)
	{
		displayImage(prevImage);
	}
}

//clearImage = function()
//{
//	if (typeof imageLoc == "object")
//	{
//		imageLoc.innerHTML = "";
//		imageLoc.style.display = "none";
//	}
//}

loadProductsByRange = function(RangeId)
{		
	if (typeof ddlTopLevel == "object" && typeof ddlMidLevel == "object" && typeof ddlBotLevel == "object")
	{
		var choices = document.TopIndex + "," + document.MiddleIndex + "," + document.BottomIndex + "," + document.LinkClicked;
		
		switch (ddlTopLevel.options[ddlTopLevel.selectedIndex].value)
		{
			case "Category":
				Public.ProductFinder.GetProductsByCategory((ddlBotLevel.options.length > 0 ? ddlBotLevel.options[ddlBotLevel.selectedIndex].value : ddlMidLevel.options[ddlMidLevel.selectedIndex].value), RangeId, choices, processProducts);
				break;
			case "Designer":
				Public.ProductFinder.GetProductsByDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, RangeId, choices, processProducts);
				break;				
			case "Brand":
				Public.ProductFinder.GetProductsByBrand(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, RangeId, choices, processProducts);
				break;				
		}
	}
}

loadProductsByCategory = function(CategoryId)
{
	if (typeof ddlTopLevel == "object" && typeof ddlMidLevel == "object" && typeof ddlBotLevel == "object")
	{
		var choices = document.TopIndex + "," + document.MiddleIndex + "," + document.BottomIndex + "," + document.LinkClicked;
		//alert(choices);
		//var txt = document.createTextNode("In here");
		//document.body.appendChild(txt);

		//alert('CategoryId = ' + CategoryId + '\n' + 'ddlBotLevel = ' + ddlBotLevel.options[ddlBotLevel.selectedIndex].value + '\n' + 'choices = ' + choices);

		Public.ProductFinder.GetProductsByDesigner(ddlMidLevel.options[ddlMidLevel.selectedIndex].value, RangeId, choices, processProducts);
	}
}

loadProductsByBrand = function(BrandId)
{
	if (typeof ddlTopLevel == "object" && typeof ddlMidLevel == "object")
	{
		var choices = document.TopIndex + "," + document.MiddleIndex + "," + document.LinkClicked;
		//alert(choices);
		//var txt = document.createTextNode("In here");
		//document.body.appendChild(txt);

		//alert('BrandId = ' + BrandId + '\n' + 'ddlBotLevel = ' + ddlBotLevel.options[ddlBotLevel.selectedIndex].value + '\n' + 'choices = ' + choices);

		Public.ProductFinder.GetProductsByCategory(BrandId, ddlMidLevel.options[ddlMidLevel.selectedIndex].value, choices, processProducts);
	}
}

populateProducts = function(PageNumber)
{
	//var template = "<div class=\"Product\" onclick=\"window.location='[URL]'\"><div class=\"pImage\"><img src=\"[image]\" height=\"83\" width=\"83\" /></div><h3>[name]</h3><p>[description]</p></div>";
	//alert(sResults);
	
	var template = "<div class=\"searchResultsContainer\"><div class=\"Product\" onclick=\"window.location='[URL]'\"><div class=\"pImage\"><img src=\"[image]\" height=\"83\" width=\"83\" /></div><h3>[name]</h3></div>";
	var template2 = "<div class=\"ProductRight\" onclick=\"window.location='[URL]'\"><div class=\"pImage\"><img src=\"[image]\" height=\"83\" width=\"83\" /></div><h3>[name]</h3></div></div>";
	//alert(PageNumber);
	//alert(template);
	if (typeof sResults == "object" && typeof sr == "object" && sResults.length > 0)
	{
		////sr.innerHTML = "<h2>Results</h2><span id=\"displayText\"></span><span id=\"pagingText\"></span>"
		sr.innerHTML = "<h2>Results</h2>"
		////sr.innerHTML += "<p>&nbsp;&nbsp;</p>"		
		//sr.innerHTML += "<div id=\"searchResultsContainer\">"
		//sr.innerHTML += "<table width=\"100%\" cellspacing=\"2\" border=\"1\">"
		sr.innerHTML += "<div style=\"height:14px;\"></div>"

		for (var i = 0; i < sResults[(PageNumber-1)].length; i++)
		{
		    if ((i % 2) == 0)
		    {
		        //alert('if\n\n' + i + '\n\n' + i % 2);
			    sr.innerHTML += template2.replace(/\[id\]/g, sResults[(PageNumber-1)][i][0]).replace(/\[name\]/g, sResults[(PageNumber-1)][i][1]).replace(/\[description\]/g, sResults[(PageNumber-1)][i][2]).replace(/\[URL\]/g, sResults[(PageNumber-1)][i][3]).replace(/\[image\]/g, sResults[(PageNumber-1)][i][4]);
		        //sr.innerHTML += "</td></tr>"
			}
			else
			{
		        //alert('else\n\n' + i + '\n\n' + i % 2);
		        //sr.innerHTML += "<tr><td>"
			    sr.innerHTML += template.replace(/\[id\]/g, sResults[(PageNumber-1)][i][0]).replace(/\[name\]/g, sResults[(PageNumber-1)][i][1]).replace(/\[description\]/g, sResults[(PageNumber-1)][i][2]).replace(/\[URL\]/g, sResults[(PageNumber-1)][i][3]).replace(/\[image\]/g, sResults[(PageNumber-1)][i][4]);
		        //sr.innerHTML += "</td>"
			}
		}
		//sr.innerHTML += "</table>"
		//sr.innerHTML += "</ div>"
		
		currentPage = PageNumber;
		generateDisplaying();
		generatePaging();
		hoverResults();
		sIFRChange();
	}
	else
	{
		sr.innerHTML = "<h2>No Results found</h2>"
	}
}

processProducts = function(array)
{
	//alert("pp");
	
	results = array.value;
	
	fillProductArray();
}

fillProductArray = function()
{
	//alert("fpa");

	if (typeof results == "object" && results.length > 0)
	{
		sResults = new Array(results.length);
		pages = Math.ceil(results.length/pageLength);
		
		for (var i = 0; i < pages; i++)
		{
			sResults[i] = new Array()
			for (var k = 0, j = i*pageLength; (j < ((i+1)*pageLength)) && j < results.length; j++,k++)
			{
				sResults[i][k] = results[j];
				if (i > 0)
				{
					AddImageToPreload(results[j][4]);
				}
			}
		}
		
		populateProducts(1);
	}
}

generateDisplaying = function()
{
	var displaying = document.getElementById("displayText");
	
	if (typeof displaying == "object" && displaying != null)
	{
		displaying.innerHTML = "Displaying " + (((currentPage-1)*pageLength)+1) + " to " + (currentPage*pageLength > results.length ? results.length : currentPage*pageLength) + " of " + results.length;
	}
}

generatePaging = function()
{
	var paging = document.getElementById("pagingText");
	if (typeof paging == "object" && paging != null)
	{
		paging.innerHTML = "";
		if (currentPage > 2) { paging.innerHTML += "<a href=\"javascript:populateProducts(1)\"><img src=\"images/icn_first.gif\" alt=\"First\" /></a>"; }
		if (currentPage != 1) { paging.innerHTML += "<a href=\"javascript:populateProducts(" + (currentPage-1) + ")\"><img src=\"images/icn_prev.gif\" alt=\"Previous\" /></a>"; }
		for (var i = 1; i < pages+1; i++)
		{
			paging.innerHTML += "<a href=\"javascript:populateProducts(" + i + ")\"" + (currentPage == i ? "class=\"selected\"" : "") + ">" + i + "</a>";
			if (i != pages) { paging.innerHTML += "<span class=\"sep\">|</span>"; }
		}
		if (currentPage != pages) { paging.innerHTML += "<a href=\"javascript:populateProducts(" + (currentPage + 1) + ")\"><img src=\"images/icn_next.gif\" alt=\"Next\" /></a>"; }
		if (currentPage < (pages-1)) { paging.innerHTML += "<a href=\"javascript:populateProducts(" + pages + ")\"><img src=\"images/icn_last.gif\" alt=\"Last\" /></a>"; }
	}
}

clearSR = function()
{
	sr.innerHTML = "";
}

hoverResults = function()
{
	var sfEls = document.getElementById("searchResults").getElementsByTagName("DIV");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
		this.className+=" sfhover";
	   }
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
}

sIFRChange = function()
{
	if (typeof sIFR == "function")
	{
		sIFR.replaceElement("#pfLeft #bottomContent h2, #searchResults h2", named({sFlashSrc: "./flash/h.swf", sColor: "#A0A0A0", sBgColor: "#3D3D3D", sCase: "upper", sWmode: "opaque"}));
	}
}


