// JavaScript Document
var $img_ratio;
var $label_margin = 30;
var $search_default_val = "search entire site";

$().ready(function(){

// all the scrollers 
	if ($(".tick_content").length){
		$(".tick_content").scrollable({circular: true, mousewheel: false, speed: 1000, easing: 'swing'}).autoscroll({ autoplay: true, interval: 4000 });
	}
	if ($("#slideshow").length){
		$('#slideshow').cycle({ fx:      'fade', speed:    600, timeout:  8000  });
		/*$(".scroller").scrollable({circular: true, mousewheel: false, speed: 800, easing: 'swing'}).autoscroll({ autoplay: true, interval: 7000 });*/
	}

	if ($(".slider_container").length){
		/*$("#home_promo").css("visibility","visible");
		$("#home_promo").fadeIn(1000);*/
		$('.slider_container').not(':eq(0)').hide();
	
		var $scrollables = $('.slider_container').scrollable({
			items: '.items',
			mousewheel: false,
			circular: true,
			next: '.next',
			prev: '.prev',
			speed: 600,
			easing: 'swing'
		});
		
		$scrollables.each(function() {
			var $itemsToClone = $(this).scrollable().getItems().slice(1);
			var $wrap = $(this).scrollable().getItemWrap();
			var clonedClass = $(this).scrollable().getConf().clonedClass;
			$itemsToClone.each(function() {
				$(this).clone(true).appendTo($wrap)
					.addClass(clonedClass + ' hacked-' + clonedClass);
			})
		})
	}
	
	if($("#search_box").length > 0){
		
		if($("input[name='search']").val() != $search_default_val){
			$search_default_val = $("input[name='search']").val();
		}//end if
		
		$("input[name='search']").focus(function(){
			if($(this).val() == $search_default_val){
				$("input[name='search']").removeClass("search_empty");
				$(this).val("");
			}
		});
		$("input[name='search']").blur(function(){
			if($(this).val() == ""){
				$("input[name='search']").addClass("search_empty");
				$(this).val($search_default_val);
			}
		});
	}//end if

	/*if($(".home_slides").length > 0){
		init_home_slides();
		auto_resize_image();
		$(window).resize(function(){
			auto_resize_image();
		});
		$('.home_slides').cycle({ fx:'fade', speed:  1000,timeout:  6000 });
	}//end if
	
	function init_home_slides(){
		var $oImgHeight = $(".slide img").eq(0).height();
		var $oImgWidth = $(".slide img").eq(0).width();
		$img_ratio = $oImgWidth/$oImgHeight;
		
		$('.slide').click(function(e){
			e.preventDefault();
			var pos = $(this).find('img').offset();
			alert(pos.left);
		});
	}
	*/
	function auto_resize_image(){
		var $window = $(window);
		var $slider_width = $window.width();
		/*var $slider_height = $(".slide").height();*/
		var $slider_height = $slider_width/$img_ratio;		
		var $minHeight = parseInt($(".slide").css("height"));
		
		//$("#monitor").text("ratio = "+$img_ratio+" |height = " + $height + " > box height = " + $(".slide").height());
		if($slider_height <  $minHeight){
			//$("#monitor").text("smalle");
			$(".slide img").height($(".slide").height());
			$(".slide img").css("width",($(".slide").height()*$img_ratio)+"px");
		} else {
			$(".slide img").css("width",$(window).width()+"px");
			$(".slide img").height($slider_height);
		}//end if
		
		$(".slide img").each(function(){
			$(this).css("left",0);	
		});
		
		//label		
		var $content_pos = $(".content").offset();		
		
		$('.slide_label').css("left",($content_pos.left + $label_margin)+"px");
	}
	
	
	
/*
//testing the slider
	$(".banner").click(function(){
	     window.location=$(this).find("a").attr("href");
	     return false;
	})
	
	function init_slides(){
				var $oImgHeight = $(".home-panes img").eq(0).height();
				var $oImgWidth = $(".home-panes img").eq(0).width();
				$img_ratio = $oImgWidth/$oImgHeight;
			}
			
			function auto_size_image(){
			
				var $window = $(window);
				var $slider_width = $window.width();
				var $slider_height = $slider_width/$img_ratio;		
				var $minHeight = 457;
				if($slider_height <  $minHeight){
					
				} else {
					$(".banner img").css("width",$(window).width()+"px");
					$(".banner img").height($slider_height);
				}//end if	
			}
		
			if($(".home-panes").length > 0){
				init_slides();
				auto_size_image();
				$(".home-tabs:first").tabs(".banner", { effect: 'fade', fadeOutSpeed: 800, rotate: true }).slideshow({ autoplay: true, interval: 7000 });
				$(window).resize(function(){
					auto_size_image();
				});
				
			}//end if
	
	
*/

//pretty photo
		
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	// stallion tabs
	if ($("ul.tabs").length){	
	//alert ("test");
		$("ul.tabs").tabs("div.panes > div");
	}
	
	
	//mega menu
	function addMega(){
    	$(this).find(".mega").addClass("megavis");
		$(this).find(".mega").slideToggle('fast');
		$(this).find("a.top").addClass("sel");
		
    }

    function removeMega(){
		$(this).find(".mega").slideToggle('fast');
		$(this).find("a.top").removeClass("sel");
    }
		
		
	if ($('#nav_vis a').length) {
		$(this).find(".mega").css("display", "none");
		
		var megaConfig = {
			interval: 100,
			sensitivity: 5,
			over: addMega,
			timeout: 400,
			out: removeMega
		};
	
		$("#nav_vis").hoverIntent(megaConfig);
	
	}
	
	if ($('#nav_st a').length) {
		$(this).find(".mega").css("display", "none");
		
		var megaConfig = {
			interval: 100,
			sensitivity: 5,
			over: addMega,
			timeout: 400,
			out: removeMega
		};
	
		$("#nav_st").hoverIntent(megaConfig);
	
	}
	
	if ($('#nav_ed a').length) {
		$(this).find(".mega").css("display", "none");
		
		var megaConfig = {
			interval: 100,
			sensitivity: 5,
			over: addMega,
			timeout: 400,
			out: removeMega
		};
	
		$("#nav_ed").hoverIntent(megaConfig);
	
	}


	/* Plugin to make variable height divs equal heights */
	$.fn.sameHeights = function() {
	
	$(this).each(function(){
	var tallest = 0;
	
	$(this).children().each(function(i){
	if (tallest < $(this).height()) { tallest = $(this).height(); }
	});
	$(this).children().css({'height': tallest});
	});
	return this;
	};

	if ($('#attract_box').length) {  
			$('#attract_box ul').sameHeights();
	}

});

