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

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>');
}

function isNumber(e) {
	if ((e.keyCode < 48) || (e.keyCode > 57))
		e.returnValue = false;
}

function file_mask(_this, id) {
	var file = _this.value.split("\\");
	file = file[file.length-1];
	x.gebi(id).value = file;
}

function toggle_lang(id) {
	var o = $_(id);
	if (o) {
		o.className = o.className.indexOf('language-visible') != -1 ? o.className.replace(' language-visible', '') : o.className + ' language-visible';
	}
}

function toggle_nav(g, id) {
	$('#' + g + ' div.b9-active').removeClass('b9-active');
	$('#' + id).addClass('b9-active');
}

function toggle_checkbox(_this, id) {
	if (!x.gebi(id)) return;
	
	var a = x.gebi(id).getElementsByTagName('input');
	var alen = a.length;
	
	for (var i=0; i < alen; i++) {
		if (a[i] != _this && a[i].type == 'checkbox') {
			if (_this.checked == true){
				a[i].checked = true;
				a[i].disabled = true;
				a[i].parentNode.parentNode.className = 'disabled';
			} else {
				a[i].checked = false;
				a[i].disabled = false;
				a[i].parentNode.parentNode.className = '';
			}
		}
	}
	
	_this.blur();
}

function toggle_tags(_this, g) {
	if (_this.className.indexOf('tags-hide') == -1) {
		$(_this).addClass('tags-hide');
		$('#' + g +' span.tag-hidden').css({ display:'block' });
	} else {
		$(_this).removeClass('tags-hide');
		$('#' + g +' span.tag-hidden').css({ display:'none' });	
	}
}

/*------------------------------------------------------- select style --------------------------------------------------*/
var select = {
	s : {},
	
	create : function(id) {
		//return false;
		
		if (id && x.gebi(id)) {
			if (typeof this.s[id] != "undefined") delete this.s[id];
			this.s[id] = new this.fc(id, 1);
			return this.s[id];
		} else {
			var tmp = $('table.fld-select select').get();
			
			for (var i=0; i<tmp.length; i++) {
				tmp[i].id = tmp[i].id ? tmp[i].id : 'rndm_' + tmp[i].name;
				
				if (typeof this.s[tmp[i].id] != "undefined" && !this.s[tmp[i].id].custom) delete this.s[tmp[i].id];
				if (typeof this.s[tmp[i].id] == "undefined" ) this.s[tmp[i].id] = new this.fc(tmp[i].id);
								
				//if (!x.gebi(tmp[i].id + '_slctd')) this.s[tmp[i].id].create();
			}

			$(document).unbind('click');
			$(document).click(function(){
				select.hide();
			});
		}
	},
	
	get : function(id) {
		if (typeof this.s[id] != "undefined") return this.s[id];
	},
	
	hide : function() {
		$('div.select-optns').css({display:'none'});
		$(document).unbind('keypress');
	}
}

select.fc = function(id, custom) {
	var obj = x.gebi(id);
	var len = obj.options.length;
	
	this.id = obj.id;	
	this.name = obj.name;
	this.dwidth = 0;
	this.owidth = null;
	this.options = [];
	this.hlight = -1;
	this.custom = custom ? 1 : 0;

	for (var o = 0; o < len; o++) {
		this.options[o] = obj.options[o].innerHTML;
	}
}

