﻿Tactica = new Object();
Tactica.basePath = "";
Tactica.head = "";
Tactica.loadAnimation = null;

// adds a click function to any javascript links, fixes default button issue in Firefox
Tactica.addLinkClickFunctions = function()
{
	var la = $("a[href^='javascript:']");

	for (var i = 0; i < la.length; i++)
	{
		var f = function() { eval(unescape(this.href.replace(/^javascript\:/i, ""))) };
		var l = la.get(i);

		if (typeof (l.click) != "function")
		{
			l.click = f;
		}
	}
}

// adds a behavior to fix PNG images in IE
Tactica.addPNGBehavior = function()
{
	if (document.all && document.createStyleSheet)
	{
		var png = Tactica.basePath + "scripts/png.htc";
		var pngSS = document.createStyleSheet();
		
		pngSS.addRule("img", "behavior: url('" + png + "');");
	}
}

// finds all popup links and adds the appropriate behaviour
Tactica.addPopupBehavior = function()
{
	$("a[rel^='popup']").click(function(event)
	{
		var rel = this.getAttribute("rel");
		var relSize = /\[(\d+)x(\d+)\]$/i.exec(rel);

		if (relSize)
		{
			Tactica.openPopup(this.href, parseInt(relSize[1]), parseInt(relSize[2]));
		}

		event.preventDefault();
	});
}

// gets the cookie value with the specified key
Tactica.getCookie = function(key)
{
	var cookies = Tactica.readCookies();
	return cookies[key];
}

// initializes common Tactica properties and functions
Tactica.init = function()
{
	var me = Tactica;

	me.basePath = $("script[src$='scripts/tactica.js']").get(0).src.replace(/scripts\/tactica.js$/gi, "");
	me.head = $("head").get(0);
	me.textSize = new Tactica.TextSizer();

	$(document).ready(function() { me.addLinkClickFunctions() });
	$(document).ready(function() { me.addPNGBehavior() });
	$(document).ready(function() { me.addPopupBehavior() });
	$(document).ready(function() { me.setEditableAreas() });
	$(document).ready(function() { me.textSize.init() });

	Tactica.readCookies();

	if (Sys && Sys.WebForms)
	{
		var rm = Sys.WebForms.PageRequestManager.getInstance();

		if (rm)
		{
			rm.add_endRequest(Tactica.hideLoadAnimation);
		}
	}
}

// dynamically loads a script into the page
Tactica.load = function(scriptName)
{
	if (typeof(Tactica.head) != "undefined")
	{
		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.closed) == false)
	{
		Tactica.popup = window.open(url, "Tactica.popup", "width=" + w + ",height=" + h);
	}
	else
	{
		Tactica.popup.location.href = url;
		Tactica.popup.resizeTo(w, h);
		Tactica.popup.focus();
	}
	
	return false;
}

// parses the document cookie and places a set of key/value pairs into memory
Tactica.readCookies = function()
{
	var cookies = new Array();
	
	if (document.cookie != null && document.cookie != "")
	{
		var data, expr = /(.*?)=(.*?);\s?/gi, text = document.cookie + "; ";
		
		while ((data = expr.exec(text)) != null)
		{
			cookies[data[1]] = unescape(data[2]);
		}
	}
	
	return cookies;
}

// fixes editable areas for IE
Tactica.setEditableAreas = function()
{
	if (document.all)
	{
		var ea = $("div[class*='editable']");
		
		ea.bind("mouseout", function(e){$("ul[class*='toolbar']", this).hide()});
		ea.bind("mouseover", function(e){$("ul[class*='toolbar']", this).show()});
	}
}

// sets the specified cookie to the specified value
Tactica.setCookie = function(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)
			{
				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");
	}
}

// stops the user from double-clicking a button
Tactica.stopDoubleClick = function(e)
{
	e.preventDefault();
}

//----------------------------------------------------------------------------------------------------

// object for increasing or decreasing body text size
Tactica.TextSizer = function()
{
	this.defaultSize = this.size = 100.0;
	this.defaultUnit = this.unit = "%";
	this.key = "CurrentTextSize";
	this.increment = 10;
}

// decreases the text size
Tactica.TextSizer.prototype.decrease = function(element)
{
	this.setSize(this.size - this.increment);
}

// increases the text size
Tactica.TextSizer.prototype.increase = function()
{
	this.setSize(this.size + this.increment);
}

// initializes the component
Tactica.TextSizer.prototype.init = function()
{
	if (typeof(this.element) == "undefined") this.element = $("body").get(0);
	if (typeof(this.element) != "undefined")
	{
		var data;
		var expr = /([\d\.]+)([\w\%]*)/;
		var text = $(this.element).css("font-size");
		
		if ((data = expr.exec(text)) != null)
		{
			this.defaultSize = this.size = parseFloat(data[1]);
			this.defaultUnit = this.unit = data[2];
		}
	}
	
	switch (this.unit)
	{
		case "em": this.increment = 0.5; break;
		case "px": this.increment = 1; break;
		case "%":  this.increment = 10; break;
	}
}

// resets the text size
Tactica.TextSizer.prototype.reset = function()
{
	this.size = this.defaultSize;
	this.unit = this.defaultUnit;
	this.setSize();
}

// sets the document's text size
Tactica.TextSizer.prototype.setSize = function(size)
{
	if (typeof(size) != "undefined")
	{
		this.size = size;
	}
	
	if (typeof(this.element) != "undefined")
	{
		$(this.element).css("font-size", this.size + this.unit);
	}
}

// initializes the page

$(document).ready(function()
{
	var forms = $("form");

	forms.submit(function(e)
	{
		var me = $(this);

		window.setTimeout(function() { me.submit(Tactica.stopDoubleClick) }, 5);
	});

	if (Sys && Sys.WebForms)
	{
		var rm = Sys.WebForms.PageRequestManager.getInstance();

		if (rm)
		{
			rm.add_endRequest(function()
			{
				forms.unbind("submit", Tactica.stopDoubleClick);
			});
		}
	}

	Tactica.init();
});

function pageLoad()
{
	Tactica.hideLoadAnimation();
}