/** Begin Core Class.js **/
/**
* Create Class
* @usage
var ClassName = Class.create();
ClassName.prototype = {
	initialize: function () {
	
	}
}

var name = new ClassName();
*/
/*
var Class = {
	create: function () {
		return function () {
			this.initialize.apply(this, arguments);
		}
	}
}
*/

function Class (name) {
	
	Class.copyMethods(name, Class);
	if (name.prototype != undefined) {
		name.prototype.superclass = Function();
	}
	return name;
}

Class.copyMethods = function (target, container) {
	
	for (var items in container) {
		if (container[items] instanceof Function) {
			target[items] = container[items];
		}
	}
	
}

Class.extend = function (name) {
	
	if (name.prototype == undefined) {
		//this.prototype = name;
		
		this.copyMethods(this.prototype, name);
		this.prototype.superclass = function () {
			return name;
		};
		
		this.copyMethods(this.prototype.superclass, name);
		this.prototype.contructor = this;
		
	} else {
		this.prototype = name.prototype;
		
		this.prototype.superclass = function () {
			
			if (arguments.length == 0) {
				return new name();
			} else if (arguments.length == 1) {
				return new name(arguments[0]);
			} else {
				return name.apply(this, arguments);
			}
			
		};
		
		this.copyMethods(this.prototype.superclass, name.prototype);
		this.prototype.contructor = this;
	}
}
/** END Core Class.js **/

/** Begin Core BroadCaster.js **/

function Broadcaster () {
	
}

Class(Broadcaster);

Broadcaster.prototype._listeners = new Array();

Broadcaster.initialize = function (o, dontCreateArray) {
	if (o.broadcastMessage != undefined) delete o.broadcastMessage;
	o.addListener = Broadcaster.prototype.addListener;
	o.removeListener = Broadcaster.prototype.removeListener;
	if (!dontCreateArray) o._listeners = new Array();
}

Broadcaster.prototype.addListener = function (o) {
	this.removeListener (o);
	
	if (this.broadcastMessage == undefined) {
		this.broadcastMessage = Broadcaster.prototype.broadcastMessage;
	}
	return this._listeners.push(o);
}

Broadcaster.prototype.removeListener = function (o) {
	var a = this._listeners;	
	var i = a.length;
	while (i--) {
		if (a[i] == o) {
			a.splice (i, 1);
			if (!a.length) this.broadcastMessage = undefined;
			return true;
		}
	}
	return false;
}

Broadcaster.prototype.broadcastMessage = function () {
	var e = String(Array.prototype.slice.call(arguments).shift());
	var a = this._listeners.concat();
	var l = a.length;
	
	for (var i=0; i<l; i++) {
		a[i][e].apply(a[i], arguments);
	}
}
/** END Core BroadCaster.js **/


/** BEGIN Core Sprite.js **/
function Sprite (target) {
	this.target = (typeof target == "object") ? target : document.getElementById(target);
	this.addListener(this);
}

Class(Sprite);

Broadcaster.initialize(Sprite.prototype, false);

Sprite.prototype.target = new Object();
Sprite.prototype.resizeTimer = 0;

Sprite.prototype.onResized = new Function;

Sprite.prototype.startResizeTimer = function (interval) {
	var classPointer = this;
	this.stopResizeTimer();
	this.resizeTimer = setInterval(function() {classPointer.checkResize(classPointer.resizeTimer)}, (interval == undefined) ? 100 : interval);
}

Sprite.prototype.stopResizeTimer = function () {
	window.clearInterval(this.resizeTimer);
}

Sprite.prototype.checkResize = function (id) {
	
	this.checkResize.newFontSize = (window.getComputedStyle) ? window.getComputedStyle(this.target, '').getPropertyValue("font-size") : this.target.clientWidth;
	this.checkResize.newWidth = (window.getComputedStyle) ? window.getComputedStyle(this.target, '').getPropertyValue("width") : this.target.clientWidth;
	this.checkResize.newHeight = (window.getComputedStyle) ? window.getComputedStyle(this.target, '').getPropertyValue("height") : this.target.clientHeight;
	
	if (this.checkResize.newFontSize != undefined && this.checkResize.oldFontSize != this.checkResize.newFontSize) {
		this.broadcastMessage("onResized", this, this.checkResize);
		//this.broadcastEvent("resize", this, this.checkResize);
	} else if (this.checkResize.newWidth != undefined && this.checkResize.oldWidth != this.checkResize.newWidth) {
		this.broadcastMessage("onResized", this, this.checkResize);
		//this.broadcastEvent("resize", this, this.checkResize);
	} else if (this.checkResize.newHeight != undefined && this.checkResize.oldHeight != this.checkResize.newHeight) {
		this.broadcastMessage("onResized", this, this.checkResize);
		//this.broadcastEvent("resize", this, this.checkResize);
	}
	
	this.checkResize.oldFontSize = this.checkResize.newFontSize;
	this.checkResize.oldWidth = this.checkResize.newWidth;
	this.checkResize.oldHeight = this.checkResize.newHeight;
	
}
/** END Core Sprite.js **/


