function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n);
}

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 _noBubbling( e ) {
	e = _fixE( e );
	if (!e.cancelBubble) e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
    return false;
}

function _fixE( e ) {
	if (!e) e = window.event;
	return e;
}

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>Loading&hellip;</div></div>");
	loading_set(id);
	$(window).resize(function() {
		loading_set(id);
	});
}

function check_delete(checked){
	var checkboxes = window.document.getElementsByTagName('input');
  var num = checkboxes.length;
  for(var i=0; i<num; i++){
  	if(checkboxes[i].id == 'delete' && checkboxes[i].type=='checkbox')
  	checkboxes[i].checked = checked;
  }
}

function load_form(id, url, _this ){
	var options = {
		type: "POST",
		url: url,
		//beforeSend: function(){ loading_show (id); },
		success: function(html) {	$("#"+id).html(html);	}
	};
	$(_this).ajaxSubmit(options);
}

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 delete_action(id, url, _this){
	if(confirm('ВНИМАНИЕ!!!\nПодтвердите удаление категории. Также будут удалены все подкатегории и товары относящиеся к этой категории.')){
		load_page(id, url, _this);
	}	
}

function title_show(_this) {
	var title = _this.childNodes[0];
	if (title.style.display != "block") {
		title.style.display = "block";
		title.style.marginLeft = 13 + "px";
		
		var pagesize = page_size();
		var left = bounds(title).left;
		var width = bounds(title).width;
		
		if ( pagesize[0] < left + width ) {
			title.style.marginLeft = - width + 25 + "px";
			title.className = title.className + " title-left"; 
		} else {
			title.style.marginLeft = 13 + "px";
			title.className = "title";
		}
	}
}
function title_hide(_this) {
	_this.childNodes[0].style.display = "none";
}

function toggleSlide( watchNode, activeNodeID ) {
	var watchNode = $( watchNode );
	var activeNode = $( '#'+activeNodeID );
	if (watchNode.hasClass('opened')) {
		watchNode.removeClass('opened');
		activeNode.slideUp();
	} else {
		watchNode.addClass('opened');
		activeNode.slideDown();
	}
}

function toggleRows( watchNode, rowsClass ) {

	var watchNode = $( watchNode );
	var activeRows = $( 'tr.'+rowsClass );
	if (watchNode.hasClass('opened')) {
		watchNode.removeClass('opened');
		activeRows.css('display', 'none');
	} else {
		watchNode.addClass('opened');
		activeRows.css('display', '');
	}

}

var prethis, preid, preeff;

function toggle_hide(_this, id, eff) 
{
	switch (eff) 
	{
		case "fade": $("#"+id).fadeOut("fast");	break; 
		case "none": $("#"+id).css({ display:"none"}); break; 
		default: $("#"+id).slideUp("fast");
	}
	$(_this).removeClass("click-active");
}

function toggle(e, _this, id, eff) 
{
	if (prethis && $_(preid) && prethis != _this) toggle_hide(prethis, preid, preeff);
	prethis = _this;
	preid = id;
	preeff = eff;
	bubbling_no(e);
	if ($_(id)) 
	{
		if (_this.className.indexOf("click-active") == -1) 
		{
			$(_this).addClass("click-active");
			switch (eff) 
			{
				case "fade":
					$("#"+id).fadeIn("fast");
				break;    
				default:
					$("#"+id).slideDown("fast");
			}
			$("#"+id).click(function(e) {	bubbling_no(e);	});

		} else {
			toggle_hide(_this, id, eff);
		}
		$(document).click(function() { toggle_hide(_this, id, eff);	});
	}
}

