jQuery(function($){$(document).ready(function(){
	$("#enq_form form").attr("action","http://deben.co.uk/wp-content/themes/bts.theme/t/process.php");
	$(".carousel_activator").cycle({cleartype: true, pause: 1, cleartypeNoBg: true,pager: '#hc_nav'});
	$("#pmn_wrap li").not(".current_page_item").not(".current_page_ancestor").not(".current_page_parent").has("ul.children").children("ul.children").remove();
	var animation_length = 0;
	var margin_top = "430px";
	var ml = 100;
	var ml2 = 165;
	$("#pmr_images ul li").each(function(i){
		if(i == 0){
			$(".small",this).hide();
			var t = $("#pmr_images ul li:eq(0) img").attr("alt");
			$("#pmr_images .sp_title").text(t);
			
			$("#pmr_images ul li:eq(0)").animate({
				width: "485px",
				marginTop: "20px",
				height: "370px"
			}, animation_length).addClass("active");
		}else if(i == 1){
			$(this).animate({
				marginTop: margin_top
			}, animation_length);
		}else if(i == 2 || i > 2){
			$(this).animate({
				marginTop: margin_top,
				marginLeft: ml2+"px"
			}, animation_length);
			ml2 = ml2+165;
		}
		
	});
	
	$("#rb_search_input").focus(function(){ if($(this).val() == $(this).attr("title")){$(this).val("");} });
	$("#rb_search_input").blur(function(){ if($(this).val() == ""){ $(this).val($(this).attr("title")); } });
	$("#rb_search form").submit(function(){ if($("#rb_search_input").val() == "" || $("#rb_search_input").val() == $("#rb_search_input").attr("title")){ return false; } });
	
	$("#pmr_images").delegate(".zoom", "click", function(){
	  $(".active .med").click();
	});
	
	$("#pmr_images ul li").live("click",function(){
	if($(this).hasClass("active") == false){
		var margin_left = $(this).css("margin-left");
		var t = $("img",this).attr("alt");
		$("#pmr_images .sp_title").text(t);
		$(".small",this).hide();
		$(".active .small").show();
		$(".active").animate({
			width: "155px",
			height: "118px",
			marginLeft: margin_left,
			marginTop: margin_top
		}, animation_length).removeClass("active");
		
		$(this).animate({
			width: "485px",
			height: "370px",
			marginLeft: "0",
			marginTop: "20px"
		}, animation_length).addClass("active");
	}			
	});
	
	$(".med").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic'
	});
	$(".youtube_link a").live("click",function() {
         $.fancybox({
          'padding'             : 0,
          'autoScale'   : false,
          'transitionIn'        : 'none',
          'transitionOut'       : 'none',
          'title'               : this.title,
          'width'               : 680,
          'height'              : 495,
          'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
          'type'                : 'swf',    // <--add a comma here
          'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
          });
         return false;
    }); 
	$(".rb_file").live("click", function(){
		var count = $("#files").children(".file_wrap").length;
		if(count == 1){
			$("#files").addClass("files_1col");
		}else if(count == 2 || count > 2){
			$("#files").addClass("files_2col");
		}
		$("#files").fadeIn("fast");
		var w = $(document).width();
		var h = $(document).height();
		$(".dbg").height(h).width(w).show();
		
		var wrap_height = 0;
			$(".file_wrap").each(function(){
				if($(this).height() > wrap_height){
					wrap_height = $(this).height();
				}
			}).each(function(){
				$(this).height(wrap_height);
			});
	return false;});
	$(".enq_trigger").live("click", function(){
		$("#enq_form").fadeIn("fast");
		var w = $(document).width();
		var h = $(document).height();
		$("#enq_bg").height(h).width(w).show();
	return false;});
	
	$("#enq_bg,#enq_close").live("click",function(){
		$("#enq_bg").fadeOut("fast");
		$("#enq_form").fadeOut("fast");
	});
	
	$(".download_zip ,.download_pdf a").attr("target", "_blank");
	$(".dbg,#files_close").live("click",function(){
		$(".dbg").fadeOut("fast");
		$("#files").fadeOut("fast");
	});
	
	$("#enq_form form").submit(function(){
		var errors = "";
		$(".val", this).each(function(){
			val = $(this).val();
			tit = $(this).attr('title');
			if(val === '' || val === tit){
				errors = errors+"<li>"+tit+" is empty<\/li>";
			}else{
				if($(this).hasClass("val_email")){
					if(!check_email($(this).val())){
						errors = errors+"<li>"+tit+" must be a valid email<\/li>";
					}
				}
			}
		});
		if(errors !== ""){
			$(".rer").slideUp("fast",function(){
				$(".rer ul").html('');
				$(".rer ul").html(errors);
			});
			$(".rer").slideDown("slow");
		}else{
			$(".rer,.t_text").slideUp("fast");
			$(this).slideUp("fast",function(){
				$(".success").slideDown("slow");
				var info = $('#enq_form form').serialize();
				var url = $('#enq_form form').attr('action');
				$.ajax({ type: "POST", url: url, data: info, success: function(){ }});
			});
		}
	return false;});
	function check_email(emailAddress) {
		var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
		return pattern.test(emailAddress);
	}
	
return true;
});});
