function showImage() 
{
	//get image name
	var thumbName = this.getAttribute('id');
	var image 	  = thumbName.replace('_th', '');
	
	
	//cycle through each image/title
	var images = $$('#images li');
	for (var i = 0; i < images.length; i++) {
		var listItem = images[i].getAttribute('id');
		var match    = listItem.search(image);
		//check against clicked thumb
		if(match != -1) {
			//if match is found
			$(listItem).className = "reveal";
		} else {
			$(listItem).className = "hidden";
		}
	}
	
}

function showOrderForm()
{
	//check/update sub-total first
	updateSubTotal();
	
	//hide Place Order button
	$("openOrderForm").className = "hidden";
	$("orderForm").className = "reveal";
	
	//reveal any tiles previously chosen
	for (var i = 1; i < 5; i++) {
		var tileTotal = "tile_total" + i;
		if($(tileTotal).innerHTML != "$0.00") {
			var tileDiv = "orderTile" + i;
			$(tileDiv).className = "reveal";	
		}
	}
	
}

function updateSubTotal()
{
	var subTotal = 0;
	var total = 0;
	
	var lineTotal1 = $("tile_qty1").value * $("tile_size1").value;
	$("tile_total1").innerHTML = lineTotal1;
	if(lineTotal1 <= 2) { lineTotal1 = 0; }
	$("tile_total1").innerHTML = "$" + lineTotal1 + ".00";

	var lineTotal2 = $("tile_qty2").value * $("tile_size2").value;
	$("tile_total2").innerHTML = lineTotal1;
	if(lineTotal2 <= 2) { lineTotal2 = 0; }
	$("tile_total2").innerHTML = "$" + lineTotal2 + ".00";
	
	var lineTotal3 = $("tile_qty3").value * $("tile_size3").value;
	$("tile_total3").innerHTML = lineTotal1;
	if(lineTotal3 <= 2) { lineTotal3 = 0; }
	$("tile_total3").innerHTML = "$" + lineTotal3 + ".00";
	
	var lineTotal4 = $("tile_qty1").value * $("tile_size4").value;
	$("tile_total4").innerHTML = lineTotal4;
	if(lineTotal4 <= 2) { lineTotal4 = 0; }
	$("tile_total4").innerHTML = "$" + lineTotal4 + ".00";

	var subTotal = lineTotal1 + lineTotal2 + lineTotal3 + lineTotal4;
	$("subTotal").innerHTML = "$" + subTotal + ".00";	

	var postage = 0;
	if(subTotal >= 2 && subTotal <= 80) { postage = 4; }
	if(subTotal >= 81 && subTotal <= 120) { postage = 8; }
	if(subTotal >= 121 && subTotal <= 240) { postage = 10; }
	if(subTotal >= 241 && subTotal <= 320) { postage = 12; }
	if(subTotal >= 321) { postage = 16; }
	total = subTotal + postage;
	$("postPack").innerHTML = "$" + postage + ".00";	
	$("totalOrder").innerHTML = "$" + total + ".00";
	
	// update order details for emails
	
	//order items
	var orderData = "";
	orderData += "THANK YOU FOR YOUR ORDER\n\n\n";
	if($("tile_qty1").value != -1) { orderData += $("tile_qty1").value + " x " + $("tile_name1").value + " $" + $("tile_size1").value + ".00 each, $" + lineTotal1 + ".00\n\n"; }
	if($("tile_qty2").value != -1) { orderData += $("tile_qty2").value + " x " + $("tile_name2").value + " $" + $("tile_size2").value + ".00 each, $" + lineTotal2 + ".00\n\n"; }
	if($("tile_qty3").value != -1) { orderData +=  $("tile_qty3").value + " x " + $("tile_name3").value + " $" + $("tile_size3").value + ".00 each, $" + lineTotal3 + ".00\n\n"; }
	if($("tile_qty4").value != -1) { orderData +=  $("tile_qty4").value + " x " + $("tile_name4").value + " $" + $("tile_size4").value + ".00 each, $" + lineTotal4 + ".00\n"; }
	
	//order totals
	orderData += "\nSub total: $" + subTotal + ".00\nPostage: $" + postage + ".00\nTotal: $" + total + ".00 \n\nPlease transfer payment within 48 hours to the following account:\nAnmea Hoskin";
	
	$("orderItems").value = orderData;

}

