// //////////////////////////// //
//	Initialisation
// //////////////////////////// //
var lastBlock = null;
var closedWidth = 80;
var openWidth = 960 - (2 * closedWidth);

$(document).ready(function(){
	$(".nav_item").click( openMenu );
	$(".closer").click( closeMenu );
	$("ul.nav li h1").hide();

	createPortfolio();
});

// //////////////////////////// //
//	Navigation control
// //////////////////////////// //
function openMenu( event ){
	if( lastBlock == this ) {
		return;
	} else {
		$(".active").removeClass("active");

		if( lastBlock != null ) {
			$(lastBlock).animate({width: closedWidth+"px"}, { queue:false, duration:700, complete:function(){
					blockClosed();
				}});
		}
		lastBlock = this;
		$(lastBlock).animate({width: openWidth+"px"}, { queue:false, duration:700});
		$(lastBlock).children("a.nav_link").addClass("active");

		//track section open
		pageTracker._trackEvent("section", lastBlock.id.split('div_')[1]);
	}
}

function closeMenu( event ){
	event.stopPropagation()
	$(lastBlock).animate({width: closedWidth+"px"}, { queue:false, duration:700, complete:function(){
				blockClosed();
			} });
	lastBlock = null;
	$(".active").removeClass("active");
}

function blockClosed(){
	var id_str = "";

	if(lastBlock != null){
		id_str = lastBlock.id;
	}

	if(id_str != "div_portfolio"){
		notifySSP();
	}
	if(id_str != "div_contact"){
		resetContact();
	}
}

function openSection( str ){
	if($('#nav_' + str) != undefined){
		$('#nav_' + str).click();
	}
}

// //////////////////////////// //
//	Flash portfolio
// //////////////////////////// //
function createPortfolio() {
	var flashvars = {};
	var params = {
		wmode: "opaque"
	};
	var attributes = {
	  id: "portfolio",
	  name: "portfolio"
	};

	swfobject.embedSWF("slideshow.swf", "portfolio_holder", "600", "400", "9.0.115","expressInstall.swf", flashvars, params, attributes);
}

function notifySSP(){
	thisMovie("portfolio").notifySSP();
}

function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 }
 }

// //////////////////////////// //
//	Contact form validation and submit
// //////////////////////////// //
var form_sent = false;
$(function() {
	$('.error').hide();
	$('#contact_success').hide();
	$('.text-input').css({borderColor:"#2A2A2A"});
	$('.text-input').focus(function(){
		$(this).css({borderColor:"#FFF289"});
	});
	$('.text-input').blur(function(){
		$(this).css({borderColor:"#2A2A2A"});
	});

	$("#submit_btn").click(function() {
		// validate and process form
		// first hide any error messages
		$('.error').hide();
		var error_found = false;

		var message = $("textarea#message").val();
		if (message == "") {
			$("label#message_error").show();
			$("textarea#message").focus();
			error_found = true;
		}
		var email = $("input#email").val();
		if (email == "") {
			$("label#email_error").html("This field is required.");
			$("label#email_error").show();
			$("input#email").focus();
			error_found = true;
		}
		if (!echeck(email)) {
			$("label#email_error").html("Valid email address required.");
			$("label#email_error").show();
			$("input#email").focus();
			error_found = true;
		}
		var name = $("input#name").val();
		if (name == "") {
			$("label#name_error").show();
			$("input#name").focus();
			error_found = true;
		}

		if (error_found){
			return false;
		}

		var dataString = 'name='+ name + '&email=' + email + '&message=' + message;
		//alert (dataString);return false;

		$.ajax({
		type: "POST",
		url: "inc/contact_email.php",
		data: dataString,
		success: function() {
			$('#contact_form').hide();
			$('#contact_success p:first').html("I'll reply to you as soon as possible at " + email + ".");
			$('#contact_success').show();
			form_sent = true;
			}
		});
		return false;
	});
});


function echeck(str) {

	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		return false;
	 }

	 if (str.indexOf(" ")!=-1){
		return false;
	 }

	 return true;
}

function resetContact(){
	if(form_sent){
		form_sent = false;
		$('#contact_form').show();
		$("input#name").val("");
		$("input#email").val("");
		$("textarea#message").val("");
		$('#contact_success').hide();
	}
}

// //////////////////////////// //
//	Tracking
// //////////////////////////// //
function portfolioView( id_str ){
	pageTracker._trackEvent("portfolio", id_str);
}
