/**
 * Copyright (c) Soft Dreams, All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * @copyright Copyright (c) Soft Dreams, All rights reserved.
 * @author The great folks at the Soft Dreams.
 */
 
var agt = navigator.userAgent.toLowerCase();

var isDom     = (document.getElementById ? true:false);
var isIe      = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var isOpera   = (agt.indexOf('opera') != -1);
var isGecko   = (agt.indexOf('gecko') != -1);
var isFirefox = (agt.indexOf('firefox') != -1);

var Proton = {
	Version: '100',
	EventHandlers: new Array(),

	GetElementsByTagNames: function(list,obj)
	{
		if(!obj)
		{
			var obj = document;
		}
		var tagNames = list.split(',');
		var resultArray = new Array();
	
		for(var i=0;i<tagNames.length;i++)
		{
			var tags = obj.getElementsByTagName(tagNames[i]);
			for (var j=0;j<tags.length;j++)
			{
				resultArray.push(tags[j]);
			}
		}
		
		var testNode = resultArray[0];
		if(!testNode)
		{
			return [];
		}
		
		if(testNode.sourceIndex)
		{
			resultArray.sort(function (a,b) {
					return a.sourceIndex - b.sourceIndex;
			});
		}
		else if (testNode.compareDocumentPosition)
		{
			resultArray.sort(function (a,b) {
					return 3 - (a.compareDocumentPosition(b) & 6);
			});
		}
		return resultArray;
	},
	
	RegisterEvent: function(eventName, handlerRef, domElement, raiseEvent)
	{
		if(raiseEvent == true)
		{
			handlerRef();
		}
		
		if(domElement = $(domElement))
		{
			if( typeof this.EventHandlers[domElement] == 'undefined' )
			{
				this.EventHandlers[domElement] = new Array();
			}
			if( typeof this.EventHandlers[domElement][eventName] == 'undefined' )
			{
				this.EventHandlers[domElement][eventName] = new Array();
			}
			this.EventHandlers[domElement][eventName].push(handlerRef);

			var eventHandler = function(evt)
			{
				var evt = evt ? evt : (window.event ? event : null);
				var maxHandlers = this.EventHandlers[domElement][eventName].length;
				for(var iterator=0; iterator<maxHandlers; iterator++)
				{
					this.EventHandlers[domElement][eventName][iterator](evt);
				}
			};
			eventHandler.bind(Proton);
			
			if(isIe)
			{
				domElement.attachEvent('on'+eventName, eventHandler.bindAsEventListener(this));
			}
			else if(isDom)
			{
				domElement.addEventListener(eventName, eventHandler.bindAsEventListener(this), false);
			}
		}
	}
}

var Window = Class.create();
Window.prototype = {
	width:   null,
	height:  null,
	scrollY: null,
	scrollX: null,
	
	initialize: function()
	{
		if( typeof( window.innerWidth ) == 'number' )
		{
			//Non-IE
			this.width = window.innerWidth;
			this.height = window.innerHeight;
		}
		else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
		{
			//IE 6+ in 'standards compliant mode'
			this.width = document.documentElement.clientWidth;
			this.height = document.documentElement.clientHeight;
		}
		else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
		{
			//IE 4 compatible
			this.width = document.body.clientWidth;
			this.height = document.body.clientHeight;
		}
		
		if( typeof( window.pageYOffset ) == 'number' )
		{
			//Netscape compliant
			this.scrollY = window.pageYOffset;
			this.scrollX = window.pageXOffset;
		}
		else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) )
		{
			//DOM compliant
			this.scrollY = document.body.scrollTop;
			this.scrollX = document.body.scrollLeft;
		}
		else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) )
		{
			//IE6 standards compliant mode
			this.scrollY = document.documentElement.scrollTop;
			this.scrollX = document.documentElement.scrollLeft;
		}
	}
}