select.fc.prototype = {
	init : function(hash) {
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
		$(document).click(function(){
			select.hide();
		});
	},
	
	replace : function(a, dw) {
		return false;

		this.dwidth = isNaN(dw) ? this.dwidth : dw;
		if (typeof a == 'object' && this.options.length == a.length) { 
			for (var i = 0; i < this.options.length; i++) {
				this.options[i] = a[i];
			}
		}
	},
	
	create : function() {
		var obj = x.gebi(this.id);
		var str = '';
		var len = this.options.length;
		
		if (!obj) return;
		
		str += '<div id="' + this.id + '_slctd" class="select-slctd" style="width:' + (obj.offsetWidth + this.dwidth) + 'px" onclick="x.bubbling(event); select.get(\'' + this.id + '\').toggle();">';
		str += '<div id="' + this.id + '_fix" class="select-fix" style="width:' + (obj.offsetWidth + this.dwidth) + 'px; overflow:hidden;">';
		str += this.options[obj.selectedIndex];
		str += '</div></div>';
		str += '<div id="' + this.id + '_optns" class="select-optns">';
		
		for (var i = 0; i < len; i++) {	
			str += '<div id="' + this.id + '_optns_' + i + '" class="select-optn" onclick="select.get(\'' + this.id + '\').change(\'' + i + '\');">' + (this.options[i] == '' ? '&nbsp;' : this.options[i]) + '</div>';
		}
		
		str += '</div>';
	
		$(obj.parentNode).prepend(str);
		
		obj.style.position = 'absolute';
	},
	
	change : function(inx) {
		var select = x.gebi(this.id);
		var obj = x.gebi(this.id + '_fix');
		
		if (select && obj) {
			obj.innerHTML = this.options[inx];
			select.selectedIndex = inx;
			if (select.getAttribute('onchange')) select.onchange('onchange');
		}
		
		this.toggle();
	},
	
	toggle : function() {
		var slctd = x.gebi(this.id + '_slctd');
		var obj = x.gebi(this.id + '_optns');
		var self = this;
		
		if (obj.style.display == 'block') {
			obj.style.display = 'none';
			$(document).unbind('keypress');
		} else {
			if (!this.owidth) {
				obj.style.visibility = 'hidden';
				obj.style.display = 'block';
				
				if (this.options.length > 10) {
					if (obj.offsetWidth + 18 < slctd.offsetWidth - 8) this.owidth = slctd.offsetWidth - 8;
						else this.owidth = obj.offsetWidth + 18;
					obj.className += ' select-scrl';
				} else {
					if (obj.offsetWidth < slctd.offsetWidth - 8) this.owidth = slctd.offsetWidth - 8;
						else this.owidth = obj.offsetWidth - 8;
				}
				obj.style.display = 'none';
				obj.style.visibility = 'visible';
			}
			obj.style.width = this.owidth + 'px';
		
			select.hide();
			obj.style.display = 'block';
			
			$(document).keypress( function(e){		
				var s = String.fromCharCode(e.which);
				self.search(s);
				return false;
			});			
		}
	},
	
	search : function(s) {
		var obj = x.gebi(this.id);		
		if (!obj) return;		
		var len = obj.options.length;
		var flag = 0;
				
		if (this.hlight != -1 && obj.options[this.hlight].innerHTML.substr(0,1).toLowerCase() != s) this.hlight = -1;
	
		for (i = 0; i < len; i++) {
			$('#' + this.id + '_optns_' + i).removeClass('select-highlight');
		
			var k = obj.options[i].innerHTML.substr(0,1).toLowerCase();

			if (k.indexOf(s.toLowerCase()) != -1) {
				if (!flag && (this.hlight == -1 || this.hlight < i)) {
					$('#' + this.id + '_optns_' + i).addClass('select-highlight');
					this.hlight = i;
					flag = 1;
				} else if (flag && this.hlight != i) {
					flag = 2;
				}
			}
			
			if (this.hlight != i) $('#' + this.id + '_optns_' + i).removeClass('select-highlight');
		}
		if (flag == 1) this.hlight = -1;
		
		if (this.hlight != -1) x.gebi(this.id + '_optns').scrollTop = x.gebi(this.id + '_optns_' + this.hlight).offsetTop;
	}
}


/*------------------------------------------------------ offers scroll -------------------------------------------------*/
var timer, timer_flag = false;
var scroll_ctop = 0;

function scroll_over(_this, d) {
	_this.className += ' hover';
	
	var o = $_('scroll_items');
	if (!o) return;
	var scrollheight = o.parentNode.offsetHeight;
	
	scroll_stop();
	if ((scroll_ctop + scrollheight < o.offsetHeight-10 && d > 0) || (scroll_ctop > 0 && d < 0)) {
		scroll_offers(d, scroll_ctop, 0, 'fast');
	}
}

function scroll_out(_this, d) {
	_this.className = _this.className.replace(' hover','');
	scroll_stop();
	//scroll_offers(d, scroll_ctop);
	timer = setTimeout("scroll_offers(" + d +", " + scroll_ctop + ")", 1000);
}

function scroll_offers(d, cliptop, ready, speed) {
	if (timer_flag && ready) return;
	timer_flag = true;

	var o = $_('scroll_items');
	if (!o) return;
	var scrollheight = o.parentNode.offsetHeight;
	var delay = 150;
	
	if (speed && speed == 'fast') {
		delay = 30;
		d = d > 0 ? 4 : -4;
	} else {
		d = d > 0 ? 1 : -1;
	}
	
	o.style.clip = "rect(" + cliptop + "px " + o.offsetWidth + "px " + (cliptop + scrollheight) + "px 0)";
	o.style.top = -cliptop + "px";

	if (scrollheight && o.offsetHeight-10 > scrollheight) {
		scroll_ctop = cliptop = (cliptop + d);
		timer = setTimeout("scroll_offers(" + d +", " + cliptop + ", 0, '" + speed + "')", delay);
		
		if (cliptop + scrollheight >= o.offsetHeight-10 && d > 0) {
			scroll_stop();
			if (delay == 150) scroll_offers(-1, cliptop);
		} else if (cliptop <= 0 && d < 0) {
			scroll_stop();
			if (delay == 150) scroll_offers(1, cliptop);
		}
		
		o.onmouseover = function(){
			scroll_stop();
		};
		o.onmouseout = function(){
			scroll_stop();
			timer = setTimeout("scroll_offers(" + d +", " + cliptop + ")", 150);
		};	
	}	
}

