// Mozilla analogs
if (window.Node && window.XMLSerializer) {Node.prototype.__defineGetter__('outerHTML', function() {return new XMLSerializer().serializeToString(this);});}
if (window.Node) {Node.prototype.__defineGetter__('innerText', function() {if (this.nodeType == 3) return this.nodeValue; else {var result = '';for (var child = this.firstChild; child; child = child.nextSibling) result += child.innerText;return result;}});}
// ---------------

function allGetElementById(id) {return document.all[id];}
function evalGetElementById(id) {eval("var e=self."+id+";");return e;}
function execGetElementById(id) {window.execScript("var e=self."+id+";","JavaScript");return e;}
function nullGetElementById(id) {return null;}
if (document.getElementById){;}
else if(document.all) {document.getElementById=allGetElementById;}
else if(eval && self) {document.getElementById=evalGetElementById;}
else if(window.execScript && self) {document.getElementById=execGetElementById;}
else {document.getElementById=nullGetElementById;}

function isUndefined(o) {return (typeof o == 'undefined' || o == null);}
if (isUndefined($)) var $ = document.getElementById;

function str_replace(search, replace, subject) {return subject.split(search).join(replace);}

function trim (str) {
	var	str = str.replace(/^\s\s*/, ''), ws = /\s/, i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
	}

function printWindow(w)
	{
	if (w.print) w.print();
	else {
        var WebBrowser = '<object id="WebBrowser1" width=0 height=0 classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2"></object>';
        w.document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
        WebBrowser1.ExecWB(6, 2);
		}
	}

function open_win(what_link,the_x,the_y,toolbar,addressbar,directories,statusbar,menubar,scrollbar,resize,history,pos,wname)
	{
    var the_url = what_link;
    the_x -= 0;
    the_y -= 0;
    var how_wide = screen.availWidth;
    var how_high = screen.availHeight;

    var the_toolbar = toolbar == "0" ? "no" : "yes";
    var the_addressbar = addressbar == "0" ? "no" : "yes";
    var the_directories = directories == "0" ? "no" : "yes";
    var the_statusbar = statusbar == "0" ? "no" : "yes";
    var the_menubar = menubar == "0" ? "no" : "yes";
    var the_scrollbars = scrollbar == "0" ? "no" : "yes";
    var the_do_resize = resize == "0" ? "no" : "yes";
    var the_copy_history = history == "0" ? "no" : "yes";
	switch(pos)
		{
		case 1: top_pos=0; left_pos=0; break;
		case 2: top_pos = 0; left_pos = (how_wide/2)-(the_x/2); break;
		case 3: top_pos = 0; left_pos = how_wide - the_x; break;
		case 4: top_pos = (how_high/2)-(the_y/2); left_pos = 0; break;
		case 5: top_pos = (how_high/2) - (the_y/2); left_pos = (how_wide/2) -  (the_x/2); break;
		case 6: top_pos = (how_high/2) - (the_y/2); left_pos = how_wide - the_x; break;
		case 7: top_pos = how_high - the_y; left_pos = 0; break;
		case 8: top_pos = how_high - the_y; left_pos = (how_wide/2) - (the_x/2); break;
		case 9: top_pos = how_high - the_y; left_pos = how_wide - the_x; break;
		}

    if (window.outerWidth )
		{
        var option = "toolbar="+the_toolbar+",location="+the_addressbar
	  +",directories="+the_directories+",status="+the_statusbar
	  +",menubar="+the_menubar+",scrollbars="+the_scrollbars
	  +",resizable="+the_do_resize+",outerWidth="+the_x
	  +",outerHeight="+the_y+",copyhistory="+the_copy_history+",left="
	  +left_pos+",top="+top_pos;

        wname=window.open(the_url, wname, option);
		}
	else
		{
        var option = "toolbar="+the_toolbar+",location="+the_addressbar
	  +",directories="+the_directories+",status="+the_statusbar
	  +",menubar="+the_menubar+",scrollbars="+the_scrollbars
	  +",resizable="+the_do_resize+",Width="+the_x+",Height="
	  +the_y+",copyhistory="+the_copy_history+",left="+left_pos
	  +",top="+top_pos;
        if (!wname.closed && wname.location)
            wname.location.href=the_url;
        else
			{
            wname=window.open(the_url, wname, option);
            //wname.resizeTo(the_x,the_y);
            wname.location.href=the_url;
        	}
    	wname.focus();
		}
return wname;
}

function popup(link,sizeX,sizeY,wname) {return open_win(link,sizeX,sizeY,0,0,0,0,0,1,0,0,5,wname);}
function popupNoScroll(link,sizeX,sizeY,wname) {return open_win(link,sizeX,sizeY,0,0,0,0,0,0,0,0,5,wname);}

