function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}


$(function(){
	input.check();
});


var input = {
	isnum: function(e) {
		var c = e.keyCode ? e.keyCode : e.charCode
		if (c >= 48 && c <= 57) 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;
		}
	},
	
	check: function() {
		$('input[type=text], select, textarea')
			.focus(function(){
				$('#' + this.id + '_check div').fadeIn();
				$('#' + this.id + '_check')
					.unbind('mouseover')
					.unbind('mouseout');
			})
			.blur(function(){
				$('#' + this.id + '_check div').fadeOut();
				$('#' + this.id + '_check')
					.mouseover(function(){
						$(this).children(':first').css({ display:'block' });
					})
					.mouseout(function(){
						$(this).children(':first').css({ display:'none' });
					});
			});
			
		$('div.check')
			.mouseover(function(){
				$(this).children(':first').css({ display:'block' });
			})
			.mouseout(function(){
				$(this).children(':first').css({ display:'none' });
			})
	}
};


var pagination = {
	ctrl: function() {
		var self = this;
		
		self.docevnt = function(e) {
			if (e.ctrlKey && !e.altKey && !e.shiftKey) {
				switch (e.keyCode) {
					case 37:
						$('td.pagination-prev a').click();
						break;
					case 39:
						$('td.pagination-next a').click();
						break;
				}
			}
		};
		$(document)
			.unbind('keydown', self.docevnt)
			.bind('keydown', self.docevnt);
	}
};


/*----------------------------------------------------------- ajax -----------------------------------------------------*/
var page = {
	settings: {},

	load: function(id, url, form, data, hash) {
		if (gebi(id)) {
			this.settings[id] = new this.fc(id, url, form, data, hash);
			this.settings[id].start();
		}
		
		return false;
	}
};

/* constructor */
page.fc = function(id, url, form, data, hash) {
	this.id = id;
	this.url = url;
	this.form = form;
	this.data = data;
	this.preloader = 1;
	this.manipulation = 'html'; //jquery manipulations
	this.fun = function(){ };
	
	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
page.fc.prototype = {
	offset: function () {
		var h = gebi(this.id).offsetHeight + 'px';
	
		$('#' + this.id + ' div.preloader div.preoverlay').css({ height:h });
		$('#' + this.id + ' div.preloader div.pretimer').css({ height:h });
	},
	
	overlay: function() {
		var self = this;
		$('#' + this.id).prepend('<div class="preloader"><div class="preoverlay"></div><div class="pretimer"></div></div>')
		
		this.offset();
		
		$(window).resize(function() {
			self.offset();
		});
	},
	
	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 );
		}
	
		switch(this.manipulation) {
			case 'append':
				$('#' + this.id).append(html);
				$('#' + this.id + ' div.preloader').remove();
				break;
			case 'replaceWith':
				$('#' + this.id).replaceWith(html);
				break;
			default:
				$('#' + this.id).html(html);
		}
		this.fun();
		input.check();
	},
	
	start: function () {
		var self = this;

		if (this.preloader) this.overlay();
		
		if (this.form) {
			var options = {
				type: 'post',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {
					self.success(html);
				}
			};
			$(this.form).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {
					self.success(html);
				}
			});
		}
	}
};


/*---------------------------------------------------------- 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;	
	},
	
	tabs: function(_this, hide, show, tabs) {
		if (_this.className.indexOf('toggle-active') != -1) return;
	
		if (tabs && _this.tagName.toLowerCase() != 'select') {
			$(tabs + ' .toggle-active').removeClass('toggle-active');
			$(_this).addClass('toggle-active');
		}

		$(hide).css({ display:'none' });
		$(show).show();
		
		if (_this.tagName.toLowerCase() == 'a') _this.blur();
	},
	
	msg: function(_this) {
		$(_this).animate({
			width:'hide'
		}, 1500);
	},
	
	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;
	this.spacermove = 0;
	this.owner = 0;
	this.focus = 0;
	this.fun = function(){ };

	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
toggle.fc.prototype = {
	suchide: function() {
		var self = this;
		$(this.opener).removeClass('toggle-active');
		$(document).unbind('click', self.docevnt);
		
		this.fun();
	},
	
	sucshow: function() {
		if (this.focus) $('#' + this.id + ' ' + this.focus).focus();
	},
	
	movespacer: function() {
		var x = this.opener.offsetLeft + this.opener.offsetWidth / 2;
		$(this.spacermove).css({ left:x });
	},

	hide: function() {
		var self = this;
		
		if (this.showtime) clearTimeout(this.showtime);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function() {
						self.suchide();
					}
				);
				break;
			case 'slide':
				$('#' + this.id).slideUp(
					'slow',
					function() {
						self.suchide();
					}
				);
				break;
			default:
				$('#' + this.id).css({ display:'none' });
				self.suchide();
		}
	},
		
	show: function() {
		var self = this;
		
		this.fun();
		
		if (this.owner)  this.owner.blur();
		if (this.spacermove) this.movespacer();
		
		$(this.opener).addClass('toggle-active');
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn(
					'slow',
					function() {
						self.sucshow();
					}
				);
				break;
			case 'slide':
				$('#' + this.id).slideDown(
					'slow',
					function() {
						self.sucshow();
					}
				);
				break;
			default:
				$('#' + this.id).css({ display:'block' });
				self.sucshow();

		}
		
		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)) && !toggle.ischild(target, self.owner)) {
					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, fun) {
		this.settings[id] = new this.fc(_this, id, url, hash, data, fun);
		return this.settings[id];
	},

	add: function(_this, id, url, hash, data, fun) {
		if (!gebi(id)) {
			this.init(_this, id, url, hash, data, fun).add();
		} else {
			var $win = $('#' + id);
			if ($win.css('display') == 'none') {
				if (hash['cache'] == -1) {
					$win.remove();
					this.init(_this, id, url, hash, data, fun).add();
				} else {
					modal.show(id);
				}
			} else {
				modal.remove(id);
			}
		}
		
		_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, fun) {
	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.fun = fun && typeof fun == 'function' ? fun : function(){ };
	
	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"><div></div></td><td class="modal-12"><div></div></td><td class="modal-13"><div></div></td></tr><tr><td class="modal-21"><div></div></td><td class="modal-22">'+ (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"><div></div></td></tr><tr><td class="modal-31"><div></div></td><td class="modal-32"><div></div></td><td class="modal-33"><div></div></td></tr></table></td></tr></table>' + (this.overlay ? '<div class="overlay"></div><iframe class="overlay"></iframe>' : '') + '</div>');
		} else {
			$(this.container).after('<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"></div><iframe class="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"></div><iframe class="overlay"></iframe></div>');
		}
		
		if (!this.modal) $(document).bind('click', self.docevnt);
	},

	remove: function() {
		var self = this;
		
		$(document).unbind('click', self.docevnt);
		$('#' + this.id).css({ display:'none' })
		
		if (this.container != 'body' && this.overlay) $('#' + this.id + '_overlay').remove();
		
		$(this.opener).removeClass('toggle-active');
		
		this.fun();
	},
	
	movespacer: function() {
		var x = this.opener.offsetLeft + this.opener.offsetWidth / 2;
		$('#' + this.id + ' div.modal-spacer').css({ left:x });
	}
};

