/*
 * Long Division
 * -------------------------------------------------------
 * Last Updated: 08/06/2011
 */

$(window).load(function() {

	// Initiate Nivo Slider
	
	$('#work-page').nivoSlider({
		effect: 'sliceDownLeft',
		slices: 15,
		pauseTime: 5000,
		captionOpacity: 0.6,
		keyboardNav: true,
	});
	
	// Preload images

	(function($) {
	  var cache = [];
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)
	
});

$(document).ready(function() {

	// Hide hidden content
	
	$('#about').hide();
	$('#contact').hide();
 	
 	// Handle URL's with appended hash tags
	
	hashId = jQuery.param.fragment();

	if (hashId == "about") {
		displayAboutPage();
	} else if (hashId == "contact") {
		displayContactPage();
	}
	
	// Initiate form validation
	
	$('#contact-form').checkForm({
		requireMark: false
	});
	
	// Update links and corresponding content
	
	$('#show-work').click(function() {
		displayWorkPage();
	});
	
	$('#show-about').click(function() {
		displayAboutPage();
	});
	
	$('#show-contact').click(function() {
		displayContactPage();
	});

});

/*
 * Send contact data off for processing
 * handle feedback and display response to user
 */
function sendMessage(){
		
	var dataString = 'name='+ $('#name-field').val() + '&email=' + $('#email-field').val() + '&message=' + $('#message-field').val();
	
	$.ajax({
		type: "POST",
		url: "_scripts/contact/contact.php",
		data: dataString,
		success: function() {
			$('#contact-error').hide();
			$('#contact-success').show();
			_gaq.push(['_trackPageview', '/made-contact']);
		},
		error: function() {
			$('#contact-success').hide();
			$('#contact-error').show();
		}
	});

}

function displayWorkPage() {
	$('#show-work').attr("class", "selected");
	$('#show-about').attr("class", "");
	$('#show-contact').attr("class", "");
	$('#work-page').show();
	$('#about-page').hide();
	$('#contact-page').hide();
	_gaq.push(['_trackPageview', '/work']);
}

function displayAboutPage() {
	$('#show-work').attr("class", "");
	$('#show-about').attr("class", "selected");
	$('#show-contact').attr("class", "");
	$('#work-page').hide();
	$('#about-page').show();
	$('#contact-page').hide();
	_gaq.push(['_trackPageview', '/about']);
}

function displayContactPage() {
	$('#show-work').attr("class", "");
	$('#show-about').attr("class", "");
	$('#show-contact').attr("class", "selected");
	$('#work-page').hide();
	$('#about-page').hide();
	$('#contact-page').show();
	_gaq.push(['_trackPageview', '/contact']);
}

/*
 * ROT13 some text to generate a link to contact email
 */
function printEmail() {
	document.write("<n uers=\"znvygb:frna@ybatqvivfvba.pb.hx\">frna@ybatqvivfvba.pb.hx</n>".replace(/[a-zA-Z]/g,
							function(c){return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);})
								);
}

