window.dhtmlHistory.create();

Function.prototype.bind   =   function() {
	var   __method   =   this;   
    var   arg   =   arguments;   
    return   function() {
		__method.apply(window, arg);   
    }   
}



var historyChange = function(newLocation, historyData) {
	if (newLocation == "") 
	{
        newLocation = "home";
    }
	
	if (newLocation.indexOf("_list") == -1 && newLocation.indexOf("_detail") == -1)
	{
		change2page(historyData);
	}
	else
	{
		if(newLocation.indexOf("product_list") >= 0)
	    {
			newLocation = newLocation.replace(/product_list\:/, "");
			if(historyData == null)
			{
				historyData = 1;
			}
			change2productlist(newLocation, historyData);
	    }
	
	    if(newLocation.indexOf("product_detail") >= 0)
	    {
			newLocation = newLocation.replace(/product_detail\:/, "");
			if(historyData == null)
			{
				change2productdetail(newLocation, null, null);
			}
			else
			{
				change2productdetail(newLocation, historyData.value1, historyData.value2);
			}
	    }
	
	    if(newLocation.indexOf("information_detail") >= 0)
	    {
		   newLocation = newLocation.replace(/information_detail\:/, "");
		   change2news(newLocation);
	    }
	}
}

window.onload = function() {		
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(historyChange);

	var mainposition = window.location.href.indexOf("#");
	var subposition  = window.location.href.indexOf("detail");

	if( mainposition == -1)
	{
		if (dhtmlHistory.isFirstLoad()) 
		{
			dhtmlHistory.add("home",1);
    		} 	
	}
	else
	{
		if(subposition == -1)
		{
			switch(window.location.href.substr(mainposition + 1))
			{
				case "home":
				break;

				case "aboutus":
				change2page(2);
				break;

				case "information":
				change2page(4);
				break;

				case "contact":
				change2page(5);
				break;
			}
			var temp = window.location.href.indexOf("product_list");
			if( temp > 0)
			{
				change2productlist(window.location.href.substr(temp + 13), 1);
			}
		}
		else
		{
			switch(window.location.href.substring((mainposition + 1), subposition + 6))
			{
				case "product_detail":
				change2productdetail(window.location.href.substr(subposition + 7), null, null);
				break;

				case "information_detail":
				change2news(window.location.href.substr(subposition + 7));
				break;
			}
		}
	}
	

	
	var Demo01a = document.getElementById("Demo01a");
		var Demo01b = document.getElementById("Demo01b");
		Demo01b.innerHTML += Demo01b.innerHTML;
		function move01(){
			Demo01a.scrollLeft = Demo01a.scrollLeft==(Demo01b.offsetWidth/2)?Demo01a.scrollLeft=1:Demo01a.scrollLeft+1;
		}
		setInterval(move01,20);
		
    var Demo02a = document.getElementById("Demo02a");
		var Demo02b = document.getElementById("Demo02b");
		Demo02b.innerHTML += Demo02b.innerHTML;
		function move02(){
			Demo02a.scrollLeft = Demo02a.scrollLeft==(Demo02b.offsetWidth/2)?Demo02a.scrollLeft=1:Demo02a.scrollLeft+1;
		}
		setInterval(move02,20);
};


function insertFlash(elm, url, w, h) {
if (!document.getElementById(elm)) return;
var str = '';

if(w == 0)
{
str += '<object width=100% height=100% classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">';
}
else
{
str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">';
}
str += '<param name="movie" value="'+ url +'">';
str += '<param name="quality" value="high">';
str += '<param name="wmode" value="opaque">';	

if( w ==0)
{
str += '<embed wmode="opaque" width=100% height=100% src="'+ url +'" quality="high" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
}
else
{
str += '<embed wmode="opaque" width="'+ w +'" height="'+ h +'" src="'+ url +'" quality="high" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
} 
str += '</object>';
document.getElementById(elm).innerHTML = str;
}

function insertTransparentFlash(elm, url, w, h) {
if (!document.getElementById(elm)) return;
var str = '';

if(w == 0)
{
str += '<object width=100% height=100% classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">';
}
else
{
str += '<object width="'+ w +'" height="'+ h +'" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0">';
}
str += '<param name="movie" value="'+ url +'">';
str += '<param name="quality" value="high">';
str += '<param name="wmode" value="transparent">';	

if( w ==0)
{
str += '<embed wmode="transparent" width=100% height=100% src="'+ url +'" quality="high" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
}
else
{
str += '<embed wmode="transparent" width="'+ w +'" height="'+ h +'" src="'+ url +'" quality="high" type="application/x-shockwave-flash" plugspace="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed>';
} 
str += '</object>';
document.getElementById(elm).innerHTML = str;
}

