﻿/// <reference path="../src/jquery-1.4.1-vsdoc.js" />

var 
context,
doc = $(document),
JQ = $(),
bod = JQ,
win = JQ,
tabs = JQ,
head = JQ,
contentItems = JQ;

function isFunction(F) { return typeof F === typeof function () { } }
function isObject(O) { return typeof O == typeof {} }
function isBool(B) { return B === true || B === false }
function isArray(A) { return A instanceof Array }
function isString(S) { return typeof S === typeof "" }
function isEmpty(S) { return !S || S == "" || (isString(S) && S.length == 0); }

function EventHandle()
{
	var Arr = [], InitOnAdd = false, initiated = false;
	this.add = function (Func) // adds an Object
	{
		Arr.push(Func);
		if (InitOnAdd)
		{
			if (isFunction(Func.init))
			{
				Func.init(); //initialises child as object
				InitObjects.push(Func);
			}
			else
			{
				Func(); //runs all children Funcs
			}
		}
		return Func;
	};

	this.remove = function (Func) // removes an Object
	{
		for (var i = 0; i < Arr.length; i++)
		{
			if (Arr[i] == Func)
				Arr.splice(i, 1);
		}
		this.populateObjects();
	};

	this.removeByName = function (Name) // removes an Object
	{
		for (var i = 0; i < Arr.length; i++)
		{
			if (typeof (Arr[i].Name) != typeof (undefined) && Arr[i].Name == Name)
				Arr.splice(i, 1);
		}
		this.populateObjects();
	};

	this.clear = function ()
	{
		Arr = [];
		this.populateObjects();
	}

	this.init = function () // page on load event
	{
		var Objs = [];
		for (var i = 0; i < Arr.length; i++)
		{
			if (isFunction(Arr[i].init))
			{
				Arr[i].init(); //initialises child as object
				Objs.push(Arr[i]);
			}
			else
			{
				Arr[i](); //runs all children Funcs
			}
		}
		initiated = true;
		InitObjects = Objs;
	};

	this.populateObjects = function ()
	{
		var Objs = [];
		for (var i = 0; i < Arr.length; i++)
		{
			if (isFunction(Arr[i].init))
			{
				Objs.push(Arr[i]);
			}
		}
		return Objs;
	}

	this.setInitOnAdd = function ()
	{
		InitOnAdd = true;
	};

	var InitObjects = [];
	this.getObjects = function ()
	{
		if (initiated)
		{
			return InitObjects;
		}
		else
		{
			return this.populateObjects();
		}
	};

	this.getArray = function ()
	{
		return Arr;
	};

	this.getObjectsOftype = function (ObjectClass)
	{
		var result = [];
		for (var i = 0; i < InitObjects.length; i++)
		{
			if (InitObjects[i] instanceof ObjectClass)
			{
				result.push(InitObjects[i]);
			}
		}
		return result;
	};
}

function getById(id) { return document.getElementById(id); }
function getByName(Dom, name) { return Dom.getElementsByName(name); }
function getByTag(Dom, tag) { return Dom.getElementsByTagName(tag); }
function isChild(Parent, Child)
{
	if (isString(Parent)) Parent = getById(Parent);
	if (isString(Child)) Child = getById(Child);
	result = false;
	if (Child != null && Parent != null)
	{
		var select = Child.parentNode;
		while (select.parentNode)
		{
			if (select == Parent)
			{
				result = true;
				break;
			}
			select = select.parentNode;
		}
	}
	return result;
}

var ElementTemplate = {
	tag: "div",
	name: null,
	id: null,
	type: null,
	value: null,
	selected: null,
	className: null,
	style: null
}

function CreatElement(O)
{
	///	<summary>
	///		1: $(O) - Create a DOMElement 
	///	</summary>
	///	<param name="O" type="Object">
	///		1: id - global property.
	///		2: type - input property string,.
	///		3: value - input property, string.
	///		4: selected - input property, boolean.
	///		4: className - global property , CSS class.
	///		4: innerHTML - global property.
	///		4: href - a property , string.
	///	</param>
	///	<returns type="DOMElement" />

	var E = null; var e = ElementTemplate;
	if (O)
	{
		E = O.tag ? document.createElement(O.tag) : document.createElement(e.tag)
		O.name ? E.name = O.name : e.name ? E.name = e.name : null;

		O.id ? E.id = O.id : e.id ? E.id = e.id : null;
		O.type ? E.type = O.type : e.type ? E.type = e.type : null;
		O.value ? E.value = O.value : e.value ? E.value = e.value : null;
		O.selected ? E.selected = O.selected : e.selected ? E.selected = e.selected : null;
		O.className ? E.className = O.className : e.className ? E.className = e.className : null;
		O.innerHTML ? E.innerHTML = O.innerHTML : null;
		O.href ? E.href = O.href : null;
	}
	return E;
}
var uniqueIDInt = 1;
function uniqueID(id)
{
	if (!id) id = "auto_";
	id += uniqueIDInt;
	uniqueIDInt++;
	while (document.getElementById(id))
	{
		id += uniqueIDInt;
		uniqueIDInt++;
	}
	return id;
}

