/* ******************************************************************************
		Coded by GS on 16.03.2008, True Vision
****************************************************************************** */

function gebi(n) {
	if (!n) return false;
	if (!document.getElementById(n)) return false;
	return document.getElementById(n)
}

function isNumber(e) {
	if ((e.keyCode < 48) || (e.keyCode > 57))
		e.returnValue = false;
}

function captcha(_this) {
	_this.src = _this.src + Math.random();
}

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>');
}

function toggle_news(_this, id) {
	if (_this.className.indexOf('toggle-active') != -1) return;
	
	$('#news-list table.news-menu a').removeClass('toggle-active');
	$('#news-list div.news-group').css({ display:'none' });
	
	$(_this).addClass('toggle-active');
	$('#' + id).css({ display:'block' });
	$('#' + id + ' div.scrollable').scroller();
	
	_this.blur();	
}


toggle = {
	init: function(_this, id, hash) {
		this.parent = _this;
		this.id = id;
		this.front = '';
		this.modal = 0;
		this.effect = 'slide';
		this.resize = 0;
		
		try {
			for (var i in hash) this[i] = hash[i];
		} catch(e) {
			alert(e);
		}
	},
	
	offset: function(overlay) {
		var de = document.documentElement;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
	
		overlay.css({ height:h });
	},
	
	overlay_add: function(hash) {
		var self = this;
		var id = this.id;
		var parent = this.parent;
		var overlay =	$('<div></div>')
							.attr('id', this.id + '_overlay')
							.addClass('toggle-overlay');
		
		$('body').append(overlay);
		
		if (!this.modal) {
			overlay.click(function(){
				if (self.modal) return;
				self.hide(parent, id, hash);
			});
		}
		
		this.offset(overlay);
		
		$(window).resize(function() {
			self.offset(overlay);
		});
		
		if (typeof this.front == 'string') $(this.front).css({ zIndex:50 });
	},
		
	overlay_remove: function() {
		$('#' + this.id + '_overlay').remove();
		if (typeof this.front == 'string') $(this.front).css({ zIndex:40 });
	},
		
	hide: function(_this, id, hash) {
		var self = this;
		
		this.init(_this, id, hash);
		this.modal = 0;
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeOut(
					'slow',
					function(){
						if (self.resize) wrapper.sizes();
					}
				);
				break;
			case 'none':
				$('#' + this.id).css({ display:'none' });
				if (self.resize) wrapper.sizes();
				break;
			default:
				$('#' + this.id).slideUp(
					'slow',
					function(){
						if (self.resize) wrapper.sizes();
					}
				);
		}
		
		$(this.parent).removeClass('toggle-active');
		
		if (this.front) this.overlay_remove();
	},
		
	show: function(_this, id, hash) {
		var self = this;
		
		$('#' + this.id).click(function(e){
			e.stopPropagation();
		});
		
		this.init(_this, id, hash);
		
		switch (this.effect) {
			case 'fade':
				$('#' + this.id).fadeIn(
					'slow',
					function(){
						$('#' + id + ' div.scrollable').scroller();
						if (self.resize) wrapper.sizes();
					}
				);
				break;
			case 'none':
				$('#' + this.id).css({ display:'block' });
				if (self.resize) wrapper.sizes();
				break;
			default:
				$('#' + this.id).slideDown(
					'slow',
					function(){
						$('#' + id + ' div.scrollable').scroller();
						if (self.resize) wrapper.sizes();
					}
				);
		}
		
		$(this.parent).addClass('toggle-active');
			
		if (this.front) this.overlay_add(hash);
	},
	
	execute: function (_this, id, hash) {
		if (!gebi(id)) return false;
		
		if (gebi(id).style.display == 'none') {
			this.show(_this, id, hash);
		} else {
			this.hide(_this, id, hash);
		}
		
		_this.blur();
	}
}


page = {
	offset: function (id) {
		var h = gebi(id).offsetHeight + 'px';
	
		$('#' + id + ' div.preloader div.preoverlay').css({ height:h });
		$('#' + id + ' div.preloader div.pretimer').css({ height:h });
	},
	
	overlay: function(id) {
		var self = this;
		$('#' + id).prepend('<div class="preloader"><div class="preoverlay"></div><div class="pretimer"></div></div>');
		
		this.offset(id);
		
		$(window).resize(function() {
			self.offset(id);
		});
	},
	
	load: function (id, url, _this, data) {
		this.overlay(id);

		if (_this) {
			var options = {
				type: 'post',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
				}
			};
			$(_this).ajaxSubmit(options);
		} else {
			$.ajax({
				type: 'get',
				url: url,
				data: data ? data : false,
				success: function(html) {
					$('#' + id).html(html);
				}
			});
		}
	}
};


