﻿Tactica = new Object();
Tactica.basePath = "";
Tactica.head = "";
Tactica.loadFunctions = new Array();

// registers a function to run when the page has finished loading
Tactica.addLoadFunction = function(fp)
{
	Tactica.loadFunctions.push(fp);
}

// gets the cookie value with the specified key
Tactica.getCookie = function(key)
{
	Tactica.parseCookies();
	
	return Tactica.cookies[key];
}

// gets the object with the specified id
Tactica.getObject = function(id)
{
	if (document.all)
		return document.all[id];
	else if (document.getElementById)
		return document.getElementById(id);
	else
		return null;
}

// initializes common Tactica properties and functions
Tactica.init = function()
{
	if (document.getElementsByTagName)
	{
		var script = document.getElementsByTagName("script");
		var scriptExpr = /scripts\/tactica.js$/gi;

		Tactica.head = document.getElementsByTagName("head")[0];
		
		for (var i = 0; i < script.length; i++)
		{
			var path = script[i].src;
			
			if (scriptExpr.test(path))
			{
				Tactica.basePath = path.replace(scriptExpr, "");
				
				break;
			}
		}
	}

	// create text sizing object
	Tactica.textSize = new TacticaTextSizer();
	
	// register startup function
	if (window.addEventListener)
	{
		window.addEventListener("load", Tactica.load, true);
	}
	else if (window.attachEvent)
	{
		window.attachEvent("onload", Tactica.load);
	}
	else
	{
		window.onload = Tactica.load;
	}
}

// loads any startup scripts
Tactica.load = function()
{
	for (var i = 0; i < Tactica.loadFunctions.length; i++)
	{
		var fp = Tactica.loadFunctions[i];
		
		if (fp && typeof(fp) == "function")
		{
			fp();
		}
	}
}

// fixes editable areas for IE
Tactica.loadEditableAreas = function()
{
	if (document.all && document.getElementsByTagName)
	{
		var div = document.getElementsByTagName("div");
		var divEditable = /editable/gi;
		
		for (var i = 0; i < div.length; i++)
		{
			if (divEditable.test(div[i].className))
			{
				div[i].onmouseout = function()
				{
					var ul = this.getElementsByTagName("ul");
					var ulToolbar = /toolbar/gi;
					
					for (var i = 0; i < ul.length; i++)
					{
						if (ulToolbar.test(ul[i].className))
						{
							ul[i].style.visibility = "hidden";
						}
					}
				}
				
				div[i].onmouseover = function()
				{
					var ul = this.getElementsByTagName("ul");
					var ulToolbar = /toolbar/gi;
					
					for (var i = 0; i < ul.length; i++)
					{
						if (ulToolbar.test(ul[i].className))
						{
							ul[i].style.visibility = "visible";
						}
					}
				}
			}
		}
	}
}

// dynamically loads a script into the page
Tactica.loadScript = function(scriptName)
{
	if (Tactica.head)
	{
		var script = document.createElement("script");
		
		script.type = "text/javascript";
		script.src = Tactica.basePath + scriptName;
		
		Tactica.head.appendChild(script);
	}
}

// opens a popup window
Tactica.openPopup = function(url, w, h)
{
	var popup = Tactica.popup;
	
	if (popup == null || (popup != null && popup.closed))
	{
		popup = window.open(url, "Tactica.popup", "width=" + w + ",height=" + h);
	}
	else
	{
		popup.location.href = url;
		popup.resizeTo(w, h);
		popup.focus();
	}
	
	Tactica.popup = popup;
	
	return false;
}

// parses the document cookie and places a set of key/value pairs into memory
Tactica.parseCookies = function()
{
	if (Tactica.cookies == null)
	{
		var cookies = new Array();
		
		if (document.cookie != null && document.cookie != "")
		{
			var entryList = document.cookie.split("; ");
			
			for (var i = 0; i < entryList.length; i++)
			{
				var entry = entryList[i].split("=");
				
				if (entry.length == 2)
				{
					cookies[entry[0]] = unescape(entry[1]);
				}
			}
		}
		
		Tactica.cookies = cookies;
	}
}

// sets the specified cookie to the specified value
Tactica.setCookie = function(key, value)
{
	Tactica.parseCookies();
	Tactica.cookies[key] = value;
	
	document.cookie = key + "=" + escape(value);
}

