/*
------------------------------------
 Quick detection of W3C DOM support
------------------------------------
*/
//var W3CDOM = (document.createElement && document.getElementsByTagName);
var W3CDOM = (document.getElementById && document.childNodes && document.createElement);

var siteRoot = '/new_site/';

if ($.browser.msie) {
	var ieVersion = parseFloat(navigator.userAgent.substr(navigator.userAgent.toLowerCase().indexOf('msie') + 5));
}

/*
---------------------------------------------------------------------------------------

	Create Element

---------------------------------------------------------------------------------------

 Description:
  This function allows you to use document.createElementNS when serving pages
  as "application/xhtml+xml", but fall back to document.createElement if the
  former is not supported in a browser.

 Author:
  Simon Willison

 URL:
  http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
  
---------------------------------------------------------------------------------------
*/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
};


/*
---------------------------------------------------------------------------------------

	Event Handlers

---------------------------------------------------------------------------------------

 Description:
  These functions allow you to add/remove event handlers without having
  to worry about browser differences. They are a much more up to date
  version of the original functions by Scott Andrew LePera.
  (http://www.scottandrew.com/weblog/articles/cbs-events)

 Author's Description:
  PPK’s addEvent() Recoding Contest (1) produced a number of very similar
  solutions. The eventual winner was chosen because of it’s simplicity.
  All of the solutions relied on object detection to some degree.

  My solution is very different.

    * it performs no object detection
    * it does not use the addeventListener/attachEvent methods
    * it keeps the correct scope (the this keyword)
    * it passes the event object correctly
    * it is entirely cross-browser (it will probably work on IE4 and NS4)
    * and from what I can tell it does not leak memory

  (1) http://www.quirksmode.org/blog/archives/2005/09/addevent_recodi.html


 Author:
  Dean Edwards

 URLs:
  http://dean.edwards.name/weblog/2005/10/add-event/
  http://dean.edwards.name/weblog/2005/10/add-event2/
  
 Modifications:
  Tino Zijdel - crisp@xs4all.nl || http://therealcrisp.xs4all.nl/upload/addEvent_dean.html
  
---------------------------------------------------------------------------------------
*/
function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element['on' + type]) {
				handlers[0] = element['on' + type];
			}
			element['on' + type] = handleEvent;
		}
	
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else if (element.events && element.events[type] && handler.$$guid) {
		// delete the event handler from the hash table
		delete element.events[type][handler.$$guid];
	}
};

function handleEvent(event) {
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(window.event);
	var returnValue = true;
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		if (!Object.prototype[i]) {
			this.$$handler = handlers[i];
			if (this.$$handler(event) === false) {
				returnValue = false;
			}
		}
	}

	if (this.$$handler) {
		this.$$handler = null;
	}

	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};

// This little snippet fixes the problem that the onload attribute on the body-element will overwrite
// previous attached events on the window object for the onload event
if (!window.addEventListener) {
	document.onreadystatechange = function() {
		if (window.onload && window.onload != handleEvent) {
			addEvent(window, 'load', window.onload);
			window.onload = handleEvent;
		}
	};
}


/*
---------------------------------------------------------------------------------------

	Gecko 1.8 Array methods for other browsers

---------------------------------------------------------------------------------------

 URL:
  http://www.bigbold.com/snippets/posts/show/718

*/
if (!Array.prototype.forEach) {
	Array.prototype.forEach = function(callback, thisObject) {
		for (var i = 0, len = this.length; i < len; i++) {
			callback.call(thisObject, this[i], i, this);
		}
	};
}
if (!Array.prototype.map) {
	Array.prototype.map = function(callback, thisObject) {
		for (var i = 0, res = [], len = this.length; i < len; i++) {
			res[i] = callback.call(thisObject, this[i], i, this);
		}
		return res;
	};
}
if (!Array.prototype.filter) {
	Array.prototype.filter = function(callback, thisObject) {
		for (var i = 0, res = [], len = this.length; i < len; i++)
			callback.call(thisObject,this[i],i,this) && res.push(this[i]);
		return res;
	};
}
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(searchElement, fromIndex) {
		var i = (fromIndex < 0) ? this.length + fromIndex : fromIndex || 0;
		for (; i < this.length; i++) {
			if (searchElement === this[i]) {
				return i;
			}
		}
		return -1;
	};
}
if (!Array.prototype.lastIndexOf) {
	Array.prototype.lastIndexOf = function(searchElement, fromIndex){
		var max = this.length - 1;
		var i = (fromIndex < 0) ? Math.max(max + 1 + fromIndex, 0) :
			(fromIndex > max) ? max : max - (fromIndex || 0) || max;
		for (; i >= 0; i--) {
			if (searchElement === this[i]) {
				return i;
			}
		}
		return -1;
	};
}
if (!Array.prototype.every) {
	Array.prototype.every = function(callback, thisObject) {
		for (var i = 0, len = this.length; i < len; i++) {
			if (!callback.call(thisObject, this[i], i, this)) {
				return false;
			}
		}
		return true;
	};
}
if (!Array.prototype.some) {
	Array.prototype.some = function(callback, thisObject) {
		for (var i = 0, len = this.length; i < len; i++) {
			if (callback.call(thisObject, this[i], i, this)) {
				return true;
			}
		}
		return false;
	};
}