function change2page(pageid) {
	document.getElementById("prevpagelink").style.display = "none";
	document.getElementById("nextpagelink").style.display = "none";
	
	document.getElementById("infodtailpage").style.display       = "none";
	document.getElementById("productdetailpage").style.display   = "none";
	
	switch (pageid)
	{
		case 1:
		  document.getElementById("homepage").style.display    = "block";
		  document.getElementById("aboutpage").style.display   = "none";
		  document.getElementById("productpage").style.display = "none";
		  document.getElementById("infopage").style.display    = "none";
		  document.getElementById("contactpage").style.display = "none";
		  dhtmlHistory.add("home",1);
		  break;
		  
		case 2:
		  document.getElementById("homepage").style.display    = "none";
		  document.getElementById("aboutpage").style.display   = "block";
		  document.getElementById("productpage").style.display = "none";
		  document.getElementById("infopage").style.display    = "none";
		  document.getElementById("contactpage").style.display = "none";
		  dhtmlHistory.add("aboutus",2);
		  break;
		  
		case 3:
		  document.getElementById("homepage").style.display    = "none";
		  document.getElementById("aboutpage").style.display   = "none";
		  document.getElementById("productpage").style.display = "block";
		  document.getElementById("infopage").style.display    = "none";
		  document.getElementById("contactpage").style.display = "none";		  
		  break;
		  
		case 4:
		  document.getElementById("homepage").style.display    = "none";
		  document.getElementById("aboutpage").style.display   = "none";
		  document.getElementById("productpage").style.display = "none";
		  document.getElementById("infopage").style.display    = "block";
		  document.getElementById("contactpage").style.display = "none";
		  dhtmlHistory.add("information",4);
		  break;
		
		case 5:
		  document.getElementById("homepage").style.display    = "none";
		  document.getElementById("aboutpage").style.display   = "none";
		  document.getElementById("productpage").style.display = "none";
		  document.getElementById("infopage").style.display    = "none";
		  document.getElementById("contactpage").style.display = "block";
		  dhtmlHistory.add("contact",5);
		  break;
	}
}

function change2productlist(sortid, limit) {
   change2page(3);
   
   while (document.getElementById("listul").hasChildNodes())
   {
     var linode = document.getElementById("listul").firstChild;
	 document.getElementById("listul").removeChild(linode);
   }
	
   if(!createRequest())
   {			
	 return;
   }
  
   SendProductListRequest (sortid, limit);
   
   dhtmlHistory.add("product_list:" + sortid, limit);
}

function change2productdetail(code, name, description) {	

	if(name == null)
	{
		if(!createRequest())
  		{			
	 		return;
   		}
  
   		SendProductDetailRequest (code);
	}
	else
	{
		document.getElementById("prevpagelink").style.display = "none";
		document.getElementById("nextpagelink").style.display = "none";
	
		while (document.getElementById("productinfo").hasChildNodes())
		{
	     		var infonode = document.getElementById("productinfo").firstChild;
	      		document.getElementById("productinfo").removeChild(infonode);
		}
		if(description == "null")
		{
			description = "";
		}
	
		document.getElementById("productinfo").innerHTML   = "<span class='leftbarcontentstyle'>" + code + "<br />" + name + "<br />STONEWEAR<br />Oven, Micro-wsve, Dishwasher<br />" + description;
	
		SetProductImage(code);
	
		document.getElementById("homepage").style.display    = "none";
		document.getElementById("aboutpage").style.display   = "none";
		document.getElementById("productpage").style.display = "none";
		document.getElementById("infopage").style.display    = "none";
		document.getElementById("contactpage").style.display = "none";
		document.getElementById("productdetailpage").style.display = "block";
	
		var complexObject = new Object();
		complexObject.value1 = name;
		complexObject.value2 = description;
	
		dhtmlHistory.add("product_detail:" + code, complexObject);
	}	
}

function SetProductImage (code) {
    var imgDiv = document.getElementById("productimage");
	
	while (imgDiv.hasChildNodes())
	{
	  var imgnode = imgDiv.firstChild;
	  imgDiv.removeChild(imgnode);
	}
	
	var itemimage   = document.createElement("img");	
    itemimage.src    = "product/" + code + ".jpg";
	
	itemimage.onerror = function () {
		alert("error!");
	   return;
	}
	
	if (itemimage.complete) 
	{
	  imgDiv.appendChild(itemimage);
	  
	  return;
    }
	
	itemimage.onload = function () { 
	  while (imgDiv.hasChildNodes())
	  {
	    var imgnode = imgDiv.firstChild;
	    imgDiv.removeChild(imgnode);
	  }	
	  
	  imgDiv.appendChild(itemimage);
	}
}


function change2news(newscode) {
	document.getElementById("homepage").style.display    = "none";
	document.getElementById("aboutpage").style.display   = "none";
	document.getElementById("productpage").style.display = "none";
	document.getElementById("infopage").style.display    = "none";
	document.getElementById("contactpage").style.display = "none";
	
	document.getElementById("infodtailcontent").src         = "news/" + newscode + ".html";
	document.getElementById("infodtailpage").style.display  = "block";
	
	dhtmlHistory.add("information_detail:" + newscode, 0);
}

