$(document).ready(function(){
	
	if ($.fn.colorbox) {
		$('#grid div.template-view a[rel="colorbox"]').colorbox();
	}
	
	if ($.fn.colorbox) {
		$('.download-lite').colorbox({href:"/wp-content/themes/akio/download-form.php"});
	}
	
	$('#download-form').live('submit',function(){
		// Submit the register form via AJAX to check if data is OK
		var name = $('#name').attr('value');
		var email = $('#email').attr('value');
		
		$.ajax({
			type: "POST",
			url: "/wp-content/themes/akio/download.php",
			data: "name="+ escape(name) +"&email="+ escape(email),
			dataType: "text",
			success: function(data, textStatus){
				if (data == "OK") {
					// Show success message
					$('#download-form .item').hide();
					$('#download-form h3').hide();
					var success_message = '<h5>Thank you for downloading!</h5>' +
						'<p>Akio User Interface Templates Lite are sent to your email!</p>';
					$('#download-form .message').html(success_message).show();
				} else {
					// Display the error
					var error_message;
					if (data == "BAD-EMAIL") {
						error_message = '<h5>Error</h5>' +
										'<p>Email address you entered is invalid!</p>';
					} // neispravan email
					else if (data == "PHPMailerException") {
						error_message = '<h5>Error</h5>' +
										'<p>Something went wrong while sending email... Please try again!</p>';
					} // Greska pri slanju email sa attachmentom
					else if (data == "PHPException") {
						error_message = '<h5>Error</h5>' +
										'<p>Something went wrong while sending email... Please try again!</p>';
					} // Greska pri slanju email sa attachmentom
					$('#download-form .message').html(data).show();
				};
			}
		});
		
		return false; // Don't submit the HTML form
	});
});
