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 = {
	val: function(_this, def, d) {
		if (d == -1) {
			if (_this.value == def) {
				_this.value = '';
				$(_this).removeClass('default');
			}
		} else {
			if (_this.value == '') {
				_this.value = def;
				$(_this).addClass('default');
			}
		}
	}
}


/*---------------------------------------------------------- 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;
	},
	
	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();
		}
	}
};


/*---------------------------------------------------------- layers ----------------------------------------------------*/
var 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.settings[id] && this.settings[id].opener != _this)) {
			modal.remove(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();
	},
	
	move: function(_this, obj, d) {
		if (_this && obj) {
			switch (d) {
				case 'left':
					$(obj).css({ marginLeft:(_this.offsetLeft) + 'px' });
					break;
					
				case 'right':
					$(obj).css({ marginLeft:(_this.offsetLeft + _this.offsetWidth) + 'px' });
					break;
					
				default:
					$(obj).removeClass('modal-moved').css({ marginLeft:'' })
				
					var position = $(_this).position();
					var w = Math.floor(_this.offsetWidth / 2 - obj.offsetWidth / 2);
				
					if ((position.left + w) >= 0) {
						$(obj)
							.css({ marginLeft:w + 'px' })
							.addClass('modal-moved');
					}
			}
		}
	}
};

/* constructor */
modal.fc = function(_this, id, url, hash, data) {
	this.id = id;
	this.opener = _this;
	this.url = url;
	this.html = '';
	this.data = data ? data : 0;
	this.scroll = 0;
	this.modal = 1;
	this.overlay = 1;
	this.view = 1;
	this.move = 0;
	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"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + this.id + '_overlay" class="overlay overlay-preloader"><table id="' + this.id + '_container" class="modal' + (this.view == 1 ? '' : 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 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' + (this.view == 1 ? '' : 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 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 + '_container'))) {
					self.remove();
				}
			};
			$(document).bind('click', self.docevnt);
		}

		
		if (self.move) {
			modal.move(self.opener, $('#' + self.id + ' div.modal-w').get(0), self.move);
		}
		
		if (this.url) {
			$.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 );
					}
					
					$('#' + self.id + '_content').html(html);
					$('#' + self.id + '_overlay').removeClass('overlay-preloader');
					
					if (self.move) {
						modal.move(self.opener, $('#' + self.id + ' div.modal-w').get(0), self.move);
					}
				}
			});
		} else {
			$('#' + self.id + '_content').html(self.html);
			$('#' + 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');
	}
};
