var http;
var url = "http://www.pricecutteronline.com/design_center/party/getItems.php?id=";
var itemNum;
var itemList = new Array();
var picBoxContents = new Array();
var depHeader = new Array();
var itemImage = new Array();
var preItemImage = new Array();
var defaultId = new Array();
var arrItemName = new Array();
var arrItemServes = new Array();
var arrItemSize = new Array();
var arrItemPrice = new Array();
var arrItemDesc = new Array();
var listBox;
var picDiv;
var itemInfo;
var itemName;
var docItemName;
var docItemSizes;
var depCount = 0;
var depNum = 1;
var numItems = 0;

window.onload=function(){
	listBox = document.getElementById('party_list');
	//picDiv = document.getElementById('itemPic');
	//itemInfo = document.getElementById('itemInfo');
	//docItemName = document.getElementById('itemName');
	docItemSizes = document.getElementById('itemSizes');
	cake=3;
	deli=2;
	//beer=1;
	
	//get department link objects
	cakeLink=document.getElementById('PPG_cakes');
	deliLink=document.getElementById('PPG_deli');
	beerLink=document.getElementById('PPG_spirits');
	
	cakeLink.onclick=function(){
		changePP('party_planner',this);
		showList(cake);
		showPicBox(defaultId[cake]);
		return false;};
	cakeLink.onmouseover=function(){setMouseOver(this);};
	cakeLink.onmouseout=function(){setMouseOut(this);};
	deliLink.onclick=function(){
		changePP('party_planner',this);
		showList(deli);
		showPicBox(defaultId[deli]);
		//document.getElementById('party_planner').style.backgroundImage="url(../images/wedding_planner_bground.jpg)";
		return false;};
	deliLink.onmouseover=function(){setMouseOver(this);};
	deliLink.onmouseout=function(){setMouseOut(this);};
	
	beerLink.onclick=function(){
		window.open("spirit_list.pdf","spiritwindow","");
		//showList(beer);
		//showPicBox(defaultId[beer]);
		return false;};
	
	
	beerLink.onmouseover=function(){setMouseOver(this);};
	beerLink.onmouseout=function(){setMouseOut(this);};
	
	if(http=getXMLHTTP()){
		updateList();			
	}else{
		//alert('WAAAH not compatible.');
	}

}

function setMouseOver(obj){
	obj.style.cursor="pointer";
	obj.style.backgroundPosition="-82px 0"; 
	obj.style.backgroundRepeat="no-repeat";
}

function setMouseOut(obj){
	obj.style.backgroundPosition="";
}

