﻿/// <reference path="jquery-1.6.4-vsdoc.js" />

String.prototype.format = function()
{
	var args = arguments;
	return this.replace(/{(\d+)}/g, function(match, number)
	{ 
		return typeof args[number] != "undefined"
			? args[number]
			: "{" + number + "}";
	});
};

String.coalesce = function()
{
	var test;
	for (var i=0, len=arguments.length; i<len; i++)
	{
		test = arguments[i];
		if (test != null && test != "") return test;
	}
	return test;
};

String.prototype.htmlDecode = function()
{
	return this
		.replace(/&quot;/gi, "\"")
		.replace(/&apos;/gi, "'")
		.replace(/&lt;/gi, "<")
		.replace(/&gt;/gi, ">")
		.replace(/&amp;/gi, "&");
};

Number.prototype.formatNumber = function(decimalPlaces, decimalSeparator, thousandsSeparator, currencySymbol)
{
	var num = this, 
	decimalPlaces = isNaN(decimalPlaces = Math.abs(decimalPlaces)) ? 2 : decimalPlaces, 
	decimalSeparator = decimalSeparator == null ? "." : decimalSeparator, 
	thousandsSeparator = thousandsSeparator == null ? "," : thousandsSeparator,
	currencySymbol = currencySymbol == null ? "$" : currencySymbol, 
	sign = num < 0 ? "-" : "", 
	fixedString = parseInt(num = Math.abs(num || 0).toFixed(decimalPlaces)).toString(), 
	frontDigits = (frontDigits = fixedString.length) > 3 ? frontDigits % 3 : 0;
	return sign + currencySymbol
		 + (frontDigits > 0 ? fixedString.substr(0, frontDigits) + thousandsSeparator : "")
		 + fixedString.substr(frontDigits).replace(/(\d{3})(?=\d)/g, "$1" + thousandsSeparator)
		 + (decimalPlaces > 0 ? decimalSeparator + Math.abs(num - fixedString).toFixed(decimalPlaces).slice(2) : "");
};

function pageInit()
{
	setupBrowserTags();

	setupNav();
	
	$.fn.qtip.defaults.hide.leave = false;

	//  Popup, external and chromeless windows
	$("a.popup, a.external").live({click: function(event){
		event.preventDefault();
		window.open(this.href);
	}});
	$("a.chromeless").live({click: function(event){
		event.preventDefault();
		window.open(this.href, "_blank", "width=700,height=400,location=0,menubar=0,resizable=1,scrollbars=1,status=0,toolbar=0");
	}});
	$("a.interstitial").live({click: function(event){
		event.preventDefault();
		
		var height;
		switch ($.cookie("text-size"))
		{
			case "large":
				height = 454;
				break;
			case "medium":
				height = 420;
				break;
			default:
				height = 404;
				break;
		}
		$.colorbox({href: this.href, scrolling: false, iframe: true, innerWidth: "650px", innerHeight: height+"px"});
	}});
	
	//  Track in WebTrends
	$("a.track").click(function(){
		var me = $(this);
		webtrends_track(me.attr("href"), $.trim(me.text()) || $.trim($("img", me).attr("alt")) || $.trim(me.attr("title")) || me.attr("href"));
	});

	//  Rollover action
	$("img.rollover[data-upImage][data-overImage]").hover(imageRolloverEnter, imageRolloverLeave);
	
	//  Glossary Tooltips
	var glossaryTerms = $("a.term");
	glossaryTerms.qtip({style: {classes: "ui-tooltip-ppr term-tip", width: 350}, hide: {}, position: {at: "bottom center", target: "mouse", viewport: $("#content"), adjust: {mouse: true, x: 15, y: 5}}, 
		content: {
			text: function(){
				return $(this).attr("data-definition");
			}, 
			title: {text: function(){
				return $(this).attr("data-term");
			}}
		}
	});
	
	//  General Tooltips
	$("#content *[title]").not(".no-tooltip").not(glossaryTerms).not(".tout").qtip({style: {classes: "ui-tooltip-ppr", width: 350}, hide: {}, position: {at: "bottom center", target: "mouse", viewport: $("#content"), adjust: {mouse: true, x: 15, y: 5}}});
	
	//  Colorbox links
	$("a.colorbox-link").colorbox();

	//  Back to top links
	$("a.back-to-top").click(function(event){
		event.preventDefault();
		$(window).scrollTop(0);
	});

	
	//  Print and text resizing
	$("#print-button").click(function(event){
		event.preventDefault();
		window.print();
	});
	$("#text-resizer a").click(function(event){
		event.preventDefault();
		switch ($(this).attr("id"))
		{
			case "resize-large":
				resizeText("large");
				break;
			case "resize-medium":
				resizeText("medium");
				break;
			default:
				resizeText("small");
				break;
		}
	});
	resizeText($.cookie("text-size"));
}

