﻿// JavaScript Document
function whichElement(event)
{
//	alert(event.target);
	var targ;
	if (typeof event == "undefined")
	  {
	  var event=window.event;
	  }
	if (event.target)
	  {
	  targ=event.target;
	  }
	else if (event.srcElement)
	  {
	  targ=event.srcElement;
	  }
	if (targ.nodeType==3) // defeat Safari bug
	  {
	  targ = targ.parentNode;
	  }
	var tname;
	tname=targ.tagName;
//	alert("You clicked on a " + tname + " element.");
	tvalue=targ.childNodes;
	var len=tvalue.length;
	for(var i=0;i<len;i++)
	{
	//	alert("tvalue["+i+"] Value is " + tvalue[i].nodeValue);
	//	alert("tvalue["+i+"] Value is " + typeof tvalue[i].nodeValue);
		if(typeof tvalue[i].nodeValue=="string")
		{
			var value = tvalue[i].nodeValue;
			var c_name = "model";
			var expiredays = 365;
			var exdate=new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString())+";path=/";	
			break;
		}
	}
}

function catalogString()
{
	var changeValue = getCookie("model");
	var model = document.getElementById(changeValue);
	var modelValue = model.childNodes[0].nodeValue;
	var parent = model.parentNode;
	var parentId = escape(parent.getAttribute("id"));
	parentId = unescape(parentId);
	catalogReplaceText(parentId.toUpperCase() ,"productType");
	catalogReplaceText(changeValue ,"titleModel");
	$('productimage').style.height=398+"px";
	
	var hash=changeValue.indexOf(" ");
	var productName=changeValue;
	var imagePath = unescape("images/product/"+parentId+"/"+productName+".png")
	$('productimage').src=imagePath;
	
//	$('productimage').setAttribute("alt","抱歉!圖片或許不存在,現正更新中");
//	$('productimage').setAttribute("title",changeValue);
	
	$('productimageA').href=imagePath
	$('productimageA').setAttribute("caption",productName)
	
	var a = catalog(parentId, modelValue);
	catalogReplaceText(a[0].toLowerCase() ,"productInfo");
}
function catalogReplaceText(text,id)
{
	var newText = document.createTextNode(text);
	var idNode = document.getElementById(id);
//	alert(idNode);
	var idNodeText = idNode.childNodes[0];
//	alert(idNodeText.nodeValue);
//	alert(text.length);
	if(text.length>0)
	{
		idNode.replaceChild( newText , idNodeText);
	}
}
function modelInput()
{
	var value=whichElement(event);
//	alert(value);
	var c_name = "model";
	var expiredays = 365;
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString())+";path=/";
}

function getCookie(searchName)
{		
	var cookies = document.cookie.split(";");
//	alert(cookies);
	var getCookieName = " ";
	var getCookieValue = " ";
	
	for( var i = 0; i < cookies.length; i++)
	{
		var cookieCrumbs = cookies[i].split("=");
		getCookieName = cookieCrumbs[0];
		getCookieValue = cookieCrumbs[1];
		
		if(getCookieName == searchName)
		{
			getCookieValue = unescape(getCookieValue);
//			alert(getCookieValue);
			return getCookieValue;
		}
	}
	return false;
}
function catalog(type,model)
{
//	alert(type);
//string
var product=['a series','animals','bookend','jewelry box','photo frame','statue','tissue box','tray'];
product['a series']=['P8173 SET','P8349 SET'];
product['a series']['P8173 SET']=[''];
product['a series']['P8349 SET']=[''];

product['bookend']=['BE0131','BE0137','BO0199','BO200','BO0165'];
product['bookend']['BE0131']=['2(17X14X23)CM'];
product['bookend']['BE0137']=['2(20X12.5X19.5)CM'];
product['bookend']['BO0199']=['2(10X10X29.5 )CM'];
product['bookend']['BO200']=['2(11X10X31)CM'];
product['bookend']['BO0165']=['2(16X12X33)CM'];

product['jewelry box']=['JW144','JW110','JW085','JW148','JW145'];
product['jewelry box']['JW144']=['31X20X23CM'];
product['jewelry box']['JW110']=['9X14.5X11CM'];
product['jewelry box']['JW085']=['13X13X9.5CM'];
product['jewelry box']['JW148']=['20.5X14X20CM'];
product['jewelry box']['JW145']=['33X25.5X17CM'];

product['photo frame']=['P7953-55','P8408-46','P8428-46','P8576-46','P8687-46'];
product['photo frame']['P7953-55']=['21X19X4CM'];
product['photo frame']['P8408-46']=['18.5x3.5x23cm'];
product['photo frame']['P8428-46']=['26x15x3cm'];
product['photo frame']['P8576-46']=['25x15.5x2.5cm'];
product['photo frame']['P8687-46']=['20.2x17.9x2.0cm'];

product['statue']=['BO328','BO394','BO302','BO353','AB08036'];
product['statue']['BO328']=['33X21.5X47CM'];
product['statue']['BO394']=['24X15X54CM'];
product['statue']['BO302']=['28X18X43CM'];
product['statue']['BO353']=['14X12.5X23.5CM'];
product['statue']['AB08036']=['22X18X34CM'];

product['tissue box']=['CS565','CS637','CS638','CS639'];
product['tissue box']['CS565']=['21.5X16X10.5CM'];
product['tissue box']['CS637']=['29X17.5X10CM'];
product['tissue box']['CS638']=['29X18X9.5CM'];
product['tissue box']['CS639']=['29X18X9CM'];

product['tray']=['CS485-1','CS437-1','CS619','CS427','CS495'];
product['tray']['CS485-1']=['33X25X14CM']
product['tray']['CS437-1']=['23X23X24CM']
product['tray']['CS619']=['27.5 x 23.5 x 18CM']
product['tray']['CS427']=['32X18X16.5CM']
product['tray']['CS495']=['15.5X15.5X29CM']

	if(model.length)
		return product[type][model];
	else
		return product[type];
}

/*
function modelInput(inputValue)
{
	var cookieName = "model";
	alert("cookieName = " + typeof cookieName);
	var cookieValue = inputValue;
	alert("cookieValue = " + typeof cookieValue);
	cookieValue = escape(cookieValue);
	alert("cookieValue = " + cookieValue);
	var theCookie = cookieName+"=";
	theCookie=theCookie+cookieValue;
	alert("Set Cookies = " + theCookie);
	document['cookie = theCookie;
	alert(document['cookie)
}*/