// displays a loading animation
Tactica.showLoadAnimation = function()
{
	window.setTimeout(function()
	{
		if (window.Page_IsValid)
		{
			if (Tactica.loadAnimation == null)
			{
				$(document).find("body").append('<div class="modal" id="LoadAnimation"><p class="overlay"><img alt="Loading, please wait..." src="' + Tactica.basePath + 'images/icons/ajax_loader.gif" /></p><p class="overlayShadow"></p></div>');

				Tactica.loadAnimation = $("#LoadAnimation");
			}

			Tactica.loadAnimation.css("display", "block").css("visibility", "visible");
		}

		window.Page_IsValid = true; // reset to true for next request
	}, 100);
}

// hides the loading animation
Tactica.hideLoadAnimation = function()
{
	if (Tactica.loadAnimation)
	{
		Tactica.loadAnimation.css("display", "none").css("visibility", "hidden");
	}
}

// displays a modal dialog box
Tactica.showModalDialog = function(url, title)
{
	if (Tactica.dialog == null)
	{
		$(document).find("body").append('<div class="modal dialog" id="Dialog"><div class="overlayShadow"></div><div class="overlay"><h1></h1><iframe id="DialogWindow" frameborder="0" src="about:blank"></iframe><p><a class="button close" href="#" onclick="Tactica.hideModalDialog()"><span>Close</span></a></p></div></div>');

		Tactica.dialog = $("#Dialog");
		Tactica.dialog.find(".overlayShadow").click(Tactica.hideModalDialog);
		Tactica.dialog.find(".overlay iframe").load(function(e)
		{
			$(this.contentDocument).find("a.cancel").click(function(e) { Tactica.hideModalDialog(); e.preventDefault(); });

			Tactica.dialog.css("display", "block").css("visibility", "visible");
			Tactica.hideLoadAnimation();
			Tactica.sizeModalDialog();
		});

		$(window).resize(Tactica.sizeModalDialog);
	}

	Tactica.dialog.find("h1").html(title);
	Tactica.dialog.find("iframe").attr("src", url);
	Tactica.showLoadAnimation();
}

// adjusts the size of the modal dialog box
Tactica.sizeModalDialog = function()
{
	if (Tactica.dialog)
	{
		var overlay = Tactica.dialog.find(".overlay");

		if (overlay.length > 0)
		{
			var doc = overlay.find("iframe").attr("contentDocument");
			var frm = Math.floor($(doc).find("body").height());
			var max = Math.floor($(window).height() * 0.75);

			overlay.find("iframe").css("height", Math.min(frm, max) + "px");
			overlay.css("margin-top", -(overlay.height() / 2) + "px");
		}
	}
}

// hides the modal dialog box
Tactica.hideModalDialog = function(reload)
{
	if (Tactica.dialog)
	{
		Tactica.dialog.css("display", "none").css("visibility", "hidden");

		if (reload != false)
		{
			document.location.hash = '';
			document.location.reload();
		}
	}
}

//----------------------------------------------------------------------------------------------------

// object for increasing or decreasing body text size
TacticaTextSizer = function()
{
	this.defaultSize = 11;
	this.key = "CurrentTextSize";
	this.unit = "px";
}

// decreases the text size
TacticaTextSizer.prototype.decrease = function()
{
	this.setSize(this.getSize - 1);
}

// gets the text size from the user's cookie
TacticaTextSizer.prototype.getSize = function()
{
	var cookie = Tactica.getCookie(this.key);
	
	if (cookie != null && cookie != "")
	{
		return parseInt(cookie);
	}
	
	return this.defaultSize;
}

// increases the text size
TacticaTextSizer.prototype.increase = function()
{
	this.setSize(this.getSize() + 1);
}

// sets the document's text size
TacticaTextSizer.prototype.setSize = function(size)
{
	if (size == null)
	{
		size = this.getSize();
	}
	
	if (document.body && document.body.style)
	{
		document.body.style.fontSize = size + this.unit;
	}

	Tactica.setCookie(this.key, parseInt(size));
}

//----------------------------------------------------------------------------------------------------

Tactica.init();

// converts a page title into an address
function ConvertTitleToPath(source, targetID)
{
	var target = document.getElementById(targetID);
	
	if (target)
	{
		var title = source.value != null ? source.value : "";
		
		if (target.value == null || target.value == "")
		{
			title = title.replace(/[^A-Z0-9]/gi, "_");
			title = title.replace(/^_+|_+$/gi, "");
			title = title.replace(/_+/gi, "_");
			
			target.value = title.toLowerCase();
		}
	}
}
