﻿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);
}

//----------------------------------------------------------------------------------------------------

// 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();
		}
	}
}