function getAbsolutePosition(el)
	{
	var r = { x: el.offsetLeft, y: el.offsetTop };
	if (el.offsetParent) {
		var tmp = getAbsolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
		}
	return r;
	}

function getDocumentSize(doc) { // getting a real size document with scrolling etc.
	var r = { width: 0, height: 0 };
	var width1=0, width2=0, width3=0, width4=0, maxWidth=0;
	var height1=0, height2=0, height3=0, height4=0, maxHeight=0;
	
	if (doc.width) maxWidth = doc.width;	
	if (doc.body) {		
		if (doc.body.scrollWidth) width1 = doc.body.scrollWidth;
		if (doc.body.offsetWidth) width2 = doc.body.offsetWidth;		
		}
	if (doc.documentElement) {
		width3 = doc.documentElement.scrollWidth;		
		width4 = doc.documentElement.clientWidth;
		}	
	
	maxWidth = Math.max(Math.max(Math.max(width1, width2), Math.max(width3, width4)),maxWidth);
		
	if (doc.height) maxHeight = doc.height;
	if (doc.body) {
		if (doc.body.scrollHeight) height1 = doc.body.scrollHeight;
		if (doc.body.offsetHeight) height2 = doc.body.offsetHeight;
		}
	if (doc.documentElement) {
		height3 = doc.documentElement.scrollHeight;
		height4 = doc.documentElement.clientHeight;
		}
	maxHeight = Math.max(Math.max(Math.max(height1, height2), Math.max(height3, height4)),maxHeight);
	
	r.width = maxWidth;
	r.height = maxHeight;
	
	return r;
	}

function show_hide(obj, pattern)
	{
	try {
		var o = obj=="" ? null : $(obj).style, p = pattern=="" ? null : $$(pattern);
		if ($chk(p)) p.each(function(i,n) {i.style.display="none";});
		if ($chk(o)) o.display = o.display == "none" ? "" : "none";
		}
	catch (e) {}
	return false;
	}

function showLayer(url, type)
	{
	var id = "layer_"+type;
	if (url!="") try {
		loading(true);
		$("layer_loading").set({
			"styles": {"cursor": "pointer"},
			"events": {"click": function(){ hideLayer(type); }}
			});
		if (!$chk($(id))) var obj = new Element("div", {
			"id": id, 
			"styles": {"display":"none", "background-color":"#ffffff", "position":"fixed", "width":"450px", "height":"50px", "border":"1px solid #CCCCCC", "opacity":".95", "filter":"alpha(opacity=95)"}
			}).inject(document.body).set("html", new Request.HTML({async: false}).get(url+"&js=1").response.html);
		var obj = $(id);
		if ($chk(obj))
			{
			if (Browser.Engine.trident==true) obj.setStyle("position", "absolute");
			obj.setStyle("display", "");
//			obj.setStyles({"display":"", "width":obj.getFirst().get("width")+10});
			obj.style.top=document.body.scrollTop+document.body.clientHeight/2-obj.clientHeight/2;
			obj.style.left=document.body.scrollLeft+document.body.clientWidth/2-obj.clientWidth/2;
			if ($chk($(type+"_form"))) $(type+"_form").elements[0].focus();
			}
		}
	catch (e) {}
	return false;
	}
function hideLayer(type) { try {loading(false); $("layer_"+type).dispose(); $("layer_loading").removeEvents("click"); } catch (e) {} }

function checkItems() {if ((del_field.value == "") && (block_field.value == "")) upd.style.display = "none"; else upd.style.display = "";}

function deleteItem(obj, _delete, _restore)
	{
	if (isUndefined(obj) || obj.id=="") return false;
	try {
		var id = obj.id;
		var tr = $("tr" + id);
		var act = obj.childNodes[0].tagName=="IMG" ? obj.childNodes[0] : obj.childNodes[1];

		var pos = to_del.indexOf(id);
		if (pos == -1) to_del.push(id); else to_del.splice(pos, 1);
		
		if (!isUndefined(del_field))
			{del_field.value = isUndefined(to_del[0])?"":to_del[0]; for(i=1;i<to_del.length;i++) del_field.value += "_"+to_del[i];}
		
		if (!isUndefined(act) && _delete!=null && _restore!=null)
			{
			if (!isUndefined(tr)) tr.className = tr.className.replace(/ modified/, "")+" modified";
			if (pos == -1)
				{
				if (!isUndefined(tr)) tr.className += " striked";
				act.src = _delete[0];
				act.alt = _delete[1];
				}
			else
				{
				if (!isUndefined(tr)) tr.className = tr.className.replace(/ striked/, "");
				act.src = _restore[0];
				act.alt = _restore[1];
				}
			}
		if (!isUndefined(upd)) checkItems();
		}
	catch (e) {}
	}

