
// **************************
// * Версия 2.0             *
// * without canvas         *
// *                        *
// **************************


var $ = function(){
	if (arguments[0]) {
		if (typeof arguments[0]=='string')	this.el=document.getElementById(arguments[0]);
		else this.el = arguments[0];
	};
	this.empty = function(){
		while(this.el.childNodes.length)
		this.el.removeChild(this.el.childNodes[0]);
		return this;
	};
	this.hide = function(){
		var obj = arguments[0]||this.el;
		if ((obj.style.display||'')== 'none') return this;
		obj._old_display = (obj.style.display||'');
		obj.style.display = 'none';
	    return this;
	};
	this.opacity = function(){
		var val = (arguments.length?arguments[0]:50);
		this.el.style.opacity = (val/100);
		return this;
	};
	this.show = function(){
		var obj = arguments[0]||this.el;
		obj.style.display = (obj._old_display||'');
	    return this;
	};
	this.css = function(obj){
		if(obj && this.el)
			for(var i in obj)
				if (i!='float') this.el.style[i] = obj[i].toString();
				else {
					this.el.style.cssFloat = obj.float.toString();
					this.el.style.styleFloat = obj.float.toString();
				}
		return this;
	};
	this.attr = function(obj){
		if(obj)	for(var i in obj) this.el[i]=obj[i];
		return this;
	};
	this.hideCnt = function(){
		this.el._content_display == 'none';
		var len = this.el.childNodes.length;
		for (var i=0; i<len; i++)
			if (this.el.childNodes[i].nodeType==1) this.hide(this.el.childNodes[i]);
	    return this;
	};
	this.showCnt = function(){
		this.el._content_display == 'block';
		var len = this.el.childNodes.length;
		for (var i=0; i<len; i++)
			if (this.el.childNodes[i].nodeType==1)this.show(this.el.childNodes[i]);
		return this;
	};
	this.drop=function(){
		try{
			this.el.parentNode.removeChild(this.el);
		}catch(e){}
		return this;
	};
	this.create = function(name){
        this.el = document.createElement(name);
        return this;
	};
	this.appendChild = function(obj){
       	if (typeof obj == 'string') obj = document.getElementById(obj);
		else if (typeof obj == 'object' && obj.nodeType != 1) return this;
       	this.el.appendChild(obj);
       	return this;
	};
	this.appendTo = function(){
       	var o = arguments[0];
       	if (typeof(o)=='string'){
       		document.getElementById(o).appendChild(this.el);
       	}else o.appendChild(this.el);
       	return this;
	};
	this.appendBefore = function(){
		var obj = arguments[0];
		if (typeof obj == 'string') obj = document.getElementById(obj);
		else if (typeof obj == 'object' && obj.nodeType != 1) return this;
		var par = obj.parentNode;
		par.insertBefore(this.el, obj);
		return this;
	};
	this.replaceObj = function(){
		var obj = arguments[0];
		if (typeof obj == 'string') obj = document.getElementById(obj);
		else if (typeof obj == 'object' && obj.nodeType != 1) return this;
		var par = obj.parentNode;
		par.replaceChild(this.el, obj);
		return this;
	};
	//Метод cloneNode создает копию данного узла и возвращает ее.
	//Если аргумент равен true или отсутствует, то создается копия поддерева документа, начиная с данного узла;
	//если он равен false, то копируется только сам узел (и его атрибуты, если это Element).
	this.cloneObj = function(){
		var p = !arguments.length?true:Boolean(arguments[0]);
		return (this.el.cloneNode(p));
	};
	// 1,true = as offsetParent
	this.position = function () {
		var x = 0;
		var y = 0;
		var obj = this.el;
		if (arguments[0]) {
			var parent = $(arguments[0]).el;
			if (!parent || !parent.nodeType) return {x:parseInt(obj.offsetLeft), y:parseInt(obj.offsetTop)};
			do{
				x += parseInt(obj.offsetLeft)+parseInt((obj.offsetParent && obj.offsetParent.scrollLeft && !browser.ie)?obj.offsetParent.scrollLeft:0);
				y += parseInt(obj.offsetTop)+parseInt((obj.offsetParent && obj.offsetParent.scrollTop && !browser.ie)?obj.offsetParent.scrollTop:0);
				obj = obj.offsetParent;
			}while(obj!=parent && obj!=parent.offsetParent);
		}
		else {
			do{
				x += parseInt(obj.offsetLeft)+parseInt((obj.offsetParent && obj.offsetParent.scrollLeft && !browser.ie)?obj.offsetParent.scrollLeft:0);
				y += parseInt(obj.offsetTop)+parseInt((obj.offsetParent && obj.offsetParent.scrollTop && !browser.ie)?obj.offsetParent.scrollTop:0);
				obj = obj.offsetParent;
			}while(obj);
		};
		return {x:x, y:y};
	};
	this.size = function(){
		if (this.el) return {width:parseInt((this.el.clientWidth||this.el.offsetWidth||parseInt(this.el.style.width))),
			height:parseInt((this.el.clientHeight||this.el.offsetHeight||parseInt(this.el.style.height)))};
		else return {width:0, height:0};
	};
	return this;
};

