function menu_title() {
	var b = $("em.title").get();
	for (i = 0; i < b.length; i++) {
		var c = b[i].parentNode.childNodes[0];
		var d = (c.offsetHeight < 33) ? 16 : 12;
		b[i].style.display = 'inline';
		b[i].style.left = (c.offsetLeft + c.offsetWidth - b[i].offsetWidth + 40) + 'px';
		b[i].style.top = (c.offsetTop - d) + "px";
	}
}

function print_preview(idform, idbtn) {
	var form = dom.gebi(idform);
	var btn = dom.gebi(idbtn);
	var action = form.action;
	var submit = btn.value;
	form.target = '_blank';
	form.action = form.action + '?print';
	btn.value = 0;
	form.submit();
	form.action = action;
	btn.value = submit;
}

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();
}

function isNumber(e) {
	if ((e.keyCode < 48) || (e.keyCode > 57))
		e.returnValue = false;
}

function calculate_sum(get, put) {
		var count = 0;
		
		$('input.' + get).each(function(){
			if (!this.disabled && this.value && !isNaN(this.value)) count = count + parseInt(this.value);
		});
		$('input#' + put).val(count);
}

function toggle_disabled(_this, id) {
	if (_this.checked) {
		$('input#' + id).each(function(){
			this.disabled = false;
			$(this.parentNode).removeClass('frm-disabled');
		});
	} else {
		$('input#' + id).each(function(){
			this.disabled = true;
			$(this.parentNode).addClass('frm-disabled');
		});	
	}
}

function toggle_table(_this, g, itm) {
	if (_this.className.indexOf('active') == -1 && dom.gebi(g) && dom.gebi(itm)) {
		var tabs = dom.gebc('active', 'a', dom.gebi(g));
		var blocks = dom.gebc('block', 'tbody', dom.gebi(g));
		
		for (i = 0; i < tabs.length; i++) {
			tabs[i].className = '';
		}
		
		for (i = 0; i < blocks.length; i++) {
			blocks[i].style.display = 'none';
		}
		
		dom.gebi(itm).style.display = '';
		
		_this.className = 'active';
	}
	_this.blur();
}

function toggle_block(_this, id, eff) {
	if (_this.className.indexOf('click-active') == -1) {
		$(_this).addClass('click-active');
		switch (eff) {
			case 'slide':
				$('#' + id).slideDown('slow');
				break;
			default:
				$('#' + id).fadeIn('slow');
		}
	} else {
		$(_this).removeClass('click-active');
		switch (eff) {
			case 'slide':
				$('#' + id).slideUp('slow');
				break;
			default:
				$('#' + id).fadeOut('slow');
		}
	}
	
	_this.blur();
}

function load_addresses(_this, g, id, url) {
	if (_this.className.indexOf('active') == -1) {
		$('#' + g + ' a.active').removeClass('active');
		$(_this).addClass('active');
		load_content(id, url);
	}
}

function toggle_address_hide(g) {
	$('#' + g + ' a.toggle-active').removeClass('toggle-active');
	$('#' + g + ' div.sublevel').slideUp('slow');
}

function toggle_address(_this, g, id) {
	if (_this.className.indexOf('toggle-active') == -1) {
		toggle_address_hide(g);
		$(_this).addClass('toggle-active');
		$('#' + id).slideDown('slow', function() {
			//$('#' + id+ ' a:first').each(function(){
				//this.onclick('onclick');
			//});
		});
	} else {
		$(_this).removeClass('toggle-active');
		$('#' + id).slideUp('slow');
	}
	
	_this.blur();
}

function toggle_tabs(_this, thisgid, gid, id) {
	if	(!$(_this).hasClass('active')) {
		$('#' + thisgid + ' a').removeClass('active');
		$(_this).addClass('active');
		
		$('#' + gid + ' div.item').css({ display:'none' });
		$('#' + id).fadeIn('fast');
	}
	
	_this.blur();
}

function toggle_sitemap(_this, id, gid) {
	if	(!$(_this.parentNode).hasClass('sitemap-active')) {
		$('#' + gid + ' div.sitemap-toggle').removeClass('sitemap-active');
		$(_this.parentNode).addClass('sitemap-active');
		
		$('#' + gid + ' div.sitemap-group').css({ display:'none' });
		$('#' + id).fadeIn('fast');
	}
	
	_this.blur();	
}

function file_mask(_this, id) {
	var file = _this.value.split("\\");
	file = file[file.length-1];
	dom.gebi(id).innerHTML = file;
	file = file.split("\.");
	file = file[file.length-1];
	dom.gebi(id).className = 'filename filename-' + file;
}

