$(document).ready(function(){

jQuery("#footer_middle_wrapper").hide();
	//start with footer middle hidden
	
jQuery(".narrow .caption").hide();
	//start with footer middle hidden
	

if($.browser.msie && $.browser.version=="7.0" || $.browser.msie && $.browser.version=="6.0"){
	$("#tab_share").css({visibility: 'hidden'});
	$("#tab_brief").css({visibility: 'hidden'});
};

// position tabs based on page hieght
var middlePageHeight = $("#wrapper").height() - 370;
var halfMiddlePageHeight = middlePageHeight/2;
$("#tab_share").css({top: halfMiddlePageHeight + 125});
$("#tab_brief").css({top: halfMiddlePageHeight + 10});

// security for brief tab form
$('<p class="first">Is there a project you would like us to do?</p>').insertBefore('.warning');
$('<p>Please send us a brief using the form below</p>').insertBefore('.warning');
$('.warning').remove();
$.get("php/token.php",function(txt){
  $(".secure").append('<input type="hidden" name="ts" value="'+txt+'" />');
});
	
// header color fade animation______________________________________________________________

	$("#navigation li").stop().hover(function() {

		},function() {

		});
    

//header nav animations__________________________________________________________________
	
//home
  $('#navigation li.home').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.home img').css({visibility: 'visible'});
				$('#nav_icons li.home').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.home').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.home img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
	//about us  
  $('#navigation li.aboutus').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.aboutus img').css({visibility: 'visible'});
				$('#nav_icons li.aboutus').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.aboutus').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.aboutus img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  	//services  
  $('#navigation li.services').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.services img').css({visibility: 'visible'});
				$('#nav_icons li.services').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.services').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.services img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  
  //work 
  $('#navigation li.work').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.work img').css({visibility: 'visible'});
				$('#nav_icons li.work').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.work').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.work img').css({visibility: 'hidden'}); 
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  //news 
  $('#navigation li.news').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.news img').css({visibility: 'visible'});
				$('#nav_icons li.news').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' );
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.news').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.news img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  //jobs
  $('#navigation li.jobs').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.jobs img').css({visibility: 'visible'});
				$('#nav_icons li.jobs').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.jobs').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.jobs img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  //contact us 
  $('#navigation li.contact').hover(function(){
		if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.contact img').css({visibility: 'visible'});
				$('#nav_icons li.contact').animate({marginTop: '-=22px'}, 200);
				$('#nav_icons').css( 'cursor', 'pointer' ); 
				$(this).children("a").stop().animate({ color: "#EC098D" }, 200);
		};	
	  }, function(){
		 if ($(this).hasClass('notCurrent')) {
				$('#nav_icons li.contact').animate({marginTop: '+=22px'}, 200);
				$('#nav_icons li.contact img').css({visibility: 'hidden'});
				$(this).children("a").animate({ color: "#FFFFFF" }, 200);
		};
	});
  
  //END header nav animations__________________________________________________________________
  
  //mouse over box sections in body _______________________________________________________________________

	$(".section a").hover(function() {
            $(this).children("span.caption").animate({opacity: "show", bottom: "0"}, "slow");
        }, function() {
            $(this).children("span.caption").animate({opacity: "hide", bottom: "-20"}, "fast");
        });
    
	  //mouse over news article summarys _______________________________________________________________________

	$("a.article_summary").hover(function() {
            $(this).find(".more").addClass('hover');
        }, function() {
            $(this).find(".more").removeClass('hover');
        }); // actives a css class to change background arrow
  
  // START SLIDER __________________________________________________________________
  
  
  //Show the paging and activate its first link
	$(".paging").show();
	$(".paging a:first").addClass("active");
	
	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging  and Slider Function
	rotate = function(){
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
	
		//Slider Animation
		$(".image_reel").animate({
			left: -image_reelPosition
		}, 700 );
	
	}; 
	
	//Rotation  and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself 
			$active = $('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 5000); //Timer speed in milliseconds 
	};
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});	
	
	//On Click
	$(".paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
	
		
	$(".next a").click(function() {
		
			clearInterval(play); //Stop the rotation
            $active = $('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
			rotateSwitch(); // Resume rotation timer
			return false; //Prevent browser jump to link anchor
	});
		
	$(".previous a").click(function() {
		
			clearInterval(play); //Stop the rotation
            $active = $('.paging a.active').prev(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:last'); //go back to first
			}
			rotate(); //Trigger the paging and slider function	
			rotateSwitch(); // Resume rotation timer
			return false; //Prevent browser jump to link anchor
	});
	
	var viewingScreen = false;
	// pause when user leaves tab
	$(window).focus(function() {
			if (viewingScreen == false){
				$viewingScreen = true;
				rotateSwitch(); // Resume rotation timer
			}
		});
	
		$(window).blur(function() {
			clearInterval(play); //Stop the rotation;
			$viewingScreen = false;
		});
  
   // END SLIDER __________________________________________________________________
   
   // allows styling of file button. see http://jstnjns.com/jquery/fileinput/
   // note this referes to another jQuery file where the fileInput function resides
   $('input[type="file"]').fileInput();
   
   // tab_brief ____________________________________________________________________________________
	$("#tab_brief_open, #tab_share_open, #tab_brief_close, #tab_share_close").hover(function() {
            $(this).addClass('hover');
        }, function() {
            $(this).removeClass('hover');
        }); // actives a css class to change background position
	
	$("#tab_brief_open, #tab_brief_close").click(function() {		
			if($("#tab_brief").hasClass('normal'))
				{ 
				$("#tab_brief").addClass('slid_out').removeClass('normal').css({zIndex: '20'});
				$("#tab_share").css({zIndex: '5'}); //Stops both tabs being above the blackout
				$("#blackout").fadeIn("slow");
				$("#tab_brief").animate({left: "-50"}, "slow");
				$("#tab_brief_open").removeClass('hover');
				clearInterval(play); //Stop the slideshow rotation
				return false; //Prevent browser jump to link anchor. Should come last in jquery as can affect statements after it
				}
			else
				{  
				$("#tab_brief").addClass('normal').removeClass('slid_out');
				$("#blackout").fadeOut("slow");
				$("#tab_brief").animate({left: "-702"}, "slow");
				$("#tab_brief_open").removeClass('hover');
				rotateSwitch(); //Resume slideshow rotation timer
				return false; //Prevent browser jump to link anchor. Should come last in jquery as can affect statements after it
				}			
        });
	

// tab_share ____________________________________________________________________________________
	$("#tab_share_open, #tab_share_close").click(function() {		
			if($("#tab_share").hasClass('normal'))
				{ 
				$("#tab_share .inner").fadeIn("fast");
				$("#tab_share .inner").css({visibility: 'visible'});
				$("#tab_share .inner").animate({width: "386"}, "slow");
				$("#tab_share").addClass('slid_out').removeClass('normal').css({zIndex: '20'}).css({display: 'visible'});
				$("#tab_brief").css({zIndex: '5'}); //Stops both tabs being above the blackout
				$("#blackout").fadeIn("slow");				
				$("#tab_share_open").removeClass('hover');
				clearInterval(play); //Stop the slideshow rotation
				return false; //Prevent browser jump to link anchor. Should come last in jquery as can affect statements after it
				}
			else
				{  
				close_tab_share(); //Trigger the paging and slider function
				return false; //Prevent browser jump to link anchor. Should come last in jquery as can affect statements after it 
				}			
        });
	
	$("#tab_share .inner").fadeOut("fast"); //ensures the share tab doesn't block whats underneath it
	
	close_tab_share = function(){
		$("#blackout").fadeOut("slow");
		$("#tab_share").addClass('normal').removeClass('slid_out');
		$("#tab_share_open").removeClass('hover');
		$("#tab_share .inner").queue("myqueue", function(next) {
			$(this).animate({width: "0"}, "slow");
			$(this).fadeOut("fast");
			})
		.dequeue("myqueue");
		rotateSwitch(); //Resume slideshow rotation timer
	};
	
	// tab share icon bounces
	
	 $('.addthis_toolbox .custom_images a').hover(function(){											 	
		$(this).animate({marginTop: '-=10px'}, 300, 'easeInOutBack');
		$(this).animate({paddingBottom: '+=10px'}, 300, 'easeInOutBack');
		  }, function(){
			$(this).animate({paddingBottom: '-=10px'}, 200);
			$(this).animate({marginTop: '+=10px'}, 200);
	  });

	
	 
   
   //footer____________________________________________________________________________________
		
	$('#footer_top').click(function(){
		  
        if($("#footer_bottom_wrapper").hasClass('expanded'))
        {          
			jQuery("#footer_middle_wrapper").slideUp(1000, function(){
			$("#footer_bottom_wrapper").addClass('collapsed').removeClass('expanded');
			});
        }
        else
        {
            jQuery("#footer_middle_wrapper").slideDown(1000, function(){
			$("html,body").animate({scrollTop: $(document).height()}, 2000);
			});
			$("#footer_bottom_wrapper").addClass('expanded').removeClass('collapsed');			
		 }
    });
	
    // footer - underline when fake links are hovered over
	$("#footer_top_left").hover(function() {
			$(this).css('cursor','pointer');
			$("#footer_top_left h4.hand").css({textDecoration: 'underline'});
        }, function() {
			$(this).css('cursor','auto');
			$("#footer_top_left h4.hand").css({textDecoration: 'none'});
        });
	
	$("#footer_top_right").hover(function() {
			$(this).css('cursor','pointer');
			$("#footer_top_right h4.newsletter").css({textDecoration: 'underline'});
        }, function() {
			$(this).css('cursor','auto');
			$("#footer_top_right h4.newsletter").css({textDecoration: 'none'});
        });
	
  
});