var E = function(){
	var e = (arguments[0]||window.event||null);
	this.button = null; // 1-left; 2-middle; 3-right;
	if (e){
		this.x = e.clientX;// возвращает координаты мыши x
		this.y = e.clientY;// возвращает координаты мыши y
		this.target = e.target||e.srcElement;
		this.keyCode = (e.keyCode||null);
		this.shiftKey = e.shiftKey;
		this.altKey = e.altKey;
		this.ctrlKey = e.ctrlKey;
		this.elX = (e.offsetX||e.layerX);
		this.elY = (e.offsetY||e.layerY);
		this.type = (e.type.toLowerCase().indexOf('on')===0?e.type.toLowerCase().substring(2):e.type.toLowerCase());
		this.related = (e.relatedTarget||e.toElement||e.fromElement||null);
		if(!e.which) {
			var b = [0,1,3,3,2];
			this.button = b[e.button];//(e.button<2)?'left':((e.button==4)?'middle':'right');
		}
		else this.button = e.which;
	}
	this.e = e;
	// прекращает всплытие события
	this.stop = function(){
		if (!e) return null;
	  	if (e.preventDefault) {e.preventDefault();} else {e.returnValue = false;}
	  	if (e.stopPropagation) {e.stopPropagation();} else {e.cancelBubble = true;}
	  	e.stopped = true;
	  	return false;
	};
	//добавить обработчик события на узел
	this.addEvent = function(eventName, action){
		var node = arguments[2]||this.target;
		if (!node) return;
		if (node.addEventListener) node.addEventListener(eventName, action, true);
		else node.attachEvent('on'+eventName, action);
	};
	//удалить обработчик события на узеле
	this.dropEvent = function(eventName, action){
		var node = arguments[2]||this.target;
		if (!node) return;
		if (node.removeEventListener) node.removeEventListener(eventName, action, true);
		else node.detachEvent('on'+eventName, action);
	};
};

var counterObjects = 0;
var Classes = {};
var CasheClasses = {};
function newClass(base, ext){
	var _base = base;
	var _ext = (typeof ext == 'function')?ext:function(){};

	return function(){
		this._parent = function(classname){
			return (window.CasheClasses[classname])||(window.CasheClasses[classname] = new (window.Classes[classname])(null, 1));
		};
		if (_base){
			var __base = new(_base)();
			for(var i in __base) this[i] = __base[i];
		};
		var __ext = new(_ext)();
		for(var i in __ext) this[i] = __ext[i];

		var o = (arguments[0]||{});
		for(var i in o) this[i] = (o[i]);

		if (!this.id && !arguments[1])this.id = '_'+(++counterObjects);
	};
}

var IE = (window.navigator.userAgent.indexOf ("MSIE") >= 0);

function px(x){return parseInt(x)+'px';};
function pr(x){return x+'%';};

String.prototype.toFirstUpperCase = function(){ return this.substr(0,1).toUpperCase()+this.substr(1)};
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
Array.prototype.search = function(val){
	for(var i=0; i<this.length; i++){
		if (this[i] === val) return i;
	}
	return -1;
};