function createRequest() {
    try 
	{
      request = new XMLHttpRequest();
    } 
	catch (trymicrosoft) 
	{
      try 
	  {
        request = new ActiveXObject("Msxml2.XMLHTTP");
      } 
	  catch (othermicrosoft) 
	  {
        try 
		{
          request = new ActiveXObject("Microsoft.XMLHTTP");
        } 
		catch (failed) 
		{
          request = false;
        }
      }
    }
    if (!request)
	  return false;
	else	  
	  return true; 
}

function SendProductListRequest(isort, limit) {	    
    var url = "productprocess.php?sort=" + escape(isort) + "&limit=" + limit;
    request.open("GET", url, true);
    request.onreadystatechange = function(){updateProductListPage(isort, limit);};
    request.send(null);
}

function updateProductListPage(isort, limit) {
    var listulid = document.getElementById("listul");	
	var listnum = 0;	
	var totalnum;	
	
    if (request.readyState == 4) 
	{
	  if (request.status == 200) 
	  { 
	    var xmlDoc     = request.responseXML;
        productElements = xmlDoc.getElementsByTagName("product");
		totalnum        = parseInt(xmlDoc.getElementsByTagName("total")[0].firstChild.nodeValue);
		
		while (listulid.hasChildNodes())
	    {
	      var linode = listulid.firstChild;
	      listulid.removeChild(linode);
	    }
		
		var listli;
		var listul;
		
		var row = 0;
		
		listli = document.createElement("li");
		listul = document.createElement("ul");		
		listulid.appendChild(listli);
		listli.appendChild(listul);
		
		for (var x=0; x<productElements.length; x++) 
		{
		    if ( row == 3)
			{
			  listli = document.createElement("li");
			  listul = document.createElement("ul");
			  listulid.appendChild(listli);
			  listli.appendChild(listul);
			  row = 0;
			}
			
			productli = document.createElement("li");
			productul = document.createElement("ul");
			
			listul.appendChild(productli);	
			productli.appendChild(productul);
			
			codeli  = document.createElement("li");
			imageli = document.createElement("li");
			
			productul.appendChild(imageli);
			productul.appendChild(codeli);	
			
			var productcode         = productElements[x].childNodes[0].firstChild.nodeValue;
			var productname         = productElements[x].childNodes[1].firstChild.nodeValue;			
			var productdescription  = productElements[x].childNodes[2].firstChild.nodeValue;
			var productcolor        = productElements[x].childNodes[3].firstChild.nodeValue;
			
			linka           = document.createElement("a");
			linka.href      = "javascript:void(0);";			
			linka.onclick   = change2productdetail.bind(productcode, productname, productdescription);
			linka.className = "productlinkstyle";
			linka.innerHTML = productname;
			
			codeli.appendChild(linka);
			
			imagea          = document.createElement("a");
			imagea.href     = "javascript:void(0);";
			imagea.onclick  = change2productdetail.bind(productcode, productname, productdescription);
			
			imageli.appendChild(imagea);
			
			image           = document.createElement("img");
			image.className = "productimagelinkstyle";
			image.src       = "product/" + productcode + "_s.jpg";
							
			imagea.appendChild(image);
			
			row++;		
		}		
		
		if(limit > 1)
		{
			document.getElementById("prevpageimage").onclick       = function() {change2productlist(isort, limit - 9);};
			document.getElementById("prevpagelink").style.display = "block"; 
		}
		else
		{
			document.getElementById("prevpagelink").style.display = "none";
		}
				
				
		if(totalnum > (limit + 9))
		{
			document.getElementById("nextpageimage").onclick       = function() {change2productlist(isort, limit + 9);};
			document.getElementById("nextpagelink").style.display = "block";
		}	
		else
		{
			document.getElementById("nextpagelink").style.display = "none";
		}	
			
      }
	  	
    }
}

function SendProductDetailRequest (code)
{
	var url = "productdetail.php?code=" + escape(code);
   	request.open("GET", url, true);
    	request.onreadystatechange = function(){updateProductDetailPage();};
    	request.send(null);
}

function updateProductDetailPage ()
{
	if (request.readyState == 4) 
	{
		if (request.status == 200) 
		{ 
			var xmlDoc     = request.responseXML;
			productElements = xmlDoc.getElementsByTagName("product");		
			
			var productcode         = productElements[0].childNodes[0].firstChild.nodeValue;
			var productname         = productElements[0].childNodes[1].firstChild.nodeValue;			
			var productdescription  = productElements[0].childNodes[2].firstChild.nodeValue;

			change2productdetail(productcode, productname, productdescription);		
      		}	  	
   	}
}