function updateOrderDetails() 
{
	//customer address and message
	var orderDetails = "ANZ 010373-0018713-00\n\nDelivery address:\n" + $("custName").value + "\n" + $("street").value + "\n" + $("suburb").value + "\n" + $("city").value + "\n\nMessage:\n" + $("msg").value + "\n" + $("email").value;

	$("custDetails").value = orderDetails;
}

function updateEmailDetails()
{
	$("to").value = $("email").value;
}


function addTile()
{
	var tileOrders = $$("#orderForm div.reveal");
	var num = tileOrders.length + 1;
	if(num <= 4) {
		var divNum = "orderTile"+num;
		$(divNum).className = "reveal";
	} 
	if(num >= 4){
		$("addTile").className = "hidden";
	}
	
}

function removeValue() 
{
	this.value = "";
}

function showJoinForm()
{
	$("newsletterForm").className = 'reveal';
}

/** MAIN LOADER FUNCTION **/
function loader() 
{
	// CHECK FOR SUPPORTED FEATURES
	// if these features are not supported by the browser, stop javascript
	if (!document.getElementById || !document.getElementsByTagName) {
		alert("Javascript features not supported");
		return; 
	}

	//PRE-CACHE OVER IMAGES
	var shop = new Image();
	shop.src = "images/littleladies/nav_shop_over.jpg";
	var bio = new Image();
	bio.src = "images/littleladies/nav_bio_over.jpg";
	var info = new Image();
	info.src = "images/littleladies/nav_info_over.jpg";
	var contact = new Image();
	contact.src = "images/littleladies/nav_contact_over.jpg";
	
	// NAV LINKS - change CSS CLASS for current page link
	var url      = document.location.href;
	var navLinks = $$('#navList a');
	
	for (var i = 0; i < navLinks.length; i++) {
		var page  = navLinks[i].innerHTML;
		var match = url.search(page);
		if(match != -1) {
			$('nav_'+page).className = (page+"Current");
		}
	}
	
	// NEWSLETTER FORM	
	if($("newsletterForm")) {
		$("join").addEvent('click', showJoinForm);
		$("from").addEvent('click', removeValue);
	}
	
	
	// IMAGE GALLERY
	if($("thumbs")) {
		// change image on thumbnail click
		var thumbNails = $$('#thumbs li.thumbNail');
		for (var i = 0; i < thumbNails.length; i++) {
			var thumbName = thumbNails[i].getAttribute('id');
			$(thumbName).addEvent('click', showImage);
		}
		
		
		// reveal first image and title by default
		var images = $$('#images li');
		var pageNum = $$('#paginationLinks b');
		if(pageNum[0]) {
			if((pageNum[0].innerHTML)==1) {
				images[0].className = "reveal";
				images[1].className = "reveal";
			}
			if((pageNum[0].innerHTML)==2) {
				images[20].className = "reveal";
				images[21].className = "reveal";
			}
			if((pageNum[0].innerHTML)==3) {
				images[40].className = "reveal";
				images[41].className = "reveal";
			}
		} else {
			images[0].className = "reveal";
			images[1].className = "reveal";
		}
	}
	
	// SHOP
	if($("openOrderForm")) {
		$$("#openOrderForm a").addEvent('click', showOrderForm);
	}
	
	if($("orderForm")) {
		//keep form open when using pagination on thumbs
		if($("subtotal") >=1) {showOrderForm();}
		
		// add another tile to form
		$("addTile").addEvent('click', addTile);
		
		// adjust the sub-total every time a selection is made
		var selectBoxes = $$("#orderForm select");
		for (var i = 0; i < selectBoxes.length; i++) {
			var selectBox = selectBoxes[i].getAttribute('id');
			var matchQty = selectBox.search("tile_qty");
			var matchSize = selectBox.search("tile_size");
			if(matchQty != -1 || matchSize != -1) {
				selectBoxes[i].onchange = updateSubTotal;
			}
		}
		
		// assign customer input details to emails
		$("submitOrderBtn").addEvent('click', updateOrderDetails);
		
		$("email").onchange = updateEmailDetails;
	}
	
}
			
			
window.onload = loader;			
			