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(){
	$('#menu dl')
		.mouseover(function(){
			mainmenu.show(this);
		})
		.mouseout(function(){
			mainmenu.hide(this);
		});
});

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;
		}
	},
	
	width: function(id, d) {
		if (gebi(id)) {
			d = d || 0;
			var val = $('#' + id + ' input').val();
			var len = val ? val.length : 0;
			
			if (len > 20) {
				var w = Math.round(len * 6.5 + d);
				w = w < 400 ? w : 400;
				
				$('#' + id).animate({ width: w + 'px' });
			}
		}
	}
};

mainmenu = {
	timer: 0,
	
	current: 0,
	
	show: function(_this){
		clearTimeout(mainmenu.timer);

		var dd = _this.getElementsByTagName('dd')[0];
		
		if (this.current != _this) {
			this.current = _this;
			$('#menu dl').removeClass('default');
			$('#menu dl dd').css({ display:'none' });
			$(_this).addClass('default');
			if (dd && $(dd).css('display') != 'block') $(dd).fadeIn();
		} 
	},
	
	hide: function(_this) {
		var self = this;
		var dd = _this.getElementsByTagName('dd')[0];
		
		if (_this.className.indexOf('active') == -1) {
			clearTimeout( mainmenu.timer );
			mainmenu.timer = setTimeout(function() {
					self.current = 0;
					
					$(_this).removeClass('default');
					if (dd) $(dd).fadeOut();
					
					$('#menu dl.active').addClass('default');
					$('#menu dl.active dd').fadeIn();
			}, 500);
		}
	}
};


toggle = {
	settings: {},

	init: function(_this, id, hash) {
		if (typeof this.settings[id] == 'undefined') {
			this.settings[id] = new this.fc(_this, id, hash);
		}
	},
	
	tabs: function(_this, container) {
		$('#' + container + ' span.item').removeClass('toggle-active');
		$(_this).addClass('toggle-active');
	},
	
	show: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash);			
			this.settings[id].show();
		}
		_this.blur();
		
		return false;	
	},
	
	hide: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash);			
			this.settings[id].hide();
		}
		_this.blur();
		
		return false;	
	},
	
	execute: function(_this, id, hash) {
		if (gebi(id)) {
			this.init(_this, id, hash);			
			this.settings[id].execute();
		}
		_this.blur();
		
		return false;	
	},
	
	move: function(_this, id, hash) {
		var parent = hash['parent'];
		var flag = this.settings[id];
		 
		if (gebi(id) && parent && gebi(parent)) {		
			this.init(_this, id, hash);
			//if (!flag) this.settings[id].flag = parent;
			this.settings[id].move(_this, parent);
		}
		_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) {
	this.id = id;
	this.opener = _this;
	this.modal = 1;
	this.effect = 'slide';
	this.flag = 0;

	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
toggle.fc.prototype = {
	hide: function() {
		var self = this;

		function hide_success() {
			$(self.opener).removeClass('toggle-active');
		}
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function() { hide_success(); }
				);
				break;
			case 'slide':
				$('#' + this.id).slideUp(
					'slow',
					function() { hide_success(); }
				);
				break;
			default:
				$('#' + this.id).css({ display:'none' });
				hide_success();
		}
	},
		
	show: function() {
		var self = this;

		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn('slow');
				break;
			case 'slide':
				$('#' + this.id).slideDown('slow');
				break;
			default:
				$('#' + this.id).css({ display:'block' });
		}
		
		$(this.opener).addClass('toggle-active');
	},

	execute: function() {
		var self = this;
		
		$('#' + this.id).click(function(e) {
			e.stopPropagation();
		});
		
		if ($('#' + this.id).css('display') == 'none') {
			self.show();
			
			if (!this.modal) {
				$(document).click(function(e){
					var target = e.srcElement || e.target;
					if (!toggle.ischild(target, self.opener) && !toggle.ischild(target, gebi(self.id))) {
						self.hide();
					}
				});
			}
		} else {
			self.hide();
		}
	},
	
	move: function(_this, id) {
		var show = 1;
	
		if (this.flag != id) {
			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' });
			
			$('#' + id).after(tmp);
			
			$(this.opener).removeClass('toggle-active');
			
			this.opener = _this;
			this.flag = id;
			
			$(this.opener).addClass('toggle-active');
		}
		
		if (show) this.execute();
	}
};


page = {
	settings: {},

	load: function(id, url, form, data, hash) {
		if (gebi(id)) {
			if (typeof this.settings[id] == 'undefined') {
				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.manipulation = 'html'; //jquery manipulations
	
	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>')
			.addClass('loading');
		
		this.offset();
		
		$(window).resize(function() {
			self.offset();
		});
	},
	
	success: function(html) {
		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.id).removeClass('loading');
	},
	
	start: function () {
		var self = this;

		this.overlay();
//alert(1);
		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);
				}
			});
		}
	}
};


modal = {
	add: function(id, url, hash, data) {
		this.scroll = 0;
		this.modal = 0;
		this.overlay = 1;
		this.view = null;
		
		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 class="modal-close"><a class="png" href="#close" onclick="modal.remove(\'' + id + '\'); this.blur; return false;"></a></div><div id="' + id + '_content"></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();
			});
		}
		
		$.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' });
				$('#' + id + ' div.scrollable').scroller();
			}
		});
	},

	remove: function (id) {
		$('#' + id).css({ display:'none' })
			.remove();
	}
};
