/*
Document: jqueryfunctions.js
Author: Graham Jelinski
Purpose: All JQuery functions are defined in this js file
*/

//$(document).ready(function() {
jQuery(document).ready(function($) { // user this instead of $document.ready to load specific jquery plugins; weird jQuery bug - doesn't make sense
	
	// determine the name of the current page and use it to alter the css of the
	// nav controls along the top of the page
	//var sPath = window.location.pathname;
	//var sFilename = sPath.substring(sPath.lastIndexOf('/') + 1);
	//var sName = '#' + sFilename.substr(0, sFilename.lastIndexOf('.')) || sFilename;
	//$(sName).addClass("selected");
	
	/*************************************************
	Content Gallery Slider
	**************************************************/
	$("#slideController").jFlow({
		slides: "#slides",
		controller: ".jFlowControl", // must be class, use . sign
		slideWrapper : "#jFlowSlide", // must be id, use # sign
		selectedWrapper: "jFlowSelected",  // just pure text, no sign
		auto: true,		//auto change slide, default true
		width: "800px",
		height: "250px",
		duration: 400,
		prev: ".jFlowPrev", // must be class, use . sign
		next: ".jFlowNext" // must be class, use . sign
	});
	
	/*************************************************
	Load Twitter Feed
	**************************************************/
	$(".tweet").tweet({
		username: "gjelinski",
		join_text: "auto",
		avatar_size: 0,
		count: 6,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "loading tweets..."
	});
	
	/*************************************************
	Contact Form Animations
	**************************************************/
	
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set height and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight, 'text-align':'left', 'position':'absolute', 'top':0, 'left':0});
		
		//transition effect		
		//$('#mask').fadeIn(500);	
		$('#mask').fadeTo("slow", 0.4);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(500); 
	
	});
	
	function CloseModalWindow(e) {
	
		e.preventDefault();
		$('#mask, .window').fadeOut(200);
	}
	
	//if close button is clicked
	
	$("#btnCancel").live('click', function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').fadeOut(200);
	});
	/*
	$('#btnCancel').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		$('#mask, .window').fadeOut(200);
	});
	*/
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
	
	/************************************************************
	Contact Form Validation
	*************************************************************/
	
	var bErrors = false;
	
	/*Validate name field*/
	
	var emNameStatus = $("#nameStatus");
	//$("#nameStatus img[title]").tooltip();
	
	function ValidateNamePre() {
		var iLength = $("#txtFullName").val().length;

		if (iLength > 0) {
			emNameStatus.removeClass('error').addClass('correct').html('<img src="img/layout/contactform_validateok.png" />').fadeIn('fast');
		}
		else {
			emNameStatus.removeClass('correct').fadeOut('slow');
		}
	}
	
	function ValidateNamePost() {
		var iLength = $("#txtFullName").val().length;

		if (iLength <= 0) {
			emNameStatus.removeClass('correct').addClass('error').html('<img src="img/layout/contactform_validateerror.png" title="Please provide your name" />').fadeIn('normal');
			bErrors = true;
		}
	}

	$("#txtFullName").keyup(ValidateNamePre);

	$("#txtFullName").blur(ValidateNamePost);

	/*Validate email field*/

	var emEmailStatus = $("#emailStatus");
	var patt = /^.+@.+[.].{2,}$/i;
	
	function ValidateEmailPre() {
		var iLength = $("#txtEmail").val().length;

		if (iLength > 0 && patt.test($("#txtEmail").val())) {
			emEmailStatus.removeClass('error').addClass('correct').html('<img src="img/layout/contactform_validateok.png" />').fadeIn('fast');
		}
		else {
			emEmailStatus.removeClass('correct').fadeOut('slow');
		}
	}
	
	function ValidateEmailPost() {
		var iLength = $("#txtEmail").val().length;

		if (iLength <= 0 || !patt.test($("#txtEmail").val())) {
			emEmailStatus.removeClass('correct').addClass('error').html('<img src="img/layout/contactform_validateerror.png" title="Please provide a valid email address" />').fadeIn('normal');
			bErrors = true;
		}
	}
	
	$("#txtEmail").keyup(ValidateEmailPre);

	$("#txtEmail").blur(ValidateEmailPost);

	
	/*Validate comments field*/

	var emCommentsStatus = $("#commentsStatus");

	function ValidateCommentsPre() {
		var iLength = $("#txtComments").val().length;

		if (iLength > 0) {
			emCommentsStatus.removeClass('error').addClass('correct').html('<img src="img/layout/contactform_validateok.png" />').fadeIn('fast');
		}
		else {
			emCommentsStatus.removeClass('correct').fadeOut('slow');
		}
	}
	
	function ValidateCommentsPost() {
		var iLength = $("#txtComments").val().length;

		if (iLength <= 0) {
			emCommentsStatus.removeClass('correct').addClass('error').html('<img src="img/layout/contactform_validateerror.png" title="Please provide your comments or questions" />').fadeIn('normal');
			bErrors = true;
		}
	}
	
	$("#txtComments").keyup(ValidateCommentsPre);

	$("#txtComments").blur(ValidateCommentsPost);
	
	$('#btnSubmit').click(function () {
    	
		ValidateNamePost();
		ValidateEmailPost();
		ValidateCommentsPost();
		
		var sDataString = 
			"txtFullName=" + $('#txtFullName').attr('value') +
			"& txtEmail=" + $('#txtEmail').attr('value') +
			"& txtComments=" + $('#txtComments').attr('value');
		
		if (bErrors == false) {
			$.ajax({
				type: "POST",
				url: "mail.php",
				data: sDataString,
				success: function (data) {
					$('#dialog').html("<div id='message'></div>");
					$('#message')
						.html(data)
						.hide()
						.fadeIn(1, function () {
							$('#message').append("<button type='button' class='closebutton' id='btnCancel'>OK</button>");
						});
				}
			});
		}
		
		return false;
	});
});
