/*
*  General jQuery functions
*
*  Author: Malcolm Elsworth (electricputty.co.uk)
*  Date: 05-11-09
*  Version: 0.9
*  Dependacies:
*     jquery-1.3.2.min.js
*
*/




// Create closed namespace for jQuery code.
(function($) {


	$(document).ready(function() {


		// ------------------------------------------------------
		// Extent the Array object for use with the hash navigation
		Array.prototype.contains = function ($element) {
			for (var $i=0; $i<this.length; $i++) {
				if (this[$i] == $element) return $i;
			}
			return false;
		}



		// ------------------------------------------------------
		// Custom functions
		//$init_lightbox = function() {
		//	$('#plans a').lightBox();
		//};



		// ------------------------------------------------------
		// Attach popup window event to floor plans
		$init_plans_popup = function() {
			$('#plans a img').hover(
				function () {
					$(this).addClass('over');
				},
				function () {
					$(this).removeClass('over');
				}
			);

			$('#plans a').click(
				function() {

					var $self = $(this);
					$self.removeAttr("target");

					var $theSrc = $self.attr("href");
					var $theURL;

					if ($self.hasClass("plans-image"))
					{
						$theSrc = $theSrc.replace(/\//g,'|');
						$theSrc = $theSrc.replace(/\./g,'~');
						$theURL = "/sales/floor_plan/" + $theSrc;
					}
					else
					{
						$theURL = $theSrc;
					}

					var $theWindowName = "Floor plan"

					var $theWinHeigh = $(window).height();
					var $theWinWidth = $(window).width();

					var $thePageHeight = $theWinHeigh - 50;
					var $thePageWidth = parseInt($theWinWidth - 400);

					var $theLeft = parseInt(($theWinWidth - $thePageWidth) / 2);
					var $theTop = 50;

					var $theProps = "height=" + $thePageHeight + ",width=" + $thePageWidth + ",left=" + $theLeft + ",top=" + $theTop + ",toolbar=no,scrollbars=yes,status=no,resizable=yes,location=no,menuBar=no";

					window.open($theURL, null, $theProps);

					return false;
				}
			);
		};



		// ------------------------------------------------------
		// Invoke FlashObject code to write movie to homepage
		$invoke_flash = function()
		{
			var fo = new FlashObject("homepage.swf", "flashIntro", 903, 648, "6", "#333333");
			fo.addParam("quality", "best");
			fo.addParam("allowScriptAccess", "sameDomain");
			fo.addVariable("movie", "homepage.swf");
			fo.write("flash_holder");
		};



		// ------------------------------------------------------
		// Add active state selector to form
		$("#search-sales fieldset :input, #search-lettings fieldset :input").click(
			function() {
				var $self = $(this);
				var $theDiv = $self.parent();
				var $theFieldset = $self.parents("fieldset");
				var $theSiblings = $theFieldset.find("div");
				$theSiblings.each(
					function() {
						$(this).removeClass("selected");
					}
				)
				$theDiv.addClass("selected");
			}
		);



		// ------------------------------------------------------
		// Add hover to detail pages navigation
		$("#search-main .left ul.nav li").mouseover(
			function() {
				var $self = $(this);
				if($self.hasClass("selected")) {
					$self.addClass("already-selected");
				}
				$self.addClass("selected");
			}
		);
		$("#search-main .left ul.nav li").mouseout(
			function() {
				var $self = $(this);
				if(!$self.hasClass("already-selected")) {
					$(this).removeClass("selected");
				}
				$(this).removeClass("already-selected");
			}
		);



		// ------------------------------------------------------
		// Add event to all the thumbnails
		$("#thumbs li").live("click",
			function() {
				var $self = $(this);
				var $index = $self.parent().children().index($self);
				var $image = $self.find("a");
				var $main_src = $image.attr("rel");
				var $selector = $("#selector");
				var $top = ($index * 85) + ($index * 2);
				$selector.css("top",$top);
				$("#details-image").attr("src", $site_url + $main_src);
				return false;
			}
		);



		// ------------------------------------------------------
		// If we are on a "details" page load the JSON
		if($("#details").length > 0) {

			// Set up the variables
			var $canvas = $('#details .inner');
			var $width = $canvas.width();

			//var $json_url = $site_url + "site-js/details-json.js";
			var $json_url = $site_url + $('#details').attr("title");

			//var $the_page = location.hash;
			var $the_page = window.location.hash.substr(1);

			var $pages = new Array();
			$pages[0] = "__main";
			$pages[1] = "__location";
			$pages[2] = "__plans";
			$pages[3] = "__hips";


			// Clear HTML and add loading anim
			$canvas.html('<p class="loading">Loading property details...</p>');


			// Move canvas function
			$move_canvas = function($i) {
				$destination = parseInt(0 - $i * $width);
				$canvas.stop().animate({ left : $destination }, 1000);
				$highlight_links($i);
			};


			// Highlight links function
			$highlight_links = function($i) {

				// Highlight correct key link
				$("a.json").each(function() {$(this).parent().removeClass("selected") });
				$("a.json:eq("+$i+")").parent().addClass("selected already-selected");

				// Highlight the correct details nav link
				$("#json-nav a").each(function() {$(this).removeClass("selected") });
				$("#json-nav a:eq("+$i+")").addClass("selected");
			};


			// Load JSON
			$.getJSON($json_url, function($data) {
				var $num_pages = $data.html.length;

				$canvas.width($width * $num_pages);
				$canvas.html($data.html.join(""));
				$canvas.css("left", 0);

				$('div.details-page', $canvas).each(function(i) {
					$(this).css("left", i * $width + "px");
				});

				// Check to see if we have a hash in the url
				if($the_page.length > 0) {
					$page_number = $pages.contains($the_page);
					$destination = parseInt(0 - $page_number * $width);
					$canvas.css("left", $destination);
					$highlight_links($page_number);
				}

			});


			// Attach event to all key links
			$("a.json").each(
				function($i) {
					var $self = $(this);
					$self.click(
						function() {
							window.location.hash = "#" + $pages[$i];
							$move_canvas($i);
							return false;
						}
					);
				}
			);


			// Attach event to details nav
			$("#json-nav a").each(
				function($i) {
					var $self = $(this);
					$self.click(
						function() {
							window.location.hash = "#" + $pages[$i];
							$move_canvas($i);
							return false;
						}
					);
				}
			);


			// Re initialise the Lightbox
			//setTimeout(function () { $init_lightbox() }, 4000);
			setTimeout(function () { $init_plans_popup() }, 4000);

		}



		// ------------------------------------------------------
		// Initalise the Lightbox function
		if($("#plans").length > 0) {
			//$init_lightbox();
			$init_plans_popup();

		}



		// ------------------------------------------------------
		// Main nav hover
		$("#header li a").hover(
			function () {
				$(this).addClass('over');
			},
			function () {
				$(this).removeClass('over');
			}
		);



		// ------------------------------------------------------
		// Load homepage movie, pause for half a second to let everything load
		$("#flash_holder").each(
			function() {
				setTimeout(function(){$invoke_flash()}, 500);
			}
		);



		// ------------------------------------------------------
		// Attach the old style form validator
		epValidateForms();

	});
})(jQuery);

