$(document).ready(function() {

	$.validator.addMethod("spamBot",		   				   
		function(value, element) {
			if(element.value == '') {
				return true;
			}
			else {
				return false;
			}
		}
	);
	
	// tabs	
	function paneOpen() {	
		
		$.each($.browser, function(i) {
   			if($.browser.msie){
				$("#locationWrapper").hide();
      			$("#shadowWrapper").show(); 
   			}else{
				$("#locationWrapper").fadeOut(600);
     			$("#shadowWrapper").fadeIn(600);	
   			}
 		});			
	}
						   
	function paneClose() {
		
		$.each($.browser, function(i) {
   			if($.browser.msie){
      			$("#shadowWrapper").hide(); 
				$("#locationWrapper").show();
   			}else{
     			$("#shadowWrapper").fadeOut(600);
				$("#locationWrapper").fadeIn("slow");
   			}
 		});			
	}	

	$(".content-tab").hide(); //Hide tabs
	$("#header ul li:first").addClass("active").show(); //Activate first tab
	$(".content-tab:first").show(); //Show first tab content

	$(".nav-home,#nav-what,#nav-news,#nav-contact").click(function() {
																   
		paneOpen();
		
		$("#header li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".content-tab").hide(); //Hide all tab content
	
		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		
		$.each($.browser, function(i) {
   			if($.browser.msie){
      			$(activeTab).show(); 
   			}else{
     			$(activeTab).fadeIn(750); //Fade in the active ID content
   			}
 		});	
		
		return false;		

	});
	
	$("#nav-locations").click(function() {
		$("#header li").removeClass("active");
		$(this).addClass("active");
		$(".content-tab").hide(); //Hide all tab content
		paneClose();
		$(this).find("a").attr("href"); 
		return false;	
	});
	
	$("#locationWrapper a").click(function() {
		$(this).find("a").attr("href"); 
		return false;
	});
	
	// remove broken links
	$('img').error(function () {
		$(this).remove();
	});

	// external sites in a new window
	$("a[rel=external]").attr('target', '_blank');
	
	// location popups
	$('.main li').replaceWith(function(){
		return '\
		<div id="'+$(this).attr('id')+'" class="slideOutTip '+$(this).attr('class')+'">\
			\
			<div class="tipVisible">\
				<div class="tipIcon"><a href="#"></a></div></div>\
			\
			<div class="slideOutContent">\
				<li>'+$(this).html()+'</li>\
			</div>\
		</div>';
	});

	/* Listening for the click event: */	
	$('.tipVisible').bind('click',function(){
		var tip = $(this).parent();		
		
		/* If a open/close animation is in progress, exit the function */
		if(tip.is(':animated'))
			return false;

		if(tip.find('.slideOutContent').css('display') == 'none')
		{
			tip.trigger('slideOut');
		}
		else tip.trigger('slideIn');

	});
	
	$('.slideOutContent h5').bind('click',function(){
		var tip = $(this).parent();		

		if(tip.find('.slideOutContent').css('display') == 'none')
		{
			tip.trigger('slideOut');
		}
		else tip.trigger('slideIn');

	});
	
	$('.slideOutTip').bind('slideOut',function(){

		var tip = $(this);
		var slideOut = tip.find('.slideOutContent');
		
		/* Closing all currently open slideouts: */
		$('.slideOutTip.isOpened').trigger('slideIn');
		
		/* Executed only the first time the slideout is clicked: */
		if(!tip.data('dataIsSet'))
		{
			tip	.data('origWidth',tip.width())
				.data('origHeight',tip.height())
				.data('dataIsSet',true);
			
			if(tip.hasClass('openTop'))
			{
				/*
					If this slideout opens to the top, instead of the bottom,
					calculate the distance to the bottom and fix the slideout to it.
				*/
				
				tip.css({
					bottom	: tip.parent().height()-(tip.position().top+tip.outerHeight()),
					top		: 'auto'
				});
				
				/* Fixing the title to the bottom of the slideout, so it is not slid to the top on open: */
				tip.find('.tipVisible').css({position:'absolute',bottom:3});
				
				/* Moving the content above the title, so it can slide open to the top: */
				tip.find('.slideOutContent').remove().prependTo(tip);
			}
			
			if(tip.hasClass('openLeft'))
			{
				/*
					If this slideout opens to the left, instead of right, fix it to the
					right so the left edge can expand without moving the entire div:
				*/
				tip.css({

				});
				
				tip.find('.tipVisible').css({});
			}
		}
		
		/* Resize the slideout to fit the content, which is then faded into view: */
		
		tip.addClass('isOpened').animate({

		},function(){
			slideOut.fadeIn();
		});

	}).bind('slideIn',function(){
		var tip = $(this);

		/* Hide the content and restore the original size of the slideout: */
		
		tip.find('.slideOutContent').fadeOut('fast',function(){
			tip.animate({

			},function(){
				tip.removeClass('isOpened');
			});
		});

	});
	
	// Visit us drop-down
    $('#footer ul > li').hover(
        function () {
            var $this = $(this);
            $('span',$this).stop().animate({
                'top':'29px'
            },400);
        },
        function () {
            var $this = $(this);
            $('span',$this).stop().animate({
                'top':'0px'
            },800);
        }
    );
	
	// entire element clickable
	$("#footer li").click(function(){
	  window.open( $(this).find("a").attr('href') ); return false;
	});
	
	// hover icons
	$("#eGraphics a,#tdi a,#hub a,#codeuk a").blend({ speed: 300 });
	$("#eGraphics span").blend({target:"#eGraphics a", speed:300});
	$("#tdi span").blend({target:"#tdi a", speed:300});
	$("#hub span").blend({target:"#hub a", speed:300});	
	$("#codeuk span").blend({target:"#codeuk a", speed:300});

	// blend only on non-IE browsers
	$.each($.browser, function(i) {
   		if($.browser.msie){
			// do nothing
   		}else{
			$(".tipIcon a").blend({ speed: 300 });
   		}
 	});
	
	// textarea width varies from browser to browser
	$.each($.browser, function(i) {
   		if($.browser.msie){
			$("#form_comments").css('width','168px');
   		}
		if ($.browser.webkit) {
			$("#form_comments").css('width','164px');
   		}
		if ($.browser.mozilla) {
			$("#form_comments").css('width','168px');
		}
 	});	

// ---------------------------------------------------
//  Sets focus on first text input
// ---------------------------------------------------
	 $("input.text:first:visible").focus();	  



// ---------------------------------------------------
// The Submit Form Controls including validation
// ---------------------------------------------------
	$("#submit").click(
		function() {		
// 			---------------------------------------------------
// 			Validation method
// 			---------------------------------------------------
			$("#form_contact").validate({
					rules: {
						form_from_firstname: "required",
						form_from_lastname: "required",
						form_from_email: {
							required: true,
							email: true
						},
						form_address: {
							spamBot: true
						}
					},
					messages: {
						form_from_firstname: "Please enter you first name",
						form_from_lastname: "Please enter you last name",
						form_from_email: "Please enter a valid email address",
						form_address: "Please accurately total the numbers to prove you are not a spambot"
					},				
							
							
					submitHandler: function(form) {
// 						---------------------------------------------------
// 						Conditional Statements based on return value of message
// 						--------------------------------------------------- 
	
						var dataString = 
								'form_from_firstname='+$('#form_from_firstname').val()+'&'+
								'form_from_lastname='+$('#form_from_lastname').val()+'&'+
								'form_title='+$('#form_title').val()+'&'+
								'form_company='+$('#form_company').val()+'&'+
								'form_from_email='+$('#form_from_email').val()+'&'+
								'form_telephone='+$('#form_telephone').val()+'&'+								
								'form_comments='+$('#form_comments').val(); 

// 						---------------------------------------------------
// 						This is the actual form post and closing message on screen
// 						--------------------------------------------------- 											
						$.ajax({
							  type: "POST",
							  url: "includes/process-ajax.php",
							  data: dataString,
							  success: function() {
									$('#form_wrap').fadeOut('fast', function() {
									$('#message').fadeIn('fast');
									$('#message').html("<div id='formStatus'><h4>Thank you. Your email has been sent.</h4></div>").fadeIn('fast').fadeOut(5000, function() {
											$("#form_contact").clearForm();
											$("#form_wrap").fadeIn('fast');
									});
								});
							}
						});						 
//					End of Success parameter
					}	
//				End Of Validate
			});
//			return false;	
		}); 

	
// ---------------------------------------------------
// Clear form data jquery plugin (being used after submitted
// and closing message. Before the user clicks the e-mail button again,
// the form data is cleared.
// ---------------------------------------------------
$.fn.clearForm = function() {
	return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
		return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea')
		this.value = '';
	else if (type == 'checkbox' || type == 'radio')
		this.checked = false;
	else if (tag == 'select')
		this.selectedIndex = -1;
	});
};	
//End of Main Function
	
});

Cufon.replace('h1');
Cufon.replace('h2');