var Ajax = function(){
	this.onSuccess = function(){};
	this.onError = function(){};
	this.xmlHttpReq = null;
	this.asinc = true;
	this.method = 'post';
	
	this._getRequestObject = function(){
		if (window.XMLHttpRequest) this.xmlHttpReq = new XMLHttpRequest();
	    else if (window.ActiveXObject) this.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    else throw new Error("There was a problem instantiating the XMLHttpRequest");
	};
	
	this.send = function(strURL, data){
		var _self = this;
	    data = (data||{});
	    this._getRequestObject();
	    if (this.method == 'post') this.xmlHttpReq.open('POST', strURL, this.asinc);
	    else{
			var url = strURL+'?'+this.getQueryString(data);
			this.xmlHttpReq.open('GET', url, this.asinc);
		}
	    
	    this.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    this.xmlHttpReq.setRequestHeader('X_REQUESTED_WITH', 'XMLHttpRequest');
	    this.xmlHttpReq.onreadystatechange = function() {
			if (this.readyState == 4) {
	            if (this.status == 200) _self.onSuccess(this);
	            else _self.onError(this);
	        }
	    };
	    this.xmlHttpReq.send(this.getQueryString(data));
	    if (!this.asinc){
			if (this.xmlHttpReq.status == 200) this.onSuccess(this.xmlHttpReq);
			else this.onError(this.xmlHttpReq);
		}
	};

	this.getQueryString=function(data) {
		data = data||{};
        var qstr =[];
        if (IE) data.ie_ajax_request = 1;
        for(var i in data)
	    qstr.push(encodeURIComponent(i)+'=' + encodeURIComponent(data[i].toString()));
	    return qstr.join('&');
	};
};

var XML = newClass(null,function(){
	this.load_file = function(xmlfile){
		if(document.implementation && document.implementation.createDocument)
	    {   var doc = document.implementation.createDocument("","",null);
	    }
	    else if(window.ActiveXObject)
	    {
	    	var doc = new ActiveXObject ('Microsoft.XMLDOM');
	    }
	    doc.async = false;
	    doc.load(xmlfile);
	    return doc || null;
	};
	this.load_string = function(xmlstr){
		if(document.implementation && document.implementation.createDocument)
	    {    var parser = new DOMParser();
	         var doc = parser.parseFromString(xmlstr,"text/xml");
	    }
	    else if(window.ActiveXObject)
	    {
	    	var doc = new ActiveXObject ('Microsoft.XMLDOM');
	        doc.async = false;
	        doc.loadXML(xmlstr);
	    }
	    return doc || null;
	};
	this.xslt = function(xmldoc,xsldoc){
		if(document.implementation && document.implementation.createDocument)
	    {   var proc = new XSLTProcessor();
	        proc.importStylesheet(xsldoc);
	        var xhtmldoc = proc.transformToDocument(xmldoc);
	        var ser = new XMLSerializer();
	        return ser.serializeToString(xhtmldoc);
	    }
	    else if(window.ActiveXObject) return xmldoc.transformNode(xsldoc);
		return null;
	};
	this.asXML = function(xmldoc){
		if(document.implementation && document.implementation.createDocument)
	    {   var ser = new XMLSerializer();
	        return ser.serializeToString(xmldoc);
	    }
	    else if(window.ActiveXObject) return xmldoc.xml;
		return null;
	};
});