function blockItem(obj, _block, _restore)
	{
	if (isUndefined(obj) || obj.id=="") return false;
	try {
		var id = obj.id;
		var pos = to_block.indexOf(id);
		if (pos == -1) to_block.push(id); else to_block.splice(pos, 1);
		
		if (!isUndefined(block_field))
			{block_field.value = isUndefined(to_block[0])?"":to_block[0]; for(i=1;i<to_block.length;i++) block_field.value += "_"+to_block[i];}
		
		var tr = $("tr" + id);
		var stat = obj.childNodes[0].tagName=="IMG" ? obj.childNodes[0] : obj.childNodes[1];
		if (!isUndefined(stat) && _block!=null && _restore!=null)
			{
			if (!isUndefined(tr)) tr.className = tr.className.replace(/ modified/, "")+" modified";
			if (stat.alt == _restore[1])
				{
				stat.src = _block[0];
				stat.alt = _block[1];
				}
			else
				{
				stat.src = _restore[0];
				stat.alt = _restore[1];
				}
			}
		if (!isUndefined(upd)) checkItems();
		}
	catch (e) {}
	}


function MM_swapImgRestore() {var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function sh_form(prnt)
{
var obj = $('sub').style;
try {
	if (!isUndefined(obj)) obj.display = obj.display=="none" ? "block" : "none";
	if (!isUndefined(prnt)) prnt.innerText = obj.display=="none" ? "Открыть форму для связи" : "Закрыть форму для связи";
	}
catch (e) {}
}

// Tabs functions
function changeArea(num)
	{
	$$("#bookmarks span").each(function(i,n) {
		var obj = $("area"+i.id), zl = $("zl"+i.id), zc = $("zc"+i.id), zr = $("zr"+i.id);
		if ($chk(obj)) obj.style.display="none";
		if ($chk(zl)) zl.className="zakladka_left_down";
		if ($chk(zc)) zc.className="zakladka_center_down";
		if ($chk(zr)) zr.className="zakladka_right_down";
		});
	var o = $("area"+num), f = $chk($("forma"+num)) ? $("forma"+num).elements : null;
	try {
		if ($chk(o))
			{
			o.style.display="";
			if ($chk(f)) if (f.length>0) f[0].focus();
			$("zl"+num).className="zakladka_left_up";
			$("zc"+num).className="zakladka_center_up";
			$("zr"+num).className="zakladka_right_up";
			}
		}
	catch(e) {}
	}

// XMLHttpReqest functions
function getXmlHttp()
	{
	var xmlhttp;
	try {xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} catch (E) {xmlhttp = false;}}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp = new XMLHttpRequest();
	return xmlhttp;
	}

function checkXmlHttp(con, callback)
	{
	if (con=="") return false; else eval("var _con="+con+";");
	if (isUndefined(_con)) return false;
	else if (_con.readyState == 4)
		{
		if(_con.status == 200)
			if (_con.responseText!="") eval(callback+"();");
		}
	else setTimeout("checkXmlHttp('"+con+"', '"+callback+"')", 100);
	}

function formSend(f, callback200) // function(form_Object, callback_func_name)
	{
	if (isUndefined(f.action) || f.action=="" || !callback200) return false;
	try {
		loading(true, f);
		var params = "", CB200 = eval('window.'+callback200);
		for (i=0;i<f.length;i++)
			if (f[i].type=="radio" || f[i].type=="checkbox") params+= f[i].checked ? params+= f[i].name + "=" + encodeURIComponent(f[i].value) + "&" : "";
			else params+= f[i].name + "=" + encodeURIComponent(f[i].value) + "&";
		var c = new Request({method: "post", url: f.action+(f.action.indexOf("?")>-1?"&":"?")+"js=1"});
		c.success=CB200;
		c.send(params);
		}
	catch(e) {}
	return false;
	}

function typograf(editor)
	{
	if (isUndefined(editor)) return false;
	else try {
		var text = editor.getNodeAtSelection().innerHTML;
		if (text=="") return false; else var params = "text="+text;
		var c = getXmlHttp();
		c.open("POST", "/admin/?action=typograf", true);
		c.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		c.onreadystatechange = function() {
			if (c.readyState == 4)
				if (c.status == 200)
					try {
						if (c.responseText!="") {
							var obj = editor.getActivePageDoc().createElement(editor.getNodeAtSelection().tagName);
							obj.innerHTML=c.responseText;
							editor.insertNodeAtSelection(obj);
							}
						}
					catch(e) {alert("Typograf not avaliable");}
			}
		c.send(params);
		}
	catch(e) {}
	}

