/*******************************************************************************
		Coded by GS on 01.07.2008, True Vision
*******************************************************************************/

function gebi(n) {
	if(!n) return false;
	if(!document.getElementById(n)) return false;
	return document.getElementById(n)
}


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 || c == 13) 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 $input = $('#' + id + ' input');
			var val = $input.val();
			var len = val ? val.length : 0;

			if (len > 20) {
				var w = Math.round(len * 5 + d);
				w = w < 400 ? w : 400;

				$input.animate({ width: w + 'px' });
			}
		}
	}
};

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.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>')

		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);
		}
		input.width('search_keyword', 68);
	},

	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);
				}
			});
		}
	}
};


toggle = {
	settings: {},

	init: function(_this, id, hash, fun) {
		this.settings[id] = new this.fc(_this, id, hash, fun);
		return this.settings[id];
	},

	show: function(_this, id, hash, fun) {
		if (gebi(id)) {
			this.init(_this, id, hash, fun).show();
		}
		_this.blur();

		return false;
	},

	hide: function(_this, id, hash, fun) {
		if (gebi(id)) {
			this.init(_this, id, hash, fun).hide();
		}
		_this.blur();

		return false;
	},

	execute: function(_this, id, hash, fun) {
		if (gebi(id)) {
			this.init(_this, id, hash, fun).execute();
		}
		_this.blur();

		return false;
	},

	form: function(_this, id, gid, tid) {
		if(_this.checked && $('#' + id).css('display') == 'none') {
			$('#' + gid + ' div.form-group').css({ display:'none' });
			$('#' + id).fadeIn();
		}
	},

	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).fadeIn();

		if (_this.tagName.toLowerCase() != 'select') _this.blur();
	},

	change: function(id, clas, btn, txt1, txt2) {
		if ($('#' + id).hasClass(clas)) {
			$('#' + id).removeClass(clas);
			$('#' + btn).html(txt2);
		} else {
			$('#' + id).addClass(clas);
			$('#' + btn).html(txt1);
		}
	},

	ischild: function (s,d) {
		while (s) {
			if (s == d) return true;
			s = s.parentNode;
		}
		return false;
	}
};

/* constructor */
toggle.fc = function(_this, id, hash, fun) {
	this.id = id;
	this.opener = _this;
	this.modal = 1;
	this.effect = 'slide';
	this.flag = 0;
	this.fun = fun && typeof fun == 'function' ? fun : function(){ };

	try {
		for (var i in hash) this[i] = hash[i];
	} catch(e) {
		alert(e);
	}
}

/* methods */
toggle.fc.prototype = {
	success: function() {
		$(this.opener).removeClass('toggle-active');
	},

	hide: function() {
		var self = this;

		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function() {
						self.success();
						self.fun();
					}
				);
				break;
			case 'slide':
				$('#' + this.id).slideUp(
					'slow',
					function() {
						self.success();
						self.fun();
					}
				);
				break;
			default:
				$('#' + this.id).css({ display:'none' });
				hide_success();
				self.fun();
		}
	},

	show: function() {
		var self = this;

		self.fun();

		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');
	},

	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();
		}
	}
};

/*
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();
	}
};
*/
