/*******************************************************************************
		Coded by GS on 05.02.2010, True Vision
*******************************************************************************/

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>');
		$.ajax({
			type: 'post',
			url: url,
			data: data ? data : false,
			success: function(html) {
				$('#' + id).html(html);
			}
		});
	},

	pass: function(_this, node) {
		$(_this).hide();
		$(node)
			.show()
			.val(_this.value);

		if (_this.type == 'password') $(node).focus();
	},

	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 = '';
		}
	},

	select: function(_this, id) {
		if (id) {
			if (gebi(id)) {
				if ($(_this).hasClass('selected')) {
					$('#' + id)
						.removeAttr('checked')
						.blur();
					$(_this).removeClass('selected');
				} else {
					$('#' + id)
						.attr('checked', true)
						.blur();
					$(_this).addClass('selected');
				}
			}
		} else {
			if (_this.checked == true) {
				$(_this.parentNode.parentNode).addClass('selected');
			} else {
				$(_this.parentNode.parentNode).removeClass('selected');
			}

			_this.blur();
		}
	}
}


var sketch = {
	navigate: function(_this, d, navid, id, input) {
		if (_this.className.indexOf('disabled') != -1) return false;

		var $prev = $('#' + navid).find('a[rel=previous]');
		var $next = $('#' + navid).find('a[rel=next]');
		var $previews = $('#' + id).find('span');
		var previews = $previews.get();
		var length = previews.length;
		var cur, next = 0;

		if (!length) return false;

		$previews.each(function(i){
		   if (this.style.display != 'none') {
				cur = i;
				next = i + d;
				return false;
		   }
		});

		if (next <= length -1 && next >= 0) {
			$('#' + navid).find('var.current').text(next + 1);
			$('#' + navid).find('var.total').text(length);

			$prev.removeClass('disabled');
			$next.removeClass('disabled');

			if (next == 0) {
				$prev.addClass('disabled');
			}

			if (next == length - 1) {
				$next.addClass('disabled');
			}

			$previews.css({ display:'none' });
			$(previews[next]).css({ display:'block' });

			$previews.css({ display:'none' });

			$(previews[next]).fadeIn();
		}
	},

	removeUser: function(row, input, val) {
	    var $input  = $(input),
            emails  = $input.val() ? $input.val().replace(' ', '').split(',') : [],
            exist   = -1;

        if (emails.length) {
            exist = $.inArray(val, emails);
            if (exist != -1) emails.splice(exist, 1);
            $input.val(emails.join(','));
            $(row).remove();
        }
	},

	addUser: function(input, val, container, html) {
	    var $input  = $(input),
            emails  = $input.val() ? $input.val().replace(' ', '').split(',') : [],
            values  = val ? val.replace(' ', '').split(',') : [],
            size    = emails.length;

        for (var i = 0, length = values.length; i < length; i++) {
            if ($.inArray(values[i], emails) == -1) {
                emails[emails.length] = values[i];
            }
        }

        if (emails.length > size) {
            $input.val(emails.join(","));
            $(container).append(html);
        }
	}
};


