
/**
 * Вспомогательные функции
 * -----------------------------------------------
 * @copyright TimeZero <http://www.timezero.ru/>
 * @autor Dmitriy Shkinev <berkel@timezero.ru>
 * @version 2.0 2008-02-20
 */

function $(sId){

	if (sId.length > 1){

		var sPrefix = sId.substring(0, 1),
			sName = sId.substring(1);

		if (sPrefix == '#'){

			return document.getElementById(sName);

		} else if (sPrefix == '.'){

			return Common.getElementsByClassName(document, '*', sName);

		} else {

			return document.getElementById(sId);
		}

	} else {

		return document.getElementById(sId);
	}
}

function var_dump(oElem){

	var sStr = '';

	if (Common.inArray(typeof(oElem), ['string', 'number'])){

		sStr = oElem;

	} else {

		var sValue = '';

		for (var oItem in oElem){

			sValue = oElem[oItem];

			if (Common.inArray(oItem, ['innerHTML', 'outerHTML'])){

				sValue = sValue.replace(/</g, '&lt;').replace(/>/g, '&gt;');
			}

			sStr += 'obj.' + oItem + ' = ' + sValue + '\n';
		}
	}

	return sStr;
}

function debug(data){

	if (document.body){

		var oItemId = 'js-debug',
			oItem = $(oItemId);

		if (!oItem){

			oItem = document.createElement('DIV');
			oItem.setAttribute('id', oItemId);

			oItem.style.left = '0';
			oItem.style.top = '0';
			oItem.style.backgroundColor = 'yellow';
			oItem.style.color = '#000';
			oItem.style.width = '100%';

			document.body.insertBefore(oItem, document.body.firstChild);
		}

		oItem.innerHTML = '<pre style="margin:0;">' + var_dump(data) + '</pre>';

		oItem = null;

	} else {

		alert(var_dump(data));
	}
}

function return_false(){

	return false;
}