function sessionUpdate(url)
	{
	try {
		var req = getXmlHttp();
		req.open("GET", url, true); 
		req.setRequestHeader("If-Modified-Since", new Date(0));
		req.onreadystatechange = function() {
			if (req.readyState == 4)
				if(req.status == 200)
					{
					if (req.responseText != "")
						{
						var snd = $("soundeffect");
						var consult_new_session = $("consult_new_session");
						if (isUndefined(consult_new_session))
							try {
								var consult_new_session = document.createElement("div");
								consult_new_session.id = "consult_new_session";
								consult_new_session.className = "consult_new_session";
								document.getElementsByTagName("BODY").item(0).appendChild(consult_new_session);
								var snd = $("consult_new_session");
								}
							catch(e) {}
						if (isUndefined(snd))
							try {
								var snd = document.createElement("bgsound");
								snd.id = "soundeffect";
								snd.src = "#";
								snd.loop = 1;
								snd.autostart = true;
								document.getElementsByTagName("BODY").item(0).appendChild(snd);
								var snd = $("soundeffect");
								}
							catch(e) {}
						if (!isUndefined(snd))
							{
							snd.src="";
							snd.src="wav/consult_new_session.wav";
							}
						}
					setTimeout("sessionUpdate('"+url+"');", 10000);
					}
			}
		req.send(null); 
		}
	catch (e) {}
	}

function loading(state, p) // function([true=show | false=hide], parent_object)
	{
	var o = $("layer_loading");
	if (!$chk(o))
		{
		var o = new Element("div", {
			"id": "layer_loading",
			"styles": {"position": "absolute", "display": "none", "top": 0, "left": 0, "background": "#E7E7E7 url(/admin/images/loading.gif) no-repeat center 50%", "border": "1px solid #CCCCCC", "padding": "10px 20px", "opacity": ".4", "filter": "alpha(opacity=40)"}
			}).inject(document.body);
		var o = $("layer_loading");
		}
	try {
		if ($chk(p) && p.getCoordinates)
			{
			var c = p.getCoordinates();
			o.setStyles({top: c.top, left: c.left, width: c.width, height: c.height});
			}
		else o.setStyles({width: getDocumentSize(document).width, height: getDocumentSize(document).height});
		o.setStyle("display", (state===true?"block":"none"));
		}
	catch(e) {}
	}

function flashObj(o, c, msg) // function(object_id, class_name, message_string)
	{
	if (!$chk($(o))) var obj = new Element("div", {"id": o}).inject(document.body);
	var obj = $(o);
	if ($chk(obj) && c!="" && msg!="")
		try {
			obj.set({
			"class": c,
			"html": msg,
			"styles": {
				"position": "fixed",
				"opacity": 0,
				"filter": "alpha(opacity=0)",
				"display": "block"}
				});
			if (Browser.Engine.trident==true) obj.setStyle("position", "absolute");
			obj.style.top = document.body.scrollTop+(window.innerHeight ? window.innerHeight : document.body.offsetHeight)/2-obj.offsetHeight/2;
			obj.style.left = (window.innerWidth ? window.innerWidth : document.body.offsetWidth)/2-obj.offsetWidth/2;
			_fx = new Fx.Morph(obj, {duration: 1500, transition: 'quad:out'});
			_fx.start({opacity: [0, 1]});
			setTimeout("_fx.start({opacity: [1, 0]}); _fx = null;", 2000);
			}
		catch(e) {}
	}

var Base64 = {
    // private property
	keyStr  : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
    // public method for encoding
    encode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		do {
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if (isNaN(chr2)) enc3 = enc4 = 64;
			else if (isNaN(chr3)) enc4 = 64;
			output = output + this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) + this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
			} while (i < input.length);
		return output;
    },
    // public method for decoding
    decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
		// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		do {
			enc1 = this.keyStr.indexOf(input.charAt(i++));
			enc2 = this.keyStr.indexOf(input.charAt(i++));
			enc3 = this.keyStr.indexOf(input.charAt(i++));
			enc4 = this.keyStr.indexOf(input.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output = output + String.fromCharCode(chr1);
			if (enc3 != 64) output = output + String.fromCharCode(chr2);
			if (enc4 != 64) output = output + String.fromCharCode(chr3);
			} while (i < input.length);
		return output;
    }
}