/*----------------------------------------------------------- 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, variable) {
		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("top.page.manipulate") != -1 ? scripts[i].indexOf("top.page.manipulate") : scripts[i].indexOf("page.manipulate");
				if (fs > -1) {
					eval(scripts[i].substring(fs));
					scripts[i] = scripts[i].substring(0, fs);
				}
			}
			catch(e) {
			}
		}

		source = source.replace(/^[\r\n]+/, '');

		if (typeof page.manipulate == 'function') {
			if (variable) {
				page.manipulate(source, variable);
			} else {
				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

	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;
		var tag = $('#' + this.id).get(0).tagName.toLowerCase();

		if (tag == 'tr') {
			$('#' + this.id).before('<tr id="' + this.id + '_preloader" class="preloader"><td><div class="pretimer"></div></td></tr>');
		} else {
			$('#' + this.id).prepend('<div id="' + this.id + '_preloader" 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>"),
		    bodyEnd     = html.toLowerCase().indexOf("</body>"),
		    $self       = $('#' + this.id);

		if (bodyStart > -1 && bodyEnd > -1)	{
			html = html.substring( bodyStart + 6, bodyEnd );
		}

		switch(this.manipulation) {
			case 'append':
				$self.append(html);
				$('#' + this.id + '_preloader').remove();
				break;
			case 'prepend':
				$self.prepend(html);
				$('#' + this.id + '_preloader').remove();
				break;
			case 'replaceWith':
				$self.replaceWith(html);
				break;
			case 0:
			case 'none':
				page.script(html);
				$('#' + this.id + '_preloader').remove()
				break;
			default:
				$self.html(html);
		}
		$self.removeClass('preloader-container');
		$self
		    .find('.autofocus')
		    .eq(0)
		    .focus();
	},

	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) {
					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;
	},

	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('active') != -1) return;

		if (tabs) {
			$(tabs + ' .active').removeClass('active');
			$(_this).addClass('active');
		}

		$(hide).hide();
		if (show) $(show).fadeIn();
	},

	toolbar: function (e, _this) {
		if (!e) var e = window.event;

		var $toolbar = $('ul.toolbar', _this);
		toggle.toolbarTime = toggle.toolbarTime ? toggle.toolbarTime : 0;

		if (e.type == 'mouseover') {
			if ($toolbar.hasClass('active')) {
				clearTimeout(toggle.toolbarTime);
			}
			toggle.toolbarTime = setTimeout(function() {
				$toolbar.stop()
						.addClass('active')
						.css({ visibility:'visible' }).animate({ opacity:1 }, 1000);
			}, 500);

			$(_this).addClass('hover');
		}

		if (e.type == 'mouseout') {
			clearTimeout(toggle.toolbarTime);
			toggle.toolbarTime = setTimeout(function() {
				$toolbar.stop()
						.removeClass('active')
						.animate({ opacity:0 }, 200, function() { $(this).css({ visibility:'hidden' }); });
			}, 50);

			$(_this).removeClass('hover');
		}
	},

	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('active') != -1) {
		$(cur.opener).removeClass('active');
	}

	this.id = id;
	this.opener = _this;
	this.modal = 1;
	this.effect = 'slide';
	this.showtime = 0;
	this.move = 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('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;

		if (self.move) {
			modal.move(self.opener, gebi(self.id), self.move);
		}

		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn();
				break;
			case 'slide':
				$('#' + this.id).slideDown();
				break;
			default:
				$('#' + this.id).css({ display:'block' });

		}

		$(this.opener).addClass('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('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.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.source = null;
	this.data = data ? data : 0;
	this.scroll = 0;
	this.modal = 1;
	this.overlay = 1;
	this.view = 1;
	this.move = 0;
	this.close = 0;
	this.container = 'body';
	this.cache = 0;
	this.autofocus = 1;

	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('active');

		if (this.container == 'body') {
		    if (!this.scroll) window.scroll(0, 0);

			$('body').append('<div id="' + this.id + '" class="modal' + (this.view ? ' modal' + this.view : '')  + '" style="visibility:hidden;"><table class="overlay' + (this.scroll ? ' overlay-scrollable' : '') + '"><tr><td id="' + this.id + '_overlay" class="overlay"><div id="' + this.id + '_window" class="modal-window"><div class="modal-toolbar">'+ (this.close == 1 ? '<a class="modal-close" href="#:close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a>' : '') + '</div><div class="modal-content" id="' + this.id + '_content"><div class="modal-preloader"></div></div></div></td></tr></table>' + (this.overlay ? '<div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div>' : '') + '</div>');
		} else {
			$(this.container).append('<div id="' + this.id + '" class="modal-bind' + (this.view ? ' modal' + this.view : '') + '" style="visibility:hidden;"><div class="modal-spacer"></div><div id="' + this.id + '_window" class="modal-window"><div class="modal-toolbar">'+ (this.close == 1 ? '<a class="modal-close" href="#:close" onclick="modal.remove(\'' + this.id + '\'); return false;"></a>' : '') + '</div><div class="modal-content" id="' + this.id + '_content"><div class="modal-preloader"></div></div></div></div>');
			if (this.overlay) $('body').append('<div id="' + this.id + '_overlay" style="visibility:hidden;"><div class="overlay' + (this.overlay == 1 ? '' : this.overlay) + '"></div></div>');
		}

		var $self = $('#' + this.id);

		$self.css({ visibility:'visible' });
		$('#' + this.id + '_overlay').css({ visibility:'visible' });

        if (!this.modal) {
            self.docevnt = function(e){
                var target = e.srcElement || e.target;
                if (!(self.opener == target || $(self.opener).has(target).length) && ($('#' + self.id + '_window').get(0) != target && !$('#' + self.id + '_window').has(target).length)) {
                        self.remove();
                }
            };
            $(document).bind('click', self.docevnt);
        }

		if (self.move) {
			modal.move(self.opener, $('#' + self.id).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');

					$self
					    .find('.autofocus')
					    .eq(0)
					    .focus();

					if (self.move) {
						modal.move(self.opener, $self.find('div.modal-w').get(0), self.move);
					}
				}
			});
		} else if (this.source && $(this.source).length) {
		    var $source = $(this.source).removeAttr('id');
		    $('#' + self.id + '_content').html($source);
		} else {
			$('#' + self.id + '_content').html(self.html);
			$('#' + self.id + '_overlay').removeClass('overlay-preloader');
		}
	},

	show: function() {
		var self    = this,
		    $self   = $('#' + this.id);

		$(this.opener).addClass('active');

		$self.show();
		$self
		    .find('.autofocus')
		    .eq(0)
		    .focus();

		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('active');
	}
};