$(window).load(function(){
		
	// home page only
	if(jQuery.fn.showhide){
		if($('#tabs a.showhide').length > 0 ){
			$('a.showhide').showhide({
				animate:true,
				event:'click'
			});
		}
		
	}
	
	if(jQuery.fn.inputfocus){
		if($('.pseudo-footer .form-container .input, .pseudo-footer .form-container .textarea')){
			$('.pseudo-footer .form-container .input, .pseudo-footer .form-container .textarea').inputfocus({
				bgColourFocus:null,
				bgColour:null,			
				animate:true
			});
		}
	}
	
	// home page only
	if(jQuery.fn.cycle){
	
		if($('#carousel, #client-slideshow, #slideshow').length > 0){
			
			// add prev and next animation  for cycle plugin
			$.fn.cycle.transitions.scrollBothWays=function($cont,$slides,opts){$cont.css('overflow','hidden');opts.before.push($.fn.cycle.commonReset);opts.fxFn=function(curr,next,opts,cb,fwd){var w=$cont.width();opts.cssFirst={left:0};opts.animIn={left:0};if(fwd){opts.cssBefore={left:w,top:0};opts.animOut={left:0-w}}else{opts.cssBefore={left:-w,top:0};opts.animOut={left:w}};var $l=$(curr),$n=$(next);var speedIn=opts.speedIn,speedOut=opts.speedOut,easeIn=opts.easeIn,easeOut=opts.easeOut,animOut=opts.animOut,animIn=opts.animIn;$n.css(opts.cssBefore);var fn=function(){$n.show();$n.animate(animIn,speedIn,easeIn,cb)};$l.animate(animOut,speedOut,easeOut,function(){if(opts.cssAfter)$l.css(opts.cssAfter);if(!opts.sync)fn()});if(opts.sync)fn()}};
								
			function onBefore(currSlideElement, nextSlideElement, options, forwardFlag) { 
			    /*$(outgoingSlide).children('div.overlay').animate({bottom:-80}, {easing: 'easeInCubic'},{duration:100}, function(){$(this).remove();});*/
				$(currSlideElement).children('div.featured-caption').animate({right:0}, {easing: 'linear'},{duration:200});
				//alert($(incomingSlide).children('div.featured-caption').html());
			} 
			function onAfter(currSlideElement, nextSlideElement, options, forwardFlag) { 
				// console.log(options);
				// apply href to view project
				if($(this).children('a').attr('href').length > 0){
					var link = $(this).children('a').attr('href');
					// console.log($(this).parent('ul'));
					// if a link exists then apply
					if(typeof(link) != 'undefined' && link.length > 0){
						
						if($(this).parent('ul').parent('div#carousel').length > 0){				
							var viewProjectLinkWrapper = $(this).parent('ul').siblings('#view-project');
							var link_text = 'View Project';
						}else if($(this).parent('ul').parent('div#client-slideshow').length > 0){
							var viewProjectLinkWrapper = $(this).parent('ul').siblings('#magnifying-glass');
							var link_text = 'View related case study';
						}
						// remove old link
						viewProjectLinkWrapper.html('');
						// create new link element
						var new_link = $('<a></a>');
						new_link.attr('href', link);
						new_link.attr('title', "Open link to "+link+" in a new window.");
						new_link.text(link_text);			
						// bind click event
						/*new_link.bind('click', function(){						
							window.open(link,'_blank');
							return false;
						});*/
						// append link
						viewProjectLinkWrapper.append(new_link);
					}
				}
				
				// animate caption
				$(this).children('div.featured-caption').animate({right:0}, {easing: 'easeInCubic'},{duration:100});
				
			}
			
			if($('#carousel').length > 0){
				// reset right offset for all captions
				$('div.featured-caption').css({'right':'0'});
				
				$('#carousel ul').cycle({ 
				    fx:     'scrollBothWays', 
				    timeout: 5000,
				    speed: 300,
				    pause: 1, // enable pause on hover
				    pager:  '#featured-projects',
				    cleartype: true,
				    cleartypeNoBg: true,
					wrap : 'circular',  
				    before:  onBefore, 
				    after:   onAfter
				});
			}
			
			if($('#client-slideshow').length > 0){
				$('#client-slideshow ul').cycle({ 
				    fx:     'fade', 
				    timeout: 5000,
				    speed: 300,
				    pause: 1, // enable pause on hover
				    cleartype: true,
				    cleartypeNoBg: true,
					wrap : 'circular',  
				    before:  onBefore, 
				    after:   onAfter
				});
			}
			
			if($('#slideshow').length > 0){
				$('#slideshow ul').cycle({ 
				    fx:     'scrollBothWays', 
				    timeout: 5000,
				    speed: 300,
				    pause: 1, // enable pause on hover
				    pager:  '#slideshow-nav',
				    cleartype: true,
				    cleartypeNoBg: true,
					wrap : 'circular'
				});
			}
		}
	}
	
	//apply form validation
 	function jqueryValidate(){
		
		if(jQuery.fn.validate){
			
			$('label.error').live('errorFadeIn', function(e){
					$(this).animate({
						opacity: 1
					}, 400);
			});
			
			$('label.error').live('errorFadeOut', function(e){
					$(this).animate({
						opacity: 0
					}, 400);
			});
			
			//setup default actions for validate plugin
			$.validator.setDefaults({
				submitHandler: function(form) {
					//on submit make sure that any inputs that have the same value as their label then blank the value
					
					//pass form id to local var
					var formId = jQuery(form).attr('id');

					//iterate through submit elements
					$('#'+formId+' input[type=text],select,textarea,radio,checkbox').each(function(){
						
						//get current_input 
						var current_input = $(this);
						//get label for current_input 
						var current_label = $('label[for=' + current_input.attr('id') + ']');
						//check that the element is either a text or textarea
						if(current_input.attr('type') == 'text' || current_input.attr('cols')){
							//if label and input the same then blank the value
							if(current_label.text() == current_input.val()){	
								current_input.val('');
							}
						}
					});
					
					//submit form
					form.submit();	
				},
				highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {							
						$(element).css('outline','1px solid #525252'); //green background for valid data
						$(element).siblings('label.error').trigger("errorFadeIn");
					});
				},					
				unhighlight: function(element, errorClass) {
					$(element).animate({
						opacity: 1.0
					}, 400, function () {
						$(element).css('background','url("/css/img/footer/pseaudo-footer-ul-li-bg.png") repeat scroll 0 0 transparent');
						$(element).css('color', '#A6E1DA');
						$(element).css('outline','none'); //green background for valid data
						$(element).siblings('label.error').trigger("errorFadeOut");
					});
					
				}			

			});
			
			$.validator.addMethod("notLabelText",
					
				function(value, element) {
					// compate elements value with its label 
					// excluding the error label generated by the validation script
					// usin jQuery selector filter element[attribute!=value]
					var result = ($(element).val() == $('label[for=' + $(element).attr('id') + '][generated!=true]').text()) ? false : true;
					return result;
				}, "Please enter a value.");


			if($('#hire-us-form').length > 0 ){ // check if element is on page
				
				$("#hire-us-form").validate({
					rules: {
						hire_us_name: {  
							required: true          
						},
						hire_us_phone: {  
							required: true,  
							phoneUK: true          
						},
						hire_us_email: {
							required: true,
							email: true
						}
					},
					messages: {
						hire_us_name: "Please enter your name.",
						hire_us_email: {
									required: "Please enter your email.",
									email: "Email address is invalid."
								   },
						hire_us_phone: {
										required: "Please enter a phone number.",
										phoneUK: "Invalid phone number."
									   }
					}

				});
			}
			if($('#work-for-us-form').length > 0 ){ // check if element is on page
				
				$("#work-for-us-form").validate({
					rules: {
						work_for_us_name: {  
							required: true          
						},
						work_for_us_phone: {  
							required: true,  
							phoneUK: true          
						},
						work_for_us_email: {
							required: true,
							email: true
						}
					},
					messages: {
						work_for_us_name: "Please enter your name.",
						work_for_us_email: {
									required: "Please enter your email.",
									email: "Email address is invalid."
								   },
						work_for_us_phone: {
										required: "Please enter a phone number.",
										phoneUK: "Invalid phone number."
									   }
					}

				});
			}
			
			if($('#comment-form').length > 0 ){ // check if element is on page
				
				$("#comment-form").validate({
					rules: {
						name: {  
							required: true,  
							notLabelText: true            
          
						},
						email: {
							required: true,
							email: true,  
							notLabelText: true            

						},
						comment: {  
							required: true,  
							notLabelText: true            
          
						}
					},
					messages: {
						name: "Please enter your name.",
						email: {
									required: "Please enter your email.",
									email: "Email address is invalid."
								   },
						comment: "Please enter your comment."
					}

				});
			}
			
			if($('#SiteContactForm').length > 0 ){ // check if element is on page
				
				$("#SiteContactForm").validate({
					highlight: function(element, errorClass) {
					$(element).animate({
						opacity: 0.9
					}, 400, function () {							
						$(element).css('outline','1px solid #FFF'); //green background for valid data
					});
				},
					unhighlight: function(element, errorClass) {
						$(element).animate({
							opacity: 1.0
						}, 400, function () {
							$(element).css('background-color','#FF003C');
							$(element).css('color', '#FFF');
							$(element).css('outline','none'); //green background for valid data
						});
					},
					rules: {
						txt_name: {  
							required: true,  
							notLabelText: true            
          
						},
						txt_email: {
							required: true,
							email: true,  
							notLabelText: true            

						},
						txt_enquiry: {  
							required: true,  
							notLabelText: true            
          
						}
					},
					messages: {
						txt_name: "Please enter your name.",
						txt_email: {
									required: "Please enter your email.",
									email: "Email address is invalid."
								   },
						txt_enquiry: "Please enter your comment."
					}

				});
			}
		}
	}

	//fire off validation setup
	jqueryValidate();

	 
	
	// homeage only
	if($('#slide-down-button a').length > 0){
	
		function slideDown(){
			
			$('#slide-down-button a').addClass('animating');
			
			$('#slide-down-wrapper').stop().animate(
					{ top: -215 }, // what we are animating
					{
						duration: 300, // how fast we are animating
						easing: 'easeInCubic', // the type of easing
						complete: function() { // the callback
							// Animation complete.
							$('#slide-down-button a').removeClass('animating').addClass('active');
							// next push the site wrapper down
							$(this).animate({'top':'0px'});
							$('.header-inner').animate({'padding-top':'277px'});
					   	}
				});
			
		}
		
		function slideUp(){
			
			$('#slide-down-button a').addClass('animating');
			
			$('#slide-down-wrapper').stop().animate(
					{ top: -321 }, // what we are animating
					{
						duration: 300, // how fast we are animating
						easing: 'easeInCubic', // the type of easing
						complete: function() { // the callback
							// Animation complete.						
							$('#slide-down-button a').removeClass('active');
							$('#slide-down-button a').removeClass('animating')
							// next pull the site wrapper up
							$('.header-inner').animate({'padding-top':'0'});
					   	}
			});
		}
		
		// bind click behaviour to contact link
		$('#slide-down-button a').bind('click', function(){
		
			if($(this).hasClass('active')){
				// slide up if active
				slideUp();			
			}else{
				// slide down
				slideDown();
			}
			
			return false;
		});
		
	}

	// what we do page only	
	if($('#what-we-do #main #main-center-panel ul.listing li div.content').length > 0){ // check if element is on page
		$('#what-we-do #main #main-center-panel ul.listing li').each(function(){
			
            var time = 250;
            var hideDelay = 500;
 			
            var hideDelayTimer = null;
 
            var beingShown = false;
            var shown = false;
			
			var details = $(this).children('div.content');
			
			if($(this).hasClass('nofade') == false){				
				
				//handle parent / child events
				$(this).mouseover(function(){
					
					$(this).addClass('active');
					if (hideDelayTimer) clearTimeout(hideDelayTimer);					   
					if (beingShown || shown) {
						// don't trigger the animation again
						return;
					} else {
						 // reset position of info box
                   		beingShown = true;
					
						// reset position of info box based on sttyle sheet values
						details.css({
							display: 'block'
						}).animate({bottom: 0}, 500, 'easeInCubic', function() {
							beingShown = false;
							shown = true;
						});
					}
					
					return false;
					
				}).mouseout(function(){					
					
					if (hideDelayTimer) clearTimeout(hideDelayTimer);
					hideDelayTimer = setTimeout(function () {
				   		hideDelayTimer = null;
						details.animate({bottom: -226}, time, 'easeInCubic', function () {
							shown = false;
							$(this).parent('li:first').removeClass('active');
						});
					}, hideDelay);
					
					return false;
				});
				
			}
		});
	}
		
	if( $(window).width() > 767) {	// our work page only	
		if($('ul.case-studies li div.caption').length > 0){ // check if element is on page
			
			$('ul.case-studies li').each(function(){
	
	            var time = 250;
	            var hideDelay = 400;
	 			
	            var hideDelayTimer = null;
	 
	            var beingShown = false;
	            var shown = false;
				
				var details = $(this).children('div.caption');
				// create underlay div
				var underlay = $('<div class="underlay" />');
				
				// add in underlay div
				$(this).prepend(underlay);
				
				if( $(window).width() > 767) {	// our work page only	
					details.css({'bottom': -111});
				}
				if( $(window).width() > 991) {	// our work page only	
					details.css({'bottom': -140});
				}				
				
				//create the rollover image path based on the css background-image
				var bgImage = $(this).css('background-image');                                 //grab backgound-image url
				if(bgImage.length > 0){		
					var extension = bgImage.substring(bgImage.lastIndexOf("."),bgImage.length); //grab file extension attribute 
					var imgPath = bgImage.substring(0,bgImage.lastIndexOf("."));            //extract path to img by clipping extension of bgImage. imgs are in same dir
					var imgColourPath = imgPath.replace('-grey','') + extension;      //replace('subject', 'replacement') to create img path
				}
				
				if($(this).hasClass('nofade') == false){				
					
					//handle parent / child events
					$(this).mouseover(function(){
						
						$(this).addClass('active');
						if (hideDelayTimer) clearTimeout(hideDelayTimer);					   
						if (beingShown || shown) {
							// don't trigger the animation again
							return;
						} else {
							 // reset position of info box
	                   		beingShown = true;
						
							// reset position of info box based on sttyle sheet values
							details.css({
								display: 'block'
							}).stop().animate({bottom: 0}, 400, 'easeInCubic', function() {
								beingShown = false;
								shown = true;
								underlay.css({'opacity':0, 'display':'block', 'background-image':imgColourPath}).animate({opacity:1},500);
							});
						}
						
						return false;
						
					}).mouseout(function(){					
						
						if (hideDelayTimer) clearTimeout(hideDelayTimer);
						hideDelayTimer = setTimeout(function () {
					   		hideDelayTimer = null;
							details.stop().animate({bottom: -143}, time, 'easeInCubic', function () {
								shown = false;
								$(this).parent('li:first').removeClass('active');
								underlay.animate({opacity:0},500, null, function() {
									$(this).css({'display':'none'});
								});
							});
						}, hideDelay);
						
						return false;
					});
					
				}
			});
		}
	}
	
	
	if(jQuery.fn.showhide){
		
		// modern webkit browsers as hover is used for the animation
		if($('.cssanimations ul#process-nav li a.showhide').length > 0 ){
			$('a.showhide').showhide({
				animate:true,
				event:'click'
			});
		}
		// older browsers
		if($('.no-cssanimations ul#process-nav li a.showhide').length > 0 ){
			$('a.showhide').showhide({
				animate:true,
				event:'mouseover'
			});
		}
		
	}
	
});
