function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}


function isNumeric(e) {
	var c = e.keyCode ? e.keyCode : e.charCode
	if (c >= 48 && c <= 57) return true;
	return false;
}


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>');
}


page = {
	offset: function (id) {
		var h = gebi(id).offsetHeight + 'px';
	
		$('#' + id + ' div.preloader div.preoverlay').css({ height:h });
		$('#' + id + ' div.preloader div.pretimer').css({ height:h });
	},
	
	overlay: function(id) {
		var self = this;
		$('#' + id).prepend('<div class="preloader"><div class="preoverlay"></div><div class="pretimer"></div></div>');
		
		this.offset(id);
		
		$(window).resize(function() {
			self.offset(id);
		});
	},
	
	load: function (id, url, _this, data) {
		this.overlay(id);
//alert(1);
		if (_this) {
			var options = {
				type: 'post',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
					setTimeout(function(){
							$('#' + id + ' div.scrollable').scroller();
						},
						500
					);
				}
			};
			$(_this).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
					setTimeout(function(){
							$('#' + id + ' div.scrollable').scroller();
						},
						500
					);
				}
			});
		}
	}
};


modal = {
	add: function(id, url, hash, data) {
		this.scroll = 0;
		this.modal = 0;
		this.overlay = 1;
		this.view = null;
		this.iframe = 0;
		this.width = 800;
		this.height = 600;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
		
		if (!this.scroll) window.scroll(0, 0);
		
		$('body').append('<div id="' + id + '" class="modal"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + id + '_overlay" class="overlay overlay-preloader"><table id="' + id + '_container" class="modal' + (this.view ? '-' + this.view : '') + '"><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 id="' + id + '_content"></div><div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + id + '\'); this.blur; return false;"></a></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>' + (this.overlay ? '<div class="overlay"></div><iframe class="overlay"></iframe>' : '') + '</div>');
		
		if (!this.modal) {
			$('#' + id).click(function(){
				modal.remove(id);
			});	
			
			$('#' + id + '_container').click(function(e){
				e.stopPropagation();
			});
		}
		
		if (this.iframe) {
			$('#' + id + '_content').html('<iframe src="' + url + '" frameborder="0" style="width:' + this.width + 'px; height:' + this.height + 'px;"></iframe>');
			$('#' + id + '_overlay').removeClass('overlay-preloader');
		} else {
			$.ajax({
				type: 'get',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id + '_content').html(html);
					$('#' + id + '_overlay').removeClass('overlay-preloader');
					$('#' + id + '_container').css({ visibility:'visible' });
					setTimeout(function(){
							$('#' + id + ' div.scrollable').scroller();
						},
						500
					);
				}
			});
		}
	},

	remove: function (id) {
		$('#' + id).css({ display:'none' })
			.remove();
	}
};
