function gebi(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>');
}


var input = {
	isnum: function(e) {
		var c = e.keyCode ? e.keyCode : e.charCode;
		if (c >= 48 && c <= 57 || c == 8 || c == 9 || c == 37 || c == 39 || c == 46) return true;
		return false;
	},
	
	val: function(_this, def, d) {
		if (d == -1) {
			_this.value = _this.value == def ? '' : _this.value;
		} else {
			_this.value = _this.value == '' ? def : _this.value;
		}
	},
	
	count: function ($source, $target, $max) {
		var $target = $($target);
		var $source = $($source);
		var $text = $source.val().replace(/\r\n/gi,'\n');
		$text= $text.replace(/\n/gi,' ');
		
		if ($text.length > $max) {
			$source.val($source.val().substring(0, $max));
		}
		
		$target.html(parseInt($max - $source.val().length));
	},
	
	checked: function(_this) {
		if ($(_this.parentNode.parentNode).hasClass('checkbox')) {
			if (_this.checked) {
				if (_this.type == "radio") {
					$('input[name=' + _this.name + ']').each(function(){
						$(this.parentNode.parentNode).removeClass('checkbox-checked');
					});
				}
				$(_this.parentNode.parentNode).addClass('checkbox-checked');
			} else {
				$(_this.parentNode.parentNode).removeClass('checkbox-checked');
			}
		}
	},
	
	children: function(_this, node) {
		if (_this.checked) {
			$(node + ' input[type=checkbox]').each(function(){
				this.checked = true;
				input.checked(this);
			});
		} else {
			$(node + ' input[type=checkbox]').each(function(){
				this.checked = false;
				input.checked(this);
			});
		}
	},
	
	parent: function(_this, node, parent) {
		if (_this.checked) {
			$(parent).attr('checked', true);
		} else {
			var f = false;
			var a = $(node + ' input[type=checkbox]').get();
			
			for (i = 0; i < a.length; i++) {
				if (a[i].checked) {
					f = true;
					break;
				}
			}
			
			$(parent).attr('checked', f);
		}
	}
}


var clndr = {
	show: 0,
	hover: null,
	$popup: null,
	
	init: function(){
		$('table.event').each(function(){
			var h = this.parentNode.offsetHeight - 5;
			
			$(this).css({ height:h + 'px' });
			
			$('td.event', this).hover(
				function(){
					var $popup = $('div.modal-bind', this);
				
					if (this != clndr.hover) {
						if (clndr.$popup) clndr.$popup.css({ display:'none' });
						clndr.hover = this;
						clndr.$popup = $popup; 

						clearTimeout(clndr.show);
						clndr.show = setTimeout(function(){
							$popup.fadeIn();
						}, 500);
					}
				},
				function(){
					if (clndr.$popup) {
						clearTimeout(clndr.show);
						clndr.$popup.css({ display:'none' });
						clndr.hover = null;
					}
				}
			);
		});
	}
};


/*---------------------------------------------------------- toggle ----------------------------------------------------*/
var toggle = {
	settings: {},

	init: function(_this, id, hash) {
		this.settings[id] = new this.fc(_this, id, hash);
		return this.settings[id];
	},
	
	show: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).show();
		}
		_this.blur();
		
		return false;	
	},
	
	hide: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).hide();
		}
		_this.blur();
		
		return false;	
	},
	
	execute: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash).execute();
		}
		_this.blur();
	
		return false;	
	},

	move: function(_this, id, hash) {
		var parent = hash['parent'];
		 
		if (gebi(id) && parent && gebi(parent)) {		
			this.init(_this, id, hash);
			this.settings[id].move();
		}
		_this.blur();
		
		return false;	
	},
	
	tabs: function(_this, hide, show, tabs) {
		if (_this.className.indexOf('toggle-active') != -1) return;
	
		if (tabs && _this.tagName.toLowerCase() != 'select') {
			var bind = Math.floor(_this.offsetLeft + _this.offsetWidth / 2);
			
			if (show) $(show + ' div.tab-spacer').css({ marginLeft:bind + 'px' });
			$(tabs + ' .toggle-active').removeClass('toggle-active');
			$(_this).addClass('toggle-active');
		}

		$(hide).css({ display:'none' });
		if (show) $(show).show();
			
		if (_this.tagName.toLowerCase() == 'a') _this.blur();
	},
	
	ischild: function (s,d) {
		while (s) {
			if (s == d) return true;
			s = s.parentNode;
		}
		return false;
	}
};

/* constructor */
toggle.fc = function(_this, id, hash) {
	var cur = toggle.settings[id];
	if (cur && cur.opener != _this && cur.opener.className.indexOf('toggle-active') != -1) {
		$(cur.opener).removeClass('toggle-active');
	}

	this.id = id;
	this.opener = _this;
	this.modal = 1;
	this.effect = 'slide';
	this.showtime = 0;

	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
toggle.fc.prototype = {
	success: function() {
		var self = this;
		
		$(this.opener).removeClass('toggle-active');
		$(document).unbind('click', self.docevnt);
	},

	hide: function() {
		var self = this;
		
		if (this.showtime) clearTimeout(this.showtime);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function() {
						self.success();
					}
				);
				break;
			case 'slide':
				$('#' + this.id).slideUp(
					'slow',
					function() {
						self.success();
					}
				);
				break;
			default:
				$('#' + this.id).css({ display:'none' });
				self.success();
		}
	},
		
	show: function() {
		var self = this;

		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn();
				break;
			case 'slide':
				$('#' + this.id).slideDown();
				break;
			default:
				$('#' + this.id).css({ display:'block' });

		}
		
		$(this.opener).addClass('toggle-active');
		
		
		if (this.showtime) {
			this.showtime = setTimeout(function(){ toggle.hide(self.opener, self.id, { effect:self.effect }); }, this.showtime);
		}
		
			
		if (!this.modal) {
			self.docevnt = function(e){
				var target = e.srcElement || e.target;
				if (!toggle.ischild(target, self.opener) && !toggle.ischild(target, gebi(self.id))) {
					self.hide();
				}
			};
			$(document).bind('click', self.docevnt);
		}
	},

	execute: function() {
		var self = this;
		
		$('#' + this.id).click(function(e) {
			e.stopPropagation();
		});
		
		if ($('#' + this.id).css('display') == 'none') {
			self.show();
		} else {
			self.hide();
		}
	},
	
	move: function() {
		var show = 1;
		
		if (gebi(this.id).style.display != 'none') show = 0;
	
		$container = $('#' + this.id);
		
		var html = $container.html();
		var clas = $container.attr('class');
		
		$container
			.css({ display:'none' })
			.remove();
			
		var tmp = $('<div></div>')
				.attr('id', this.id)
				.addClass(clas)
				.html(html);
		
		if (show) tmp.css({ display:'none' });
		
		$('#' + this.parent).append(tmp);
		
		$(this.opener).addClass('toggle-active');
		
		if (show) this.show();
	}
};


