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) {
			if (_this.value == def) {
				_this.value = '';
				$(_this).removeClass('default');
			}
		} else {
			if (_this.value == '') {
				_this.value = def;
				$(_this).addClass('default');
			}
		}
	},
	
	check: function(id, url, data) {
		$('#' + id).html('<div class="preloader-field"><div></div></div>');
		$.ajax({
			type: 'post',
			url: url,
			data: data ? data : false,
			success: function(html) {
				$('#' + id).html(html);
			}
		});
	},
	
	file: function(_this, id) {
		var file = _this.value.split("\\");
		file = file[file.length-1];
		var html = '<a href="#:delete" onclick="input.reset(\'' + _this.id + '\'); $(\'#' + id + '\').html(\'\'); this.blur(); return false;"></a>' + file;
		$('#' + id).html(html);
	},
	
	reset: function(id) {
		var o = gebi(id);
		if (o.type == 'file') {
			o.parentNode.innerHTML = o.parentNode.innerHTML;
		} else {
			o.value = '';
		}
	}
}


var tariff = {
	offset: function() {
		var o = gebi('tariff_type');
		var h = o ? o.offsetHeight + 'px' : 'auto';
		
		$('table.period').css({ height:h });
	},
	
	status: function(_this) {
		var h = _this.parentNode.offsetTop - gebi('item_current').offsetTop;
		
		if (h < 0) $('div.spacer-current div').addClass('spacer-down');
			else $('div.spacer-current div').removeClass('spacer-down');
		$('div.spacer-current span').css({ height:(h ? Math.abs(h) + 3 : 0) + 'px' });
	},
	
	toggle: function(_this, group, item) {
		_this.blur();
		
		$('#tariff_add div.tariff-add').css({ display:'none' });		
		$('#' + group + ' tr.type td.item-add div').addClass('item-add-hide');
		$('#' + group + ' tr.type')
			.removeClass('type-ainc')
			.removeClass('type-adec');

		if (gebi('type_' + item).className.indexOf('type-dec') != -1) {
			$('#tariff_add')
				.removeClass('tariff-inc')
				.addClass('tariff-dec');
			$('#type_' + item + '.type-dec').addClass('type-adec');
		} else {
			$('#tariff_add')
				.removeClass('tariff-dec')
				.addClass('tariff-inc');
			$('#type_' + item + '.type-inc').addClass('type-ainc');
		}
		
		$('#type_' + item + ' td.item-add div').removeClass('item-add-hide');
		$('#tariff_add_' + item).css({ display:'block' });
		
		this.offset();
	}
}


var selectmac = {
	set: function(){
		var self = this;
		
		$('div.select-mac').each(function(){
			var select = this;
	
			$(select).addClass('select-mac-active');
		
			var wrap = $('ul', this).get(0);
			var width = wrap.offsetWidth + 'px';
			
			$(this).css({ width:width });
			$(wrap).css({ width:width });
			
			$(select).removeClass('select-mac-active');
			
			select.docevnt = function(e){
				var target = e.srcElement || e.target;
				if (!toggle.ischild(target, select)) {
					$(select).removeClass('select-mac-focus');
					self.hide(select, wrap);
				}
			};
			
			$('input[type=radio]', this)
				.focus(function(){
					$(select).addClass('select-mac-focus');
				})
				.blur(function(){
					$(select).removeClass('select-mac-focus');
				});
			
			$('li', this).click(function(e){
				var target = e.srcElement || e.target;
				
				$('input[type=radio]', this)
					.click()
					.focus();
				$(select).addClass('select-mac-focus');
				$(document).bind('click', select.docevnt);
				$('input').bind('focus', select.docevnt);
				
				if (this.className.indexOf('selected') != -1 && target.tagName.toLowerCase() != 'input') {
					if (select.className.indexOf('select-mac-active') != -1) {
						self.hide(select, wrap);
					} else {
						self.show(this, select, wrap);
					}
				} else {
					self.change(this, select, wrap);
				}
			})
			
			$('label', this).click(function(e){
				e.preventDefault();
			});
		});
	},
	
	change: function(_this, select, wrap) {
		$('li.selected', wrap).removeClass('selected');

		$(_this).addClass('selected');
		
		this.hide(select, wrap);
	},
	
	show: function(_this, select, wrap){
		$(select).addClass('select-mac-active');
		
		var top = (- _this.offsetTop + 1) + 'px';
		
		$(wrap).css({ marginTop:top });
	},
	
	hide: function(select, wrap){
		$(select).removeClass('select-mac-active');
		$(wrap).css({ marginTop:0 });
		
		$(document).unbind('click', 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;
	},
	
	script: function(source, test) {
		var scripts = [];
 
		// Strip out tags
		while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
			var s = source.indexOf("<script");
			var s_e = source.indexOf(">", s);
			var e = source.indexOf("</script", s);
			var e_e = source.indexOf(">", e);
 
			// Add to scripts array
			scripts.push(source.substring(s_e+1, e));
			// Strip from source
			source = source.substring(0, s) + source.substring(e_e+1);
		}
		
		// Loop through every script collected and eval it
		for(var i=0; i<scripts.length; i++) {
			try {
				var fs = scripts[i].indexOf("page.manipulate");
				if (fs > -1) {
					eval(scripts[i].substring(fs));
					scripts[i] = scripts[i].substring(0, fs);
				}
			}
			catch(e) {
			
			}
		}
		
		if (typeof page.manipulate == 'function') {
			page.manipulate(source);
			page.manipulate = null;
		}

		for(var i=0; i<scripts.length; i++) {
			try {
				eval(scripts[i]);
			}
			catch(e) {
			
			}
		}		
	}
};

/* 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.test = 0;
	
	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 'prepend':
				$('#' + this.id).prepend(html);
				$('#' + this.id + ' div.preloader').remove();
				break;
			case 'replaceWith':
				$('#' + this.id).replaceWith(html);
				break;	
			case 0:
			case 'none':
				page.script(html, this.test);
				$('#' + this.id + ' div.preloader').remove()
				break;
			default:
				$('#' + this.id).html(html);
		}
		$('#' + this.id).removeClass('preloader-container');
	},
	
	start: function () {
		var self = this;
		
		$('#' + this.id).addClass('preloader-container');

		if (this.preloader) this.overlay();
		
		if (this.form) {
			var options = {
				type: 'post',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {
					if (self.preloader) {
						setTimeout(function(){
							self.success(html);
						}, 500);
					} else {
						self.success(html);
					}
				}
			};
			$(this.form).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: self.url,
				data: self.data ? self.data : false,
				success: function(html) {
					if (self.preloader) {
						setTimeout(function(){
							self.success(html);
						}, 500);
					} else {
						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;	
	},

	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).fadeIn();
			
		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 ----------------------------------------------------*/
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.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) {
		if (_this && obj) {
			w = Math.floor(_this.offsetWidth / 2 - obj.offsetWidth / 2);
			$(obj).css({ marginLeft:w + 'px' });
		}
	}
};

/* 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))) {
					self.remove();
				}
			};
			$(document).bind('click', self.docevnt);
		}
				
		if (this.url) {
			$.ajax({
				type: 'get',
				url: self.url,
				data: self.data,
				success: function(html) {
					$('#' + 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));
					}
				}
			});
		} 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');
	}
};