// JavaScript Document

$(function(){
/*    $("head").append("<link rel='shortcut icon' href='favicon.ico' >");*/
		$.getScript("../scripts/facebox.js",function(){
			$("#order_form").submit(function(){
				$("ul #name,ul #email").blur();
				if($(".order_form img[alt='error']").length > 0){
					alert("Please complete all require information!");
				}else{
					$(".order_form #width,.order_form #height").removeAttr("disabled");
					var formData = $("#order_form")
						.find("input,textarea,select,hidden")
						.not("#__VIEWSTATE,#__EVENTVALIDATION")
						.serialize();
	$.post('../painting.order.php', formData, function(data){ $.facebox(data,"facebox_ajax");});
					if($(".order_form #sizetype").attr("checked")){
					$(".order_form #width,.order_form #height").attr("disabled","disabled");
					}
				}
				return false;
			});
			$("#contact_form").submit(function(){
				$("ul #name,ul #email,ul #requirement").blur();
				if($(".contact_form img[alt='error']").length > 0){
					alert("Please complete all require information!");
				}else{
					var formData = $("#contact_form")
						.find("input,textarea,select,hidden")
						.not("#__VIEWSTATE,#__EVENTVALIDATION")
						.serialize();
					$.post('../contact.us.php', formData, function(data){
						$.facebox(data,"facebox_ajax");
					});
				}
				return false;
			});
			$("#image_lightbox a:has(img)").facebox("facebox_img");
		});

		$(".warning").remove();
		
		/**check whether there is a form in the page or not**/
		if($(".order_form").length > 0){
			// Set initial status for order form
			var originalWidth = $(".order_form #width").attr("ori");
			var originalHeight = $(".order_form #height").attr("ori");
			
			setInitial(originalWidth,originalHeight);
			
			$(".order_form #sizetype").bind("click", function(){
				switchOrderType(originalWidth,originalHeight);
			});
			
			$(".order_form #width,.order_form #height").bind("change", function(){
				var ratio = originalWidth/originalHeight;
				if($(".order_form #cbaspect").attr("checked")){
					if($(this).is("#height")){
						$(".order_form #width").val(Math.round($(this).val()*ratio));
					}else{
						$(".order_form #height").val(Math.round($(this).val()/ratio));
					}
				}
			});
			
			$.getScript("../scripts/validator.js",function(){
				$("ul input").bind("blur", function(){
					$(this).validate.init(this);
				});
			});
		}
		
		if($(".contact_form").length > 0){
			$.getScript("../scripts/validator.js",function(){
				$("ul #name,ul #email,ul #requirement").bind("blur", function(){
					$(this).validate.init(this);
				});
			});
		}
		
		if($(".treeview").length > 0){
			$.getScript("../scripts/jquery.treeview.pack.js",function(){
				$(".treeview").treeview({unique: true});
			});
		}
		
		fixheight();
		
		if($("#image_thumb").length > 0){
			$.getScript("../scripts/jquery.preload-min.js",function(){
				$.preload('#content>#image_thumb img', {
		   			placeholder:'../images/layout/placeholder.jpg',
		   			notFound:'../images/layout/notfound.jpg',
			   		threshold: 2 //'2' is the default, how many at a time, to load.
				});
			});
		}
	});


<!-- Fix page content height if shorter than left side bar -->
   function fixheight(){
	var sidebarheight = $("#main > div:eq(0)").height();
	var contentheight = $("#main > div:last").height();
	var newheight = contentheight;
	
	if(contentheight<sidebarheight){
		newheight = sidebarheight+20;
		$("#main > div:last").height(newheight);
		}
	}<!-- End fix height -->
	
	function setInitial(width,height){
		$(".order_form #width").val(width);
		$(".order_form #height").val(height);
		$(".order_form #sizetype").attr("checked","checked");
		$(".order_form #cbaspect").attr("checked","checked");
		$(".order_form #width").attr("disabled","disabled");
		$(".order_form #height").attr("disabled","disabled");
	}
	
	function switchOrderType(width,height){
		/*$(".order_form fieldset").slideToggle("fast");*/
		if($(".order_form #width").attr("disabled")){
			$(".order_form #aspect").fadeIn();
			$(".order_form #original_size").fadeOut(function(){
				$(".order_form #custom_size").fadeIn();
			});
			$(".order_form #width").removeAttr("disabled");
			$(".order_form #height").removeAttr("disabled");
		}else{
			$(".order_form #width").val(width);
			$(".order_form #height").val(height);
			$(".order_form #aspect").fadeOut();
			$(".order_form #custom_size").fadeOut(function(){
				$(".order_form #original_size").fadeIn();															
			});
			$(".order_form #cbaspect").attr("checked","checked");
			$(".order_form #width").attr("disabled","disabled");
			$(".order_form #height").attr("disabled","disabled");
			$("#width_img,#height_img").html("");
		}
	}