/**
 * application-jquery.js
 * Matt Farmer for Boxkite Media
 *
 * Designed for distribution with Spool 1.8
 * Include this file after jQuery.
 * DO NOT EDIT THIS FILE TO INCLUDE CLIENT SPECIFIC FUNCTIONALITY.
 */

 /* Frontend Application class */
 var Application = {
 	appUrl: '',

 	updateAttributeOptions: function(caller, level) {
		trailid = caller.options[caller.selectedIndex].value;

		$.ajax({
			url: Application.appUrl + '/products/updateAttributeOptions/' + trailid,
			dataType: 'html',
			type: 'post',
			data: {
				product_id: $('#pId').val(),
				level: level
			},
			success: function(data) {
				$('#attrib_' + level).css({display: "none"});
				$('#attrib_' + level)[0].innerHTML = data;
				$('#attrib_' + level).css({display: ""});
			}
		});
	},

	addToCart: function() {
		var frm = $('#frmAdd')[0];

		//> validate quantity
		if(!parseInt(frm.qty.value)) {
			alert("Please enter a valid quantity");
			return false;
		}

		//> validate attributes
		var aryAttributes = frm.getElementsByTagName("select");
		for(var i = 0; aryAttributes[i]; i++) {
			if(!parseInt(aryAttributes[i].value)) {
				alert("Please select a value for " + aryAttributes[i].options[0].text);
				return false;
			}
		}

		//> submit form
		$.ajax({
			url: Application.appUrl + '/cart/addToCart',
			type: "post",
			dataType: 'html',
			data: {
				pId: $('#pId').val(),
				qty: $('#qty').val()
			},
			success: function(data) {
				Spool.popupText("alert", data);
			},
		});
	},

	removeItem: function(itemId) {
		$.ajax({
			url: Application.appUrl + '/cart/removeFromCart',
			type: "post",
			dataType: 'html',
			data: { lId: itemId },
			success: function(data) {
				$("#row_" + itemId).fadeOut(300);
				$('#subtotal')[0].innerHTML = data;
			}
		});
	},

	updateItem: function(lId, qty) {
		$.ajax({
			url: Application.appUrl + '/cart/updateCart',
			type: "post",
			dataType: "html",
			data: { lId: lId, qty: qty },
			success: function(data) {
				if(data == "INVALID" || data == "ERROR") {
					document.location.reload();
				} else {
					$('#lineprice_' + lId)[0].innerHTML = ($('#price_' + lId)[0].innerHTML * qty).toFixed(2);
					$('#subtotal')[0].innerHTML = data;
				}
			}
		});
	},

	updateShipping: function(select) {
		$.ajax({
			url: Application.appUrl + '/cart/updateShipping',
			type: "post",
			dataType: 'html',
			data: { sId: select.value },
			success: function(data) {
				//alert(transport.responseText);
				document.location.reload();
			}
		});
	},

	promo: function(promo) {
		$.ajax({
			url: Application.appUrl + '/cart/promo',
			type: "post",
			data: { code: promo },
			dataType: 'html',
			success: function(data) {
				//alert(transport.responseText);
				if(data == "n") {
					alert("Code is invalid or code cannot be applied to this cart.");
				}
				else {
					$('#promo-desc')[0].innerHTML = data;
					$('#promo-entry').slideUp(300);
					$('#current-promo').slideDown(300);
					$('#promocode')[0].value = "";
				}
			}
		});
	},

	removePromo: function() {
		$.ajax({
			url: Application.appUrl + '/cart/nopromo',
			type: "post",
			success: function(transport) {
				$('#promo-entry').slideDown(300);
				$('#current-promo').slideUp(300);
			},
		});
	},
 }

 /* DOCUMENT ON READY FOR FRONTEND DATA */
 $(document).ready(function() {
 	//Fancybox code
 	$('a[rel="fancybox"]').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	false
 	});

 	//Login form handler
 	$('#login-form').submit(function() {
		$.ajax({
			url: Application.appUrl + "/spool/user/loginChallenge",
			type: 'POST',
			data: 'email=' + $('#username')[0].value,
			success: function(data) {
				//Error?
				if(data == 'No such user.') {
					alert(data);
					return;
				}

				//Encrypt the user's password using MD5
				var md5pass = hex_md5($('#password')[0].value);
				var correctHash = hex_md5(md5pass + data);

				//Reset form value
				$('#password')[0].value = correctHash;

				//Submit!
				$('#login-form')[0].submit();
			}
		});

		return false;
	});

	//The spool box (for people who are logged in)
	$('#spool-box #spool-tab').click(function() {
		if($('#spool-box')[0].style.left == "0px") {
			$("#spool-box").animate({"left": '-250px'}, 500);
		}
		else {
			$("#spool-box").animate({"left": '0px'}, 500);
		}

		return false;
	});

	//The on submit handler for the spool box
	$('#spool-box-form').submit(function () {
		var data = $('#spool-box-form').serialize();

		$.post($('#spool-box-form')[0].action, data, function() {
			document.location.reload();
		});

		return false;
	});

	////
	// CODE FOR COMMENTS
	////

	//Reply handler
	$('.comment-reply a').click(function() {
		var id = $(this)[0].parentNode.parentNode.parentNode.id;

		document.frmComment.parent_id.value = id;

		$('#comment-reply-text').css({display: ""});

		$('html, body').animate({scrollTop: $(".comment-form").offset().top}, 500);

		return false;
	});

	//Clear the reply association
	$('a.clear-reply').click(function() {
		document.frmComment.parent_id.value = 0;
		$('#comment-reply-text').css({display: "none"});
		return false;
	});

	//comment form handler
	$('.comment-form form').submit(function() {
		var err = false;
		var errorText = "";

		if(document.frmComment.author_name.value.length == 0) {
			errorText += "- Name is required\n";
			err = true;
		}
		if(document.frmComment.author_email.value.length == 0) {
			errorText += "- Email is required\n";
			err = true;
		}
		if(document.frmComment.comment_body.value.length == 0) {
			errorText += "- Comment is required\n";
			err = true;
		}

		if(errorText.length) {
			alert(errorText);
		}

		return !err;
	});
 });