function setDropDown(JQobject, value)
{
	var dropDown = JQobject;

	if (dropDown.length && dropDown[0].tagName.toLowerCase() =="select" && isString(value))
	{
		value = trimString(value.toUpperCase());
		dropDown = dropDown[0];
		var options = dropDown.options;
		var match = false;
		for (var i = 0; i < options.length; i++)
		{
			if (trimString(options[i].value.toUpperCase()) == value)
			{
				match = true;
				dropDown.selectedIndex = i;
				break;
			}
		}

		if (match == false)
		{
			for (var i = 0; i < options.length; i++)
			{
				if (trimString(options[i].innerHTML.toUpperCase()) == value)
				{
					dropDown.selectedIndex = i;
					break;
				}
			}
		}
	}
}

function trimString(value)
{
	if (isString(value))
	{
		var fistIx = 0;
		var lasttIx = 0;
		for (var i = 0; i < value.length; i++)
		{
			if (value[i] != " ")
			{
				fistIx = i;
				break;
			}
		}

		for (var i = value.length-1; i >= 0; i--)
		{
			if (value[i] != " ")
			{
				lasttIx = i + 1;
				break;
			}
		}

		if (fistIx == value.length - 1)
		{
			fistIx = 0;
		}

		value = value.substring(fistIx, lasttIx);
	}
	return value;
}

function capitaliseString(value)
{
	value = $.trim(value);

	if (value == "")
		return "";

	var upper = true, ch = "", result = "";

	for (cnt = 0; cnt < value.length; cnt++)
	{

		ch = new String(value.substr(cnt, 1));

		if (upper)
		{
			result += ch.toUpperCase();
			upper = false;
		}
		else
		{
			result += ch.toLowerCase();
		}

		if (ch == "." || ch == "'" || ch == " " || ch == "-")
			upper = true;
	}
	return result;
}


