(function($){
	EYE.register(
		function() {
			$('#copyright').css('fontVariant', 'small-caps');
			Cufon.replace('#sideNews dt,ul.mainMenu a.mainItem, #topMenu a', {hover: true});
			Cufon.replace('#contentRight h1, #history td.historyDate, #contentLeftWrapper h1, #contentLeftWrapper p.contactPerson, #partnersInfo h3, #copyright p', {
				fontFamily: 'fontin'
			});
			Cufon.replace('#contentRight h2, #contentWrapper p.info,#newsList dt, #contentWrapper p.spotLink', {
				fontFamily: 'fontin'
			});
			$('#contentWrapper>div').jScrollPane({
				scrollbarWidth: 11,
				showArrows: true,
				arrowSize: 10
			});
		},
		'init'
	);
	EYE.submenu = function() {
		var contentHeight,
			height = 72,
			top = 0,
			maxTop,
			indicMax = 40,
			indicOffset = 18;
		var initScroller = function(left) {
			maxTop = contentHeight - height;
			top = Math.max(
					Math.min(
						$('#submenuContent li.active').get(0).offsetTop,
						maxTop
					),
					0
				);
			$('#submenuScroll')
				.css({
					display: 'block',
					left: left - 10 + 'px'
				})
				.find('a')
					.bind('click', doScroll);
			$('#submenuScrollIndic').css('top', indicOffset + parseInt(top * indicMax / (contentHeight-height)));
			$('#submenuContent ul').css('top', -top);
			$('#submenuContent').bind('mousewheel',wheel);
		};
		var wheel = function(ev, dir) {
			if (dir == 1) {
				$('#submenuScrollUp').trigger('click');
			} else {
				$('#submenuScrollDown').trigger('click');
			}
			return false;
		};
		var doScroll = function(ev) {
			top = Math.max(
					Math.min(
						top + height * (this.id == 'submenuScrollUp' ? -1 : 1),
						maxTop
					),
					0
				);
			$('#submenuContent ul').stop().animate({top: -top}, 300);
			$('#submenuScrollIndic').stop().animate({top: indicOffset + parseInt(top * indicMax / (contentHeight-height))}, 300);
			this.blur();
			return false;
		};
		return {
			init: function() {
				var main = $('div.mainMenu');
				if (main.is('.hasSubmenu')) {
					var current = main.find('>ul>li.active a.mainItem');
					var left = main.find('ul.mainMenu').get(0).offsetLeft;
					current.parent().prevAll().each(function() {
						left += this.offsetWidth;
					});
					$('#submenu')
						.show()
						.css(
							'backgroundPosition',
							((current.get(0).offsetWidth/2) - 700 + left) + 'px top'
						);
					contentHeight = $('#submenuContent')
										.css(
											'left',
											left + 20
										)
										.find('ul').get(0).offsetHeight;
					if (contentHeight > height) {
						initScroller(left+15);
					}
				}
			}
		};
	}();
	EYE.register(EYE.submenu.init, 'init');
	
	EYE.photos = function () {
		var images, width = 229, maxLeft, left = 0;
		var scroll = function() {
			left = Math.max (
				maxLeft,
				Math.min (
					0, 
					left - width * (this.id == 'contentPhotosLeft' ? -1 : 1)
				)
			);
			$(images).stop().animate({left: left}, 500);
			this.blur();
			return false;
		};
		var showImage = function() {
			$('#contentLeftWrapper, #contentLeftVideo').hide();
			var dim = $(this).attr('rel').split(',');
			$('#contentLeftPhoto')
				.empty()
				.show().html('<img src="'+this.href+'" />')
				.find('img')
					.css({
						left: (274 - parseInt(dim[0]))/2,
						top: (434 - parseInt(dim[1]))/2 
					});
			this.blur();
			return false;
		};
		return {
			init: function() {
				if (document.getElementById('contentPhotos')) {
					images = document.getElementById('contentPhotosContainer');
					var imagesWidth = 0;
					$('a', images).each(function(){
						imagesWidth += this.offsetWidth;
					});
					maxLeft = width - imagesWidth;
					if (maxLeft < 0) {
						$('#contentPhotosLeft, #contentPhotosRight')
							.css('display', 'block')
							.bind('click', scroll);
					}
					if ($(images).is('.print')) {
						$('a', images).fancybox({padding: 0});
					} else {
						$('a', images).bind('click', showImage);
					}
				}
			}
		};
	}();
	EYE.register(EYE.photos.init, 'init');
	
	EYE.history = function () {
		var images, width = 269, maxLeft, left = 0;
		var scroll = function() {
			left = Math.max (
				maxLeft,
				Math.min (
					0, 
					left - width * (this.id == 'historyLeft' ? -1 : 1)
				)
			);
			$(images).stop().animate({left: left}, 500);
			this.blur();
			return false;
		};
		return {
			init: function() {
				if (document.getElementById('history')) {
					images = document.getElementById('historyContainer');
					maxLeft = width - images.offsetWidth;
					if (maxLeft < 0) {
						$('#historyLeft, #historyRight')
							.css('display', 'block')
							.bind('click', scroll);
					}
				}
			}
		};
	}();
	EYE.register(EYE.history.init, 'init');
	
	EYE.video = function() {
		var loadVideo = function() {
			$('#contentLeftWrapper, #contentLeftPhoto').hide();
			$('#contentLeftVideo').html($(this).closest('div.spot').find('div.spotContent').html()).show();
			Cufon.replace('#contentLeftVideo h3');
		};
		return {
			init: function() {
				$('#contentWrapper p.spotLink a').bind('click', loadVideo);	
			}
		};
	}();
	EYE.register(EYE.video.init, 'init');
	
	EYE.imageBackground = function() {
		var loaded = false, el, ratio;
		var load = function() {
			loaded = true;
			ratio = el.width/el.height;
			$(window).bind('resize', resize);
			resize();
		};
		var resize = function () {
			if (loaded) {
				var width = $(document).width();
				var height = Math.round(width / ratio);
				var docHeight = $(document).height();
				if (height < docHeight) {
					height = docHeight;
					width = height * ratio;
				}
				el.width = width;
				el.height = height;
				el.style.width = width + 'px';
				el.style.height = height + 'px';
				el.style.top = -Math.round((height - $(window).height()) / 2) + 'px';
				el.style.left = -Math.round((width - $(window).width()) / 2) + 'px';
			}
		};
		return {
			init: function() {
				el = document.getElementById('imageBackground').getElementsByTagName('IMG')[0];
				if (el.complete) {
					load();
				} else {
					$(el).bind('load', load);
				}
			}
		};
	}();
	EYE.register(EYE.imageBackground.init, 'init');
	
	EYE.sideScroll = function(){
		var height = 374, cont, maxTop = 0, top = 0, padding = 0;
		var scroll = function() {
			top = Math.max (
				maxTop,
				Math.min (
					0, 
					top - height * (this.id == 'scrollSideUp' ? -1 : 1)
				)
			);
			$(cont).stop().animate({top: top}, 500);
			this.blur();
			return false;
		};
		var wheel = function(ev, dir) {
			if (dir == 1) {
				$('#scrollSideUp').trigger('click');
			} else {
				$('#scrollSideDown').trigger('click');
			}
			return false;
		};
		return {
			init: function() {
				if (document.getElementById('contentLeftWrapper')) {
					cont = document.getElementById('contentLeftEd');
					if (!cont) {
						cont = document.getElementById('newsList');
						padding = 0;
					}
					if (cont) {
						maxTop = height - cont.offsetHeight - padding;
						if (maxTop < 0) {
							$('#scrollSideUp, #scrollSideDown').css('display', 'block').bind('click', scroll);
							$('#contentLeft').bind('mousewheel',wheel);
						}
					}
				}
			}
		};
	}();
	EYE.register(EYE.sideScroll.init, 'init');
	
	EYE.partners = function() {
		return {
			init: function() {
				if (document.getElementById('partners')) {
					$('#map').bind('click', function(ev) {
						var targetEl = $(ev.target).closest('a');
						if (targetEl.size() == 1) {
							$('#partnersInfo>div').hide();
							$('#'+targetEl.attr('rel')).show();
						}
						return false;
					});
				}
			}
		};
	}();
	EYE.register(EYE.partners.init, 'init');
})(jQuery);