/* GENERAL FUNCTIONS
-----------------------------------------------------*/
(function($) {	  
	var scrollSpeeed = 500;
	var topNavArrowSpeed = 150;
	var topNavFadeSpeed = 300;
	var middleNavFadeSpeed = 300;

	function _setupSmoothScroll() {
		$("#Map area").click(function() {
			$.scrollTo(0,scrollSpeeed);
			return false;	
		});
		$("a.contact").click(function(event) {
			event.preventDefault();
			$.scrollTo($("a[name='contact']"), scrollSpeeed, _animateHelloBubble);
			return false;
		});
	};	
	
	function _animateHelloBubble() {
		$("input[name='name']").focus();
	};

	function _switchIosBackground() {
		if(navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i)) {
			var bg = String($("body").css("backgroundImage"));
			if(bg.indexOf("backgroundHome") >= 0) {
				$("body").css({ background:"#e6e6e6 url(/images/backgroundHomeIphone.jpg) no-repeat top" });
			} else {
				$("body").css({ background:"#e6e6e6 url(/images/backgroundIphone.jpg) no-repeat top" });
			};
		};
	};
	
	function _setupMenuRollover() {
		$("#navigation a").not(".logo-button").each(function(i,a) {
			if(!$(a).hasClass("selected")) {
				var div = $("<div></div>").insertBefore(a).css({ width:$(a).width(), height:50, position:"relative", display:"block", float:"left" });
				$(a).appendTo(div);
				$("<div class='menu-hover'></div>").prependTo(div).css({ width:$(a).width(), height:25, background:"transparent url(/images/menu-hover.png) no-repeat center center", position:"absolute", top:0, left:0 }).css({ opacity:0 });
				$(a).css({ height:25, position:"absolute", top:25, left:0, backgroundPosition:"left bottom" });
				var clone = $(a).clone().appendTo(div).css({ backgroundPosition:"bottom right", opacity:0 });
			
				$(clone).hover(function() {
					$(this).stop().animate({ opacity:1 }, topNavFadeSpeed);
					$("#navigation a").not(".logo-button").not(this).find(".menu-hover").animate({ opacity:0 }, topNavFadeSpeed);				
					$(this).siblings(".menu-hover").rotate("-90deg").css({ left:-10 }).animate({ rotate:"0", opacity:1, left:0 }, topNavArrowSpeed);
				}, function() {
					$(this).stop().animate({ opacity:0 }, topNavFadeSpeed).siblings(".menu-hover").animate({ opacity:0 }, 150);
				});
			};
		});
		
		if($.browser.msie) return;
		$("#middleNav a").not(".selected").each(function(i,a) {
			var d = $("<div></div>").insertBefore(a).css({ width:$(a).width(), height:$(a).height(), position:"relative", marginTop:$(a).css("marginTop"), marginLeft:$(a).css("marginLeft"), display:"block", float:"left" });
			$(a).appendTo(d).css({ margin:0, top:0, left:0, position:"absolute" });
			var c = $(a).clone().appendTo(d).css({ backgroundPosition:"bottom right", opacity:0 }).hover(function() {
				$(this).stop().animate({ opacity:1 }, middleNavFadeSpeed);
			}, function() {
				$(this).stop().animate({ opacity:0 }, middleNavFadeSpeed);
			});
			
		});
	};
	
	function _setupContactCheckbox() {
		$("#contactForm .checkbox").hide();
		$("#contactForm input[name^=email]").focus(function() { $("#contactForm .checkbox").slideDown(); });
	};

	$(document).ready(function() {
		// THIS TARGETING HAS TO GO BEFORE THE MENU ROLLOVER OR IE7 WILL GET MESSED UP
		$("a[href^='http://'],a[href^='https://'],a[href$='.doc'],a[href$='.pdf'],a[href$='.jpg']").attr("target", "_blank");	// EXTERNAL LINKS IN NEW WINDOW
		
		_switchIosBackground();
		_setupMenuRollover();
		_setupSmoothScroll();
		_setupContactCheckbox();
	});
	
	
})(jQuery);