function serialize( mixed_value ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Arpad Ray (mailto:arpad@php.net)
    // +   improved by: Dino
    // +   bugfixed by: Andrej Pavlovic
    // +   bugfixed by: Garagoth
    // %          note: We feel the main purpose of this function should be to ease the transport of data between php & js
    // %          note: Aiming for PHP-compatibility, we have to translate objects to arrays
    // *     example 1: serialize(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'a:3:{i:0;s:5:"Kevin";i:1;s:3:"van";i:2;s:9:"Zonneveld";}'
    // *     example 2: serialize({firstName: 'Kevin', midName: 'van', surName: 'Zonneveld'});
    // *     returns 2: 'a:3:{s:9:"firstName";s:5:"Kevin";s:7:"midName";s:3:"van";s:7:"surName";s:9:"Zonneveld";}'

    var _getType = function( inp ) {
        var type = typeof inp, match;
        var key;
        if (type == 'object' && !inp) {
            return 'null';
        }
        if (type == "object") {
            if (!inp.constructor) {
                return 'object';
            }
            var cons = inp.constructor.toString();
            if (match = cons.match(/(\w+)\(/)) {
                cons = match[1].toLowerCase();
            }
            var types = ["boolean", "number", "string", "array"];
            for (key in types) {
                if (cons == types[key]) {
                    type = types[key];
                    break;
                }
            }
        }
        return type;
    };
    var type = _getType(mixed_value);
    var val, ktype = '';

    switch (type) {
        case "function":
            val = "";
            break;
        case "undefined":
            val = "N";
            break;
        case "boolean":
            val = "b:" + (mixed_value ? "1" : "0");
            break;
        case "number":
            val = (Math.round(mixed_value) == mixed_value ? "i" : "d") + ":" + mixed_value;
            break;
        case "string":
			var _len = mixed_value.length;
			for(var jj=0; jj<mixed_value.length; jj++) if (mixed_value.charCodeAt(jj)>256) ++_len;
            val = "s:" + _len + ":\"" + mixed_value + "\"";
            break;
        case "array":
        case "object":
            val = "a";
            /*
            if (type == "object") {
                var objname = mixed_value.constructor.toString().match(/(\w+)\(\)/);
                if (objname == undefined) {
                    return;
                }
                objname[1] = serialize(objname[1]);
                val = "O" + objname[1].substring(1, objname[1].length - 1);
            }
            */
            var count = 0;
            var vals = "";
            var okey;
            var key;
            for (key in mixed_value) {
                ktype = _getType(mixed_value[key]);
                if (ktype == "function") {
                    continue;
                }

                okey = (key.match(/^[0-9]+$/) ? parseInt(key) : key);
                vals += serialize(okey) +
                        serialize(mixed_value[key]);
                count++;
            }
            val += ":" + count + ":{" + vals + "}";
            break;
    }
    if (type != "object" && type != "array") val += ";";
    return val;
}

function print_r(theObj){
	var str = '<ul style="padding:0 0 0 15px; list-style-type:none; margin:2px 0;">';
	if(theObj !== null && (theObj.constructor == Array || theObj.constructor == Object  || theObj.constructor == Function)){
		if (!arguments[1]) str += "<li>"+(theObj.constructor == Array?'array[':'object{');

		for(var p in theObj){
			if (theObj[p] == null){
				str += "<li>["+p+"]&nbsp;=>&nbsp;null</li>";
			}
			else if(theObj[p].constructor == Array || theObj[p].constructor == Object  || theObj.constructor == Function){
				str += "<li>["+p+"]&nbsp;=>&nbsp;"+(theObj[p].constructor == Array?'array[':'object{');
				str += print_r(theObj[p], 1);
				str += (theObj[p].constructor == Array?']':'}')+"</li>";
			}
			else str += "<li>["+p+"]&nbsp;=>&nbsp;"+theObj[p]+"</li>";
		}

		if (!arguments[1]) str += (theObj.constructor == Array?']':'}')+"</li>"
	}
	else {
		var type = theObj !== null?theObj.constructor:null;
		switch(type){
			case String: type = 'string'; break;
			case Number: type = 'number'; break;
			case Boolean: type = 'boolean'; break;
			case null: type = '---'; break;
		}
		str += '<li>['+type+']&nbsp;=>&nbsp;'+theObj+'</li>';
	}
	str += '</ul>';
	return str;
}

function clearSelection(){
	try{
		if (window.getSelection) var selected_obj = window.getSelection();
		else if (document.getSelection) var selected_obj = document.getSelection();
		else if (document.selection) var selected_obj = document.selection;
	}catch (e){return;}
	if (!selected_obj) return;
	if (selected_obj.empty) selected_obj.empty();
	else if (selected_obj.removeAllRanges) selected_obj.removeAllRanges();
};

function dump(){
	var str = '', i=0;
	while(typeof arguments[i]!='undefined') {str += print_r(arguments[i]); i++;};
	$('dump').el.innerHTML += str;
}

function clone(o) {
	if(!o || 'object' !== typeof o) return o;
	var c = 'function' === typeof o.pop ? [] : {};
	var p, v;
	for(p in o){
		if(o.hasOwnProperty(p)) {
			v = o[p];
			if(v && 'object' === typeof v) c[p] = clone(v);
			else c[p] = v;
		}
	}
	return c;
}

