function onFoldersLoadSuccess(xml) {


      var ss = xml.split("+");

	var item_text = ss[0];
	var at_group_text = ss[1];

	$("div.attr").css("display", "none");
	$("#subfolders").html(ss[0]);


	if(at_group_text){
		$("#"+at_group_text).css("display", "");
		$("#type").find("option[@value='+at_group_text+']").attr("selected", "selected");
	}
}


function GetFolders(idparent) {

	if(parent==0){
		document.getElementById("subfolders").innerHTML = "";
		return;
	}

	document.getElementById("subfolders").innerHTML = "Загружаются подразделы, подождите...";

	$.get('/jslib/jfolders.php', {parent: idparent}, onFoldersLoadSuccess);

	
	if(idparent==10)
		document.getElementById("zemtype").innerHTML = '<p>Назначение участка: <span class="red">*</span> <select name="zemtype"><option value="1">ИЖС</option><option value="2">Сельхоз</option><option value="3">Промышленное</option></select></p>';
	
	else
		document.getElementById("zemtype").innerHTML = '';
}


function DisplayAtGroup(value){
	$("div.attr").css("display", "none");
	var id = value.split("-");
	var group_id = "group-"+id[0];
	DisplayDiv(group_id);
}





function openWind(url,width,height) {
        number = Math.round(Math.random() * 100000000);
        auxWnd = window.open(url, 'newWindow_' + number,'width=' + width + ',height=' + height + ',resizable=yes,scrollprint=yes,toolbar=no,menubar=no,location=no,status=no,directories=no,copyhistory=no,left=0,top=0');
}

function SwapImage(target, fname) {
	document[target].src = fname;
}

function DisplayFlash(path, width, height, wmode){

    text = "<object type=\"application/x-shockwave-flash\" data=\""+path+"\" width=\""+width+"\" height=\""+height+"\" wmode=\""+wmode+"\" quality=\"high\">\r\n";
    text += "<param name=\"movie\" value=\""+path+"\" />\r\n";
    text += "<param name=\"wmode\" value=\""+wmode+"\" />\r\n";
    text += "<param name=\"quality\" value=\"high\" />\r\n";
    text += "<param name=\"width\" value=\""+width+"\" />\r\n";
    text += "<param name=\"height\" value=\""+height+"\" />\r\n";
    text += "</object>";
    document.write(text);
}



function DisplayDiv(ID) {


	atts = document.getElementsByName('attributes');
	for (i = 0; i < atts.length; i++){
		item = atts[i];
		item.style.display = "none";
	}


	if(ID){

		var elem = document.getElementById(ID);
		if(elem)
			document.getElementById(ID).style.display = (document.getElementById(ID).style.display == "none" ) ?  "" :   "none";

	}
}


function ShowSearch() {

	document.getElementById("search").style.display = (document.getElementById("search").style.display == "none" ) ?  "" :   "none";

	var a_elem = document.getElementById("showlink");

	if(a_elem.innerHTML=="Свернуть"){
		a_elem.innerHTML = "Развернуть";
		a_elem.className = "maximize"
	}
	else{
		a_elem.innerHTML = "Свернуть";
		a_elem.className = ""

	}



}




function hideHint(){

var div = document.getElementById('hint');
div.style.display = 'none';
return false;
}

function showHint(source, divName, id)
{
	var div = document.getElementById('hint');
	var hintText = document.getElementById(divName);
	var coords = getElementPosition(source);
  var hintT = hintText.innerHTML.replace("ITEM", id);
	div.innerHTML = '<div class="page-hint">' + hintT + '</div>';
	div.style.top = (coords.top + source.offsetHeight) + 'px';
	div.style.left = coords.left - 250 + 'px';
	div.style.display = 'none';
	div.style.display = 'block';
	return false;
}

function getElementPosition(offsetTrail)
{
var offsetLeft = 0;
var offsetTop = 0;
while(offsetTrail) {
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
return { left:offsetLeft, top:offsetTop }
}


function onAuthSuccess(data) {

	if(data=="")
		alert("Ошибка! Неверное имя пользователя или пароль!");
	else
		$("#auth").html(data);

}


function Auth() {
	var login = document.getElementById("login").value;
	var pwd = document.getElementById("password").value;

	$.post('/jslib/auth.php', {login: login, pwd: pwd}, onAuthSuccess);
}


//
/* Rooms options */
jQuery(function($) {
	var $ro = $('.check-params');

	//$ro.addClass('form-item-rooms-js');

	$('.check-option', $ro)
		.each(function() {
			this.$input = $('input', this);
			this.$label = $('label', this);
			this.update = function() {
				if(this.$input.is(':checked')) {
					this.$label.addClass('active');
				} else {
					this.$label.removeClass('active');
				}
			};

			this.$input.change(function() { this.parentNode.update(); });
			this.$label.click(function() { this.parentNode.update(); });
			this.$input.click(function() { this.parentNode.update(); });
			this.$input.keypress(function() { this.parentNode.update(); });

			this.$input.focus(function() {
				this.parentNode.$label.parent().addClass('focus');
			});
			this.$input.blur(function() {
				this.parentNode.$label.parent().removeClass('focus');
			});

			this.update();
		});
});




//
var Drag = {

	obj : null,

	init : function(o, oRef, oSlide, aText, minX, maxX, initX, x1, x2, sSize)
	{
		o.onmousedown	= Drag.start;

		o.root = o;

    o.ref = oRef;
    o.slide = oSlide;
    o.sSize = sSize;
    o.x1 = x1; o.x2 = x2; o.scale = (x2-x1)/200;
    //if (isNaN(parseInt(o.root.style.left))) 
    o.root.style.left = initX + "px";

    o.aText = aText;
    o.aText.value = parseInt(initX*o.scale+x1);

    if(initX==maxX) o.aText.value = x2;

    o.refX = (o.ref.style.left) ? parseInt(o.ref.style.left) : 0;
    slideL = Math.min(o.refX, initX);
    slideR = Math.max(o.refX, initX);
    o.slide.style.left = slideL + "px";
    
	//!
    //o.slide.style.width = (slideR - slideL) + "px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;


		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var x = parseInt(o.root.style.left);
    o.refX = parseInt(o.ref.style.left);
		o.root.onDragStart(x);

		o.lastMouseX	= e.clientX;

			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if ((o.refX > o.minX)&&(o.refX < x)) o.minMouseX	= e.clientX - x + o.refX+o.sSize;
			if (o.maxX != null)	o.maxMouseX	= Math.min((o.minMouseX + o.maxX - o.minX),(e.clientX - x + o.maxX));
  		if ((o.refX < o.maxX)&&(o.refX > x)) o.maxMouseX	= o.minMouseX + o.refX - o.minX - o.sSize;


		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ex	= e.clientX;
		var x = parseInt(o.root.style.left);
		var nx, ny;

		if (o.minX != null) ex = Math.max(ex, o.minMouseX);
		if (o.maxX != null) ex = Math.min(ex, o.maxMouseX);

		nx = x + (ex - o.lastMouseX);

    Drag.obj.root.style.left = nx + "px";
		Drag.obj.lastMouseX	= ex;

    slideL = Math.min(o.refX, nx);
    slideR = Math.max(o.refX, nx);
    o.slide.style.left = slideL + "px";
    o.slide.style.width = (slideR - slideL) + "px";


		Drag.obj.root.onDrag(nx);
    Drag.obj.aText.value = parseInt((o.x1 + (nx)*o.scale));
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(parseInt(Drag.obj.root.style.left));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		return e;
	}
};

function SwapImage(target, fname) {
	document[target].src = fname;
}
