/*
Theme Name: See more
Theme URI: http://www.rightmove.co.uk/property-to-you/
Description: jQuery page functions
Author: Rob Douglas
Tags: Rightmove, custom header, custom footer, fixed width, two columns

*/

jQuery.fn.ie6MaxWidthHeight = function(maxWidth, maxHeight) {
	var resizeRatio = maxHeight/maxWidth;
	var newWidth = maxWidth, newHeight = maxHeight;
	
	$this = $(this);
	var currentWidth = $this.width();
	var currentHeight = $this.height();
	var currentResizeRatio = currentHeight/currentWidth;
	
	if (currentWidth > maxWidth || currentHeight > maxHeight) {
		if (currentResizeRatio > resizeRatio) {
			newWidth = Math.floor(maxHeight / currentHeight * currentWidth);
		} else if (currentResizeRatio < resizeRatio) {
			newHeight = Math.floor(maxWidth / currentWidth * currentHeight);
		}
		$this.width(newWidth);
		$this.height(newHeight);
	}
	
	return this;
};

jQuery.fn.resizeTabs = function() {
	$this = $(this);
	$this.parent().height("auto"); 
	var $tabWindowWidth = $this.parent().width();
	var $tabs = $this.children().filter(":not(.ui-tabs-hide)");
	var $numberOfTabs = $tabs.length;
	var $tabWidth = Math.floor($tabWindowWidth/$numberOfTabs) - 1; // Minus 1px for borders
	var $lastTabWidth = $tabWindowWidth - (($numberOfTabs-1)*$tabWidth) - ($numberOfTabs+1); // Minus x for borders on each tab

	$tabs.width($tabWidth);
	$tabs.filter(".last").width($lastTabWidth);

	return this;
};

$(document).ready(function(){
	

    var $hero = $("#tabs");
	var $tabs = $hero.children("ul").tabs().resizeTabs();
	
	(function(){
		makeLinksToOtherTabsClickable();
		if ($("body").attr("id") == "regional-trends") {
			addRolloverToMaps();
		}
	})();
	
	// Set onclick events for calls to action
	function makeLinksToOtherTabsClickable(){
		$("div.ui-tabs-panel a").filter(function(){
			return this.href.match(/#tab-\d/);
		}).click(function(){ // bind click event to link
			$tabs.tabs("select", this.href.substr(this.href.length - 1)); // switch to correct tab (convulated due to stupid IE bug where you can't use -ve numbers in substr)
			return false;
		});
	}


	function addRolloverToMaps() {
		// Preload images
		var hoverImage = new Image();
		$("table.nationalTrendsTable").hide()
		$("table#74").show()
		var mainMap = $("#hpimap > img").attr("src"); // Get the source of the UK map
		var mainMapDirectory = mainMap.substring(0,mainMap.lastIndexOf("/")+1); // Get the images path
		hoverImage.src = mainMapDirectory+'74.png';
		hoverImage.src = mainMapDirectory+'232.png';
		hoverImage.src = mainMapDirectory+'282.png';
		hoverImage.src = mainMapDirectory+'311.png';
		hoverImage.src = mainMapDirectory+'319.png';
		hoverImage.src = mainMapDirectory+'331.png';
		hoverImage.src = mainMapDirectory+'486.png';
		hoverImage.src = mainMapDirectory+'488.png';
		hoverImage.src = mainMapDirectory+'629.png';
		hoverImage.src = mainMapDirectory+'638.png';
		
		// Add the rollover image placeholder
		$('#hpimap').append('<img id="ukrolloverimage" usemap="#hpimap" border="0" src="'+mainMapDirectory+'0.png" />');
		// Set mouseover function
		$('#hpimap area').mouseover(function() {
			var areaNumber = $(this).attr('tabindex');
			$('#ukrolloverimage').attr({src: mainMapDirectory+areaNumber+'.png'});
		}).mouseout(function() {
			$('#ukrolloverimage').attr({src: mainMapDirectory+'0.png'});
		}).click(function() {
			regionId = $(this).attr("tabindex");
			$("table.nationalTrendsTable").hide()
			$("table#"+regionId).show();
			return false;
		});
		$(".regionalNumbers").mouseover(function() {
			var areaNumber = $(this).attr('id').match(/\d+/);
			$('#ukrolloverimage').attr({src: mainMapDirectory+areaNumber+'.png'});
		}).mouseout(function() {
			$('#ukrolloverimage').attr({src: mainMapDirectory+'0.png'});
		}).click(function() {
			var regionId = $(this).attr('id').match(/\d+/);
			$("table.nationalTrendsTable").hide()
			$("table#"+regionId).show();
			return false;
		});

	}
			
});
