(function($){
	EYE.history = function () {
		var observer = function (hash) {
				var changed = true;
				if (currentURL != hash) {
					currentURL = hash;
				} else {
					changed = false;
				}
				if (hash == '') {
					if (document.getElementById('homepage')) {
						EYE.loadMovie();
					} else {
						$('#intro, #buffer').hide();
					}
				} else {
					if (changed) {
						$('#right').stop(true, true).fadeOut(300);
					}
					$('#ajaxLoader').stop().animate({opacity: 0.8}, 300, function () {
						$.ajax({
							data: $.extend(post, {url: hash}),
							dataType: 'xml',
							error: function () {
								alert('Ajax error');
							},
							complete: function () {
								$('#ajaxLoader').stop().animate({opacity: 0}, 300);
							},
							success: function (response) {
								$('#buffer').hide();
								$('#right').html($('content', response).text()).fadeIn(500);
								Cufon.replace('#right h1');
								Cufon.replace('#right h2, #right h3',{fontFamily: 'helv2'});
								$('body').get(0).id = $('id', response).text();
								document.title = $('title', response).text();
								menu.activate($('id', response).text(), $('current', response).text());
								EYE.forms.init();
								EYE.clientWork.init();
								EYE.carousel.init();
								EYE.carouselVertical.init();
								EYE.scroller.init();
								$('#right').bind('click', click);
								$('#right a.openImage').fancybox();
							},
							type: 'POST',
							url: '/index.php'
						});
					});
				}
			},
			change = function(id) {
				$('body').get(0).id = id;
				if (document.getElementById('homepage')) {
					EYE.loadMovie();
				} else {
					$('#intro').hide();
				}
			},
			go = function (data) {
				post = {};
				if (data) {
					$.each(data, function (nr, val) {
						post[val.name] = val.value;
					});
				}
				var hash = this.href;
				hash = hash.replace(/[^\.]+\.[^\/]+/, '');
				$.historyLoad(hash);
				return false;
			},
			click = function (ev) {
				var targetEl = $(ev.target).closest('a');
				if (targetEl.size() == 1) {
					var hash = targetEl.get(0).href;
					hash = hash.replace(/[^\.]+\.[^\/]+\//, '');
					if (hash != '' && hash != '#' &&
					hash.indexOf('images/') == -1 &&
					hash.indexOf('files/') == -1) {
						go.apply(targetEl.get(0));
						return false;
					}
				}
			},
			menu = {
				collpase: function () {
					$('#mainMenu')
						.find('ul:visible').stop(true, true).slideUp(300);
				},
				expand: function (nr) {
					var targetEL = $('#mainMenu>li').eq(nr).find('ul:hidden');
					$('#buffer').hide();
					if (targetEL.size() > 0) {
						menu.collpase();
						targetEL.stop(true, true).slideDown(300);
					}
				},
				activate: function (main, sub) {
					var mainItem = $('#item'+main);
					if (!mainItem.is('.current')) {
						if (!mainItem.find('ul').is(':visible')) {
							menu.collpase();
							menu.expand($('#mainMenu>li').index(mainItem.get(0)));
						}
						$('#mainMenu>li.current').removeClass('current');
						mainItem.addClass('current');
					}
					$('#mainMenu li li.current').removeClass('current');
					if (sub) {
						$('#item'+sub).addClass('current');
					}
				},
				click: function (ev) {
					var targetEl = $(ev.target).closest('a');
					if (targetEl.size() == 1) {
						targetEl.blur();
						if (targetEl.is('.mainItem')) {
							if (targetEl.parent().find('ul').size() == 0 && !targetEl.parent().is('.current')) {
								go.apply(targetEl.get(0));
							}
							else {
								menu.expand(targetEl.parent().parent().find('>li').index(targetEl.parent().get(0)));
							}
						}
						else 
							if (!targetEl.parent().is('.current')) {
								go.apply(targetEl.get(0));
						}
						return false;
					}
				}
			},
			currentURL = '',
			post = {};
		return {
			init: function() {
				$.historyInit(observer);
				$('#mainMenu ').bind('click', menu.click);
				$('#logo').bind('click', go);
				$('#right, #sitemap').bind('click', click);
				Cufon.replace('#right h1');
				Cufon.replace('#right h2, #right h3',{fontFamily: 'helv2'});
				$('#right a.openImage').fancybox();
			},
			submit: function (data) {
				go.call({href: EYE.domain+currentURL}, data);
			}
		};
	}();
	EYE.register(EYE.history.init, 'init');
	
	EYE.playing = false;
	EYE.isMovie = false;
	
	EYE.movieDone = function() {
		$('#intro, #controlls, #buffer').hide();
		$('#intro').empty();
		$('#top, #bottom, #backImage').show();
		EYE.playing  = false;
		EYE.isMovie = false;
		EYE.overlay.setBackground();
		EYE.overlay.resize();
		return false;
	};
	
	EYE.loadMovie = function (movie) {
		$('#top, #bottom, #backImage').hide();
		$('#buffer').show();
		$('#intro').show().flash({
			src: '/video.swf',
			width: '100%',
			height: '100%',
			wmode: 'transparent',
			id: 'introMovie',
			name: 'introMovie',
			allowScriptAccess: 'sameDomain',
			flashvars: {
				movieFile: movie || $('#branding a:last').attr('href')
			}
		});
		EYE.isMovie = true;
	};
	
	EYE.bufferEmpty = function() {
		if (EYE.playing == true && EYE.isMovie) {
			$('#buffer').show();
		}
	};
	EYE.bufferFull = function() {
		$('#buffer').hide();
	};
	
	EYE.movieStart = function() {
		$('#controlls').show();
		$('#buffer').hide();
		EYE.playing = true;
		EYE.overlay.setBackground();
		EYE.overlay.resize();
	};
	
	EYE.moviePause = function() {
		EYE.playing = !EYE.playing;
		if (EYE.playing) {
			document.getElementById('introMovie').playMovie();
			document.getElementById('controlls').style.backgroundPosition = "left top";
		} else {
			document.getElementById('introMovie').pauseMovie()
			document.getElementById('controlls').style.backgroundPosition = "left bottom";
		}
		return false;
	};
	
	EYE.swicthMovie = function (ev) {
		var targetEl = $(ev.target);
		if (targetEl.is('a')) {
			if (EYE.playing) {
				EYE.movieDone();
			}
			EYE.loadMovie(targetEl.attr('href'));
			return false;
		}
	};
	
	EYE.register(function(){
		$('#controllPause').bind('click', EYE.moviePause);
		$('#controllSkip').bind('click', EYE.movieDone);
		$('#branding').bind('click', EYE.swicthMovie);
		$('#buffer')
			.css('opacity', 0.7)
			.find('a').bind('click', EYE.movieDone);
	}, 'init');
	
	EYE.overlay = function() {
		var el, el2, el3, el4, backgroundImage, img;
		var setImage = function(width, height) {
			ratio = img.width / img.height;
			if (ratio) {
				if (!height) {
					width = Math.max(980, $(window).width());
					height = Math.max(630, $(window).height());
				}
				var imageWidth = width;
				var imageHeight = width / ratio;
				if (imageHeight < height) {
					imageHeight = height;
					imageWidth = height * ratio;
				}
				$('#backImage').css({
					width: imageWidth,
					height: imageHeight
				});
			}
		}
		return {
			init: function () {
				el = document.getElementById('overlay');
				el2 = document.getElementById('top');
				el3 = document.getElementById('controlls');
				el4 = document.getElementById('intro');
				backgroundImage = $(el).css('backgroundImage');
				img  = new Image();
				img.src = '/images/media/background.jpg';
				if (img.complete) {
					setImage();
				} else {
					img.onload = setImage;
				}
				$(window).bind('resize', EYE.overlay.resize);
				EYE.overlay.resize();
			},
			setBackground: function () {
				$(el).css('backgroundImage', !EYE.playing ? backgroundImage : 'none');
			},
			resize: function() {
				var width = Math.max(980, $(window).width()),
					height = Math.max(630, $(window).height());
				el.style.height = el2.style.height = el4.style.height = height + 'px';
				el.style.width = el2.style.width = el4.style.width = width + 'px';
				el3.style.top = height - 110 + 'px';
				if (img.complete) {
					setImage(width, height);
				}
			}
		};
	}();
	EYE.register(EYE.overlay.init, 'init');
	
	EYE.scroller = function () {
		var contentEl, wrapperEl, scrollEl, indicEl, mouseY, indicY, maxY, diff,
			mouseDown = function (ev) {
				mouseY = ev.pageY;
				indicY = indicEl.offsetTop;
				maxY = scrollEl.offsetHeight - indicEl.offsetHeight;
				$(document).bind('mousemove', mouseMove).bind('mouseup', mouseUp);
				return false;
			},
			mouseMove = function (ev) {
				var newTop = Math.min(Math.max(0, indicY + ev.pageY - mouseY), maxY);
				indicEl.style.top = newTop + 'px';
				contentEl.style.top = (diff * newTop / maxY) + 'px';
				return false;
			},
			mouseUp = function () {
				$(document).unbind('mousemove', mouseMove).unbind('mouseup', mouseUp);
				return false;
			},
			wheel = function (ev, dir) {
				maxY = scrollEl.offsetHeight - indicEl.offsetHeight;
				var newTop = Math.min(Math.max(0, indicEl.offsetTop - 50 * dir), maxY);
				indicEl.style.top = newTop + 'px';
				contentEl.style.top = (diff * newTop / maxY) + 'px';
				return false;
			};
		return {
			init: function() {
				scrollEl = document.getElementById('scroller');
				indicEl = scrollEl.getElementsByTagName('A')[0];
				if ((wrapperEl = document.getElementById('contentWrapper'))) {
					contentEl = document.getElementById('contentInner');
					if (wrapperEl.offsetHeight < contentEl.offsetHeight) {
						diff = wrapperEl.offsetHeight - contentEl.offsetHeight;
						indicEl.style.top = '0px';
						indicEl.style.display = 'block';
						var st = scrollEl.style;
						st.top = wrapperEl.offsetTop+'px';
						st.display = 'block';
						st.height = wrapperEl.offsetHeight + 'px';
						$(indicEl).bind('mousedown', mouseDown);
						$(wrapperEl).bind('mousewheel',wheel);
						return;
					}
				}
				indicEl.style.display = 'none';
			}
		};
	}();
	EYE.register(EYE.scroller.init, 'init');
	
	EYE.carousel = function () {
		var el, wrapper, maxLeft, currentLeft = 0, lastEl,
			scroll = function () {
				currentLeft = Math.max(maxLeft, Math.min(0, currentLeft - (this.id == 'carouselLeft' ? -1 : 1) * 448 ));
				$(el)
					.stop()
					.animate({
						left: currentLeft
					}, 800);
				return false;
			},
			mouseEnter = function () {
				$('#tooltip')
					.html($(this).html())
					.css('left', this.offsetLeft + el.offsetLeft - 10)
					.show();
			},
			mouseLeave = function () {
				$('#tooltip').hide();
			};
		return {
			init: function() {
				if ((wrapper = document.getElementById('carouselWrapper'))) {
					el = wrapper.getElementsByTagName('UL')[0];
					maxLeft = Math.min(0, 448 - $(el).css('width', $('a', el).bind('mouseenter', mouseEnter).bind('mouseleave', mouseLeave).size() * 64).get(0).offsetWidth);
					currentLeft = Math.max(Math.min(0, -($('a', el).index($('a.current', el)) - 3) * 64), maxLeft);
					$(el).css({
							left: currentLeft
						});
					if (maxLeft == 0 ){
						$('#carouselLeft, #carouselRight').hide();
					} else {
						$('#carouselLeft, #carouselRight').bind('click', scroll);
					}
				}
			}
		};
	}();
	EYE.register(EYE.carousel.init, 'init');
	
	EYE.carouselVertical = function() {
		var el, wrapper, maxTop = 0, currentTop = 0, lastEl,
			scroll = function () {
				currentTop = Math.max(maxTop, Math.min(0, currentTop - (this.id == 'carouselUp' ? -1 : 1) * 410 ));
				$(el)
					.stop()
					.animate({
						top: currentTop
					}, 800);
				return false;
			};
		return {
			init: function() {
				if ((wrapper = document.getElementById('newsGallery'))) {
					el = wrapper.getElementsByTagName('UL')[0];
					var height = 0;
					$('li', el).each(function(){
						height += this.offsetHeight + 20;
					});
					maxTop = Math.min(0,410 - height);
					el.style.height = height + 'px';
					if (maxTop == 0) {
						$('#carouselUp, #carouselDown').hide();
					} else {
						$('#carouselUp, #carouselDown').bind('click', scroll);
					}
					$("li a", wrapper).fancybox();
				}
			}
		};
	}();
	EYE.register(EYE.carouselVertical.init, 'init');
	
	EYE.forms = function() {
		var focus = function() {
				if (this.value == $(this).attr('accesskey')) {
					this.value = '';
				}
			},
			blur = function () {
				if (this.value == '') {
					this.value = $(this).attr('accesskey');
				}
			},
			submit = function () {
				$('input:text, textarea', this).each(function(){
					if (this.value == $(this).attr('accesskey')) {
						this.value = '';
					}
				});
				EYE.history.submit($(this).serializeArray());
				return false;
			};
		return {
			init: function () {
				$('form.form')
					.bind('submit', submit)
					.find('input:text, textarea')
						.bind('focus', focus)
						.bind('blur', blur)
						.each(function(){
							if (this.value == '') {
								this.value = $(this).attr('accesskey');
							}
						});
			}
		};
	}();
	EYE.register(EYE.forms.init, 'init');
	
	EYE.clientWork = function() {
		var current = 0, maxItems,
			goTo = function(ev) {
				if (ev) {
					var targetEl = $(ev.target).closest('a');
					if (targetEl.size() == 1) {
						targetEl.blur();
						if (targetEl.get(0).id == 'clientWorkPrev') {
							current --;
						} else if (targetEl.get(0).id == 'clientWorkNext') {
							current ++;
						} else {
							current = $('#clientWorkControls div a').index(targetEl);
						}
						if (current<0) {
							current = maxItems -1 ;
						} else if (current >= maxItems) {
							current = 0;
						}
					}
				}
				$('#clientWorkContentWrapper')
					.stop()
					.animate({
						top: -$('#clientWorkContentWrapper p').eq(current).get(0).offsetTop
					}, 500);
				$('#clientWorkControls div a')
					.removeClass('current')
					.eq(current).addClass('current');
				return false;
			};
		return {
			init: function () {
				if (document.getElementById('clientWork')) {
					current = 0;
					var html = [];
					$('#clientWorkContent p').each(function(nr){
						html.push('<a href="#">');
						html.push(nr+1);
						html.push('</a>');
					}).find('a').fancybox();
					maxItems = $('#clientWorkControls').bind('click', goTo)
								.find('div').html(html.join(''))
									.find('a').size();
					goTo();
				}
			}
		};
	}();
	EYE.register(EYE.clientWork.init, 'init');
	
	
	EYE.localOverlay = function (){
		var inited = false;
		return {
			init: function (){
				if (inited == false) {
					inited = true;
					$('#localOverlay').css('opacity', 0.8);
				}
			},
			show: function (el) {
				var offset = $(el).offset();
				$('#localOverlay').css({
					top: offset.top,
					left: offset.left,
					width: el.offsetWidth,
					height: el.offsetHeight,
					display: 'block'
				});
			},
			hide: function (el) {
				$('#localOverlay').hide();
			}
		};
	}();
	EYE.register(EYE.localOverlay.init, 'init');
	
	EYE.newsletter = function() {
		var inited = false,
			initValue,
			focus = function () {
				if (this.value == initValue) {
					this.value = '';
				}
			},
			blur = function () {
				if (this.value == '') {
					this.value = initValue;
				}
			},
			hide = function () {
				$('#newsletterTooltip').hide(400);
			},
			subscribe = function () {
				var email = $('#newsletterEmail');
				this.blur();
				if (email.val() == '' || email.val() == initValue) {
					email.focus();
				} else {
					EYE.localOverlay.show($('#newsletter').get(0));
					$.ajax({
						type: 'POST',
						data: $.param({
							plugin: 'subscribe',
							email: email.val()
						}),
						complete: function(){
							EYE.localOverlay.hide();
						},
						success: function(xmlResponse){
							var responseErros = $('errorResponse', xmlResponse);
							if (responseErros.size() > 0) {
								var offset = email.offset();
								var tooltip = $('#newsletterTooltip').show();
								$('#newsletterEmail').addClass('error');
							} else {
								$('#newsletterEmail').removeClass('error');
							}
						},
						url: '/ajaxserver.php'
					});
				}
				return false;
			};
		return {
			init: function () {
				if (inited === false) {
					inited = true;
					initValue = $('#newsletterEmail')
						.bind('focus', focus)
						.bind('blur', blur)
						.val();
					$('#newsletterSubmit').bind('click', subscribe);
				}
			}
		};
	}();
	EYE.register(EYE.newsletter.init, 'init');
})(jQuery);