// JavaScript Document

function swapMainImg(href, evtObj, sizeInfo) {
	// evtObj is the thumnail image
	if (document.createTextNode) {
		//alert("i support create text node");
	}
	if (evtObj.target) {
		var tgtImg = evtObj.target;
		var classVar = "class";
		var isIE = false;
	} else if (evtObj.srcElement) {
		var tgtImg = evtObj.srcElement;
		var classVar = "className";
		var isIE = true;
	}
	
	//alert("tgt: "+tgtImg.src);
	// get the alt tag value for the photo title
	var mainImgAlt = tgtImg.getAttribute("alt");
    var mainImgTitle = tgtImg.getAttribute("title");
 

	var imgParent = $("gallery");
	var infoBar = $("sidebar");
	
	// set class for red border on clicked thumbnail
	
	tgtImg.setAttribute(classVar, "activeThumb");
	
	// create var for thumbnails div
	var thumbDiv = $("gallery-thumbnails");
	// create an array of the images in gallery-thumbnails
	var thumbImgs = thumbDiv.getElementsByTagName("img");
	// loop through array of images and set class to inactive 
	for (var i=0; i<thumbImgs.length; i++) {
		//alert("t src: "+thumbImgs[i].src+" ~ tgt src: "+tgtImg.src)
		if (thumbImgs[i].src != tgtImg.src)	{
			thumbImgs[i].setAttribute(classVar, "inactiveThumb");
		}
	}	
	// create node for the photo title
	var title = document.createElement("h3");
	title.setAttribute("id", "purchase-title");
	var titleTxt = document.createTextNode(mainImgTitle);
	title.appendChild(titleTxt);
	infoBar.replaceChild(title, $("purchase-title"));
	
	// check for size info and replace photo-dimensions div
	var dimensions = document.createElement("p");
	dimensions.setAttribute("id", "photo-dimensions");
	if (sizeInfo) {		
		var sizeTxt = document.createTextNode("Actual matted size: "+sizeInfo);		
	}else {
		var sizeTxt = document.createTextNode("Mounted sizes vary. Custom sizes are also available.");
	}
	dimensions.appendChild(sizeTxt);
	infoBar.replaceChild(dimensions, $("photo-dimensions"));
	
	// create div element to replace #article
	var imgHolder = document.createElement("div");
	imgHolder.setAttribute("id", "img-container");
	// create image element and set attributes
	var image = document.createElement("img");
	image.setAttribute("src", href);
	image.setAttribute("alt", mainImgAlt);
	image.setAttribute("title", mainImgTitle);
	image.setAttribute(classVar, "main-photo");
	// add image node to document tree
	imgHolder.appendChild(image);
	// replace article div
	imgParent.replaceChild(imgHolder, $("img-container"));	
 	
}

function prePurchase() {
	var thumbDiv = $("gallery-thumbnails");
	// create an array of the images in gallery-thumbnails
	var thumbImgs = thumbDiv.getElementsByTagName("img");
	// loop through array of images and set class to inactive 
	for (var i=0; i<thumbImgs.length; i++) {
		if (thumbImgs[i].className == "activeThumb") {
			alert(thumbImgs[i].alt);
			var printTitle = thumbImgs[i].alt;
			var thumbSrc = thumbImgs[i].src;
			window.location = "order.php?pt="+encodeURIComponent(printTitle)+"&ts="+thumbSrc;
		}
	}		
}

function updatePrice(radioBtn) {
	//alert("details: "+details);
	var photoTitle = $("purchase-title");
	var radio = radioBtn;
	var priceVal = radioBtn.parentNode.nextSibling.firstChild.nodeValue;
	var itemDetails = String(radioBtn.value);
	var paypalItemField = $('item_name');
	var paypalAmountField = $('amount');
	var paypalShippingField = $('shipping');
	
	if (itemDetails.indexOf("Mounted") != -1) {
		var shipPrice = "20";
	}else if (itemDetails.indexOf("Rolled") != -1) {
		var shipPrice = "16";
	}
	
	//alert("price val: "+priceVal);
	paypalItemField.value = photoTitle+" "+itemDetails;
	paypalAmountField.value = priceVal;
	paypalShippingField.value = shipPrice;
}


