﻿Tactica = new Object();
Tactica.basePath = "";
Tactica.head = "";

// adds a click function to any javascript links, fixes default button issue in Firefox
Tactica.addLinkClickFunctions = function() {
    $('a').each(function() {
        if (typeof (this.click) != 'function') {
            this.click = function() {
                this.href.indexOf('javascript:') == 0 ? eval(this.href.replace(/^javascript:/, '')) : window.location.href = this.href;
            };
        }
    });
}

// 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 + "');");
	}
}

// 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();
        me.addPNGBehavior();
        me.setEditableAreas();
        me.textSize.init();
    });

    Tactica.readCookies();
}

// 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;
}

// sets the specified cookie to the specified value
Tactica.setCookie = function(key, value)
{
	document.cookie = key + "=" + escape(value);
}

// 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()});
	}
}

// displays a loading animation
Tactica.showLoadAnimation = function()
{
	if (window.Page_IsValid)
	{
		if (Tactica.loadAnimation == null && document.getElementById) Tactica.loadAnimation = document.getElementById("LoadAnimation");
		if (Tactica.loadAnimation != null)
		{
		    Tactica.loadAnimation.style.display = "block";
        }
	}
}

// swaps the class on the specified element
Tactica.swapClass = function(id, class1, class2)
{
	var e = $(id);
	
	if (e.hasClass(class1))
	{
		e.removeClass(class1);
	}
	
	if (e.hasClass(class2) == false)
	{
		e.addClass(class2);
	}
}

//----------------------------------------------------------------------------------------------------

// object for increasing or decreasing body text size
Tactica.TextSizer = function()
{
	this.defaultSize = this.size = "small";
	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 text = $(this.element).css("font-size");
		var data = this.parseSize(text);
		
		if (data != null)
		{
			this.defaultSize = this.size = data.size;
			this.defaultUnit = this.unit = data.unit;
		}
	}
	
	switch (this.unit)
	{
		case "em": this.increment = 0.5; break;
		case "px": this.increment = 1; break;
		case "%":  this.increment = 10; break;
	}
}

// parses text size information
Tactica.TextSizer.prototype.parseSize = function(size)
{
	var data;
	var expr = /([\d\.]+)([\w\%]*)/gi;
	
	if ((data = expr.exec(size.toString())) != null)
	{
		return {size:parseFloat(data[1]), unit:data[2]};
	}
	
	return {size:(size ? size : ""), unit:""};
}

// 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" && size != "")
	{
		var data = this.parseSize(size);
		
		if (data != null)
		{
			this.size = data.size;
			this.unit = data.unit;

			if (typeof(this.element) != "undefined")
			{
				$(this.element).css("font-size", data.size, data.unit);
			}
		}
	}
}

//----------------------------------------------------------------------------------------------------

Tactica.init();

