jQuery(document).ready(function($){

	/* add eco-friendly strip */
	$('div#container').before('<p id="eco"><a href="' + path_to_root + '/services/eco-friendly/">We\'re eco-friendly</a></p>');


	
	



	/* Menu behaviour */
	$('ul#nav').superfish({
		delay: 300                            // one second delay on mouseout 
	});
	$('ul#nav a').removeAttr('title');		 // remove title attributes ... they conflict with menu visibility and add no further information
	


	$('li#nav-portfolio > a, li#nav-portfolio ul').hover(
		function () {
			hover_menu('portfolio');
		},
		function () {
			hover_out('portfolio');
		}
	);
	$('li#nav-services > a, li#nav-services ul').hover(
		function () {
			hover_menu('services');
		},
		function () {
			hover_out('services');
		}
	);
	$('li#nav-klog a').hover(
		function () {
			hover_menu('klog');
		},
		function () {
			hover_out('klog');
		}
	);
	$('li#nav-contact > a, li#nav-contact ul').hover(
		function () {
			hover_menu('contact');
		},
		function () {
			hover_out('contact');
		}
	);
	$('li#nav-frontpage a').hover(
		function () {
			hover_menu('home');
		},
		function () {
			hover_out('home');
		}
	);
	
	
	// we want the 'contact' section to be both the parent and a child
	$('li#nav-contact-careers').before('<li id="nav-contact-contact"><a href="' + path_to_root + '/contact/">Contact Us</a></li>');
	
	













var h1 = '0';
var h2 = '-78';
var h3 = '-151';
var h4 = '-200';
var h5 = '-269';
var v1 = '0';
var v2 = '-105';
var v3 = '-210';

function reset_states() {
	$('body#portfolio li#nav-portfolio a, body#web-design li#nav-portfolio a, body#logos-branding li#nav-portfolio a, body#graphic-design li#nav-portfolio a').css({'background-position' : h1+'px '+v1+'px'});
	$('body#services li#nav-services a, body#design li#nav-services a, body#web-development li#nav-services a, body#internet-marketing li#nav-services a, body#extras li#nav-services a, body#eco-friendly li#nav-services a').css({'background-position' : h2+'px '+v1+'px'});
	$('body#klog li#nav-klog a').css({'background-position' : h3+'px '+v1+'px'});
	$('body#contact li#nav-contact a, body#careers li#nav-contact a').css({'background-position' : h4+'px '+v1+'px'});
	$('body#home li#nav-frontpage a').css({'background-position' : h5+'px '+v1+'px'});
}



function restore_states() {
	$('body#portfolio li#nav-portfolio a, body#web-design li#nav-portfolio a, body#logos-branding li#nav-portfolio a, body#graphic-design li#nav-portfolio a').css({'background-position' : h1+'px '+v3+'px'});
	$('body#services li#nav-services a, body#design li#nav-services a, body#web-development li#nav-services a, body#internet-marketing li#nav-services a, body#extras li#nav-services a, body#eco-friendly li#nav-services a').css({'background-position' : h2+'px '+v3+'px'});
	$('body#klog li#nav-klog a').css({'background-position' : h3+'px '+v3+'px'});
	$('body#contact li#nav-contact a, body#careers li#nav-contact a').css({'background-position' : h4+'px '+v3+'px'});
	$('body#home li#nav-frontpage a').css({'background-position' : h5+'px '+v3+'px'});
}

function hover_menu(current) {
	
		var ul_1 = '0';
		var ul_2 = '-192';
		var ul_3 = '-384';
		var ul_4 = '-576';
		var ul_5 = '-768';
		var ul_position = ul_5;
		
		
		reset_states();		

		if(current == 'portfolio') {
			ul_position = ul_1;
			$('ul#nav li#nav-portfolio > a').css({'background-position' : h1+'px '+v3+'px'});
		} else if (current == 'services') {
			ul_position = ul_2;
			$('ul#nav li#nav-services > a').css({'background-position' : h2+'px '+v3+'px'});
		} else if (current == 'klog') {
			ul_position = ul_3;
			$('ul#nav li#nav-klog > a').css({'background-position' : h3+'px '+v3+'px'});
		} else if (current == 'contact') {
			ul_position = ul_4;
			$('ul#nav li#nav-contact > a').css({'background-position' : h4+'px '+v3+'px'});
		} else {
			ul_position = ul_5;
		}

		$('ul#nav').css({'background-position' : '0 '+ul_position+'px'}); // show handwritten image
}


function hover_out(current) {
	

		$('ul#nav').css({'background-position' : '-999px -999px'});

		if(current == 'portfolio') {
			$('ul#nav li#nav-portfolio > a').css({'background-position' : h1+'px '+v1+'px'});
		} else if (current == 'services') {
			$('ul#nav li#nav-services > a').css({'background-position' : h2+'px '+v1+'px'});
		} else if (current == 'klog') {
			$('ul#nav li#nav-klog > a').css({'background-position' : h3+'px '+v1+'px'});
		} else if (current == 'contact') {
			$('ul#nav li#nav-contact > a').css({'background-position' : h4+'px '+v1+'px'});
		}
		
		restore_states();
}


	/* live-validate email business form field */
	
	var validator = $("#mc-embedded-subscribe-form")
	.validate({
		debug: false,
		errorElement: "small",
		success: function(label) {
			label.text("Great!").addClass("awesome");
		},
		rules: {
			EMAIL: {
				required:true,
				email: true
			},
			FNAME: {
				required:true
			}
		},
		messages: {
			EMAIL: "Oops! we need an email",
			FNAME: "That ain't no name :)"
		}
	});
	



	/*
	---------------------------------------
	add label to newsletter, search and comment forms
	---------------------------------------	
	*/
	$("form#mc-embedded-subscribe-form input#EMAIL").focus(function(){
			$(this).css('background-position', '0 0');
	});	
	$("form#mc-embedded-subscribe-form input#EMAIL").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -127px');
		}
	});
	
	$("form#mc-embedded-subscribe-form input#FNAME").focus(function(){
			$(this).css('background-position', '0 0');
	});	
	$("form#mc-embedded-subscribe-form input#FNAME").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -96px');
		}
	});

	$("form#searchform input#s").focus(function(){
			$(this).css('background-position', '0 0');
	});
	$("form#searchform input#s").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -384px');
		}
	});


	$("form#commentform input#author").focus(function(){
			$(this).css('background-position', '0 0');
	});	
	$("form#commentform input#author").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -96px');
		}
	});
	$("form#commentform input#email").focus(function(){
			$(this).css('background-position', '0 0');
	});	
	$("form#commentform input#email").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -129px');
		}
	});
	$("form#commentform input#url").focus(function(){
			$(this).css('background-position', '0 0');
	});	
	$("form#commentform input#url").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 0');
		} else {
			$(this).css('background-position', '10px -225px');
		}
	});
	$("form#commentform textarea#comment").focus(function(){
			$(this).css('background-position', '0 -10000px');
	});	
	$("form#commentform textarea#comment").blur(function(){
		if ($(this).val() != '') {
			$(this).css('background-position', '0 -10000px');
		} else {
			$(this).css('background-position', '10px -352px');
		}
	});
	
	
	
	
	
	
	
	
	
	
	



	$("a[rel*=external]").attr('target','_blank');
	
	
	
	$('a[@rel*=prettyPhoto]').prettyPhoto({
		animationSpeed: 'fast', 
		padding: 10,
		opacity: 0.35,
		showTitle: false,
		allowresize: false,
		theme: 'light_rounded'
	});
	
	
	$("div#contact-vcard p#download a").hover(
		function () {
			$("div#contact-vcard").css({'background-position' : '-228px -238px'});
		},
		function () {
			$("div#contact-vcard").css({'background-position' : '0 -238px'});
	   }		
	);
	
	
	$('div#sidebar ul.twitter li:last-child').attr('id', 'frag-ie');


	
});