function getXMLHTTP(){
	try{
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(ex){
		//either this is not IE, or it is a version of IE which does not support XMLHTTP
		var notIECompatibleXMLHTTP=true;
	}
	if(notIECompatibleXMLHTTP==true){
		try{
			request_o = new XMLHttpRequest();
		}catch(ex){
			//we can't use AJAX because this browser is not compatible.
			request_o = false;
		}
	}
	return request_o;
}

function handleHttpResponse(){
	if ((http.readyState == 4)&&(http.status == 200)) {
		var response = http.responseXML;
		docRoot = response.getElementsByTagName('party')[0];
		for (i=0;i<docRoot.childNodes.length;i++){			
			elChild = docRoot.childNodes[i];
			if (elChild.nodeName=="department"){
				depCount++;
				itemCount=0;						
				itemList[depCount] = "";
				depId = elChild.getAttribute('id');
				for (j=0;j<elChild.childNodes.length;j++){
					depNode = elChild.childNodes[j];
					depNodeName = depNode.nodeName;
					switch (depNodeName){
						case "depname":
							depName = depNode.firstChild.data;
							break;
						case "header":
							depHeader[depCount] = depNode.firstChild.data;				
							break;
						case "item":
							itemCount++;							
							itemId = depNode.getAttribute('id');		
							if (itemCount==1) defaultId[depId]=itemId;
							arrItemServes[itemId] = new Array();
							arrItemSize[itemId] = new Array();
							arrItemPrice[itemId] = new Array();
							for (k=0;k<depNode.childNodes.length;k++){						
								itemNode = depNode.childNodes[k];
								itemNodeName = itemNode.nodeName;
								switch (itemNodeName){
									case "itemname":		
										itemName = itemNode.firstChild.data;
										arrItemName[itemId] = itemName;
										break;
									case "description":
										if (itemNode.childNodes.length>0){
											itemDescription = itemNode.firstChild.data;
											arrItemDesc[itemId] = itemDescription;
										}
										break;
									case "imago":
										if (itemNode.childNodes.length>0){
											itemImage[itemId] = itemNode.firstChild.data;
											preItemImage[itemId] = new Image();
											preItemImage[itemId].src = itemNode.firstChild.data;											
										}
										break;
									case "serving":
										servingId = itemNode.getAttribute('id');
										for (l=0;l<itemNode.childNodes.length;l++){
											servingNode = itemNode.childNodes[l];
											servingNodeName = servingNode.nodeName;
											switch (servingNodeName){
												case "size":
													itemSize = servingNode.firstChild.data;	
													arrItemSize[itemId].push(itemSize);
													break;
												case "serves":
													itemServes = servingNode.firstChild.data;
													arrItemServes[itemId].push(itemServes);
													break;
												case "price":
													itemPrice = servingNode.firstChild.data;   
													arrItemPrice[itemId].push(itemPrice);
													break;
												default :
													//alert();
											}
											
										}
										createList(depCount,itemCount,itemName,itemId,itemSize,itemServes,itemPrice);
										break;
									default:
										//alert();
								}
								
							}								
							break;
						default : 
							//alert();
					}
				}
			}
		
		}
		
		showDefault();
		
		//alert('XML loaded');
  	}
}

function showDefault() {
	if (defaultPP!=='none') {
		showList(defaultPP);	
		showPicBox(defaultId[defaultPP]);
	}
}

function createList(depNumTemp,itemNumTemp,itemName,itemId,itemSize,itemServes,itemPrice){
	
	if (itemNum!=itemNumTemp){
		itemNum=itemNumTemp;
		
		if (depNumTemp!==depNum) {
			depNum = depNumTemp;			
		}else{
			itemList[depNumTemp] += "<tr><td style='margin:0;padding:0px 5px;' colspan=\"3\"><hr></td></tr>";
		}
		
		itemList[depNumTemp] += "<tr><td valign=\"top\" class='party-cell party-cell-upper' style='margin:0;padding:0px 5px;' onclick=\"changePicBox(" + itemId + ",this); ";
		if (itemNum>11) {
			itemList[depNumTemp] += "document.location='#anchor';";
		}
		itemList[depNumTemp] += "\" onmouseover=\"this.style.color='#000000';this.style.cursor='pointer';\" onmouseout=\"this.style.color='#000000'\"><strong><div class='dc_item_name'>" + itemName + "</div></strong></td>";
		itemList[depNumTemp] += "<td valign=\"top\" style='margin:0;padding:0;' class='party-cell party-cell-upper'>" + itemSize + " serves " + itemServes + "</td><td valign=\"top\" style='margin:0;padding:0;' class='party-cell party-cell-upper'>" + itemPrice + "</td></tr>";
		numItems++;
	}else{
		itemList[depNumTemp] += "<tr><td style='margin:0;padding:0;' class='party-cell'></td>";
		itemList[depNumTemp] += "<td valign=\"top\" style='margin:0;padding:0;' class='party-cell'>" + itemSize + " serves " + itemServes + "</td><td style='margin:0;padding:0;' class='party-cell'>" + itemPrice + "</td></tr>";
		
	}
	
	
	
}

function changePicBox(id,obj) {
	showPicBox(id);
	//obj.style.color="#ff0000";
}

function showList(id){
	//listHeader = "<div class='depHeader'>" + depHeader[id] + "</div>";
	listHeader = '<table width="427" cellspacing="0" cellpadding="0" border="0" bgcolor="#FFFFFF" style="border:1px #4c4c4c solid; margin-top:4px; margin-bottom:-10px; padding:10px;">';
	listFooter = "<tr><td colspan='3'><div style='height:5px;'></div></td></tr></table>";
	listBox.innerHTML=listHeader + itemList[id] + listFooter;
	
}

function showPicBox(id,obj){
	htm = "";
	htm = '<table width="292" cellspacing="10" cellpadding="0" border="0" bgcolor="#FFFFFF" style="float:right; border:1px #4c4c4c solid; margin:4px 0px 0px 6px;">';	
	htm += '<tr><td colspan="2" align="center" class="dc_price3" style="line-height:1em;">';
	htm += arrItemName[id];
	htm += '</td></tr><tr><td colspan="2" align="center"><img src="http://www.pricecutteronline.com/design_center/party/';
	htm += itemImage[id];
	htm += '" /></td></tr><tr><td colspan-"2" align="center">';
	
	for (i=0;i<arrItemSize[id].length;i++){
		htm += '<div style="float:left; width:130px; line-height:1.8em; font-size:14px;"><strong>' + arrItemSize[id][i] + '</strong><br /><span class="dc_price">' + arrItemPrice[id][i] + '</span><br />Serves ' + arrItemServes[id][i] + '<br /></div>';			 
	}
	
	htm += '</td></tr><tr><td colspan="2">';
	htm += arrItemDesc[id];
	htm += '</td></tr></table>';
	
	/*
	docItemName.innerHTML = arrItemName[id];
	picDiv.src = itemImage[id];
	
	htm += "</td></tr><tr><td align='left' style='padding-left:10px'><div style='width:250px; font-weight:bold;'>" + arrItemDesc[id] + "</div></p>";
	htm += "</td></tr></table>";
	*/
	
	docItemSizes.innerHTML = htm;

}

function updateList(id){
	http.open("GET", url + id, true);
	//http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  	http.onreadystatechange = handleHttpResponse;
  	http.send(null);
}
