﻿/// <reference path="/Scripts/jquery.intellisense.js"/>
var App =
{
	openWindow: function openWindow(url, name, width, height, resizable, scrollbars, statusbar, menubar, toolbar)
	{
		var win = null;
		var optionString = "";
		if (width) optionString += "width=" + width + ",";
		if (height) optionString += "height=" + height + ",";
		if (resizable) optionString += "resizable=1,";
		if (scrollbars) optionString += "scrollbars=1,";
		if (statusbar) optionString += "status=1,";
		if (menubar) optionString += "menubar=1,";
		if (toolbar) optionString += "toolbar=1,";
		if (optionString != "") optionString = optionString.substr(0, optionString.length - 1);
		win = window.open(url, name, optionString);
		if (win) win.focus();
		return win;
	},

	printPage: function()
	{
		if (window.print) window.print();
		else alert("Please select \"Print...\" from the File menu.");
	},
	
	parseMoney: function(amount)
	{
		if (amount.length == 0) return 0;
		else
		{
			amount = amount.replace(",", "");
			var isNegative = (amount.substr(0, 1) == "-");
			while (isNaN(amount.substr(0, 1))) amount = amount.substr(1);
			amount = parseFloat(amount);
			if (isNegative)
			{
				amount = 0 - amount;
			}
			return amount;
		}
	},

	formatMoney: function(amount)
	{
		amount = Math.round(amount * 100) / 100; // fix for stupid JS rounding bug
		var value = "$" + amount;
		var newValue = "";
		var tokens = value.split(".");
		if (tokens.length == 1)
		{
			newValue += tokens[0] + ".00";
		}
		else if (tokens.length == 2)
		{
			newValue = tokens[0] + "." + tokens[1];
			if (tokens[1].length == 1) newValue += "0";
		}
		return newValue;
	}
};

App.popBox = function(width, height, scroll, content)
{
	var mask = $("#PopBoxMask");
	var box = $("#PopBox");
	if (!mask.length)
	{
		mask = $("<div id=\"PopBoxMask\"></div>");
		$("body").append(mask);
		mask.click(App.popBoxClose);
	}
	if (!box.length)
	{
		box = $("<div id=\"PopBox\"><img src=\"/Images/Close.png\" width=\"30\" height=\"30\" alt=\"Close\" /><div></div></div>");
		$("body").append(box);
		box.find("img:first").click(App.popBoxClose);
	}
	var win = $(window);
	var visWidth = (document.body.scrollWidth > win.width() ? document.body.scrollWidth : win.width());
	var visHeight = (document.body.scrollHeight > win.height() ? document.body.scrollHeight : win.height());
	mask.width(visWidth).height(visHeight);
	box.width(width).height(height);
	box.css("left", ((win.width() / 2) - (width / 2)) + "px");
	box.css("top", ((win.height() / 2) - (height / 2)) + "px");
	box.css("overflow", scroll ? "auto" : "visible");
	box.find("div").html(content);
	mask.show();
	box.show();
};

App.popBoxClose = function()
{
	$("#PopBoxMask, #PopBox").hide();
};

App.bindCalculatorLinks = function()
{
	$("a").each(function()
	{
		if (this.href.toLowerCase().indexOf("http://www.nmb.com.au/nmb/brokerweb/") != -1)
		{
			var link = $(this);
			var url = link.attr("href");
			link.click(function()
			{
				var width = 550;
				var height = 400;
				App.popBox(width, height, false, "<iframe src=\"" + url + "\" style=\"border:0px;width:" + width + "px;height:" + height + "px;\"></iframe>");
				return false;
			});
		}
	});
};

App.bindTopSearch = function()
{
	var defaultText = "Search";
	var qInput = $("#TopSearch input[type=text]");
	qInput.val(defaultText).focus(function()
	{
		if (this.value == defaultText) this.value = "";
		this.select();
	});
	$("#TopSearch").submit(function()
	{
		window.location.href = this.action + "?q=" + qInput.val();
		return false;
	});
};

App.bindMemberLogin = function()
{
	var loginForm = $("#ContentHeader #MemberLogin form");
	if (loginForm.length)
	{
		var defaultEmailText = "Email Address";
		$("#MemberLogin input[name=email]").val(defaultEmailText).focus(function()
		{
			if (this.value == defaultEmailText) this.value = "";
			this.select();
		});

		var sliding = false;
		$("#ContentHeader #MemberLogin").add(loginForm).mouseover(function(e)
		{
			e.stopPropagation();
			if (!sliding)
			{
				sliding = true;
				loginForm.slideDown(400, function()
				{
					sliding = false;
				});
			}
		});
		$("body").mouseover(function()
		{
			if (!sliding)
			{
				sliding = true;
				loginForm.slideUp(400, function()
				{
					sliding = false;
				});
			}
		});

		var defaultPasswordText = "Password";
		var passwordField = $("#MemberLogin input[name=password]");
		if ($.browser.msie)
		{
			var overlay = $("<span>" + defaultPasswordText + "</span>");
			passwordField.parent().prepend(overlay);
			passwordField.focus(function()
			{
				overlay.hide();
			});
			overlay.click(function()
			{
				overlay.hide();
				passwordField.focus();
			});
		}
		else
		{
			passwordField[0].type = "text";
			passwordField.val(defaultPasswordText).focus(function()
			{
				this.type = "password";
				if (this.value == defaultPasswordText) this.value = "";
				this.select();
			});
		}
	}
};

App.bindContentNav = function()
{
	$("#ContentNav > ul > li").click(function()
	{
		window.location.href = $(this).find("a")[0].href;
	});
}

App.bindContentFeatureBoxes = function()
{
	var items = $("#MainContent #FeatureBoxes li");

	// Get tallest title & box & set the others the same
	var itemsPerLine = 3;
	var numLines = Math.ceil(items.length / itemsPerLine);
	for (var i = 0; i < numLines; i++)
	{
		var boxHeight = 0, titleHeight = 0;
		for (var j = 0; j < itemsPerLine; j++)
		{
			var box = items[(i * itemsPerLine) + j];
			if (box)
			{
				box = $(box);
				var title = box.find("h3");
				if (box.height() > boxHeight) boxHeight = box.height();
				if (title.height() > titleHeight) titleHeight = title.height();
			}
		}
		for (var j = 0; j < itemsPerLine; j++)
		{
			var box = items[(i * itemsPerLine) + j];
			if (box)
			{
				box = $(box);
				box.height(boxHeight);
				box.find("h3").height(titleHeight);
			}
		}
	}

	// If there's a link, apply to whole box
	items.each(function()
	{
		var box = $(this);
		var link = box.find("h3 a");
		if (link.length)
		{
			box.click(function()
			{
				if (link[0].target)
				{
					if (link[0].href.toLowerCase().indexOf("http://www.nmb.com.au/nmb/brokerweb/") != -1) {
						var width = 550;
						var height = 400;
						App.popBox(width, height, false, "<iframe src=\"" + link[0].href + "\" style=\"border:0px;width:" + width + "px;height:" + height + "px;\"></iframe>");
					}
					else {
						App.openWindow(link[0].href, link[0].target);
					}
				}
				else
				{
					window.location.href = link[0].href;
				}
			});
		}
	});
}

$(document).ready(function()
{
	App.bindCalculatorLinks();
	App.bindTopSearch();
	App.bindMemberLogin();
	App.bindContentNav();
	App.bindContentFeatureBoxes();
});