$(function(){

	$('#slides').slides({
		effect: 'fade',
		crossfade: 'true',
		preload: true,
		preloadImage: 'images/loading.gif',
		play: 5000,
		pause: 2500,
		fadeSpeed: 1000,
		hoverPause: true/*,
		animationStart: function(){
			$('.caption').animate({
				bottom:-35
			},300);
		},
		animationComplete: function(current){
			$('.caption').animate({
				bottom:0
			},300);
			if (window.console && console.log) {
				// example return of current slide number
				console.log(current);
			};
		}*/
	});
	
	$("#slides .slide").each(function(index) {
		var text = $(this).find("a").attr("title");
		var desc = $(this).find("img").attr("alt");
		$("#slides .pagination li:eq(" + index + ") a").text(text);
		$("#slides .slides_container .slide:eq(" + index + ")").append("<div class='caption'><p>" + desc + "</p></div>");
	})
	
	if($(".accordian").length > 0) {
		
		$(".accordian").each(function() {
			accordian($(this));
		})
		
	}
	
	function accordian($target) {
		
		$target.addClass("js");
	
		$target.find(".heading").hover(function() {
			$(this).css('cursor','pointer');
		});
		
		$target.find("li .heading")	
			.click(function() {
				if($(this).parent().hasClass('open')) {
					$(this).parent().removeClass("open").end().next().slideUp(300);
				}else{
					$(this).parent().parent().find('.content').slideUp(300).parent().removeClass("open");
					$(this).parent().addClass("open").end().next().slideDown(300);
				}
			});
		
		if($target.hasClass("closed")) {
			$target.find("li .content").hide();
		}else{
			$target.find("li:first .heading").click();
		}
	}

	if($("#map_canvas").length > 0) {
		
		function loadScript() {
			var script = document.createElement("script");
			script.type = "text/javascript";
			script.src = "http://maps.google.com/maps/api/js?sensor=true&callback=initialize";
			document.body.appendChild(script);
		}
		
		loadScript();
	}
	
	if($(".fancybox_map").length > 0) {
		$(".fancybox_map").fancybox({
			type: 'iframe',
			width: 1000,
			height: 800
			
		});
	}
	
	
	
	
	
	var form_options = { 
        //target:        '#output1',   // target element(s) to be updated with server response 
        //beforeSubmit:  validateForm,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 
 
    // bind form using 'ajaxForm'
    
   $(".contact_form").submit(function() {
    
    	validated = validateForm($(this));

    	if(validated) {
    		
    		$(this).ajaxSubmit(form_options);
    	}

        return false; 
    });
    
    if($("input.datepicker").length > 0) {
		$("input.datepicker").datepicker({
			dateFormat: 'D d M yy',
			numberOfMonths: 2,
			minDate: 0
		});
	}

	
	$(".enquiry_form_link").fancybox();
	
	if($(".ajax_load").length > 0) {
		$(".ajax_load a").live("click", function() {
			
			$(this).append(" <span class='loading'>(loading...)</span>");
			target = $(this).attr("href");
			
			$.get(target, function(data) {
				$data = $(data).find("#main").children();
  				$("#main").html($data).find(".menu").hide().fadeIn();
			});
			
			return false;
		})
	}
	
	if($(".excerpt").length > 0) {
		
		$(".excerpt p").append("&#8230;");
	}
	
	
	$(window).scroll(function () {
	
		$(".fix").each(function(e) {
			
			target = $(this);
			targetHeight = target.outerHeight();
			offset = target.offset();
			
			// what the y position of the scroll is
			var y = $(window).scrollTop();
			
			// whether that's below the target
			if (y >= targetHeight) {
			  // if so, ad the fixed class
			  target
			  	.addClass("fixed")
			  	
			  if(!target.hasClass("fixed")) {
			  	target
			  		.css("left", offset.left)
			  }
			  
			} else {
			  // otherwise remove it
			  target
			  	.removeClass("fixed")
			  	.css("left", "auto")
			}
		
		});
		
	});
	
});

function validateForm($form) {
	
	validated = true; 
	// Check for requied fields
	$form
		.find(".alert-message").remove().end()
		.find("input[required], textarea[required]")
			.removeClass("error")
			.each(function() {
	
				text = $(this).val();
				
				if( text == "") {
					$(this)
						.addClass("error")
						.before("<div class='alert-message small error' style='display: none;'>Please fill out this field</div>")
						.siblings(".alert-message").slideDown(150);
					
					validated = false;
					return false;
				}
		
			})
	
	if(validated) {
		$form
			.find("fieldset:first").fadeTo(150, .6).end()
			.find(".button").val("Sending...")
		return true;
	}else{
		return false;
	}
	
}

function showResponse(responseText, statusText, xhr, $form)  {
	
	$form
		.find("fieldset:first").slideUp(175).end()
		.find(".success").slideDown(175);

}

