//--------------------------------- gallery 1.10
var galleryReq;
var slideshow_loaded=false;
var currentPID=0;
var currentPIDloaded=false;
function onGReqError(){
	alert("ERROR");
}
function loadSlideShow(){
	//try{
	galleryReq=new mAJAXRequest();
	//}catch(err){
		//document.getElementById("jsclassess").src="scripts/classes.js";
	//}
	//---------------------------------------------------------[create gallery container
	var mainBody=document.getElementsByTagName("body").item(0);
	var gContainerDIV=document.createElement("div");
	gContainerDIV.setAttribute("id","slideshow");
	gContainerDIV.style.display="none";
	gContainerDIV.innerHTML='<div id="slideback"><iframe id="ifrgx" style="position:absolute;left:0;top:0;z-index:100;width:100%;height:100%;filter:progid:DXImageTransform.Microsoft.alpha(style=0,opacity=0);border:none;"></iframe></div><div id="slideholder"><div class="content group"><div class="padding"><div class="image_slide"><img id="image_slide" width="640" alt="" /></div><div class="optcontainer"><p class="cls"><a class="close" href="javascript:;" onclick="closeSlideshow()">Close Gallery</a></p><h1 id="productname_container"></h1><div class="detailed-button group"><p class="price"><span id="price_container">Call</span></p><form id="addtocartform" method="post"><p id="options_list" style="display:none"></p><!--<label>Qty</label><input type="text" name="quantity" value="1" />--><button type="submit" class="bact">Add to Cart</button></form><br class="clean" /></div><p id="freeshipping_notice"style="display:none">Free Shipping</p><p id="instocknotice" style="display:none"></p><ul class="specifications" id="specification_container"></ul><p id="stockno_container"></p><p id="description_container"></p></div></div></div></div>';
	mainBody.appendChild(gContainerDIV);
	//-----------------------------------------
	galleryReq.setRequestLocation("/isapi_xml.php");
	galleryReq.setResponseType('xml');
	galleryReq.assignParser(galleryParser);
	galleryReq.assignErrorHandler(onGReqError);
	//-----------------------------------------
	slideshow_loaded=true;
}
function galleryParser(){
	var resp=galleryReq.getResponse();
	document.getElementById("productname_container").innerHTML=resp.getElementsByTagName("PRODUCT_NAME")[0].childNodes[0].nodeValue;
	document.getElementById("description_container").innerHTML=resp.getElementsByTagName("PRODUCT_DESCRIPTION")[0].childNodes[0].nodeValue;
	document.getElementById("stockno_container").innerHTML=resp.getElementsByTagName("PRODUCT_STOCKNO")[0].childNodes[0].nodeValue;
	document.getElementById("specification_container").innerHTML=resp.getElementsByTagName("PRODUCT_SPECIFICATION")[0].childNodes[0].nodeValue;
	
	var isEstore=resp.getElementsByTagName("ESTORE")[0].childNodes[0].nodeValue;
	if(isEstore==1){
		//document.getElementById("estoredetails_container").style.display="block";//<-we need to have this container in gContainerDIV to turn it on or off
		document.getElementById("addtocartform").action="/addToCart.php?pid="+currentPID;
		var instockAsQty=resp.getElementsByTagName("INSTOCKASQTY")[0].childNodes[0].nodeValue;
		var instock=resp.getElementsByTagName("INSTOCK")[0].childNodes[0].nodeValue;
		var isFreeShipping=resp.getElementsByTagName("FREE_SHIPPING")[0].childNodes[0].nodeValue;
		var price=resp.getElementsByTagName("PRICE")[0].childNodes[0].nodeValue;
		
		if(price=="$0.00") price="Call";
		document.getElementById("price_container").innerHTML=price;
		
		if(isFreeShipping==1) document.getElementById("freeshipping_notice").style.display="block";
		if(instock>0){
			if(instockAsQty==1&&instock<4){
				document.getElementById("instocknotice").innerHTML="Only "+instock+" items left";
				document.getElementById("instocknotice").style.display="block";
			}
		}else{
			document.getElementById("instocknotice").innerHTML="Sorry, this item is on backorder.<br />Will Ship within 2-4 Weeks.";
			document.getElementById("instocknotice").style.display="block";
		}
		var pOpt=resp.getElementsByTagName("OPTIONS")[0];//.childNodes;//get options element
		if(pOpt){
			pOpt=pOpt.childNodes;
			if(pOpt.length>0){
				var options_list=document.getElementById("options_list");
				options_list.innerHTML="";
				var optCategory=resp.getElementsByTagName("CATEGORY");//get all options categories
				for(var i=0;i<optCategory.length;i++){
					var optCategory_name=optCategory[i].attributes.getNamedItem("category_name").nodeValue;
					var pOtions=optCategory[i].childNodes;
					var listOpts=document.createElement("LABEL");//create label with category name
					listOpts.innerHTML=optCategory_name+": ";
					options_list.appendChild(listOpts);
				
					listOpts=document.createElement("SELECT");//create drop down for options
					options_list.appendChild(listOpts);
				
					listOpts.name="options[]";
				
					for(var j=0;j<pOtions.length;j++){
						var oOption=document.createElement("OPTION");
						listOpts.options.add(oOption);
						oOption.text=pOtions[j].childNodes[0].nodeValue;
						oOption.value=pOtions[j].attributes.getNamedItem("value").nodeValue;
					}
				}
				options_list.style.display="block";
			}
		}
	}
	var slideback=document.getElementById("slideback");
	slideback.style.width=document.documentElement.scrollWidth+"px";
	slideback.style.height=document.documentElement.scrollHeight+"px";
		
	document.getElementById("slideholder").style.top=document.documentElement.scrollTop+"px";
	document.getElementById("slideshow").style.display="block";
	//----------------------------------------------------------------------------------------
	currentImage=new Image();//<-clear old images
	currentImage.src=resp.getElementsByTagName("PRODUCT_IMG")[0].childNodes[0].nodeValue;
	document.getElementById("image_slide").src=currentImage.src;
	
	currentPIDloaded=true;
}
function getProduct(pid){
	if(!slideshow_loaded){
		loadSlideShow();
	}
	if(pid==currentPID){
		if(currentPIDloaded)//display same screen if already loaded, so if no images then we won't request it again
			document.getElementById("slideshow").style.display="block";
	}else{
		currentPIDloaded=false;
		currentPID=pid;
		document.getElementById("image_slide").src="";
		galleryReq.setData("module=product&pid="+pid);
		galleryReq.send();
	}
}
function closeSlideshow(){
	document.getElementById("slideshow").style.display="none";
}