function scroll_stop() {
	clearTimeout(timer);
}


/*------------------------------------------------------- navigation --------------------------------------------------*/
var scrollTimer;

function scroll_ini(g, count) {
	var iw = 135;
	var scrollable = x.gebi(g + '_scrollable');
	var spacer = x.gebi(g + '_scrollable_spacer');
	
	scrollable.scrollLeft = 0;
	scrollable.style.width = '405px';
	spacer.style.width = (iw * count) + 'px';
	
	var offset = Math.floor((x.gebi(g).offsetWidth - 36) / iw);
	
	scrollable.style.width = (iw * (offset > count ? count : offset)) + 'px';
	
	if (offset > count || (scrollable.scrollLeft >= (scrollable.scrollWidth - scrollable.offsetWidth))) {
		x.gebi(g + '_next').className = 'scroll-next deactive';
	} else {
		x.gebi(g + '_next').className = 'scroll-next';
	}
	x.gebi(g + '_prev').className = 'scroll-prev deactive';

	$('#' + g + '_prev').mouseover(function(e) {
		clearTimeout(scrollTimer);
		scroll_action(g, -1);
	}).mouseout(function(e) {
		clearTimeout(scrollTimer);
	});
	
	$('#' + g + '_next').mouseover(function(e) {
		clearTimeout(scrollTimer);
		scroll_action(g, 1);
	}).mouseout(function(e) {
		clearTimeout(scrollTimer);
	});
}

function scroll_action(g, d) {
	var scrollable = x.gebi(g + '_scrollable');
	
	scrollable.scrollLeft += 13.5 * d;
	
	if (scrollable.scrollLeft > 0) {
		x.gebi(g + '_prev').className = 'scroll-prev';
	} else {
		x.gebi(g + '_prev').className = 'scroll-prev deactive';
	}
	
	if (scrollable.scrollLeft < (scrollable.scrollWidth - scrollable.offsetWidth)) {
		x.gebi(g + '_next').className = 'scroll-next';
	} else {
		x.gebi(g + '_next').className = 'scroll-next deactive';
	}
		
	scrollTimer = setTimeout('scroll_action("' + g + '", ' + d + ')',100);
}


/*------------------------------------------------------------ ajax -----------------------------------------------------*/
function loading_set(id) {
	var w = $_(id).offsetWidth + "px";
	var h = $_(id).offsetHeight + "px";

	$("#"+id+" div.loading").css({ width:w, height:h });
}

function loading_show(id) {
	$('#'+id).prepend('<div class="loading"></div>');
	loading_set(id);

	$(window).resize(function() {
		loading_set(id);
	});
}

function load_content(id, url, _this) {
	if (_this) {
		var options = {
			type: 'post',
			url: url,
			beforeSend: function() {
				loading_show(id);
			},
			success: function(html) {
				$('#'+id).html(html);
			}
		}
		$(_this).ajaxSubmit(options);	
	} else {
		$.ajax({
			type: 'get',
			url: url,
			beforeSend: function() {
				loading_show(id);
				//alert();
			},
			success: function(html) {
				$('#'+id).html(html);
			}
		});
	}
}

function reload_content(id, url, _this) {
	if (_this) {
		var options = {
			type: 'post',
			url: url,
			beforeSend: function() {
				loading_show(id);
			},
			success: function(html) {
				$('#'+id).replaceWith(html);
			}
		}
		$(_this).ajaxSubmit(options);	
	} else {
		$.ajax({
			type: 'get',
			url: url,
			beforeSend: function() {
				loading_show(id);
				//alert();
			},
			success: function(html) {
				$('#'+id).replaceWith(html);
			}
		});
	}
}


/*-------------------------------------------------------- promting ---------------------------------------------------*/
function prompting(e, _this, id, url) {
	var obj = x.gebi(id);
	
	if (!obj) return;
	
	obj.style.display = 'block';
	obj.style.width = _this.offsetWidth + 'px';

	$(document).click(function(){ obj.style.display = 'none'; });

	$('#' + id).click(function(e) { bubbling_no(e); });
	
	var values = _this.value.split(',');
	val = values[values.length-1].replace(' ','');

	$.ajax({
		type: 'get',
		url: url + val,
		beforeSend: function() {
			$(_this).addClass('loading');
		},
		success: function(html) {
			$('#' + id).html(html);
			$(_this).removeClass('loading');
		}
	});
}

function prompting_add(_this, vel, el) {
	if ($_(vel)) {
		var v = $_(vel).value.split(',');
		var n = _this.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';
}


/*---------------------------------------------------------- popup -----------------------------------------------------*/
function winpopup(url, w, h) {
	w = w ? w : 800;
	h = h ? h : 500;

	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,'mf',settings)
	win.focus();
}
