/* 
	Written By: Peter Evans
	Last Modified: 15/09/2008
*/

(function($) {
	// All the main categories, used for the nav.
	var categories = Array("house", "area", "stay", "gallery", "visitors", "contact", "prices");
	
	// Variable used for the .content background previous and next.
	var img_index = 1;
	
	/*
		$.stripPx - Returns the string entered as an arg minus the last 2 values (px hopefully).
	 	Syntax: $.stripPx('myvaluepluspx');
	*/
	jQuery.stripPx = function(str) {
		return str.substring(0, str.length - 2);	
	};
	/*
		$.pos - Centers the object, as an arg, both horizontally and vertically.
	 	Syntax: $.pos('#id');
	*/
	jQuery.pos = function(obj) {
		// Get the height and width of the page.
		var w = $(window).width();
		var h = $(window).height();
		
		// Get the height and width of the object.
		var wcut = $(obj).css("width");
		var hcut = $(obj).css("height");
		
		// Strip the 'px' at the end of the string.
		var objw = $.stripPx(wcut);
		var objh =  $.stripPx(hcut);
		
		// New top and left values achieved by doing:
		// Window - Object Width/Height / 2 + px
		var nw = (w - objw) / 2 + "px";
		var nh = (h - objh) / 2 + "px";
		
		// Apply it to the object's css.
		$(obj).css("top", nh);
		$(obj).css("left", nw);
	};
	/*
		$.showHide - Toggles the visibility of the object.
		Syntax: $.showHide("#id");
	*/
	jQuery.showHide = function(id) {
		($(id).is(":hidden") ? $("#showhideBtn").html("hide text") : $("#showhideBtn").html("show text"));
		($(id).is(":hidden") ? $(id).fadeIn("slow") : $(id).fadeOut("slow"));
	};
	/*
		$.randomImage - Applies a random image to the .content css background.
		Syntax: $.randomImage();
	*/
	jQuery.randomImage = function () {
		var randGallery = Math.floor(Math.random() * 9);
		var gallery = Array("bleu", "grenier", "house", "kitchen", "livingroom", "miel", "poolgarden", "rose", "tilleul");
	
		$(".content").css("background", "url(media/" + gallery[randGallery] + "/1.jpg) no-repeat");
	};
	/*
		$.bgNext - Cycles forward through the images within a specified category (folder).
		Syntax: $.bgNext("tilleul", 2);
	*/
	jQuery.bgNext = function(area, total) {
		if(img_index < total) {
			img_index += 1;
			$(".content").css("background", "url(media/" + area + "/" + img_index + ".jpg) no-repeat");
		}
		else {
			img_index = 1;
			$(".content").css("background", "url(media/" + area + "/" + img_index + ".jpg) no-repeat");	
		}
	};
	/*
		$.bgNext - Cycles backwards through the images within a specified category (folder).
		Syntax: $.bgNext("tilleul", 2);
	*/
	jQuery.bgPrevious = function(area, total) {
		if(img_index > 1) {
			img_index -= 1;
			$(".content").css("background", "url(media/" + area + "/" + img_index + ".jpg) no-repeat");	
		}
		else {
			img_index = total;
			$(".content").css("background", "url(media/" + area + "/" + img_index + ".jpg) no-repeat");	
		}
	};
	/*
		$.hideAllExcept - Used by the navigation. Should an *open* tree be exposed, and another node were clicked, the old
						  one would need to collapse, but maintain the new node open. This function is to keep the second
						  node visible.
		Syntax: $.hideAllExcept("#id");
	*/
	jQuery.hideAllExcept = function(id) {
		for(var i = 0; i < categories.length; i++) {
			if(categories[i] == id) {
				$("#" + categories[i] + "-child, " + "#" + categories[i] + "-child li").css("clear", "left").css("display", "block");
			}
			else {
				$("#" + categories[i] + "-child, " + "#" + categories[i] + "-child li").css("display", "none");
			}
		}
	};
	/*
		$.hideAll - Used by the navigation. Hides all nodes.
		Syntax: $.hideAll();
	*/
	jQuery.hideAll = function() {
		for(var i = 0; i < categories.length; i++) {
			$("#" + categories[i] + "-child, " + "#" + categories[i] + "-child li").css("display", "none");
		}
	};
	/*
		$.validateContact - Validates the contact form in the contact pages.
		Syntax: $.validateContact();
	*/
	jQuery.validateContact = function() {
		var name = document.contact.name.value;
		var email = document.contact.email.value;
		var message = document.contact.message.value;
		
		if((name.length > 0) && (email.length > 0) && (message.length > 0)) {
			document.contact.submit();
		}
		else {
			$.completeFields();	
		}
	};
	/*
		$.validatePosted - Validates the 'keep posted' form in the keep-posted.php page.
		Syntax: $.validatePosted();
	*/
	jQuery.validatePosted = function() {
		var name = document.posted.name.value;
		var email = document.posted.email.value;
		
		if((name.length > 0) && (email.length > 0)) {
			document.posted.submit();
		}
		else {
			$.completeFields();	
		}
	};
	/*
		$.validateBrochure - Validates the request brochure form in the request-brochure.php page.
		Syntax: $.validateBrochure();
	*/
	jQuery.validateBrochure = function() {
		var name = document.brochure.name.value;
		var address = document.brochure.address.value;
		
		if((name.length > 0) && (address.length > 0)) {
			document.brochure.submit();
		}
		else {
			$.completeFields();	
		}
	};
	/*
		$.validateComment - Validates the comment form in the guests.php page.
		Syntax: $.validateComment();
	*/
	jQuery.validateComment = function() {
		var name = document.submitcomment.name.value;
		var comment = document.submitcomment.comment.value;
		
		if((name.length > 0) && (comment.length > 0)) {
			document.submitcomment.submit();
		}
		else {
			$.completeFields();	
		}
	};
	/*
		$.completeFields - Toggles the visibility of the dropped opacity info box over the contact boxes.
		Syntax: $.completeFields();
	*/
	jQuery.completeFields = function() {
		($("#error").is(":hidden") ? $("#error").css("visibility", "visible") : $("#error").css("visibility", "hidden"));
	};
})(jQuery);