function isValidEmailAddress(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);
}

function do_booking(){
	$("#booking_form").submit(function(e){	
		if($("#message_error").length > 0){
			$("#message_error").remove();
		}//end if
		e.preventDefault();
		var errors = false;
		var message = "<ul>";
		if($("input[name='stallion[]']:checkbox").length > 0){
			if($("input[name='stallion[]']:checked").length == 0){
				message += "<li>Please select stallions you are interested in</li>";
				errors = true;
			}
		}//end if	
		if($("input[name='contactmeby[]']:checked").length == 0){
			message += "<li>Please enter at least one method of contact</li>";
			errors = true;
		}
		
		if($("#email").val() == ""){
			message += "<li>Please enter a valid email address</li>";
			errors = true;
		} else if($("#email").val() != "" & isValidEmailAddress($("#email").val()) == false){
			message += "<li>Please enter a valid email address</li>";
			errors = true;
		}
		
		var requestUrl = $("#booking_form").attr("action")+"?ajaxCall=true";				
		if(errors == false){		
			$.post(requestUrl,$("#booking_form").serialize(),function(data){			
				if(data == "ok"){
					$("#booking_form").html('<div id="message_ok">Thanks for your booking enquiry. We\'ll send you confirmation and a contract soon.</div>');
					$('html, body').animate({
						scrollTop: $("#message_ok").offset().top
					}, 300);
				} else {
					
				}//end if
			});
		} else {
			$("#booking_form").prepend('<div id="message_error"><h2>There was a problem!</h2>'+message+'</div>');
			$('html, body').animate({
				scrollTop: $("#message_error").offset().top
			}, 300);
		}//end if
	});
}

function do_enquiry(){
	$("#booking_form").submit(function(e){		
		if($("#message_error").length > 0){
			$("#message_error").remove();
		}//end if
		e.preventDefault();
		var errors = false;
		var message = "<ul>";		
		
		if($("#email").val() == ""){
			message += "<li>Please enter a valid email address</li>";
			errors = true;
		} else if($("#email").val() != "" & isValidEmailAddress($("#email").val()) == false){
			message += "<li>Please enter a valid email address</li>";
			errors = true;
		}
		
		var requestUrl = $("#booking_form").attr("action")+"?ajaxCall=true";				
		if(errors == false){
			$.post(requestUrl,$("#booking_form").serialize(),function(data){			
				if(data == "ok"){
					$("#booking_form").html('<div id="message_ok">Thanks for your booking enquiry. We\'ll send you confirmation and a contract soon.</div>');
					$('html, body').animate({
						scrollTop: $("#message_ok").offset().top
					}, 300);
				} else {
					
				}//end if
			});
		} else {
			$("#booking_form").prepend('<div id="message_error"><h2>There was a problem!</h2>'+message+'</div>');
			$('html, body').animate({
				scrollTop: $("#message_error").offset().top
			}, 300);
		}//end if
	});
}
