// Created with MAX's HTML Beauty++ 2004

var _ua = navigator.userAgent.toLowerCase();
var browser = {
	version: (_ua.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
	opera: /opera/i.test(_ua),
	msie: (!this.opera && /msie/i.test(_ua)),
	msie6: (!this.opera && /msie 6/i.test(_ua)),
	msie8: (!this.opera && /msie 8/i.test(_ua)),
	mozilla: /firefox/i.test(_ua),
	chrome: /chrome/i.test(_ua),
	safari: (!(/chrome/i.test(_ua)) && /webkit|safari|khtml/i.test(_ua)),
	iphone: /iphone/i.test(_ua)
}

// Псевдо ООП ////////////////////////////////////////////////////
function CreateClass(parentClass, prop) {
	var cls = function() {
		if (cls.preparing) return delete(cls.preparing);
		if (cls.constr) {
			this.constructor = cls;
			cls.constr.apply(this, arguments);
		}
	}
	cls.prototype = {};
	if (parentClass) {
		parentClass.preparing = true;
		cls.prototype = new parentClass;
		cls.prototype.constructor = parentClass;
		cls.constr = parentClass;
	}
	if (prop) {
		var cname = "constructor";
		for (var k in prop) {
			if (k != cname) cls.prototype[k] = prop[k];
		}
		if (prop[cname] && prop[cname] != Object)
			cls.constr = prop[cname];
	}
	return cls;
}

///////////////////////////////////////////////////////////////////
// Работа с объектами /////////////////////////////////////////////

function clone(pObject) {
	var newObj = {};
	for (var p in pObject)
		newObj[p] = pObject[p];
	return newObj;
}

function each(pObject, pCallback) {
	for (var p in pObject) {
		if (pCallback.call(pObject[p], p, pObject[p]) === false )
			return pObject;
	}
	return pObject;
}

// Работа с элементами ////////////////////////////////////////////

function element_get(sID) {
	return document.getElementById(sID);
}

function element_create(sName, pAttributes) {
	if (!pAttributes)
		return document.createElement(sName);
	var e	= document.createElement(sName);
	for (var a in pAttributes)
		e.setAttribute(a, pAttributes[a]);
	return e;
}

function element_child_create(pElement, sChildName, pAttributes) {
	return pElement.appendChild(element_create(sChildName, pAttributes));
}
	// CSS
	function element_style(pElement, cssName, cssValue) {
		if (typeof(cssName) == 'object') {
			return each(cssName, 
				function(sName, sValue){
					element_style(pElement, sName, sValue);
				}
			);
		}
		if (cssName == 'opacity') {
			if (browser.msie) {
				pElement.style.filter = String(cssValue).length != 0 ? "alpha(opacity=" + parseInt(cssValue)*100 + ")" : '';
				pElement.style.zoom = 1;
			} else
				elem.style.opacity = cssValue;
		} else {
			var isNum = typeof(cssValue) == 'number' && !(/z-?index|font-?weight|opacity|zoom|line-?height/i).test(cssName);
			if(isNum && cssValue < 0 && (/^(width|height)$/i).test(cssName)){
				cssValue = 0; //fix for IE;
			}
			pElement.style[cssName] = cssValue + (isNum ? 'px' : '');
		}
	}
	
	function element_hasClass(pElement, clsName) {
		return pElement && (new RegExp('(\\s|^)' + clsName + '(\\s|$)')).test(pElement.className);
	}
	
	function element_clsAdd(pElement, clsName) {
		if (!pElement)	return false;
		if (element_hasClass(pElement, clsName)) return true;
		pElement.className = (pElement.className ? pElement.className + ' ' : '') + clsName;
		return !!pElement.className;
	}
	
	function element_clsRemove(pElement, clsName) {
		if (!pElement)	return false;
		if (!element_hasClass(pElement, clsName))	return true;
		pElement.className = pElement.className.replace((new RegExp('(\\s|^)' + clsName + '(\\s|$)')), ' ');
		return true;
	}
	
	//EVENTS
	function element_evtHandler(pElement, sEvtType, pHandler) {
		if (!pElement || pElement.nodeType == 3 || pElement.nodeType == 8 )
			return false;
    	// For whatever reason, IE has trouble passing the window object
  		// around, causing it to be cloned in the process
		if (pElement.setInterval && pElement != window)
			pElement = window;
			
		if (pElement.addEventListener)
			pElement.addEventListener(sEvtType, pHandler, false);
		else if (pElement.attachEvent)
			pElement.attachEvent('on' + sEvtType, pHandler);
		else
			return false;
		pElement	= null;
		return true;
	}

// EVENTS /////////////////////////////////////////////////////////////////////////////////
function event_get(pEvent) {
	var originalEvent = pEvent || window.event;
	e = clone(originalEvent);
	e.originalEvent = originalEvent;
	
	if (!e.target)
		e.target = e.srcElement || document;
	
	// check if target is a textnode (safari)
	if (e.target.nodeType == 3 )
		e.target = e.target.parentNode;
	
	if (!e.relatedTarget && e.fromElement)
		e.relatedTarget = e.fromElement == e.target;
	
	if (e.pageX == null && e.clientX != null ) {
		var de = document.documentElement, b = document.body;
		e.pageX = e.clientX + (de && de.scrollLeft || b && b.scrollLeft || 0) - (de.clientLeft || 0);
		e.pageY = e.clientY + (de && de.scrollTop || b && b.scrollTop || 0) - (de.clientTop || 0);
	}
	
	if ( !e.which && ((e.charCode || e.charCode === 0) ? e.charCode : e.keyCode) )
		e.which = e.charCode || e.keyCode;
	
	// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
	if ( !e.metaKey && e.ctrlKey )
		e.metaKey = e.ctrlKey;
	
	// Add which for click: 1 == left; 2 == middle; 3 == right
	// Note: button is not normalized, so don't use it
	if ( !e.which && e.button )
		e.which = (e.button & 1 ? 1 : ( e.button & 2 ? 3 : ( e.button & 4 ? 2 : 0 ) ));

	return e;
}