modal = {
	add: function(id, url, hash, data) {
		this.scroll = 0;
		this.modal = 0;
		this.iframe = 0;
		this.width = 800;
		this.height = 600;
		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><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();
			});
		}
		
		if (this.iframe) {
			$('#' + id + '_content').html('<iframe src="' + url + '" frameborder="0" style="width:' + this.width + 'px; height:' + this.height + 'px;"></iframe>');
			$('#' + id + '_overlay').removeClass('overlay-preloader');
			$('#' + id + '_container').css({ visibility:'visible' });
		} else {
			$.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();
	}
};

gallery = {
	id: 'gallery',

	init: function() {
		$('a.preview').click(function(){
			var group = this.rel || false;
			gallery.show(this.href, group);
			this.blur();
			return false;
		});
	},

	show: function(url, group) {
		try {
			var type = /\.jpg|\.jpeg|\.png|\.gif/g;
			type = url.toLowerCase().match(type);
					
			if (type == '.jpg' || type == '.jpeg' || type == '.png' || type == '.gif') {
				var self = this;
				
				this.navigator(url, group);	
				
				preloader = new Image();
				preloader.onload = function(){
					preloader.onload = null;
					
					var w = preloader.width;
					var h = preloader.height;
					
					$('body').append('<div id="' + self.id + '" class="modal"><table class="overlay"><tr><td id="' + self.id + '_overlay" class="overlay"><table id="' + self.id + '_container" class="modal modal-gallery"><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="gallery.remove(\'' + self.id +'\'); this.blur; return false;"></a></div><div id="' + self.id + '_content" class="modal-content modal-preloader"><img id="' + self.id +'_image" class="modal-image" style="visibility:hidden;" src="' + url + '" alt="" width="60px" height="60px" />' + ( group ? '<div id="' + self.id + '_navigator"></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><div class="overlay"></div><iframe class="overlay"></iframe></div>');
					
					var $navigator = $('#' + self.id + '_navigator');
					var $img = $('#' + self.id + '_image');
					
					if (self.next) $navigator.append(self.next);
					if (self.prev) $navigator.append(self.prev);
					
					self.animate($img, w, h);

					$('#' + self.id + '_container').click(function(e){
						e.stopPropagation();
					});					

					$('#' + self.id + '_overlay').click(function(){
						self.remove(self.id);
					});					
				}
				preloader.src = url;
			}
		} catch(e) {
			alert( e );
		}
	},
	
	animate: function($img, w, h) {
		var self = this;
		$img.animate(
			{ width:w + 'px', height:h + 'px' },
			1000,
			'',
			function() {
				$img.css({ visibility:'visible' });
				$('#' + self.id + ' a.modal-next').css({ visibility:'visible' });
				$('#' + self.id + ' a.modal-prev').css({ visibility:'visible' });
			}
		);
		
	},
	
	navigator: function(url, group) {
		this.prev = null;
		this.next = null;
	
		if (group) {
			var self = this;
			var flag = 0;
			var tmp = $('a[rel=' + group + ']').get();
			
			for (i = 0; ((i < tmp.length) && (this.next == null)); i++) {
				if (!(tmp[i].href == url)) {
					if (flag == 1) {
						this.next	=	$('<a></a>')
											.addClass('modal-next')
											.attr('href', tmp[i].href)
											.click(
												function() {
													self.navigate(this.href, group);
													this.blur();
													return false;
												}
											);
					} else {
						this.prev	=	$('<a></a>')
											.addClass('modal-prev')
											.attr('href', tmp[i].href)
											.click(
												function() {
													self.navigate(this.href, group);
													this.blur();
													return false;
												}
											);
					}
				} else {
					flag = 1;
				}
			}
		}
	},
	
	navigate: function(url, group) {
		var type = /\.jpg|\.jpeg|\.png|\.gif/g;
		type = url.toLowerCase().match(type);
				
		if (type == '.jpg' || type == '.jpeg' || type == '.png' || type == '.gif') {
			var self = this;
			var $img = $('#' + this.id + '_image');
			
			$('#' + this.id + ' a.modal-next').remove();
			$('#' + this.id + ' a.modal-prev').remove();
			
			$img.css({ visibility:'hidden' });
			
			this.navigator(url, group);	
		
			preloader = new Image();
			preloader.onload = function(){
				preloader.onload = null;
				
				var $navigator = $('#' + self.id + '_navigator');
				var w = preloader.width;
				var h = preloader.height;
				
				$img.attr('src', url);
				
				if (self.next) $navigator.append(self.next);
				if (self.prev) $navigator.append(self.prev);
				
				self.animate($img, w, h);
			}
			preloader.src = url;
		}
	},
	
	remove: function(id) {
		$('#' + id + ' div.modal-close').remove();
		$('#' + id + '_navigator').remove();
		$('#' + id +'_image')
			.css({ visibility:'hidden' })
			.animate(
				{ width:0, height:0 },
				1000,
				'',
				function() {
					$('#' + id).remove();
				}
			);
	}
};

wrapper = {
	page: 2,
		
	pages: 1,
		
	screen: 1000,
	
	offset: {},
	
	init: function(navigation){
		if (navigation) {
			this.navigation = navigation;
			this.pages = this.navigation.pages.length;
		}
		
		if (typeof this.navigation != 'object') return;
		
		this.current();
		this.sizes(1);
		this.buttons(1);

		$('#container').css({ visibility:'visible' });
		$('#next').css({ visibility:'visible' });
		$('#prev').css({ visibility:'visible' });
	},
		
	resize: function(c, h){
		c.css({ height:h + 'px' });
		/*c.animate(
			{ height:h + 'px' },
			1500
		);*/
	},
		
	sizes: function(init){
		var self = this;
		var de = document.documentElement;
		var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
		var $container = $('#container');
		var $items = $('#container div.container');
		var count = 1;
		
		this.screen = w < 900 ? 900 : w;

		$items.removeClass('container-current');
		$items.each(function(){
				self.offset[count] = this.offsetHeight;
				if (self.page == count) $(this.parentNode).addClass('container-current');
				count++;
			})
			.css({ width:this.screen + 'px', minHeight:h + 'px' });
			
		var offset = this.offset[this.page];
				
		if (typeof document.body.style.maxHeight == "undefined") {
			$container.css({ width:w + 'px' });
			$items.css({ width:this.screen + 'px', height:h + 'px' })
		}
		
		if (!init) {
			this.resize($container, (offset < h ? h : offset));
		} else {
			$container.css({ height:(offset < h ? h : offset) + 'px' });
		}
		
		if (this.page != 1 && init) {
			var scroll = (this.page - 1) * this.screen;
			$container.scrollLeft(scroll);
		}
	},
		
	buttons: function(init){
		var $next = $('#next');
		var $prev = $('#prev');
		var pages = this.navigation.pages;
		
		if (this.page > 1) {
			var prev = this.navigation[pages[this.page - 2]];
			$('#prev a.toggle').html(prev.title);
		}
		if (this.page < this.pages) {
			var next = this.navigation[pages[this.page]];
			$('#next a.toggle').html(next.title);
		}
		
		if (this.page == this.pages) {
			if (init) {
				$next.css({ top:'-334px' });
			} else {
				$next.stop();
				$next.animate({ top:'-334px' });
			}
		} else {
			if (init) {
				$next.css({ top:'334px' });
			} else {
				$next.stop();
				$next.animate({ top:'334px' });
			}
		}
		
		if (this.page == 1) {
			if (init) {
				$prev.css({ top:'-334px' });
			} else {
				$prev.stop();
				$prev.animate({ top:'-334px' });
			}
		} else {
			if (init) {
				$prev.css({ top:'334px' });
			} else {
				$prev.stop();
				$prev.animate({ top:'334px' });
			}
		}
	},
		
	current: function(){
		var url = window.location.href;
		
		if (url.indexOf('#') != -1) {
			url = url.split('#');
			
			if (url[1].indexOf('/') != -1) {
				url = url[1].split('/');
				
				for (i = 0; i < this.pages; i++) {
					if (this.navigation.pages[i] == url[1]) {
						this.page = i + 1;
						break;
					}
				}
			}
		}
	},
		
	url: function(){
		var url = window.location.href;
		var pages = this.navigation.pages;
		
		if (url.indexOf('#') != -1) {
			url = url.split('#');
			url = url[0];
		}
		window.location.href = url + '#/' + pages[this.page - 1];
	},
		
	scroll: function(_this, d) {
		if ((d < 0 && this.page == 1) || (d > 0 && this.page == this.pages) || typeof this.navigation != 'object') return;
		
		var self = this;
		
		if (d) {
			this.page = this.page + d;
		} else {
			this.page = 2;
		}
		
		var scroll = (this.page - 1) * this.screen;
		var $container = $('#container');
		var h = this.offset[this.page];
		
		this.url();
		this.buttons();		

		$container.stop()
			.animate(
				{ scrollLeft: scroll },
				1500,
				'',
				function(){ self.resize($container, h);	}
			);
	
		_this.blur();
	}
}

$(function(){
	gallery.init();
});

$(window).resize(function(){
	wrapper.init();
});

