$(document).ready(function(){

	// Hide all the elements with class "hidden"
	$(".hidden").hide();

	// Make select items with class autosubmit auto-submit their form
	$(".autosubmit").bind("change", function() {
		$(this).parents("form").submit();
	});

	// Make external links open in a new window
	$(".external_link").attr('target', '_blank');
	
	// Form validation
	if ($(".form_validation").length) {
		var form = $(".form_validation"), 
			isAsync = (form.hasClass("not_async")) ? false : true;

		form.html5form({
			async : isAsync, 
	    	allBrowsers: true, 
	        messages : 'en', 
	        responseDiv : '.form_response'
	    });
    }

	// Hightlight the summary basket
	$(".notify").fadeOut(2000);

	// Examples of how to assign the ColorBox event to elements
	$(".lightbox").colorbox({
		onComplete: function() {
			$.fn.colorbox.resize();
			// Focus management
			if($(this).attr("rel").length){
				$("#cboxNext").focus();
			} else if($(this).attr("rev").length){
				$("#cboxToolbar").focus();
			} else {
				$("#cboxClose").focus();
			}
		}
	});
	$(".lightbox.video").colorbox({
		iframe: true
	});

	
	// Remove the slider image placeholder
    $(".nivoSlider .placeholder").remove();

	// Form validation (checkbox) - The target/s is defined by the class "submit_controller"
	if ($(".submit_controller").length) {

		var form = $(".submit_controller form");
		var fields = form.find('input[type="checkbox"]:checked');
		var submit = form.find("input:submit");

		if (!fields.length) {
			submit.attr("disabled", "disabled").addClass("disabled")
		}

	    form.find("input:checkbox").click(function() {
	        var fields = form.find('input[type="checkbox"]:checked');
	        if (!fields.length) {
	            submit.attr("disabled", "disabled").addClass("disabled");
	        } else {
	            submit.removeAttr("disabled").removeClass("disabled");
	        }
	    });
	};

	// Check if the element supports HTML5 attributes
	function elementSupportsAttribute(element, attribute) {
		var test = document.createElement(element);
		if (attribute in test) {
			return true;
		} else {
			return false;
		}
	};

    // Placeholder
	if(!elementSupportsAttribute('input','placeholder')) {
		$('[placeholder]').not('.form_validation [placeholder]').focus(function() {
			var input = $(this);
			if(input.attr('type') == 'password') {
				var label = $('label[for=' + input.attr('id') + ']');
				label.attr('hidden', 'hidden').removeClass('pseudo_placeholder');
			} else {
				if (input.val() == input.attr('placeholder')) {
					input.val('');
					input.removeClass('placeholder');
				}
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				if(input.attr('type') == 'password') {
					var label = $('label[for=' + input.attr('id') + ']');
					label.removeAttr("hidden").addClass('pseudo_placeholder');
				} else {
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				}
			}
		}).blur().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
	};

});

$(window).load(function() {

	// Image Slider
    $('#slider').nivoSlider({
        effect: 'fade', 
        slices: 1,
        animSpeed: 500, 
        pauseTime: 5000,
        startSlide: 0, 
        directionNav: false, 
        directionNavHide: true, 
        controlNav: false, 
        controlNavThumbs: false, 
        controlNavThumbsFromRel: false, 
        controlNavThumbsSearch: '.jpg', 
        controlNavThumbsReplace: '_thumb.jpg', 
        keyboardNav: false, 
        pauseOnHover: true, 
        manualAdvance: false, 
        captionOpacity: 0.9, 
        beforeChange: function(){},
        afterChange: function(){},
        slideshowEnd: function(){}, 
        lastSlide: function(){}, 
        afterLoad: function(){} 
    });

	// Home height equaliser
	if ($("#content_home").length) {
		
		// Equalise top elements
		$("#content_home .section_rowgroup").each(function() {

			var twinChildren = $(this).find(".twin");
			var maxHeight = 0;

			twinChildren.each(function() {
				maxHeight = Math.max(maxHeight, $(this).outerHeight());
			});
			twinChildren.each(function() {
				if($(this).outerHeight() !== maxHeight) {
					var innerElHeight = $(this).outerHeight();
					$(this).css({ "margin-top": (maxHeight + 10 - innerElHeight) + "px" });
				}
			});
		});
		
		// Equalise elements on the right hand side
		var rowHeight = $("#content_home .section_rowgroup:first-child").outerHeight();

		if($("#section_4").length) {
			var sideChild = $("#section_4").outerHeight();
			$("#section_4").css({ "margin-top": (rowHeight - sideChild - 5) + "px" });
		}
	};
	
	// Page height equaliser - Just add the class "twin" to the elements and "twin_wrapper" to the container.
	// The target/s is defined by the class "twin_controller"
	if ($(".twin_controller").length) {

		$(".twin_wrapper").each(function() {

			var twinChildren = $(this).find(".twin");
			var maxHeight = 0;

			twinChildren.each(function() {
				maxHeight = Math.max(maxHeight, $(this).outerHeight());
			});
			twinChildren.each(function() {
				if($(this).outerHeight() !== maxHeight) {
					$(this).css({ "height": maxHeight + "px" });
				}
			});
		});
	};

});