/*---------------------------------------------------------- layers ----------------------------------------------------*/
modal = {
	settings: {},

	init: function(_this, id, url, hash, data) {
		this.settings[id] = new this.fc(_this, id, url, hash, data);
		return this.settings[id];
	},

	add: function(_this, id, url, hash, data) {
		if (!gebi(id)) {
			this.init(_this, id, url, hash, data).add();
		} else {
			var $win = $('#' + id);
			if ($win.css('display') == 'none') {
				if (hash && hash['cache'] && hash['cache'] == 1) {
					modal.show(id);
				} else {
					$win.remove();
					this.init(_this, id, url, hash, data).add();
				}
			} else {
				modal.remove(id);
			}
		}

		if (_this) _this.blur();
	},
	
	remove: function(id) {
		var win = this.settings[id];
		if (win) win.remove();
	},
	
	show: function(id) {
		var win = this.settings[id];
		if (win) win.show();
	}
};

/* constructor */
modal.fc = function(_this, id, url, hash, data) {
	this.id = id;
	this.opener = _this;
	this.url = url;
	this.data = data ? data : 0;
	this.scroll = 0;
	this.modal = 1;
	this.overlay = 1;
	this.view = 1;
	this.close = 1;
	this.container = 'body';
	this.spacermove = 0;
	this.cache = 0;
	
	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
};

/* methods */
modal.fc.prototype = {
	add: function() {
		var self = this;
		
		$(this.opener).addClass('toggle-active');
		
		if (this.container == 'body') {
			if (!this.scroll) window.scroll(0, 0);
			
			$('body').append('<div id="' + this.id + '" class="modal' + (this.view == 1 ? '' : this.view) + '"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + this.id + '_overlay" class="overlay overlay-preloader"><table id="' + this.id + '_container" 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 png">'+ (this.close == 1 ? '<div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a></div>' : '') + '<div class="modal-content"><div id="' + this.id + '_content"><div class="modal-preloader"></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>' + (this.overlay ? '<div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay"></iframe>' : '') + '</div>');
		} else {
			$(this.container).append('<div id="' + this.id + '" class="modal-bind' + (this.view == 1 ? '' : this.view) + '"><div class="modal-w"><div class="modal-spacer png"></div><table id="' + this.id + '_container" 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-content">' + (this.close == 1 ? '<div class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a></div>' : '') + '<div id="' + this.id + '_content"><div class="modal-preloader"></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></div></div>');
			if (this.overlay) $('body').append('<div id="' + this.id + '_overlay"><div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></iframe></div>');
		}
		
		if (!this.modal) {
			self.docevnt = function(e){
				var target = e.srcElement || e.target;
				if (!toggle.ischild(target, self.opener) && !toggle.ischild(target, gebi(self.id))) {
					self.remove();
				}
			};
			$(document).bind('click', self.docevnt);
		}
		
		$.ajax({
			type: 'get',
			url: self.url,
			data: self.data,
			success: function(html) {
				var bodyStart = html.toLowerCase().indexOf("<body>");
				var bodyEnd = html.toLowerCase().indexOf("</body>");

				if (bodyStart > -1 && bodyEnd > -1)	{
					html = html.substring( bodyStart + 6, bodyEnd );
				}
				
				evalScriptsOnResponse( html );
				
				if (self.spacermove) self.movespacer();
				
				$('#' + self.id + '_overlay').removeClass('overlay-preloader');
			}
		});
	},
	
	show: function() {
		var self = this;
		
		$(this.opener).addClass('toggle-active');
		$('#' + this.id).css({ display:'block' });
		
		if (this.container == 'body') {
			if (!this.scroll) window.scroll(0, 0);
		} else if (this.overlay) {
			$('body').append('<div id="' + this.id + '_overlay"><div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div><iframe class="overlay"></iframe></div>');
		}
		
		if (!this.modal) $(document).bind('click', self.docevnt);
	},

	remove: function() {
		var self = this;
		
		$(document).unbind('click', self.docevnt);
		
		if (this.cache) {
			$('#' + this.id).css({ display:'none' });
		} else {
			$('#' + this.id).remove();
		}
		
		if (this.container != 'body' && this.overlay) $('#' + this.id + '_overlay').remove();
		
		$(this.opener).removeClass('toggle-active');
	},
	
	movespacer: function() {
		if (this.opener) {
			var x = this.opener.offsetLeft + this.opener.offsetWidth / 2;
			$('#' + this.id + ' div.modal-spacer').css({ left:x });
		}
	}
};

