$(document).ready(function(){

	/**********************************************
	 *  CONTACT FORM
	 *********************************************/

	// manages the contact form values
	$('#contactMeContainer textarea, #contactMeContainer input').focus(function(){
		var $field = $(this);
		var defaultText = $field.val();
		$field.val('');
		$field.blur( function () {
			var userInput = $field.val();
			if (userInput == ""){
				$field.val(defaultText);
			}
		});
	});

	// let's open the contact form
	$('#contactMeContainer').hover(function(e){
		$('#twitter').fadeOut();
		$(this).stop().animate({
			'marginTop': '0px'
		}, 300 );
	},function(e){
		$('#twitter').fadeIn();
		$(this).stop().animate({
			'marginTop': '-117px'
		}, 300 );
	});

	// submit the comment form via AJAX
	$('form#contactMe').submit(function(e) {
		// prevent the default form submit
		e.preventDefault();
		// set some default variables
		var msg = '';
		var $form = $(this);
		var $name = $('#yourname');
		var $email = $('#youremail');
		var $comments = $('#yourcomments');
		var action = $form.attr('action');

		// the regular expression used to test email validity
		var re = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;

		msg += ($name.val() == '' || $name.val() == 'name') ? '- Name is required\n' : '';
		msg += ($comments.val() == '' || $comments.val() == 'comments') ? '- A comment is required\n' : '';
		msg += (re.test($email.val()) == false) ? '- A valid mail is required\n' : '';

		// only run the post if the visitor name, email, and comments aren't blank
		if (msg != '') {
			alert(msg);
			return false;
		} else {
			$.post(
					action,{
					yourname: $name.val(),
					yourcomments: $comments.val(),
					youremail: $email.val()
				},
				function(data){
					var status = data.split('!!!')[1];
					alert(status);
					/* clear the form fields:*/
					$name.val('name');
					$comments.val('comments');
					$email.val('email');
				}
			);
		}
		return false;
	});



	/**********************************************
	 *  EXTERNAL LINKS
	 *********************************************/


	// manage external links
	$('a[href^="http"]').click(function(e){
		// stop the default action
		e.preventDefault();
		// store a reference to the link
		var link = this.href;
		// the dataType for external links
		var fkStatstype = 7;
		// also, let's keep some stats on how many people click
		// the external links
		// if the msg var has NO values, then it's time to make the AJAX call to send the email
		$.get("/com/jrnl/stats.cfc?method=statsInsert", {
			value: link,
			fkStatstype: fkStatstype,
			ip: ipAddress // this var comes from header.cfm
		});
		// trigger a new window so that we don't get
		// busted with validation errors. Silly really
		window.open(link);
	});



	/**********************************************
	 *  COMMENT FORM
	 *********************************************/

	// submit the comment form via AJAX
	$('form[name=addcomment]').submit(function(e) {
		// prevent the default form submit
		e.preventDefault();
		// set some default variables
		var $form = $(this);
		var $comments = $('#commentContainer');
		var action = $form.attr('action');
		var $postid = $('input[name=fkPosts]',$form);
		var $fVisitor = $('input[name=visitor]',$form);
		var $fEmail = $('input[name=email]',$form);
		var $fLink = $('input[name=website]',$form);
		var $fSub = $('input[name=subscribe]',$form);
		var $fComment = $('textarea[name=comment]',$form);

		// only run the post if the visitor name and comments aren't blank
		if ($fVisitor.val() == '') {
			alert('Name is required');
		} else if ($fComment.val() == '') {
			alert('A comment is required');
		} else if ($fEmail.val() == '') {
			alert('Email is required');
		} else {
			$.post(
					action,{
					visitor: $fVisitor.val(),
					email: $fEmail.val(),
					link: $fLink.val(),
					comment: $fComment.val(),
					fkPosts: $postid.val(),
					subscribe: $fSub.get(0).checked
				},
				function(data){
					// delete the "noComment" block if it exists
					$('.noComment').remove();
					// place the returned string just above the form
					$comments.append(data.split('!!!')[1]).slideDown();
					/* clear the form fields:*/
					$fVisitor.val('');
					$fEmail.val('');
					$fLink.val('');
					$fComment.val('');
				}
			);
		}
		return false;

	});


	/**********************************************
	 *  PRETTY PHOTO (lightbox plugin)
	 *********************************************/
	$("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		showTitle: true, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'dark_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
		callback: function(){}
	});

});