/* -----------------------------------------------
   JavaScript Framework v2.0
   Copyright to James Kirby - www.jameskirby.me.uk
   Creative Commons Attribution-Noncommercial-No Derivative Works 2.0 UK: England & Wales License (http://creativecommons.org/licenses/by-nc-nd/2.0/uk/)
----------------------------------------------- */

$(document).ready(function() {

  if (document.ondragstart) {
    document.ondragstart = function () {
      return false
    }
  }

	$('#content p').last().css('margin-bottom','0');
	$('#content .article-left p').last().css('margin-bottom','0');
	$('#content .article-right p').last().css('margin-bottom','0');
	$('#content .article-rightalt p').last().css('margin-bottom','0');

	var backgrounds = new Array(
		"James_Kirby_0330.jpg",
		"James_Kirby_0346.jpg",
		"James_Kirby_0348.jpg",
		"James_Kirby_0369.jpg",
		"James_Kirby_0435.jpg",
		"James_Kirby_0485.jpg",
		"James_Kirby_0590.jpg",
		"James_Kirby_0906.jpg",
		"James_Kirby_0968.jpg",
		"James_Kirby_1347.jpg",
		"James_Kirby_1511.jpg",
		"James_Kirby_1612.jpg",
		"James_Kirby_1672.jpg",
		"James_Kirby_1675.jpg",
		"James_Kirby_1843.jpg",
		"James_Kirby_2117.jpg",
		"James_Kirby_2146.jpg",
		"James_Kirby_2481.jpg",
		"James_Kirby_2886.jpg",
		"IMG_2496.jpg",
		"James_Kirby_2913.jpg",
		"James_Kirby_2991.jpg",
		"James_Kirby_3107.jpg",
		"James_Kirby_3153.jpg",
		"James_Kirby_3302.jpg",
		"James_Kirby_3520.jpg",
		"James_Kirby_3540.jpg",
		"James_Kirby_3788.jpg",
		"James_Kirby_3885.jpg",
		"James_Kirby_3964.jpg",
		"James_Kirby_4304.jpg",
		"James_Kirby_4435.jpg",
		"James_Kirby_4729.jpg",
		"James_Kirby_4757.jpg",
		"James_Kirby_4953.jpg",
		"James_Kirby_4986.jpg",
		"James_Kirby_4996.jpg",
		"James_Kirby_5647.jpg",
		"James_Kirby_5998.jpg",
		"James_Kirby_6128.jpg",
		"James_Kirby_6157.jpg",
		"James_Kirby_6209.jpg",
		"James_Kirby_6361.jpg",
		"James_Kirby_6484.jpg",
		"James_Kirby_6886.jpg",
		"James_Kirby_7238.jpg",
		"James_Kirby_7247.jpg",
		"James_Kirby_7414.jpg",
		"James_Kirby_7449.jpg",
		"James_Kirby_7598.jpg",
		"James_Kirby_7671.jpg",
		"James_Kirby_7834.jpg",
		"James_Kirby_8043.jpg",
		"James_Kirby_8131.jpg",
		"James_Kirby_8452.jpg",
		"James_Kirby_8875.jpg",
		"James_Kirby_9509.jpg",
		"James_Kirby_9670.jpg",
		"James_Kirby_9800.jpg",
		"IMG_0136.jpg",
		"IMG_0302.jpg",
		"IMG_3789.jpg"
	);

	var random = Math.floor( Math.random()*backgrounds.length );
  $.backstretch("/images/background/"+backgrounds[random], { speed:250 });

	setInterval(function() {
		var random = Math.floor( Math.random()*backgrounds.length );
		$.backstretch("/images/background/"+backgrounds[random], { speed:500 });
		$('#home').fadeOut();
	}, 10000);
	
	$('#home').click(function () {
		$('#home').fadeOut();
	});
	
	/* Model Window */
	$('a[name=modal]').click(function(e) {
		e.preventDefault();
		var id = $(this).attr('href');
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		/*$('#mask').fadeIn();*/
		$('#mask').fadeTo("slow",0.5);	
		var winH = $(window).height();
		var winW = $(window).width();
		$(id).css('top',winH/2-$(id).height()/2);
		$(id).css('left',winW/2-$(id).width()/2);
		$(id).fadeIn(); 
	});

	$('.window .close').click(function (e) {
		e.preventDefault();
		$('#mask').fadeOut();
		$('.window').fadeOut();
	});

	$('#mask').click(function () {
		$(this).fadeOut();
		$('.window').fadeOut();
	});
	
	sfHover();
	
	$('#home').delay(1500).fadeIn();
	
});

jQuery.event.add(window, "resize", resizeModel);

function resizeModel() {
	var winH = $(window).height();
	var winW = $(window).width();
	$("#dialog").css('top',winH/2-$("#dialog").height()/2);
	$("#dialog").css('left',winW/2-$("#dialog").width()/2);
	$('#mask').css({'width':winW,'height':winH});
}

