function $_(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function bubbling_no(e) {
	e = bubbling_ie(e); /* for ie */
	if (!e.cancelBubble) e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}
function bubbling_ie(e) {
	if (!e) var e = window.event;
	return e;
}

function bounds(el) {
	var left = el.offsetLeft;
	var top = el.offsetTop;
	for (var parent = el.offsetParent; parent; parent = parent.offsetParent) {
		left += parent.offsetLeft;
		top += parent.offsetTop;
	}
	return {left: left, top: top, width: el.offsetWidth, height: el.offsetHeight};
}

function check_uncheck(_this) {
	if(_this.checked == true) $("input.checkbox").attr({checked:"checked"});
		else $("input.checkbox").removeAttr("checked");
}

function loading_set(id) {
	var w = $_(id).offsetWidth + "px";
	var h = $_(id).offsetHeight + "px";
	
	$("#"+id+" div.loading-overlay").css({ width:w, height:h });
}

function loading_show(id) {
	//$("#"+id).prepend("<div class=\"loading-overlay\"></div>");
	//$("#"+id).prepend("<div class=\"loading\"><div>а�аАаГб�б�аЖаАаЕаМ&hellip;</div></div>");
	//$("#"+id).prepend("");
	
	loading_set(id);
	
	$(window).resize(function() {
		loading_set(id);
	});
}

function scroll_top(id) {
	var s = bounds($_(id)).top - 20;	
	scroll(0, s);
}

function load_page(id, url, _this) {

	var data = _this ? $(_this).serialize() : "";
	$.ajax({
		type: "get",
		url: url,
		data: data,
		beforeSend: function(){			
			loading_show(id);
		},
		success: function(html) {
			$("#"+id).html(html);
		}
	});
}

function reload_page(id, url, _this) {
	var data = _this ? $(_this).serialize() : "";
	$.ajax({
		type: "get",
		url: url,
		data: data,
		beforeSend: function(){
			loading_show(id);
		},
		success: function(html) {
			$("#"+id).replaceWith(html);
		}
	});
}

function load_form(url, id, submit, _this) {
	if ($_(id)) {
		var data = _this && submit ? submit+"=1&"+$(_this).serialize() : "";
		var type = _this && submit ? "post" : "get";
		$.ajax({
			type: type,
			url: url,
			data: data,
			beforeSend: function(){
				loading_show(id);
			},
			success: function(html) {
				$("#"+id).html(html);
			}
		});
	}
}

function reload_form(url, id, submit, _this) {
	if ($_(id)) {
		var data = _this && submit ? submit+"=1&"+$(_this).serialize() : "";
		var type = _this && submit ? "post" : "get";
		$.ajax({
			type: type,
			url: url,
			data: data,
			beforeSend: function(){
				loading_show(id);
			},
			success: function(html) {
				$("#"+id).replaceWith(html);
			}
		});
	}
}

function load_rating(url, id, value) {
	if ($_(id)) {
		var data = "rate=1&ratingValue="+value;
		
		$.ajax({
			type: "post",
			url: url,
			data: data,
			success: function(html) {
				$("#"+id).html(html);
			}
		});
	}
}

var prethis, preel, preeff, prebubble;

function toggle_hide(_this, el, eff) {
	switch (eff) {
		case "fade":
			$("#"+el).fadeOut("fast");
			break; 

		case "none":
			$("#"+el).css({ display:"none"});
			break; 

		default:
			$("#"+el).slideUp("fast");
	}
	
	$(_this).removeClass("click-active");
	
	if (prebubble) prebubble.style.display = "none";
}

function toggle(e, _this, el, eff) {
	if (prethis && $_(preel) && prethis != _this) toggle_hide(prethis, preel, preeff);
	prethis = _this;
	preel = el;
	preeff = eff;
	bubbling_no(e);
	if ($_(el)) {
		if (_this.className.indexOf("click-active") == -1) {
			$(_this).addClass("click-active");

			switch (eff) {
				case "fade":
					$("#"+el).fadeIn("fast");
					break;    

				default:
					$("#"+el).slideDown("fast");
			}
			
			$("#"+el).click(function(e) {
				bubbling_no(e);
			});

		} else {
			toggle_hide(_this, el, eff);
		}
		
		$(document).click(function() {
			toggle_hide(_this, el, eff);
		});
	}
	_this.blur();
}

function toggle_only(_this, el, eff) {
	if ($_(el)) {
		if (_this.className.indexOf("click-active") == -1) {
			$(_this).addClass("click-active");
			
			switch (eff) {
				case "fade":
					$("#"+el).fadeIn("fast");
					break;
				
				case "block":
					$("#"+el).css({ display:"block"});
					break;

				default:
					$("#"+el).slideDown("fast");
			}
		} else {
			toggle_hide(_this, el, eff);
		}
	}
	_this.blur();
}

function toggle_bubble2(block, bubble) {
	block = block ? $_(block) : 0;
	prebubble = $_(bubble);
	if (block && bubble) {
		if (block.style.display == "none") prebubble.style.display = "block";
		else prebubble.style.display = "none";
	} else if (!block) {
		prebubble.style.display = "none";
	}
}

function toggle_bubble(block, bubble) {
	toggle_bubble2(block, bubble); //calculator prototype conflict
}

function toggle_form(e, _this, el, eff, shift) {
	if ($_(preel) && preel != el) toggle_hide(prethis, preel, preeff);
	prethis = _this;
	preel = el;
	preeff = eff;
	bubbling_no(e);
	if ($_(el)) {
		if ($_(el).style.display == "none") {
			$(_this).addClass("click-active");
			
			switch (eff) {
				case "fade":
					$("#"+el).fadeIn("fast");
					break;    

				default:
					$("#"+el).slideDown("fast");
			}
			
			$("#"+el).click(function(e) {
				bubbling_no(e);
			});
			
		} else {
			toggle_hide(_this, el, eff);
		}
		
		$_(el).style.top = bounds(_this).top + "px";
		$_(el).style.left = (shift ? bounds(_this).left + bounds(_this).width : bounds(_this).left) + "px";
		
		$(window).resize(function() {
			$_(el).style.top = bounds(_this).top + "px";
			$_(el).style.left = (shift ? bounds(_this).left + bounds(_this).width : bounds(_this).left) + "px";
		});
		
		$(document).click(function() {
			toggle_hide(_this, el, eff);
		});
	}
	_this.blur();
}

function toggle_cookie(_this, name, value) {
	if (_this.className.indexOf("click-active") == -1) set_cookie(name, value);
		else delete_cookie(name, value);
}

function toggle_bord(el) {
	$("#"+el).fadeOut("slow");
	set_cookie("del", el);
}

function toggle_tag(_this) {
	if (_this.className.indexOf("tag-active") == -1) {
		$("a.tag").removeClass("tag-active");
		$(_this).addClass("tag-active");
	} else {
		$(_this).removeClass("tag-active");
	}
	_this.blur();
}

function toggle_more(_this, f) {
	if ($_("more_hide") && $_("more_show") && $_("more_list") && $_("more_popular")) {
		if (f && f == "hide") {
			_this.parentNode.style.display = "none";
			$_("more_list").style.display = "none";
			$_("more_show").style.display = "inline";
			$_("more_popular").className = "more-popular";
		} else {
			_this.parentNode.style.display = "none";
			$_("more_list").style.display = "inline";
			$_("more_hide").style.display = "inline";
			$_("more_popular").className = "";
		}
	}
}

function toggle_tabs(_this, id) {
	if (!$(_this.parentNode).hasClass('active')) {
		$("#" + id + " li").removeClass('active');
		$(_this.parentNode).addClass('active');
	}
	_this.blur();
}

function toggle_msg(id) {
	$("#" + id).fadeOut("slow");
}

function toggle_preview(_this, g, id) {
	$("#" + g + " li").removeClass("active");
	$("#" + g + " div.preview-item").css({ display:"none" });

	$(_this).addClass("active");
	$("#" + id).css({ display:"block" });
}

function thumb(id, options) {
	img = $_(id+"i");
	
	if ($_(id) && img) {	
		items = $("#"+id+" a.thumb").get();
		if (items && items.length > 0) {
			img.src = items[0].href;
			items[0].parentNode.parentNode.className = "i i-2";
		}

		$("#"+id+" a.thumb").click(function(){
			thumb_show(this, id);
			return false;
		});
	}
}

function thumb_show(_this, id) {
	$("#"+id+" div.i-2").addClass("i-1");
	$("#"+id+" div.i-2").removeClass("i-2");
	_this.parentNode.parentNode.className = "i i-2";
	$_(id+"i").src = _this.href;
}

function row_add(_this) {
	var row = _this.parentNode.parentNode.parentNode;
	var n = row.cloneNode(true);

	row.parentNode.insertBefore(n, row.nextSibling);
	
	$("a", row).css({display:"inline"});
	$("select", n).val("");
	$("input", n).val("");
	
	_this.parentNode.removeChild(_this);
}

function row_delete(_this) {
	var row = _this.parentNode.parentNode.parentNode;
	row.parentNode.removeChild(row);
}

function prompting(e, _this) {
	$_("prompting").style.display="block";

	$(document).click(function(){
		$_("prompting").style.display = "none";
	});
	
	$(_this).click(function(e) {
		toggle_hide(prethis, preel, preeff);
		bubbling_no(e);
	});
	
	$("#prompting").click(function(e) {;
		bubbling_no(e);
	});
	
	var values = _this.value.split(";");
	val = values[values.length-1].replace(" ","");

	$.ajax({
		type: "get",
		url: "./?ajaxMsgPrompting&user=" + val,
		success: function(html) {
			$("#prompting").html(html);
			
		}
	});
}

function prompting_add(_this, vel, el) {
	if ($_(vel)) {
		var v = $_(vel).value.split(";");
		var n = _this.childNodes[0].innerHTML.replace(" ","");
		var o = "";

		for (i = 0, iLen = v.length; i < iLen; i++) {
			v[i] = v[i].replace(" ", "");
			if (i < (iLen - 1) && v[i] != n && v[i] != "") o = o + v[i] + "; ";
		}
		
		$_(vel).value = o + n + "; ";
	}
	
	if ($_(el)) $_(el).style.display = "none";
}

function address_add(_this, vel) {
	if ($_(vel)) {
		var v = $_(vel).value.split(";");
		var n = _this.childNodes[0].innerHTML.replace(" ","");
		var o = "";

		for (i = 0, iLen = v.length; i < iLen; i++) {
			v[i] = v[i].replace(" ", "");
			if (v[i] != n && v[i] != "") o = o + v[i] + "; ";
		}
		
		if (_this.className.indexOf("mrk-add") != -1) {
			$_(vel).value = o + n + "; ";
			_this.className = "click mrk-delete";
		} else {
			$_(vel).value = o;
			_this.className = "click mrk-add";
		}
	}
}

function address_ini(vel, el) {
	if ($_(vel) && $_(el)) {
		var v = $_(vel).value.split(";");
		var a = $("#" + el + " a").get();

		for (i = 0, iLen = v.length; i < iLen; i++) {
			v[i] = v[i].replace(" ", "");
			for (z = 0, zLen = a.length; z < zLen; z++) {
				if (a[z].childNodes[0].innerHTML.replace(" ","") == v[i]) a[z].className = "click mrk-delete";
			}
		}
	}
	
	if ($_("prompting")) $("#prompting").fadeOut("fast");
}

function set_cookie(name, value, expires, path, domain, secure) {
	if (!expires ) expires=60*60*1000;
	var path = "/";  
	var domain = domain ? domain : null;  
	var secure = secure ? secure : false; 
	var todaydate=new Date();
	var expdate=new Date(todaydate.getTime() + expires*1000);
	document.cookie = name + "=" + escape(value) +
	";  expires =" + expdate.toGMTString()  +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function get_cookie(name) {
	var allCookies = document.cookie.split("; ");
	for (var i=0, cLength=allCookies.length; i<cLength; i++) {
		cookie = allCookies[i].split("=");
		if (cookie[0] == name)
			return unescape(cookie[1]);
	}
	return null;
}

function delete_cookie(name, value, expires, path, domain, secure) {
	var newCookie = new Array();
	
    if(get_cookie(name)) { 
		var cookie = get_cookie(name);
		var tmp = cookie.split(",");
		
		for (var i=0, iLength=tmp.length; i<iLength; i++) {
			if (tmp[i] != value) { 
				newCookie[newCookie.length] = tmp[i];
			}
		}
	}
    set_cookie(name, newCookie, expires, path, domain, secure)
}


function xwin(url, w, h) {
	w = w ? w : 800;
	h = h ? h : 600;
	pos_left = (screen.width) ? (screen.width-w)/2 : 0;
	pos_top  = (screen.height) ? (screen.height-h)/3 : 0;

	settings = "width=" + w + ",height=" + h + ",top=" + pos_top + ",left=" + pos_left + ",scrollbars=yes";
	win = window.open(url,"xwin",settings)
	win.focus();
}

/* http://www.kryogenix.org/code/browser/searchhi/ */
function highlightWord( node, word ) {
	if (!node || !word) return;
	// Iterate into this nodes childNodes
	if (node.hasChildNodes) {
		var hi_cn;
		for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
			highlightWord(node.childNodes[hi_cn],word);
		}
	}

	// And do this node itself
	if (node.nodeType == 3) { // text node
		tempNodeVal = node.nodeValue.toLowerCase();
		tempWordVal = word.toLowerCase();
		if (tempNodeVal.indexOf(tempWordVal) != -1) {
			pn = node.parentNode;
			// check if we're inside a "nosearchhi" zone
			checkn = pn;
			while (checkn.nodeType != 9 &&
			checkn.nodeName.toLowerCase() != 'body') {
			// 9 = top of doc
				if (checkn.className.match(/bnosearchhib/)) { return; }
				checkn = checkn.parentNode;
			}
			if (pn.className != "searchword") {
				// word has not already been highlighted!
				nv = node.nodeValue;
				ni = tempNodeVal.indexOf(tempWordVal);
				// Create a load of replacement nodes
				before = document.createTextNode(nv.substr(0,ni));
				docWordVal = nv.substr(ni,word.length);
				after = document.createTextNode(nv.substr(ni+word.length));
				hiwordtext = document.createTextNode(docWordVal);
				hiword = document.createElement("span");
				hiword.className = "searchword";
				hiword.appendChild(hiwordtext);
				pn.insertBefore(before,node);
				pn.insertBefore(hiword,node);
				pn.insertBefore(after,node);
				pn.removeChild(node);
			}
		}
	}
}

function highlightSentence( node, str ) {
	var words = str.split(/\s+/);
	for (var i = 0; i < words.length; i++) {
		highlightWord( node, words[i] );
	}

}