// JavaScript Document
function showEmail() {
  document.write("<n uers=\"znvygb:eragnyf@synfucbvageragnyf.pbz\" pynff=\"rznvy\">eragnyf@synfucbvageragnyf.pbz</n>".replace(/[a-zA-Z]/g, function(c) {
      return String.fromCharCode((c<="Z"?90:122)>=(c=c.charCodeAt(0)+13)?c:c-26);
	   }
	));
}
  
function resizeContentBlock() {
	winHeight = $(window).height();
	if (winHeight > 100) {
	  winHeight = winHeight - 64;
	  if($.browser.msie) {
		  $('div#content').height(winHeight);
	  }
	  else {  
		  $('div#content').css("max-height", winHeight + "px");
	  }
	}
}
  
$(window).resize(resizeContentBlock);

function slideLi() {
	var timer = 0;
	$("li.slider:visible").each(
	  function(i)	{
		  $(this).css("margin-left","-170px");
		  timer = ((timer * 0.8) + 200);
		  $(this).animate({ marginLeft: "15px" }, timer);
		  $(this).animate({ marginLeft: "0" }, timer);
	  }
  );
}

function showLoading() {};

function hideLoading() {};
  
var loadContent = function (loadUrl) {
	var contentBlock = $('div#content');
	contentBlock.css("opacity", "0");
	if (loadUrl) {
		showLoading();
		$("div#container").load(loadUrl,
			function(response, status, xhr) { 
				if (status === "error") {
					contentBlock.html("Sorry. The page you requested could not be found.<br><br><h3>Error status: " + xhr.statusText + "</h3>");
				}
				else {
					resizeContentBlock();
				}
				$('div#content').css("opacity", "1");
			}
		);
		hideLoading();
	}
	else {
		contentBlock.empty();
	};
};

var storeLink = function (newUrl, oldUrl, name, submenu) {
  return storeLink[newUrl] = {
    hash : newUrl,
  	url : oldUrl,
  	title : name,
		submenu : submenu
  };
};

var urlToHash = function (str) {
	if ((str) && (str !== "")) {
		str = str.toLowerCase().substring(str.lastIndexOf('/') + 1);
		var tempInt = str.lastIndexOf('.');
		if (tempInt > 0) {
			str = str.substring(0, tempInt);
		}
		return str.replace(/[^a-z0-9]/g, '');
	}
	return "";
}

var openSubmenu = function () {
  var currentID = "";
  return function (menuId, clicked) {
    if (menuId) {
      if (currentID) {
				if (currentID === menuId) {
	        if (clicked) {
					  $("li.slider.sm." + currentID).slideUp();
						$("li.slider.mh#" + currentID).css("background-image", "url(/images/menudown.png)");
	          currentID = "";
		        return false;
					}
				}
				else {
					$("li.slider.sm." + currentID).slideUp();
					$("li.slider.mh#" + currentID).css("background-image", "url(/images/menudown.png)");
	      }
	    }
		  currentID = menuId; 
		  $("li.slider.sm." + currentID).slideDown();					
			$("li.slider.mh#" + currentID).css("background-image", "url(/images/menuup.png)");
	  }
	  else {
		  if (currentID) {
		    $("li.slider.sm." + currentId).slideUp();
				$("li.slider.mh#" + currentId).css("background-image", "url(/images/menuup.png)");
			  currentID = "";
		  }
		  return false;
    };
		return true;
	}
}();

var initNavMenu = function () {
	var prevMH = "";
	$("li.slider").each( 
		function (index) {
			if ($(this).hasClass("mh")) {
				prevMH = Math.random().toString();
				prevMH = prevMH.substring((prevMH.length - 8), prevMH.length);
				$(this).attr("id", prevMH);
				$(this).click(
					function (evnt) {
						evnt.preventDefault();
						var menuId = $(this).attr("id");
						if (!(openSubmenu(menuId, true))) {
						  location.hash = "";
						}
					}
				);
			}
			else {
				firstLink = $("a:first", this).not("[href^=#]").not("[href^=http]").not("[href^=mailto]");
				if (firstLink.length > 0) {
					var subMenuId = "";
					if ($(this).hasClass("sm")) {
						subMenuId = prevMH;
						$(this).addClass(subMenuId);
					}
					var tempTitle = firstLink.attr("title");
					var oldHash = firstLink.attr("href");
					var newHash = urlToHash(oldHash); 
					storeLink(newHash, oldHash, tempTitle, subMenuId);
					firstLink.attr("href", ("#" + newHash));
				}
			}
		}
	);
}

var pageChanged = function () {
	var hash = location.hash;
	if (!(hash) || (hash === "#")) {
		document.title = "Flashpoint Rentals"
		loadContent("");
		openSubmenu("");
	}
	else {
		hash = hash.substring(1, hash.length);
		if (storeLink[hash].title) {
		  document.title = "Flashpoint Rentals - " + storeLink[hash].title;
		  loadContent(storeLink[hash].url + " div#content");
		  openSubmenu(storeLink[hash].submenu);
		}
	}   
}

jQuery(document).ready(
  function($) {
		$("div#content").css("opacity","0");
		$("li.slider.sm").slideUp();
		$("li.slider.mh").css("background-image", "url(/images/menudown.png)");
		resizeContentBlock();
		initNavMenu();
		$(window).hashchange(pageChanged);
		slideLi();
		var hash = location.hash;
	  if ((hash !== "") && (hash !== "#")) {
			pageChanged();
	  }
	}
);