/* ----------------------
   Scripts
---------------------- */

/* jQuery Backstretch - Version 1.2.5 */
(function(a){a.backstretch=function(l,b,j){function m(c){try{h={left:0,top:0},e=f.width(),d=e/k,d>=f.height()?(i=(d-f.height())/2,g.centeredY&&a.extend(h,{bottom:"-"+i+"px"})):(d=f.height(),e=d*k,i=(e-f.width())/2,g.centeredX&&a.extend(h,{left:"-"+i+"px"})),a("#backstretch, #backstretch img:not(.deleteable)").width(e).height(d).filter("img").css(h)}catch(b){}"function"==typeof c&&c()}var n={centeredX:!0,centeredY:!0,speed:0},c=a("#backstretch"),g=c.data("settings")||n;c.data("settings");var f="onorientationchange"in window?a(document):a(window),k,e,d,i,h;b&&"object"==typeof b&&a.extend(g,b);b&&"function"==typeof b&&(j=b);a(document).ready(function(){if(l){var b;0==c.length?c=a("<div />").attr("id","backstretch").css({left:0,bottom:-82,position:"fixed",overflow:"hidden",zIndex:-999999,margin:0,padding:0,height:"100%",width:"100%"}):c.find("img").addClass("deleteable");b=a("<img />").css({position:"absolute",display:"none",margin:0,padding:0,border:"none",zIndex:-999999}).bind("load",function(b){var d=a(this),e;d.css({width:"auto",height:"auto"});e=this.width||a(b.target).width();b=this.height||a(b.target).height();k=e/b;m(function(){d.fadeIn(g.speed,function(){c.find(".deleteable").remove();"function"==typeof j&&j()})})}).appendTo(c);0==a("body #backstretch").length&&a("body").append(c);c.data("settings",g);b.attr("src",l);a(window).resize(m)}});return this}})(jQuery);

function sfHover() {
	if (document.all&&document.getElementById) {
		var sfEls = document.getElementById("nav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" over";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" over\\b"), "");
			}
		}
	}
}

/* ----------------------
   Contact Functions
---------------------- */

$(document).ready(function() {

	///////////////////////////////////

	$("#formFirstName").blur(function() {
		var formFirstNameError = false;
 		if ( $(this).val().length < 3 ) {
 			formFirstNameError = true;
 		}
 		if ( formFirstNameError == true ) {
 			$("#formFirstName").css('background-color','#FFDADA');
 			$("#formFirstNameError").css('color','#ff5252');
 		} else {
 			$("#formFirstName").css('background-color','#FFF');
 			$("#formFirstNameError").css('color','#666');
 		}
	});
	
	
	///////////////////////////////////
	
	$("#formLastName").blur(function() {
		var formLastNameError = false;
 		if ( $(this).val().length < 3 ) {
 			formLastNameError = true;
 		}
 		if ( formLastNameError == true ) {
 			$("#formLastName").css('background-color','#FFDADA');
 			$("#formLastNameError").css('color','#ff5252');
 		} else {
 			$("#formLastName").css('background-color','#FFF');
 			$("#formLastNameError").css('color','#666');
 		}
	});
	
	
	///////////////////////////////////
	
	$("#formEmail").blur(function() {
		var formEmailError = false;
 		if ( echeck($(this).val()) == false ) {
 			formEmailError = true;
 		}
 		if ( formEmailError == true ) {
 			$("#formEmail").css('background-color','#FFDADA');
 			$("#formEmailError").css('color','#ff5252');
 		} else {
 			$("#formEmail").css('background-color','#FFF');
 			$("#formEmailError").css('color','#666');
 		}
	});
	
	
	///////////////////////////////////
	
	$("#formMessage").blur(function() {
		var formMessageError = false;
 		if ( $(this).val().length < 3 ) {
 			formMessageError = true;
 		}
 		if ( formMessageError == true ) {
 			$("#formMessage").css('background-color','#FFDADA');
 			$("#formMessageError").css('color','#ff5252');
 		} else {
 			$("#formMessage").css('background-color','#FFF');
 			$("#formMessageError").css('color','#666');
 		}
	});
	
	///////////////////////////////////
	
	$("#submit").click(function() {
		formSubmitError = false;
		
		if ( $("#formFirstName").val().length < 3 ) {
			formSubmitError = true;
		}
		if ( $("#formLastName").val().length < 3 ) {
			formSubmitError = true;
		}
		if ( echeck($("#formEmail").val()) == false ) {
			formSubmitError = true;
		}
		if ( $("#formMessage").val().length < 3 ) {
			formSubmitError = true;
		}
		if ( formSubmitError == true ) {
			return false;
		} else {
			return true;
		}
	});
});

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;				
}