/** BEGIN Core UADetector.js **/
/**
 * Same logic as before. Have removed some detection capabilities in an effort
 * to reduce reliance on browser detection in general.
 * 
 * If there is a particualr detection you feel you need please file a ticket
 * with the request or add the detection yourself with the exact reason you
 * need said detection. Hopefully we'll be able to keep the number of
 * detection utility functions to a minimum in favor of behavioral testing.
 * 
 * This may also be the place for very common behavioral functions.
 * 
 */

function UADetector () {
	
}

Class(UADetector);

UADetector.prototype.getAgent = function () {
	return navigator.userAgent.toLowerCase();
}

// detect platform
UADetector.prototype.isMac = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/mac/i);
}

UADetector.prototype.isWin = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/win/i);
}

UADetector.prototype.isWin2k = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return this.isWin(agent) && (agent.match(/nt\s*5/i));
}

UADetector.prototype.isWinVista = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return this.isWin(agent) && (agent.match(/nt\s*6/i));
}

// detect browser
UADetector.prototype.isWebKit = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/AppleWebKit/i);
}

UADetector.prototype.isOpera = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/opera/i);
}

UADetector.prototype.isIE = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/msie/i);
}

UADetector.prototype.isIEStrict = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/msie/i) && !this.isOpera(agent);
}

UADetector.prototype.getIEVer = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return this.isIE(agent) && Number(agent.match(/\s[0-9]/i));
}

UADetector.prototype.isFirefox = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/firefox/i);
}

UADetector.prototype.isiPhone = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return agent.match(/iPhone/i);
}

// itunes compabibility
UADetector.prototype.isiTunesOK = function (userAgent) {
	var agent = userAgent || this.getAgent();
	return this.isMac(agent) || this.isWin2k(agent);
}

UADetector.prototype.isQTInstalled = function () {
	
	var qtInstalled = false;
	
	if(navigator.plugins && navigator.plugins.length) {
		
		for(var i=0; i < navigator.plugins.length; i++ ) {
			
			var plugin = navigator.plugins[i];
			
			if(plugin.name.indexOf("QuickTime") > -1) { 
				qtInstalled = true; 
			}
		}
	} else {
		qtObj = false; //global variable written to by vbscript for ie
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
		qtInstalled = qtObj;
	}
	
	return qtInstalled;
}

UADetector.prototype.getQTVersion = function () {
	
	var version = "0";
	
	if (navigator.plugins && navigator.plugins.length) {
		for (var i = 0; i < navigator.plugins.length; i++) {
			
			var plugin = navigator.plugins[i];
			
			//Match: QuickTime Plugin X.Y.Z
			var match = plugin.name.match(/quicktime\D*([\.\d]*)/i);
			if (match && match[1]) {
				version = match[1];
			}
		}
	} else {
		ieQTVersion = null; //global for vbscript to write to
		
		execScript('on error resume next: ieQTVersion = (Hex(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1").QuickTimeVersion)/1000000)','VBScript');

		if (ieQTVersion) {
			//only grab the major version:  7.xyz => 7
			version = (ieQTVersion +"").split(/\./)[0];
		}
	}
	
	return version;
}

UADetector.prototype.isQTCompatible = function (required, actual) {
	
	function areCompatible(required, actual) {
		
		var requiredValue = parseInt(required[0])
		if (isNaN(requiredValue)) {
			requiredValue = 0;
		}
		
		var actualValue = parseInt(actual[0])
		if (isNaN(actualValue)) {
			actualValue = 0;
		}
		
		if (requiredValue == actualValue) {
			if (required.length > 1) {
				return areCompatible(required.slice(1), actual.slice(1));
			} else {
				return true;
			}
		} else if (requiredValue < actualValue) {
			return true;
		} else {
			return false;
		}
	}

	var expectedVersion = required.split(/\./);
	var actualVersion = actual ? actual.split(/\./) : this.getQTVersion().split(/\./);
	
	return areCompatible(expectedVersion, actualVersion);
	
}

UADetector.prototype.isValidQTAvailable = function (required) {
	return this.isQTInstalled() && this.isQTCompatible(required)
}
/** END Core UADetector.js **/


/** BEGIN Core ColumnGroup.js **/

function ColumnGroup (target) {
	
	this.target = (typeof target == "object") ? target : document.getElementById(target);
	if (this.target == undefined) return;
	
	var container = document.createElement("div");
	container.id = "resize";
	
	container.style.width = "1em";
	container.style.height = "0px";
	container.style.overflow = "hidden";
	container.style.display = "block";
	
	document.body.appendChild(container);
	
	this.start();
	
}

Class(ColumnGroup);

ColumnGroup.prototype.target = new Object();
ColumnGroup.prototype.sprite = new Object();

ColumnGroup.prototype.update = function () {
	this.target.style.height = "";
	this.target.style.height = this.target.clientHeight + "px";
}

ColumnGroup.prototype.stop = function () {
	this.sprite.stopResizeTimer();
}

ColumnGroup.prototype.start = function () {
	
	var classPointer = this;
	
	this.sprite = new Sprite(document.body);
	this.sprite.startResizeTimer();
	
	var resizeListener = new Object();
	resizeListener.onResized = function () {
		classPointer.update();
	}
	
	this.sprite.addListener(resizeListener);
	
}
/** END Core ColumnGroup.js **/
