/* Set globals */
var $transitionSpeed=400; //Fade transition speed
var $homeInterval=10000; //Home page services transition interval

$(document).ready(function() {
	initDoc();
});

function initDoc() {

	/***************************************************************/
	// Set active navigation background colour
	/***************************************************************/
	$("#masthead-wrapper, #showcase-wrapper, #home-featured-wrapper, #process-masthead-wrapper").each(function() {
		$("#header ul li.active").css("background-color",$(this).css("background-color"));
	});
	
	
	
	/***************************************************************/
	// Home page - services carousel
	/***************************************************************/
	$("#home-left").each(function() {
		carouselTimeout=setTimeout(jQuery.nextService,$homeInterval);
	});

	$("#home-left div.service:gt(0)").each(function() {
		$(this).hide();
		$(this).css("position","absolute");
	});

	$("#home-left div.service:eq(0)").css("position","absolute");
	
	$("#home-left ol li:eq(0) img").attr("src","/images/buttons/dot-over.gif");
	
	$("#home-left ol li").each(function() {
		$(this).click(function() {
			clearTimeout(carouselTimeout);
			thisIndex=$("#home-left ol").find("li").index(this); //Get thumbnail index
			$("#home-left ol li img").attr("src","/images/buttons/dot.gif"); //De-activate all thumbnails
			$("#home-left ol li:eq("+thisIndex+") img").attr("src","/images/buttons/dot-over.gif"); //Activate current thumbnail
			$("#home-left div.service:eq("+$currentService+")").fadeOut($transitionSpeed, function() { //Fade services
				$currentService=thisIndex;
				$("#home-left div.service:eq("+thisIndex+")").fadeIn($transitionSpeed);				
			});
			return false;
		});
	});

	/***************************************************************/
	// Work
	/***************************************************************/

	
	$("#work ol li:eq(0)").click(function() { //Case studies click event
		$("#work #client-stories").fadeOut($transitionSpeed, function() { //Toggle panels
			$("#work #featured-work").fadeIn($transitionSpeed);
		});
		$("img",this).attr("src","/images/buttons/featured-work-active.gif"); //Activate/deactivate navigation
		$("#work ol li:eq(1) img").attr("src","/images/buttons/case-studies.gif");
		$("#work h2:eq(0)").html("Featured Projects"); //Change page heading
		return false;
	});
	
	$("#work ol li:eq(1)").click(function() { //Featured work click event
		$("#work #featured-work").fadeOut($transitionSpeed, function() { //Toggle panels
			$("#work #client-stories").fadeIn($transitionSpeed);
		});
		$("img",this).attr("src","/images/buttons/case-studies-active.gif"); //Activate/deactivate navigation
		$("#work ol li:eq(0) img").attr("src","/images/buttons/featured-work.gif");
		$("#work h2:eq(0)").html("Client Stories"); //Change page heading
		return false;
	});
	
	$("#featured-work dd a, #client-stories div.case-study dl dt a").each(function() { //Thumbnail rollovers
		$(this).prepend("<span></span>");
	});
	
	$("#featured-work dd span").hover(
		function() {
			$(this).css("background-image","url('images/work/listings/featured/rollover.gif')");
		},
		function() {
			$(this).css("background-image","none");
		}
	);

	$("#client-stories div.case-study dt span").hover(
		function() {
			$(this).css("background-image","url('images/work/listings/case-studies/rollover.gif')");
		},
		function() {
			$(this).css("background-image","none");
		}
	);
	
	/***************************************************************/
	// Services
	/***************************************************************/
	var $navActive=true;

	$("#services ol li a").click(function() { //Services navigation click event
		if ($navActive) {
			$navActive=false;
			$("#services ol li").removeClass(); //Deactivate all navigation links
			$(this).parent("li").addClass("active"); //Activate selected link

			$content=$(this).attr("href"); //Get content path
		
			$("#services-wrapper").fadeOut($transitionSpeed, function() { //Fade out current content
				$("#loader").css("display","block");
				$("#services-wrapper").load($content,{async: "yes"}, function($response,$status,$xhr) { //Load new content
					var $response_length = $response.length; //Remove response
					$response = $response.slice(0,$response_length-1);
					$("#services-wrapper").html($response); //Set new content
					$("#services-wrapper").hide(); //Fade new content in
					$("#services-wrapper").fadeIn($transitionSpeed);
					$("#loader").css("display","none");
					initDoc(); //Refresh
				});
			});
		}
		
		return false;
	});
	
	/***************************************************************/
	// SME
	/***************************************************************/
	var $navActive=true;
	var $currentSME=0;

	$("#sme-wrapper .panel:gt(0)").css("display","none");

	$("#sme ol li a").click(function() { //SME navigation click event
		if ($navActive) {
			$navActive=false;
			$("#sme ol li").removeClass();
			$(this).parent("li").addClass("active");

			thisIndex=$("#sme ol li").find("a").index(this);

			$("#sme-wrapper div.panel:eq("+$currentSME+")").fadeOut($transitionSpeed, function() {
				$("#sme-wrapper div.panel:eq("+thisIndex+")").fadeIn($transitionSpeed);
				$currentSME=thisIndex;
				$navActive=true;
			});
		}
		
		return false;
	});

	/***************************************************************/
	// Process
	/***************************************************************/
	var currentProcess=0;

	$("#process ol li:eq(0)").addClass("active");
	$("#process .panel:gt(0)").hide();
	
	$("#process ol li").click(function() {
		$("#process ol li").removeClass();
		$(this).addClass("active");

		thisIndex=$("#process ol").find("li").index(this);
		$("#process .panel:eq("+currentProcess+")").fadeOut($transitionSpeed, function() {
			currentProcess=thisIndex;
			$("#process .panel:eq("+currentProcess+")").fadeIn($transitionSpeed);
		});
		
		return false;
	});
	

	
	/***************************************************************/
	// Contact form
	/***************************************************************/
	$("#name, #email, #phone, #message").focus(function() {
		$(this).css("color","#444");
		$(this).css("border-color","#dbdbdb");
	});

	$("#contact-form").submit(function() {
		var $name=$("#name").val();
		var $nameDefault=$("#name").attr("rel");
		var $email=$("#email").val();
		var $emailDefault=$("#email").attr("rel");
		var $phone=$("#phone").val();
		var $phoneDefault=$("#phone").attr("rel");
		var $company=$("#company").val();
		var $hear=$("#hear").val();
		var $website=$("#website").val();
		var $message=$("#message").val();
		var $messageDefault=$("#message").attr("rel");
		
		var $error=false;
		
		if (!jQuery.isString($name) || $name==$nameDefault) {
			$error=true;
			$("#name").css("color","#de4483");
			$("#name").css("border-color","#de4483");
			$("#name").val($nameDefault);
		}
		
		if (!jQuery.isEmail($email) || $email==$emailDefault) {
			$error=true;
			$("#email").css("color","#de4483");
			$("#email").css("border-color","#de4483");
			$("#email").val($emailDefault);
		}
		
		if (!jQuery.isString($phone) || $phone==$phoneDefault) {
			$error=true;
			$("#phone").css("color","#de4483");
			$("#phone").css("border-color","#de4483");
			$("#phone").val($phoneDefault);
		}
		
		if (!jQuery.isString($message) || $message==$messageDefault) {
			$error=true;
			$("#message").css("color","#de4483");
			$("#message").css("border-color","#de4483");
			$("#message").val($messageDefault);
		}
		
		if ($error) {
			return false;
		} else {
			$("#form-container").html("");
			$.post("/contact-submit/",{contact_add: "1", name: $name, email: $email, phone: $phone, company: $company, hear: $hear, website: $website, message: $message}, function($xml) {
				$("#form-container").html($("response",$xml).text());
				$("#form-container small").hide();
				$("#form-container small").fadeIn($transitionSpeed);
				setTimeout(jQuery.showContactForm,5000);
			});
		}
	});
	
	
	/***************************************************************/
	// General document enhancements
	/***************************************************************/
	$("a.email").each(function() { //Email address obfuscation
		e = this.rel.replace("/","@");
		this.href = "mailto:"+e;
		$(this).text($(this).attr("title"));
	});

	$("a.external").click(function() { //Open link in new window
		window.open(this.href);
		return false;
	});
	
	$(".rollover").each(function() { //Preload rollover images
		jQuery.preLoadImages(jQuery.imageOver($(this)));
	});

	$(".rollover").hover( //Image rollovers
		function() {
			$(this).attr("src",jQuery.imageOver($(this)));
		},
		function() {
			$(this).attr("src",jQuery.imageOff($(this)));
		}
	);
	
	$(".input-text").each (function() { //Define default text for each text field
		this.rel=this.value;
	});

	$(".input-text").focus(function() {
		if (this.value==this.rel) {
			this.value='';
		}
	});

	$(".input-text").blur(function() {
		if (this.value=='') {
			this.value=this.rel;
		}
	});
	
	/***************************************************************/
	// Planner links
	/***************************************************************/
		$(document).ready(function() {
				$("a[rel=fan]").fancybox({
			'padding'		: 0,
			'autoScale'		: true,
			'titleShow': false,
			'overlayOpacity': 0,
			'overlayColor'	: '#000',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'speedIn'		: 600,
			'speedOut'		: 600,
			'type'			: 'image'
		});
			$("a[rel=fanurl]").fancybox({
			'padding'		: 0,
			'autoScale'		: true,
			'titleShow': false,
			'overlayOpacity': 0,
			'overlayColor'	: '#000',
			'transitionIn'	: 'fade',
			'transitionOut'	: 'fade',
			'width'			: '100%',
			'height'		: '100%',
			'speedIn'		: 600,
			'speedOut'		: 600,
			'type'			: 'iframe'
		});
		$(".leftMenuAction").click( function() {
			if ($("#openCloseIdentifier").is(":hidden")) {
				$("#slider").animate({ 
					marginLeft: "-151px"
					}, 500 );
				$("#leftMenuImage").html('<img src="images/menu/00.gif" alt="open" />');
				$("#openCloseIdentifier").show();
			} else {
				$("#slider").animate({ 
					marginLeft: "0px"
					}, 500 );
				$("#leftMenuImage").html('<img src="images/menu/open.gif" alt="close" />');
				$("#openCloseIdentifier").hide();
			}
		});  

		return false;
	});
	

	

	
	/***************************************************************/
	// Images to preload
	/***************************************************************/
	jQuery.preLoadImages("images/loader.gif");
}

