﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

/* CONTENTS ************************************************

	1.0 Custom Functions
		1.1 Set External Links
		1.2 Equalise Modules
		1.3 Page Tools Init

********************************************************* */

/***** 1.1 Set External Links *****/
var SetExternalLinks = {
	Init:function(){
		$('a[rel*=external]').click(function(){ window.open(this.href); return false; });
	}
}

var SetDimensions = {
	Init:function(){
		FrontEnd.SetDimensions.Reset();
		$(window).resize(function(){
			FrontEnd.SetDimensions.Reset();
		});
	},
	
	Reset:function(){
		// Indentify surrounding dimensions
		var windowHeight = $(window).height();
		var headerHeight = $("#header").height();
		var navHeight = $("#nav").height();
		var footerHeight = $("#footer").height();
		var controlsHeight = $("#guide .controls").height();
		var guideWidth = $("#guide").width();
		
		// Set Heights
		var setHeight = windowHeight - (headerHeight + navHeight + controlsHeight + footerHeight);
		$("#geo-map").height(setHeight);
		$("#menu .tab-content").each(function(){
			$(this).height(setHeight-10)
		});
	}
}

//AK added NAV tab functions
var SetNavTabs = {
	Init:function(){
		$("#nav .navtabs a").click(function(){
			// Remove selected content and states
			FrontEnd.SetNavTabs.DeselectTabs();
			
			// Select content and states
			var tabContentId = $(this).attr("href");
			var tabContentElm = "#nav "+tabContentId;
			FrontEnd.SetNavTabs.SelectTab($(this).parent("li"));
			return false;
		});
	},

	DeselectTabs:function() {
		$("#nav .navtabs li.selected").removeClass("selected");
	},
	
	SelectTab:function(elm) {
		$(elm).addClass("selected");
	}
}

var SetTabs = {
	Init:function(){
		$("#menu .tabs a").click(function(){
			// Remove selected content and states
			FrontEnd.SetTabs.HideAllTabContent();
			FrontEnd.SetTabs.DeselectTabs();
			
			// Select content and states
			var tabContentId = $(this).attr("href");
			var tabContentElm = "#menu "+tabContentId;
			FrontEnd.SetTabs.SelectTab($(this).parent("li"));
			FrontEnd.SetTabs.ShowTabContent(tabContentElm);
			return false;
		});
	},
	
	ShowTabContent:function(elm) {
		$(elm).show();
	},
	
	HideAllTabContent:function() {
		$("#menu .content .tab-content").hide();
	},
	
	DeselectTabs:function() {
		$("#menu .tabs li.selected").removeClass("selected");
	},
	
	SelectTab:function(elm) {
		$(elm).addClass("selected");
	}
}

var SetTabSearch = {
	Init:function(){
		
		// Show / Hide Search Item
		$("#tab-search h2 > a,#tab-search a.hide").click(function(){
			if($(this).parents("li").hasClass("selected")){
				FrontEnd.SetTabSearch.CloseItem($(this));
			} else {
				FrontEnd.SetTabSearch.OpenItem($(this));
			}
			return false;
		});
		
		// Expand / Go back to results from result detail
		$("#tab-search a.expand,#tab-search a.back").click(function(){				
			// Update expand link to hide link
			if($(this).hasClass("expand")) {
				FrontEnd.SetTabSearch.ShowResults($(this));
				FrontEnd.SetTabSearch.ChangeExpandToHide($(this));
			} else {
				var expandLink = $(this).parents("li.selected").children("a.expand");
				FrontEnd.SetTabSearch.ShowResults($(this));
				FrontEnd.SetTabSearch.RemoveResultDetail($(this));
				FrontEnd.SetTabSearch.ChangeExpandToHide(expandLink);
			}
			
			//Ak added
			var searchdiv = document.getElementById('addrResults');
             searchdiv.style.display = "block";
			
			return false;
		});
		
		// Clear / Remove results  IDCLEAR  //AK added
		$("#tab-search a.idclear").click(function(){
			document.getElementById('idResults').style.display = 'none';  //AK added
			
			return false;
		});

		// Clear / Remove results
		$("#tab-search a.clear").click(function(){
			FrontEnd.SetTabSearch.RemoveResults($(this));
			FrontEnd.SetTabSearch.RemoveResultDetail($(this));  //AK added
			
			return false;
		});
		
		// Show Rates / Hide Results
		$("#tab-search li.rates a").click(function(){
			var hideLink = $(this).parents("li").find("a.hide");
			FrontEnd.SetTabSearch.ChangeHideToExpand(hideLink);
			FrontEnd.SetTabSearch.HideResults($(this));
			
			//Ak added
			var searchdiv = document.getElementById('addrResults');
	        searchdiv.style.display = 'none';
		});
	},
	
	CloseItem:function(elm){
		$(elm).parents("li").removeClass("selected");
	},
	
	OpenItem:function(elm){
		$(elm).parents("li").addClass("selected");
	},
	
	ShowResults:function(elm){
		$(elm).parents("li.expand").removeClass("expand");
	},
	
	HideResults:function(elm){
		$(elm).parents("li").addClass("expand");
	},
	
	RemoveResults:function(elm){
	//Ak changed/added
	$(elm).parents("div").find(".results").remove();
	
		document.getElementById('addrResults').style.display = 'none';
	//var searchdiv = document.getElementById('addrResults');
	//searchdiv.style.display = 'none';
	   // $(elm).parents("li").children("div.results").remove();
	},
	
	RemoveResultDetail:function(elm){
		$(elm).parents("li").find(".detail").remove();
	},
	
	ChangeHideToExpand:function(elm){
		var expandHtml = '<a class="expand" href="#">Expand</a>';
		// Insert new expand link
		$(elm).after(expandHtml);
		$(elm).siblings("a.expand").click(function(){
			FrontEnd.SetTabSearch.ShowResults($(this));
			FrontEnd.SetTabSearch.ChangeExpandToHide($(this));
		});
		// Remove hide link
		$(elm).remove();
	},
	
	ChangeExpandToHide:function(elm){
		var hideHtml = '<a class="hide" href="#">Hide</a>';
		// Insert new hide link
		$(elm).after(hideHtml);	
		$(elm).siblings("a.hide").click(function(){
			FrontEnd.SetTabSearch.CloseItem($(this));
			return false;
		});
		// Remove expand link
		$(elm).remove();
	}
}

// Initialise methods onto Frontend Object ================
var FrontEnd = {
	SetExternalLinks:SetExternalLinks,
	SetDimensions:SetDimensions,
	SetNavTabs:SetNavTabs,
	SetTabs:SetTabs,
	SetTabSearch:SetTabSearch
};
