function advOpen(openUrl, openWidth, openHeight, openSwitch) {
	var srw = screen ? (screen.width-50)    : 750;
    var srh = screen ? (screen.height-36)   : 564;
    var openScroll  	= "0";
	var documentWrite	= "";
	var	openParameters	= "";
    if ((openWidth>srw) || (openHeight>srh)) {
        openScroll  = "1";
        if (openHeight>srh) { openWidth  = openWidth+16; openHeight = srh-50; }
        if (openWidth>srw)  { openHeight = openHeight+16; openWidth = srw-50; }
    }

    srw     = (srw - openWidth)/2;
    srh     = (srh - openHeight)/5;

  	var UTCstring;
  	Today = new Date();
  	UTCstring = Today.getUTCHours()+Today.getUTCMinutes()+Today.getUTCSeconds()+Today.getTime();

	var openName;
	openName        = "openPopup"+UTCstring;

    openParameters  = "left="+srw+",top="+srh+",outerHeight=0,innerWidth,width="+openWidth+",innerHeight,height="+openHeight+",scrollbars="+openScroll+",titlebar=1,resizable=0";
    switch(openSwitch) {
        case "print":   openParameters  = "left="+srw+",top="+srh+",outerHeight=0,innerWidth,width="+openWidth+",innerHeight,height="+openHeight+",scrollbars=1,titlebar=1,resizable=0,menubar=1"; break;
        case "scroll":  openParameters  = "left="+srw+",top="+srh+",outerHeight=0,innerWidth,width="+openWidth+",innerHeight,height="+openHeight+",scrollbars=1,titlebar=1,resizable=0"; break;
    }
    var openPopup   = open(openUrl, openName, openParameters);
    return false;
}

function findPosY(obj) {
	var curtop = 0;

	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

function doSort($sort) {
	document.fList.sort.value = (document.fList.sort.value == $sort) ? $sort+"-" : $sort;
	document.fList.submit();
	return false;
}

function trimString(str) {
	while (str.charAt(0) == ' ')  str = str.substring(1);
	while (str.charAt(str.length - 1) == ' ')  str = str.substring(0, str.length - 1);
	return str;
}

function formActivate($form) {
	$($form).find(".text").bind("blur focus", function(e) {
		switch (e.type) {
			case "blur":
				$(this).toggleClass("act", 0);
				if (!this.value) { this.value = trimString(this.title); }
			break;
			case "focus":
				$(this).toggleClass("act", 1);
				if (trimString(this.value) == trimString(this.title)) {
					this.value = "";
				}
			break;
		}
		this.value	= trimString(this.value);
		return true;
	});
	$($form).find(".text").each(function($i){
		if (!this.value) {
			this.value = this.title;
		}
	});
}

/* AJAX */
function tryLogin($form) {
	jQuery.post("/tools/login.php", $($form).serialize(), function($html) {
		$("#fAuth").html($html);
	}, "html");
	return false;
};

/* onload shit */

$(document).ready(function(){
	$(document).focus();
	$("FORM").each(function($i){
		formActivate(this);
	});

	if (!$("IMG#indicator").length) {
		$("BODY")
			.append(
				$('<IMG>')
					.attr("id", "indicator")
					.attr("src", "images/indicator.gif")
					.hide()
			);
	}

});


function showIndicator($target) {
	// overlay
	$($target).parent().css("position", "relative");

	$("<DIV>")
		.attr("id", "overlay")
//		.css("top", $($target).parent().offset().top)
//		.css("left", $($target).parent().offset().left)
		.css("top", 0)
		.css("left", 0)
		.width($($target).parent().width())
		.height($($target).parent().height())
		.appendTo($($target).parent())
	;
	/*
	var $top	= $($target).offset().top + 5;
	var $left	= $($target).offset().left + $($target).width() - 1;

	$("IMG#indicator")
		.css("position", "absolute")
		.css("top", $top+"px")
		.css("left", $left+"px")
		.show()
	;
	*/
}

function hideIndicator() {
//	$("IMG#indicator").hide();
	$("DIV#overlay").remove();
}

var $urlCall	= '/tools/call.php';
var $baseURL	= dirname(location.href) + "/" + basename(location.href);

$baseURL		= $baseURL.split(document.domain)[1] . replace(/^\/+/g, '');

function module($mId, $eId, $sUrl, $callback) {
//	showIndicator($eId + " H1");
//	$sUrl		= $urlCall + ($sUrl ? $sUrl : '');
//	var $sUrl	= $sUrl;

	var $url	= $sUrl ? $sUrl : $baseURL;

	showIndicator("DIV.main H1");
	$.get($urlCall, {
		// eto URL na rasparku
		url: $url,

		// eto module, kotorij nado pozvatj
		mid: $mId
	}, function($data) {
		hideIndicator();
		$($eId).html($data);
		if ($callback) { eval($callback); }
	});

	return false;
}

function basename(path) {
	return path.replace(/\\/g,'/').replace( /.*\//, '' );
}

function dirname(path) {
	return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, '');
}

function nofilter(path) {
	return path.replace(/(\/[^\/]+:[^\/]+)/ig, '');
}

function fileext(path) {
	return basename(path).replace(/[^.]+[\s\S]([\s\S]*)/g, '$1');
}
