/**
 * jQuery calls for Anemone Design web site
 * This file is dependant on jquery-1.4.4.min.js
 *
 * Onload:
 *
 */
$(document).ready(function() {
	
/**
 * General
 *
 *
 */

	// make sure legal div goes to bottom
	// the 88 is the height of the legal div
	$('#legal').height(function(index, height) {  
		return window.innerHeight - $(this).offset().top - 70;  
	});
	
	// when resize window, we need to re-extend the legal div
	$(window).resize(function() {
		$('#legal').height(function(index, height) {  
			return window.innerHeight - $(this).offset().top - 70;  
		});
	});

	// clear input text fields
	$('.clearfield').searchField();
	
	// check jobs form
	jQuery.validator.addMethod("doesNotEqualTitle", function(value, element)
	{
		return element.value != element.title;
	}, "Invalid entry.");


	$("form.jobs").validate({
		rules:
		{
			name:
			{
				required: true,
				doesNotEqualTitle: true
			},
			email:
			{
				required: true,
				email: true,
				doesNotEqualTitle: true
			},
			portfolio:
			{
				required: true,
				doesNotEqualTitle: true
			},
			comments:
			{
				required: true,
				doesNotEqualTitle: true
			}
		},
		errorLabelContainer: "#error_display",
		wrapper: "li",
		errorClass: "invalid"
	});
	
	$("form.contact").validate({
		rules:
		{
			name:
			{
				required: true,
				doesNotEqualTitle: true
			},
			email:
			{
				required: true,
				email: true,
				doesNotEqualTitle: true
			},
			phone:
			{
				required: true,
				doesNotEqualTitle: true
			},
			comments:
			{
				required: true,
				doesNotEqualTitle: true
			}
		},
		errorLabelContainer: "#error_display",
		wrapper: "li",
		errorClass: "invalid"
	});
	
/**
 * Work Details
 *
 *
 */

	$(".full").hide();		// hide all full size
	$("#full-1").show();	// show first

	// when click on thumb, hide all, and show full
	$(".thumb").click(function() {
		$(".full").hide();
		$("#full-"+$(this).attr("id").replace("thumb-","")).show();
		return false;
	});
	
/**
 * Open external links in new window
 *
 *
 */
	
	$('html a:not(.popupwindow)').filter(function(){
		var theHref = this;
		if (theHref.hostname && theHref.hostname !== location.hostname) {
			$(theHref).not(".noAutoIcon").addClass("offSite");
			$(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
				var code=event.charCode || event.keyCode;
				if (!code || (code && code == 13)){

					// Build tracking url
					var fixedLink = this.href;
					fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
					fixedLink = '/outgoing/' + fixedLink;

					// Track
					//pageTracker._trackPageview(fixedLink);
				};
			});
		};
	});
	
/**
 * Search tags form
 *
 *
 */
	
	$('#search').submit(function() {
		var site_url = $("#search .search-input").attr("rel");
	  	var formvalue = $("#search .search-input").val();

		if(formvalue != "Search our portfolio")
		{
			// replace spaces with pipe
			var replaced = formvalue.replace(/\s/g,"|");
			
			// redirect to results
			window.location = site_url+'search/'+replaced;
		}
		else
		{
			alert("Please enter tags to search for.");
			return false;
		}
	  return false;
	});
	
});