function mail(name, dom, a, display) {
	var m = 'mailto:';
	a = a ? '.'+a : '.lv';
	document.write('<a href="'+m+name+'@'+dom+a+'">'+(display?display:name+'@'+dom+a)+'</a>');
}


/*------------------------------------------------------- navigation --------------------------------------------------*/
function scroll_init(id, count, d) {
	var scrollable = dom.gebi(id + '_content');
	
	scrollable.scrollLeft = 0;
	//scrollable.style.width = '405px';
	d = d ? d : 1;
	
	var offset = Math.floor((dom.gebi(id).offsetWidth - 28) / d);
	
	scrollable.style.width = (d * (offset > count ? count : offset)) + 'px';
	
	if (offset > count || (scrollable.scrollLeft >= (scrollable.scrollWidth - scrollable.offsetWidth))) {
		$('#' + id + '_next').addClass('scroll-deactive');
	} else {
		$('#' + id + '_next').removeClass('scroll-deactive');
	}
	$('#' + id + '_prev').addClass('scroll-deactive');

	$('#' + id + '_prev').click(function(e) {
		scroll_action(id, -d);
	});
	
	$('#' + id + '_next').click(function(e) {
		scroll_action(id, d);
	});
}

function scroll_action(id, d) {
	var scrollable = dom.gebi(id + '_content');
	
	scrollable.scrollLeft += d;
	
	if (scrollable.scrollLeft > 0) {
		$('#' + id + '_prev').removeClass('scroll-deactive');
	} else {
		$('#' + id + '_prev').addClass('scroll-deactive');
	}
	
	if (scrollable.scrollLeft < (scrollable.scrollWidth - scrollable.offsetWidth)) {
		$('#' + id + '_next').removeClass('scroll-deactive');
	} else {
		$('#' + id + '_next').addClass('scroll-deactive');
	}
}


function loading_bounds(id) {
	var w = dom.gebi(id).offsetWidth + "px";
	var h = dom.gebi(id).offsetHeight + "px";
	$("#"+id+" div.loading").css({ width:w, height:h });
}

function loading(id) {
	$('#'+id).prepend('<div class="loading"><iframe></iframe></div>');
	loading_bounds(id);

	$(window).resize(function() {
		loading_bounds(id);
	});
}

function load_content(id, url, _this, data) {
	loading(id);
	if (_this) {
		var options = {
			type: 'post',
			url: url,
			data: data ? data : false,
			success: function(html) {
				$('#'+id).html(html);
			}
		}
		$(_this).ajaxSubmit(options);
	} else {
		$.ajax({
			type: 'get',
			url: url,
			data: data ? data : false,
			success: function(html) {
				$('#'+id).html(html);
			}
		});
	}
}


/*---------------------------------------------------------- layers ----------------------------------------------------*/
function modal(id, url, scrl, view, data) {
	window.scroll(0, 0);
	
	$('body').append('<div id="' + id + '" class="modal"><table class="overlay' + (scrl ? ' overlay-scrollable' : '') + '"><tr><td id="' + id + '_overlay" class="overlay overlay-preloader"><table class="modal"><tr><td class="modal-11 png"><div></div></td><td class="modal-12 pngscale"><div></div></td><td class="modal-13 png"><div></div></td></tr><tr><td class="modal-21 pngscale"><div></div></td><td class="modal-22"><div class="modal' + (view ? '-' + view : '') + '"><div class="modal-close"><a href="#close" onclick="modal_remove(\'' + id + '\'); this.blur; return false;"></a></div><div id="' + id + '_content"></div></div></td><td class="modal-23 pngscale"><div></div></td></tr><tr><td class="modal-31 png"><div></div></td><td class="modal-32 pngscale"><div></div></td><td class="modal-33 png"><div></div></td></tr></table></td></tr></table><div class="overlay"></div><iframe class="overlay"></iframe></div>');

	$.ajax({
		type: 'get',
		url: url,
		data: data ? data : false,
		success: function(html) {
			$('#' + id + '_content').html(html);
			$('#' + id + '_overlay').removeClass('overlay-preloader');
		}
	});
}

function modal_remove(id) {
	$('#' + id).css({ display:'none' })
		.remove();
}


/*-----------------------------  http://www.kryogenix.org/code/browser/searchhi/ --------------------------------*/
function highlightWord(node,word) {
	// 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(/\bnosearchhi\b/)) { 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("var");
				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] );
	}
}