$(document).ready(function(){
	$(".scroll").jScrollPane();
});

$(document).ready(function(){
	
	// Ensure that the main content area is always taller than the sidebar
	var sidebarHeight = $("div.sideBar").outerHeight();
	if(sidebarHeight >= $("div.mainContent").outerHeight()){
		sidebarHeight = sidebarHeight + 10;
		$("div.mainContent div.middle:first").css("height",sidebarHeight+"px");
	}
	
	//target blank fix
	$("a.external").attr({ target: "_blank" });
	
	//img hover
	$(function(event) {
	  $("img.hover").hover(function(event) {
			$(this).attr("src", $(this).attr("src").split(".").join("_on."));  // adds -hover to the name of the image
	  }, function(event) {
			$(this).stop(true,false); // prevents the creation of stacked actions
			$(this).attr("src", $(this).attr("src").split("_on.").join("."));  // removes -hover from the name of the image
	  });
	});
	
	//enquire
	$(".enquireBut").click(function() {
		$(".enquireSlide").slideToggle("slow");
	});
		
	// External Link
	$("a[href*='http://']:not([href*='"+location.hostname+"']),.external").click( function() {
    	window.open(this.href);
    	$(this).addClass("external");
    	return false;
    });
	
	//slider feature
	$("#slider").carousel({
		itemsPerPage: 1,
		itemsPerTransition: 1,
		speed: 500
	}); 
	
	//slider feature
	$("#product-slider").carousel({
		itemsPerPage: 3,
		itemsPerTransition: 3,
		speed: 500
	}); 
	

	//hiddenContent
	$("a.smoothScroll").click(function() {
		$(".hiddenContent").slideToggle("slow");
		$(".moreInfo").toggleClass("active"); return false;
	});
	
	//smooth scroll
	$("a.smoothScroll").anchorAnimate();
	
	//tabs
		//hides all tabAreas except first and add class to first tab
		$(".tabArea").find(".tabContent").hide("").end("").find(".tabContent:first").show("");
		$(".tabNav").find("ul li:first").addClass("selected");
				
		//gives all items with class tab an id
		$(".tab").attr("id", function (number) {
	          return "tab" + number;
	    })
		
		//gives all items with class tabContent an id
		$(".tabContent").attr("id", function (number) {
				  return "tabContent" + number;		  
		});
						
		//on click of tab hides all tabContents then shows one with same id
		$(".tab").click(function(event){
									
			var id = this.id.replace("tab" , 'tabContent');
			$(".tabContent").css({"display" : "none"});
			$("#" + id).css({"display" : "block"});
			$(".tab").removeClass("selected");
			$(this).addClass("selected");
			//alert(this.id);
			if ($(this).is(".hideSpec")){
				 //alert("word");  
				$(".sideBar").css({"display" : "none"});
			}else {
				//alert("woop");
				$(".sideBar").css({"display" : "block"});
			}	
		
			return false;  
				
		});
		
	//add colors to table rows 
	$(".tabContent").find("tr:odd").css({"background" : "#fbf0a4"});
	
	//news changer
	$('#news').cycle({ 
	    fx:     'fade', 
	    speed:   1000, 
	    timeout: 7000, 
	    next:   '.clicker', 
	    pause:   1 
	});
			
						
	$('a.iframe, a.lightBox').fancybox({
		'onStart'		:   showHideHome,
		'onClosed'		: 	showHideHome,
		'titleShow'		: 	true,
		'width'			: 	640,
		'height'		:	390,
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	
	$("a.videolink").click(function() {
		var video = $(this).attr("href");
		var video_html = $(video).html();
		
		$.fancybox({
			'scrolling'		 : 'no',
			'overlayShow'	 : true,
			'overlayOpacity' : 0.7,
			'content' : video_html
		});
	});	
				
});

function showHideHome () { 
	if ($('#homeVideo').is(':visible')) { 
		$('#homeVideo').hide(); 
	} else {
		$('#homeVideo').show();
	}
}

//smooth scroll function
jQuery.fn.anchorAnimate = function(settings) {

 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed, function() {
				window.location.hash = elementClick
			});
		  	return false;
		})
	})
}


function IsNumeric(strString)	
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

for (i = 0; i < strString.length && blnResult == true; i++)
   {
   strChar = strString.charAt(i);
   if (strValidChars.indexOf(strChar) == -1)
      {
      blnResult = false;
      }
   }
return blnResult;
}

function echeck(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   alert('Please enter a valid email address');
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert('Please enter a valid email address');
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert('Please enter a valid email address');
	    return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert('Please enter a valid email address');
	    return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert('Please enter a valid email address');
	    return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert('Please enter a valid email address');
	    return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
	    alert('Please enter a valid email address');
	    return false;
	 }

	 return true;					
}


function validateContactFrm() {			
	if ($('#name').val().length < 1) {
		alert('Please enter your name');
		$('#name').focus();
		return false;
	}
		
	if($('#email').val().length < 1){
		alert('Please enter your email address');
		$('#email').focus();
		return false;
	}
	
	if (echeck($('#email').val()) == false){
		$('#email').focus();
		return false;
	}

	if ($('#telephone').val().length > 0 && IsNumeric($('#telephone').val()) == false){
		alert('Please ensure the amount needed contains only 0-9, characters.');
		$('#telephone').focus();
		return false;
	}

	return true;		
}


//carousel auto function
function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};



