/**
 * 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::url("products", "updateAttributeOptions") ?>/' + trailid,
			dataType: 'html',
			type: 'post',
			data: {
				product_id: <?= $product->id ?>,
				level: level
			},
			success: function(data) {
				$('#attrib_' + level).css({display: "none"});
				$('#attrib_' + level)[0].innerHTML = data;
				$('#attrib_' + level).css({display: ""});
			}
		});
	},*/
 }
 
 /* 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;
	});
 });