// ---------------------------- PC3Engine Class Declaration
function PC3Engine()
{
	this.onInit = new EventHandle();
	this.add = function (Obj) // adds an Object
	{
		this.onInit.add(Obj);
	};
	this.remove = function (Obj) // removes an Object
	{
		this.onInit.remove(Obj);
		this.postInit.remove(Obj);
		this.preInit.remove(Obj);
		this.onAjaxUpdate.remove(Obj);
		this.onAjaxException.remove(Obj);
	};
	this.removeByName = function (Name) // removes an Object
	{
		this.onInit.removeByName(Name);
		this.postInit.removeByName(Name);
		this.preInit.removeByName(Name);
		this.onAjaxUpdate.removeByName(Name);
		this.onAjaxException.removeByName(Name);
	};
	this.init = function () // page on load event
	{
		bod = $(document.body);
		win = $(window);
		head = $("head");

		win.resize(function () { bod.height(win.height()) });

		// ---------------------------- Pre initialisation Objects
		this.preInit.init();
		this.preInit.setInitOnAdd();

		// ---------------------------- initialisation Objects
		this.onInit.init();
		this.onInit.setInitOnAdd();
		if (this.isMasterMapSet())
		{
			MasterMap.init();
		}
		DetectBrowser();

		// ---------------------------- Post initialisation Objects
		this.postInit.init();
		this.postInit.setInitOnAdd();
		win.resize();
	};
	this.postInit = new EventHandle();
	this.preInit = new EventHandle();
	this.onAjaxUpdate = new EventHandle();

	this.getObjects = function ()
	{
		var result = [];
		result = result.concat(this.preInit.getObjects());
		result = result.concat(this.onInit.getObjects());
		result = result.concat(this.postInit.getObjects());
		return result;
	}
	this.getObjectsOftype = function (ObjectClass)
	{
		var result = [], Objs = this.getObjects();
		for (var i = 0; i < Objs.length; i++)
		{
			if (Objs[i] instanceof ObjectClass)
			{
				result.push(Objs[i]);
			}
		}
		return result;
	};

	this.getEventsArrays = function ()
	{
		var result = [];
		result = result.concat(this.preInit.getArray());
		result = result.concat(this.onInit.getArray());
		result = result.concat(this.postInit.getArray());
		return result;
	}
	this.getEventsItemsOfType = function (ObjectClass)
	{
		var result = [], Objs = this.getEventsArrays();
		for (var i = 0; i < Objs.length; i++)
		{
			if (Objs[i] instanceof ObjectClass)
			{
				result.push(Objs[i]);
			}
		}
		return result;
	};

	this.getObjectsOfGroupName = function (GroupName)
	{
		var arr = this.getObjects();
		var result = [];
		for (var i = 0; i < arr.length; i++)
		{
			if (arr[i].GroupName && arr[i].GroupName == GroupName)
			{
				result.push(arr[i]);
			}
		}
		return result;
	}

	this.getFirstByTypeAndName = function (type, Name)
	{
		var arr = PC3.getObjectsOftype(type);
		var result = null;
		for (var i = 0; i < arr.length; i++)
		{
			if (arr[i].Name && arr[i].Name == Name)
			{
				result = arr[i];
				break;
			}
		}
		return result;
	}

	this.getByTypeAndName = function (type, Name)
	{
		var arr = PC3.getObjectsOftype(type);
		var result = [];
		for (var i = 0; i < arr.length; i++)
		{
			if (arr[i].Name && arr[i].Name == Name)
			{
				result.push(arr[i]);
			}
			else if (arr[i].ID && arr[i].id == Name)
			{
				result.push(arr[i]);
			}
		}
		return result;
	}

	this.getByTypeAndID = function (type, ID)
	{
		var arr = PC3.getObjectsOftype(type);
		var result = null;
		for (var i = 0; i < arr.length; i++)
		{
			if (arr[i].ID && arr[i].ID == ID)
			{
				result = arr[i];
				break;
			}
		}
		return result;
	}

	this.Button = function (Name) { return this.getFirstByTypeAndName(ToolButton, Name); };
	this.ButtonGroup = function (Name) { return this.getFirstByTypeAndName(ButtonGroup, Name); };
	this.Wall = function (ID) { return this.getByTypeAndID(Wall, ID); };
	this.Popup = function (ID) { return this.getByTypeAndID(PopUp, ID); };



	function DetectBrowser()
	{
		if (window.navigator.userAgent.match(/MSIE/i))
		{
			PC3.isMSIE = true;
			var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
			if (re.exec(window.navigator.userAgent) != null)
			PC3.MSIE_V = parseFloat(RegExp.$1);
		}

		if (
			typeof (UseCurvy) != typeof (undefined) && 
			UseCurvy === true && PC3.isMSIE && 
			typeof (UseCurvy) != typeof (undefined))
		{
			PC3.onAjaxUpdate.add(curvyCorners.redraw);
		}
	}

	this.isMSIE = false;
	this.MSIE_V = 0;

	var hasMasterMap = false;
	var MasterMap = null;
	this.addMasterMap = function (Map)
	{
		hasMasterMap = true;
		MasterMap = Map;
	}
	this.getMasterMap = function ()
	{
		return MasterMap;
	}
	this.isMasterMapSet = function ()
	{
		return hasMasterMap;
	}
	this.onMasterMapPostInit = new EventHandle();

	var WiredWebForms = false;
	this.WireWebForms = function ()
	{
		if (WiredWebForms == false)
		{
			if (typeof (Sys) != typeof (undefined) && Sys != null && Sys.WebForms)
			{
				Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
				Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
				WiredWebForms = true;
			}
			else
			{
				window.setTimeout(function () { PC3.WireWebForms(); }, 50);
			}
		}
	}

	function BeginRequest(sender, args)
	{
		
	}

	function EndRequest(sender, args)
	{
		if (args.get_error() != undefined)
		{
			PC3.ThrowAjaxException();
			args.set_errorHandled(true);
		}
		PC3.onAjaxUpdate.init();
	}

	this.onAjaxException = new EventHandle();

	this.AjaxExceptionMessage = "";
	this.ThrowAjaxException = function ()
	{
		this.onAjaxException.init();
	}
}
// ---------------------------- PC3Engine Class Implementation
var PC3 = new PC3Engine();
doc.ready(function () { PC3.init(); });