function setupBrowserTags()
{
	var $html = $("html");
	
	if ($.browser.msie)
	{
		$html.addClass(
			"browser-ie " + 
			"browser-ie-" + $.browser.version.charAt(0)  //  Only care about major versions right now
		);
	}
	else if ($.browser.webkit)
	{
		if (navigator.userAgent.indexOf("iPhone") >= 0)
		{
			$html.addClass(" browser-iPhone");
		}
		else if (navigator.userAgent.indexOf("iPad") >= 0)
		{
			$html.addClass("browser-iOS browser-iPad");
		}
	}
}
	
function setupNav()
{
	if ($("#navigation").length < 1) return;
	
	//  Flag parents of selected items
	$("#navigation ul li.selected").parentsUntil("#navigation").filter("li:not(.selected)").addClass("selected").each(function(){
		//  Use over image
		var img = $("img:first", this);
		img.attr("src", img.attr("data-overImage"));
	});

	//  Add Rollover flag
	$("img:first", "#navigation ul li:not(.selected)").addClass("rollover");
}

function imageRolloverEnter(event)
{
	var me = $(this);
	me.attr({src: me.attr("data-overImage")});
	me.closest("li").addClass("hover");
}

function imageRolloverLeave(event)
{
	var me = $(this);
	me.attr({src: me.attr("data-upImage")});
	me.closest("li").removeClass("hover");
}

function resizeText(size)
{
	var item, cookie, title;
	switch (size)
	{
		case "large":
			item = $("#resize-large");
			title = "text-size-large";
			cookie = "large";
			break;
		case "medium":
			item = $("#resize-medium");
			title = "text-size-medium";
			cookie = "medium";
			break;
		default:
			item = $("#resize-small");
			title = "text-size-small";
			cookie = "small";
			break;
	}
	item = item.closest("li");
	
	$("#text-resizer li").not(item).removeClass("active");
	item.addClass("active");
	
	$.cookie("text-size", cookie, {path: "/"});
	
	var css = $("link[rel~='stylesheet'][title='" + title + "']");
	$("link[rel~='stylesheet'][title]").not(css).each(function(){
		this.disabled = true;
	});
	css.each(function(){
		this.disabled = false;
	});
}

function webtrends_track(url, label)
{
	if (!url || url.length == 0) return;
	if (url.indexOf("://") < 0)
	{
		//  No protocol. Assume link in relative.
		url = window.location.protocol + "//" + window.location.host + (url.indexOf("/") == 0 ? "" : "/") + url;
	}
	
	var regex = /((\w+:)\/\/(([^:\/]+):?(\d+)?)(\/[^?#]*)?(\?[^#]*)?(#.*)?)/gi;
	var parts = regex.exec(url);
	if (parts == null) return;
	
	var args = [
		"DCS.dcssip", 
		parts[3] || window.location.host,
		"DCS.dcsuri", 
		parts[6] || "/", 
		"WT.ti", 
		label
	];
	if (parts[7] != null)
	{
		args.push("DCS.dcsqry");
		args.push(parts[7]);
	}
	dcsMultiTrack.apply(window, args);
}