//______________________________	NON JQUERY JAVASCRIPT  ___________________________________________________

	// validate newsletter form

	function validateContactForm()
		{						  
			if ( document.forms["contact_form"]["name"].value == "" )
			{
				alert ( "Please enter your name" );
				return false;
			}
			
			var x=document.forms["contact_form"]["email"].value
			var atpos=x.indexOf("@");
			var dotpos=x.lastIndexOf(".");
			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
			  {
			  alert("Please enter a valid e-mail address");
			  return false;
			  }
			  
			if ( document.forms["contact_form"]["message"].value == "" )
			{
				alert ( "Please enter a message" );
				return false;
			}
			if ( document.forms["contact_form"]["recaptcha_response_field"].value == "" )
			{
				alert ( "Please enter the text shown in the reCaptcha box" );
				return false;
			}
		}

	function validateForm()
		{
						  
			if ( document.forms["newsletterForm"]["cm-f-nijdil"].value == "" )
			{
				alert ( "Please enter your first name" );
				return false;
			}
			
			if ( document.forms["newsletterForm"]["cm-f-nijdir"].value == "" )
			{
				alert ( "Please enter your second name" );
				return false;
			}
			
			var x=document.forms["newsletterForm"]["cm-hudjid-hudjid"].value
			var atpos=x.indexOf("@");
			var dotpos=x.lastIndexOf(".");
			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
			  {
			  alert("Please enter a valid e-mail address");
			  return false;
			  }
			
			if ( document.newsletterForm.terms.checked == false )
	
			{
				alert ( "Please agree to the terms and conditions" );
				return false;
			}
		  
		}
		
function validateBriefForm()
		{						  
			if ( document.forms["brief_form"]["name1"].value == "" )
			{
				alert ( "Please enter your name" );
				return false;
			}
			
			var x=document.forms["brief_form"]["email1"].value
			var atpos=x.indexOf("@");
			var dotpos=x.lastIndexOf(".");
			if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
			  {
			  alert("Please enter a valid e-mail address");
			  return false;
			  }
			  
			if ( document.forms["brief_form"]["message1"].value == "" )
			{
				alert ( "Please enter a message" );
				return false;
			}		  
		}
		
// change image script used on rollovers
function changeImage(pic,name)
{document.images[name].src=pic}