var $currentService=0;

(function($) {

	$.nextService=function() { //Home page - services carousel
		var $oldService=$currentService;
	
		$currentService++;
	
		if ($currentService==($("#home-left ol li").size())) {
			$currentService=0;
		}
		
		$("#home-left ol li img").attr("src","images/buttons/dot.gif");
		$("#home-left ol li:eq("+$currentService+") img").attr("src","images/buttons/dot-over.gif");
		
		$("#home-left div.service:eq("+$oldService+")").fadeOut($transitionSpeed, function() {
			$("#home-left div.service:eq("+$currentService+")").fadeIn($transitionSpeed);
			carouselTimeout=setTimeout(jQuery.nextService,$homeInterval);
		});
	}
	
	$.showNewsletter=function() { //Show newsletter form
		$("#newsletter").load("/includes/html/_newsletter.inc.php", function() {
			$("#newsletter").hide();
			$("#newsletter").fadeIn($transitionSpeed);
			initDoc();
		});
	}

	$.showContactForm=function() { //Show contact form
		$("#form-container").load("/includes/html/_contact-form.inc.php", function() {
			$("#form-container").hide();
			$("#form-container").fadeIn($transitionSpeed);
			initDoc();
		});
	}
	
	$.animateBackground=function($colour) {
		$("#home-featured, #home-featured-wrapper, #header ul li.active").animate({ backgroundColor: $colour }, 500);
	}

	var cache=[]; //Preload images
	$.preLoadImages=function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}

	$.imageOver=function($el) { //Rollover image
		if($($el).attr("src").indexOf("-over")==-1) {
			var newSrc = $($el).attr("src").replace(".gif","-over.gif");
			newSrc = newSrc.replace(".jpg","-over.jpg");
			newSrc = newSrc.replace(".png","-over.png");
			return newSrc;
		}
	}
	
	$.imageOff=function($el) { //Rollout image
		if($($el).attr("src").indexOf("-over")!=-1) {
			var oldSrc = $($el).attr("src").replace("-over.gif",".gif");
			oldSrc = oldSrc.replace("-over.jpg",".jpg");
			oldSrc = oldSrc.replace("-over.png",".png");
			return oldSrc;
		}
	}
	
	$.isString=function($str) {  //Data type validation
		if ($str.length!="") { return true; } else { return false;}
	}
	
	$.isEmail=function($str) {
		var regex=new RegExp("^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$");
		return regex.test($str);
	}
	
	$.isInteger=function($str) {
		return ($str.toString().search(/^-?[0-9]+$/) == 0);
	}

})(jQuery)