$(document).ready(function() {
	
	// Centers the #wrapper container.
	$.pos("#wrapper");
	
	$("#logo").click(function() {
		document.location = "index.php";						  
	});
	
	// If #housenav is clicked...
	$("#housenav").click(function() {
		// If the child class under #housenav is hidden.
		if($("#housenav + .child").is(":hidden")) {
			// Hide everything except for House.
			$.hideAllExcept('house');
		}
		else {
			// House is visible. Hide everything.
			$.hideAll();
			
		}
	});
	/* Click event for the bedroom page, to display the individual bedrooms */
	$("#bedroomParent").click(function() {
		if($("#bedroom-list").is(":hidden")) {
			$("#bedroom-list, #bedroom-list li").css("clear", "left").css("display", "block");	
		}
		else {
			$("#bedroom-list, #bedroom-list li").css("display", "none");	
		}
	});
	$("#areanav").click(function() {
		if($("#areanav + .child").is(":hidden")) {
			$.hideAllExcept('area');
		}
		else {
			$.hideAll();
			
		}
	});
	$("#staynav").click(function() {
		if($("#staynav + .child").is(":hidden")) {
			$.hideAllExcept('stay');
		}
		else {
			$.hideAll();
			
		}
	});
	$("#gallerynav").click(function() {
		if($("#gallerynav + .child").is(":hidden")) {
			$.hideAllExcept('gallery');
		}
		else {
			$.hideAll();
			
		}
	});
	$("#visitorsnav").click(function() {
		if($("#visitorsnav + .child").is(":hidden")) {
			$.hideAllExcept('visitors');
		}
		else {
			$.hideAll();
			
		}
	});
	$("#contactnav").click(function() {
		if($("#contactnav + .child").is(":hidden")) {
			$.hideAllExcept('contact');
		}
		else {
			$.hideAll();
			
		}
	});
	$("#pricesnav").click(function() {
		if($("#pricesnav + .child").is(":hidden")) {
			$.hideAllExcept('prices');
		}
		else {
			$.hideAll();
			
		}
	});
	$(window).resize(function() {
		$.pos("#wrapper");						
	});
});