var Common = {

	/*********Стандартные вспомогательные функции***********/

	aImgs : [],

	preloadImages : function(){

		for (var i=0, length=arguments.length, iImgLength=0; i<length; i++){

			iImgLength = Common.aImgs.length;

			Common.aImgs[iImgLength] = new Image();
			Common.aImgs[iImgLength].src = arguments[i];
		}
	},

	informationObject : null,

	information : function(){

		var sUserAgent = navigator.userAgent.toLowerCase();

		if (window.opera){

			this.bOpera = true;
		}

		if (sUserAgent.indexOf('safari') != -1){

			this.bSafari = true;

		} else if (!this.bOpera && sUserAgent.indexOf('msie') != -1){

			this.bIE = true;

		} else if (navigator.product == 'Gecko'){

			this.bGecko = true;
			this.bMozilla = true;
		}

		if (sUserAgent.indexOf('mac') != -1){

			this.bMAC = true;
		}

		return this;
	},

	cloneObject : function(oObject){

		var oNewObject = {};

		for (var sProperty in oObject){

			oNewObject[sProperty] = oObject[sProperty];
		}

		return oNewObject;
	},

	inArray : function(sNeedle, aHaystack, bStrict){

		if (bStrict){

			function equals(a, b){

				return (a === b);
			}

		} else {

			function equals(a, b){

				return (a == b);
			}
		}

		for (var oItem in aHaystack){

			if (equals(aHaystack[oItem], sNeedle)){

				return true;
			}
		}

		return false;
	},

	recursiveInArray : function(sNeedle, aHaystack, bStrict){

		for (var i=0, length=aHaystack.length; i<length; i++){

			var oItem = aHaystack[i];

			if (Common.inArray(typeof(oItem), ['array', 'object'])){

				if (Common.recursiveInArray(sNeedle, oItem, bStrict)){

					return true;
				}

			} else {

				if (Common.inArray(sNeedle, aHaystack, bStrict)){

					return true;
				}
			}
		}

		return false;
	},

	getSelectedText : function(){

		var sText = '';

		if (window.getSelection){

			sText = window.getSelection();

		} else if (document.getSelection){

			sText = document.getSelection();

		} else if (document.selection){

			sText = document.selection.createRange().text;
		}

		return sText;
	},

	location : function(sUrl){

		window.location.href = sUrl;
	},

	getRandom : function(iMin, iMax){

		return (Math.round(Math.random() * (iMax - iMin)) + iMin);
	},

	between : function(iNum, iMin, iMax, bInclusive){

		if (bInclusive){

			return (iNum >= iMin && iNum <= iMax);

		} else {

			return (iNum > iMin && iNum < iMax);
		}
	},

	getScrollXY : function(){

		var iX = document.documentElement.scrollLeft || document.body.scrollLeft,
			iY = document.documentElement.scrollTop || document.body.scrollTop;

		return {x: iX, y: iY};
	},

	getElementsByClassName : function(oElem, sTagName, sClassName){

		var aElements = (sTagName == '*' && oElem.all) ? oElem.all : oElem.getElementsByTagName(sTagName);

		sClassName = sClassName.replace(/\-/g, '\\-');

		var oRegExp = new RegExp('(^|\\s)' + sClassName + '(\\s|$)');

		for (var i=0, oElement=null, aReturnElements=[], length=aElements.length; i<length; i++){

			oElement = aElements[i];

			if (oRegExp.test(oElement.className)){

				if (aReturnElements.push){

					aReturnElements.push(oElement);
				}
			}
		}

		return aReturnElements;
	},

	getAbsolutePos : function(oElem){

		var iX=0, iY=0;

		if (oElem){

			do {
				iX += oElem.offsetLeft;
				iY += oElem.offsetTop;

			} while (oElem = oElem.offsetParent);
		}

		return {x: iX, y: iY};
	},

	pairsStringGetValue : function(sText, sName, sFrom, sBefore){

		var sValue = '';

		if (sText){

			if (!sFrom){

				sFrom = '=';
			}

			if (!sBefore){

				sBefore = ';';
			}

			sText = sText.replace(new RegExp('(' + sBefore + ')\\s+', 'g'), '$1');

			var iStart = sText.indexOf(sBefore + sName + sFrom);

			if (iStart >= 0){

				iStart += (sBefore.length + sName.length + sFrom.length);

			} else {

				iStart = sText.indexOf(sName + sFrom);

				if (iStart == 0){

					iStart += (sName.length + sFrom.length);

				} else {

					iStart = -1;
				}
			}

			if (iStart >= 0){

				var iEnd = sText.indexOf(sBefore, iStart);

				if (iEnd < 0){

					iEnd = sText.length;
				}

				sValue = sText.substring(iStart, iEnd);
			}
		}

		return sValue;
	},

	/*******************************************************/

	//Класс для работы с Cookie
	Cookie : {

		set : function(sName, vValue, dExpires){

			document.cookie = sName + '=' + escape(vValue) + ((dExpires == null) ? '' : ('; expires=' + dExpires.toGMTString())) + '; path=/';
		},

		get : function(sName){

			return Common.pairsStringGetValue(document.cookie, sName);
		}
	},

	/*******************************************************/

	//Класс для работы с Event
	Event : {

		add : function(eOn, sEvent_type, ptrFunction){

			if (eOn.addEventListener){

				eOn.addEventListener(sEvent_type, ptrFunction, false);

			} else if (eOn.attachEvent && sEvent_type != 'selectstart'){

				if (!eOn[sEvent_type + ptrFunction]){

					eOn['e' + sEvent_type + ptrFunction] = ptrFunction;

					eOn[sEvent_type + ptrFunction] = function(){

						eOn['e' + sEvent_type + ptrFunction](window.event);
					};

					eOn.attachEvent('on' + sEvent_type, eOn[sEvent_type + ptrFunction]);
				}

			} else {

				eOn['on' + sEvent_type] = ptrFunction;
			}
		},

		remove : function(eOn, sEvent_type, ptrFunction){

			if (eOn.removeEventListener){

				eOn.removeEventListener(sEvent_type, ptrFunction, false);

			} else if (eOn.detachEvent && sEvent_type != 'selectstart'){

				if (eOn[sEvent_type + ptrFunction]){

					eOn.detachEvent('on' + sEvent_type, eOn[sEvent_type + ptrFunction]);

					eOn[sEvent_type + ptrFunction] = null;

					eOn['e' + sEvent_type + ptrFunction] = null;
				}

			} else {

				eOn['on' + sEvent_type] = null;
			}
		}
	},

	/*******************************************************/

	//Класс для работы с AJAX
	AJAX : {

		hError : 0,

		includeJSFiles : [],

		isLoading : false,

		createRequest : function(){

			var hRequest = null;

			if (window.XMLHttpRequest){

				hRequest = new XMLHttpRequest();

				if (hRequest.overrideMimeType){

					hRequest.overrideMimeType('text/xml');
				}

			} else if (window.ActiveXObject){

				var XMLHTTP_IDS = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];

				for (var i=0, c=XMLHTTP_IDS.length; i<c; i++){

					try	{

						hRequest = new ActiveXObject(XMLHTTP_IDS[i]);

						if (hRequest){

							break;
						}

					} catch (e){}
				}
			}

			return hRequest;
		},

		readyStateChange : function(hRequest, r_handler){

			r_handler(hRequest);

			if (hRequest.readyState == 4){

				this.isLoading = false;

				if (hRequest.status == 200){

					this.hError = 0;

				} else {

					this.hError = hRequest.status;
				}

			} else {

				this.isLoading = true;
			}
		},

		sendRequest : function(r_method, r_url, r_args, r_handler, r_synchronous){

			var eThis = this;

			var hRequest = eThis.createRequest();

			if (!hRequest){

				return false;
			}

			hRequest.onreadystatechange = function(){

				eThis.readyStateChange(hRequest, r_handler);
			};

			hRequest.open(r_method, r_url, r_synchronous || false);

			if (r_method.toLowerCase() == 'post'){

				hRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=utf-8');
				hRequest.send(r_args);

			} else {

				hRequest.send(null);
			}
		},

		evalJS : function(hRequest){

			if (hRequest.readyState == 4 && hRequest.status == 200){

				var code = hRequest.responseText;

				if (code){

					if (typeof(execScript) != 'undefined'){

						execScript(code);

					} else {

						window.eval(code);
					}
				}
			}
		},

		includeJSFileAsin : function(file){

			var eThis = this;

			eThis.includeJSFiles[file] = true;

			if (eThis.isLoading){

				setTimeout(

					function(){

						eThis.includeJSFileAsin(file);

					}, 10
				);

			} else {

				eThis.sendRequest('GET', file, '', eThis.evalJS, true);
			}
		},

		includeOnceJSFileAsin : function(file){

			if (this.includeJSFiles[file]){

				return true;
			}

			return this.includeJSFileAsin(file);
		},

		includeJSFile : function(file){

			var eThis = this;

			eThis.includeJSFiles[file] = false;

			eThis.sendRequest('GET', file, '', eThis.evalJS);

			if (!eThis.hError){

				eThis.includeJSFiles[file] = true;
			}

			return eThis.includeJSFiles[file];
		},

		includeOnceJSFile : function(file){

			if (this.includeJSFiles[file]){

				return true;
			}

			return this.includeJSFile(file);
		},

		getContent : function(r_method, r_url, r_args){

			var eThis = this;

			eThis.content = '';

			eThis.sendRequest(r_method, r_url, r_args, eThis._getContent);

			return eThis.content;
		},

		_getContent : function(hRequest){

			if (hRequest.readyState == 4 && hRequest.status == 200){

				var eThis = Common.AJAX;

				eThis.content = hRequest.responseText;
			}
		}
	},

	/*******************************************************/

	//Функции для работы с классами
	CSSClass : {

		switchClass : function(eOn, sClass_name, sInstead){

			if (this.match(eOn, sClass_name)){

				this.set(eOn, sInstead, sClass_name);

			} else {

				this.set(eOn, sClass_name, sInstead);
			}
		},

		remove : function(eOn, sClass_name){

			this.set(eOn, '', sClass_name);
		},

		change : function(eOn, sClass_name){

			if (eOn){

				sClass_name = (sClass_name.length) ? sClass_name.replace(/(^\s+|\s+$)/, '') : '';

				eOn.className = sClass_name;
			}
		},

		set : function(eOn, sClass_name, sInstead){

			if (eOn){

				sClass_name = (sClass_name.length) ? sClass_name.replace(/(^\s+|\s+$)/, '') : '';

				if (eOn.className.length){

					var sOld = sClass_name;

					if (sInstead && sInstead.length){

						sInstead = sInstead.replace(/\s+(\S)/g, '|$1');

						if (sOld){

							sOld += '|';
						}

						sOld += sInstead;
					}

					eOn.className = eOn.className.replace(new RegExp('(^|\\s+)(' + sOld + ')($|\\s+)', 'g'), '$1');
				}

				eOn.className += (eOn.className.length && sClass_name ? ' ' : '') + sClass_name;
			}
		},

		match : function(eOn, sClass_name){

			return (sClass_name && eOn.className && eOn.className.length && eOn.className.match(new RegExp('(^|\\s+)(' + sClass_name + ')($|\\s+)')));
		}
	},

	Serializer : {

		serialize : function(object){

			return this.recursiveSerialize(object, [], '').join('&');
		},

		recursiveSerialize : function(object, values, prefix){

			for (key in object){

				if (Common.inArray(typeof(object[key]), ['array', 'object'])){

					if (prefix.length > 0){

						prefix += '[' + key + ']';

					} else {

						prefix += key;
					}

					values = this.recursiveSerialize(object[key], values, prefix);

					prefixes = prefix.split('[');

					prefix = prefixes.slice(0, prefixes.length - 1).join('[');

				} else {

					value = encodeURIComponent(object[key]);

					if (prefix.length > 0){

						prefixed_key = prefix + '[' + key + ']';

					} else {

						prefixed_key = key
					}

					prefixed_key = encodeURIComponent(prefixed_key);

					if (value){

						values.push(prefixed_key + '=' + value);
					}
				}
			}

			return values;
		}
	},

	Timer : {

		start : function(){

			var date = new Date();

			this.time = 0;
			this.timer = date.getTime();
		},

		stop : function(){

			var date = new Date();

			this.time = date.getTime() - this.timer;
			this.timer = 0;

			return this.getTime();
		},

		getTime : function(){

			return this.time / 1000;
		}
	}
};

Common.informationObject = new Common.information();