$(document).ready(function(){
	
	// Open order form and fill initial values
	$("#skin_gallery .view_skins, #skin_gallery .image").click(function() {
		arrPageSizes = ___getPageSize();
		arrPageScroll = ___getPageScroll();
		skin_name = $(this).parent().find("h4 a").html()
		
		// Set order form skin name
		$("#order_name").html("<strong>" + skin_name + "</strong> Order Form")
		
		// Set price from page
		$(".total strong").html($(this).parent().parent().find(".image strong").html())
		
		// Set input to skin val
		$("input[name='skin']").val($(this).attr('rel'))
		$("input[name='skin_name']").val(skin_name)
		
		$("#overlay").css({
			width:	arrPageSizes[0],
			height:	arrPageSizes[1]
		}).fadeIn()
		
		$("#order_form").css({
			top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
			left:	arrPageScroll[0]
		}).fadeIn()

		$("#username").focus()
		
		return false
	})
	
	// Dynamic price updating
	$("input[type='checkbox']").not("#agree").click(function() {
		total = $("h4.total strong")
		price = parseFloat(total.html())
		
		if ($(this).attr('checked')) {
			total.html(price + 30)
		} else {
			total.html(price - 30)
		}
	})
	
	// Close the form
	$(".cancel").click(function() {
		$("#overlay, #order_form").fadeOut('fast', function() {
			$("#order_form input").val("")
			$(".fail").hide()
		})
		
		return false
	})

	// Verify and submit form
	$("#purchase").click(function() {
		result = submitPurchase();
		return result;
	});
	
	// If there were errors, run submitPurchase right away
	if ($("#purchase").length == 0) {
		submitPurchase();
	}
});


function submitPurchase() {
	
	$("form .fail").hide();
	$("#order_form input, #order_form label").removeClass("error")
	var hasError = false;
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	// Username availability check
	$("#username").blur(function(){
		$("label[for='username']").load("/gallery.php?&q=" + this.value);
	});
    
	// Email must the there if they are not logged in
	if ($("#email").length > 0) {

		// Email verify
		var email_field = $("#email")
		var emailVal = email_field.val();
		if (emailVal == '') {
			email_field.addClass("error");
			hasError = true;
		} else if (!emailReg.test(emailVal)) {
			$("#email").addClass("error").before('<p class="fail">Please enter a valid email address.</p>');
			hasError = true;
		}
		
		// Username verify
		var username_field = $("#username")
		var usernameVal = username_field.val();
		if (usernameVal.length < 3) {
			username_field.addClass("error").before('<p class="fail">Must be at least 3 characters.</p>');
			hasError = true;
		}
    	
		// Password verify
		var password_fields = $("#password,#password_verify")
		var password_field = $("#password")
		var passwordVal = password_field.val()
		if (passwordVal.length < 6) {
			password_fields.addClass("error").before('<p class="fail">Must be at least 6 characters.</p>');
			hasError = true;
		}
    	
		// Password Verified verify
		var password_verify_field = $("#password_verify")
		var passwordVerifyVal = password_verify_field.val()
		if (passwordVerifyVal != passwordVal) {
			password_fields.addClass("error").before('<p class="fail">Your passwords do not match.</p>');
			hasError = true;
		}
	}
	
	// Terms verify
	if (!$("#agree").attr('checked')) {
		$("label[for='agree']").addClass("error");
		hasError = true;
	}
    
	if (hasError == false) {
		$(this).hide();
		$(this).after('<span id="sending">Sending form...</span>')
	} else {
		return false
	}
	
}

function ___getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
};

function ___getPageScroll() {
	var xScroll, yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}
	arrayPageScroll = new Array(xScroll,yScroll);
	return arrayPageScroll;
};
