(function($){

	$(document).ready(function() {
		
		var positionElements = function() {
			$('#logo').css($(window).width() < 1000 ? {'right' : 'auto', 'left' : (1000-260-$(this).scrollLeft()) + 'px'} : {'right' : '10px', 'left' : 'auto'});
			$('#mainMenu, #language').css('left', '-' + $(this).scrollLeft() + 'px');
		};
		$(window).bind('load resize scroll', positionElements);	
		
		/* nice hover animation */
		$('#language a').not('.active')
			.mouseover(function(){
				$(this).stop(true).animate({ backgroundColor : '#000000', color : '#fff' }, 500);
			})
			.mouseout(function(){
				$(this).stop(true).animate({ backgroundColor : '#eee809', color : '#000' }, 1000);
			});
			
		/* threemenu */
		var menu = $('#mainMenu'),
			toggleState = null;
		
		/* toggle opened */
		$('a.toggle', menu).click(function(e){
			var opened = $('a.open:visible', menu);
			if (opened.length > 0) {
				toggleState = $(opened.get(0));
				toggleState.trigger('click');
			} else {
				$(toggleState).trigger('click');
			}
			e.preventDefault();
		});
		
		$('a.open, a.closed', menu).click(function(e){
			if ($(this).hasClass('open')) {
				$(this).nextAll('ul').slideUp();
				$(this).removeClass('open');
				$(this).addClass('closed');
			} else {
				
				var parent = $($(this).parents('ul').get(0));
				parent.children().children('a.open').trigger('click');
				
				$(this).nextAll('ul').slideDown();
				$(this).removeClass('closed');
				$(this).addClass('open');
			}
			e.preventDefault();
		});
					
	});
	
})(jQuery); 

/* gallery plugin */
(function($){
	$(document).ready(function() {
		
		/* grid */
		var items = $('a[rel=gallery]'),
			overlay = null,
			wrap = null,
			maxHeight = 0,
			current = null,
			prev = null,
			next = null,
			timeout = null,
			lastKey = null,
			prevNextViewable = 100;
		
		var createImage = function () {
		
		};
		
		var viewport = function() {
			
			// the horror case
			if ($.browser.msie) {
				
				// if there are no scrollbars then use window.height
				var d = $(document).height(), w = $(window).height();
				
				return [
					window.innerWidth || 								// ie7+
					document.documentElement.clientWidth || 	// ie6  
					document.body.clientWidth, 						// ie6 quirks mode
					d - w < 20 ? w : d
				];
			} 
			
			// other well behaving browsers
			return [$(window).width(), $(document).height()];
		
		} 
		
		var resizeOverlay = function(){
			var size = viewport();
			overlay.css({
				'width' : size[0] + 'px',
				'height' : size[1] + 'px'
			});
		};
		var showOverlay = function (){
			if (overlay.is(':visible')) {
				return;
			}
			if($(document).scrollTop() > 0){ 
				$('html').animate({'scrollTop' : '0'});
			}
			$('html,body').css({'width' : '100%', 'overflow-x' : 'hidden'});
			var size = viewport();
			overlay.css({
				width :  '0px',
				height :  '0px',
				opacity : '0',
				display : 'block'
			}).stop(true).animate({
				'opacity' : '0.8',
				'width' : size[0] + 'px',
				'height' : size[1] + 'px'
			}, 300);
			
			$(window).bind('resize', resizeOverlay);
		};
		
		var hideOverlay = function () {
			$(window).unbind('resize', resizeOverlay);
			overlay.stop(true).animate({
				'opacity' : '0',
				'width' : '0px',
				'height' : '0px'	
			}, 300, function(){
				$(this).hide();
			});
		};
		
		var hideGallery = function () {
			
			current.fadeOut('fast');			
			if (prev) {	
				prev.animate({
					'left' : (-1 * (prev.data('dimensions').w / 2)) + 'px'
				}, function(){ $(this).hide(); });	
			}
			
			if (next) {	
				next.animate({
					'right' : (-1 * next.data('dimensions').w / 2) + 'px'
				}, function(){ $(this).hide(); });
			} 
			hideOverlay();
			current = null;
			prev = null;
			next = null;
			lastKey = null;
			timeout = setTimeout(function(){
				$('html,body').css({'width' : 'auto', 'overflow-x' : 'auto'});
			},1000);
		};
	
		var showImage = function(itemKey) {
			clearTimeout(timeout);
			showOverlay();
			var totalItems = items.length - 1;
			
			/* first load */
			if (current == null) {
				current = $(items.get(itemKey));
				lastKey = itemKey;
				current.css({
					'position' : 'absolute',
					'top' : '50%',
					'left' : '50%',
					'width' : current.data('dimensions').w + 'px',
					'height' : 'auto',
					'display' : 'none',
					'margin-left' : (-1 * Math.ceil(current.data('dimensions').w / 2)) + 'px',
					'margin-top' : (-1 * Math.ceil(current.data('dimensions').h / 2)) + 'px'
				}).fadeIn('fast');
				
				//prev
				if(itemKey > 0) {
					prev = $(items.get(itemKey-1)); 
				}
				
				//next
				if(itemKey < totalItems) {
					next = $(items.get(itemKey+1)); 
				}
				
				if (prev) {
					
					prev.css({
						'height' : Math.ceil(prev.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(prev.data('dimensions').w / 2) + 'px',
						'left' : (-1 * (prev.data('dimensions').w / 2)) + 'px',
						'display' : 'block',
						'top' : '50%',
						'right' : 'auto',
						'z-index' : '310',
						'margin-left' : 'auto',
						'margin-top' : (-1 * Math.ceil(prev.data('dimensions').h / 4)) + 'px'
					}).animate({
						'left' :  (-1 * (prev.data('dimensions').w / 2) + prevNextViewable) + 'px'
					});
				}
				
				if (next) {	
					next.css({
						'height' : Math.ceil(next.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(next.data('dimensions').w / 2) + 'px',
						'right' : (-1 * next.data('dimensions').w / 2) + 'px',
						'left' : 'auto',
						'margin-left' : 'auto',
						'display' : 'block',
						'z-index' : '310',
						'top' : '50%',
						'margin-top' : (-1 * Math.ceil(next.data('dimensions').h / 4)) + 'px'
					}).animate({
						'right' :  (-1 * (next.data('dimensions').w / 2 - prevNextViewable)) + 'px'
					});
				} 
			}
			
			
			
			
			/* show prev */
			if (itemKey < lastKey) {
				lastKey = itemKey;			
				/* next animeerida ära */
				if (next) {	
					next.css({'z-index' : '290'}).animate({
						'right' : (-1 * next.data('dimensions').w / 2) + 'px'
					}, function(){ $(this).hide(); });
				} 
			
				/* current positsioneerida paremale */
				if (current) {
					
					var pos = current.position();
					current.css({'left' : 'auto','z-index' : '310', 'margin-left' : pos.left + 'px'}).animate({						
						'right' :  (-1 * (current.data('dimensions').w / 2 - prevNextViewable)) + 'px',
						'height' : Math.ceil(current.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(current.data('dimensions').w / 2) + 'px',
						'margin-top' : (-1 * Math.ceil(current.data('dimensions').h / 4)) + 'px'
						
					});
					next = current;
				}

				/* prev positsioneerida keskele */
				if (prev) {
					var size = viewport();
					var left = size[0] / 2;
					prev.css({'z-index' : '300'}).animate({
						'left' : left + 'px',
						'height' : prev.data('dimensions').h + 'px',
						'width' : prev.data('dimensions').w + 'px',
						'margin-top' : (-1 * prev.data('dimensions').h / 2) + 'px',
						'margin-left' : (-1 * Math.ceil(prev.data('dimensions').w / 2)) + 'px'					
					}, function(){ $(this).css({'left' : '50%'}); });
					current = prev;
					prev = null;
				}
				
				/* uus prev */
				//prev
				if(itemKey > 0) {
					prev = $(items.get(itemKey-1)); 
				}
				if (prev) {
					
					prev.css({
						'height' : Math.ceil(prev.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(prev.data('dimensions').w / 2) + 'px',
						'left' : (-1 * (prev.data('dimensions').w / 2)) + 'px',
						'display' : 'block',
						'z-index' : '310',
						'top' : '50%',
						'right' : 'auto',
						'margin-left' : 'auto',
						'margin-top' : (-1 * Math.ceil(prev.data('dimensions').h / 4)) + 'px'
					}).animate({
						'left' :  (-1 * (prev.data('dimensions').w / 2) + prevNextViewable) + 'px'
					});
				}				
			}
			
			/* show next */
			if (itemKey > lastKey) {
				lastKey = itemKey;
				/* prev animeerida ära */
				if (prev) {	
					prev.css({'z-index' : '290'}).animate({
						'left' : (-1 * (prev.data('dimensions').w / 2)) + 'px'
					}, function(){ $(this).hide(); });	
				}
			
				/* current positsioneerida vasakule */
				if (current) {
						
					current.css({'z-index' : '310'}).animate({
						'margin-left' : '0px',
						'left' :  (-1 * (current.data('dimensions').w / 2) + prevNextViewable) + 'px',
						'height' : Math.ceil(current.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(current.data('dimensions').w / 2) + 'px',
						'margin-top' : (-1 * Math.ceil(current.data('dimensions').h / 4)) + 'px'
					});
					prev = current;
				}
				
				/* next positsioneerida keskele */
				if (next) {
					var size = viewport();
					var left = size[0] / 2;
					var pos = next.position();
					next.css({'left' : pos.left, 'right' : 'auto','z-index' : '300'}).animate({
						'left' : left + 'px',
						'height' : next.data('dimensions').h + 'px',
						'width' : next.data('dimensions').w + 'px',
						'margin-top' : (-1 * next.data('dimensions').h / 2) + 'px',
						'margin-left' : (-1 * Math.ceil(next.data('dimensions').w / 2)) + 'px'						
					}, function(){ $(this).css({'left' : '50%'}); });
					current = next;
					next = null;
				}
				
				/* uus next */
				//next
				if(itemKey < totalItems) {
					next = $(items.get(itemKey+1)); 
				}
				if (next) {	
					next.css({
						'height' : Math.ceil(next.data('dimensions').h / 2) + 'px',
						'width' : Math.ceil(next.data('dimensions').w / 2) + 'px',
						'right' : (-1 * next.data('dimensions').w / 2) + 'px',
						'left' : 'auto',
						'margin-left' : 'auto',
						'display' : 'block',
						'top' : '50%',
						'z-index' : '310',
						'margin-top' : (-1 * Math.ceil(next.data('dimensions').h / 4)) + 'px'
					}).animate({
						'right' :  (-1 * (next.data('dimensions').w / 2 - prevNextViewable)) + 'px'
					});
				} 
			}
			
			
			
			
			/*
				- suvaline pilt
				 - keskele default aken  x * y -> laadimisel õige suurus
				 - vasakule eelnev, paremale järgnev - 50% kõrgusega -> laadimine
				
				- järgmine pilt 
					- liigutame olemasoleva vasakule välja - 50% kõrgus
					- liigutame parempoolse keskele + normaalsuurus
					- toome paremale uue pildi nähtavale
				- eelmine pilt
					- liigutame parempoolse välja
					- liigutame vasakpoolse keskele
					- liigutame keskmise paremale
				
				
				1. salvestan olemasolevad -> teen siin otsuse -> saadan animeerimisse
				
				
				- keskelt paremale
				- keskelt vasakule
				
				- vasakult keskele
				- vasakult välja
				- vasakule sisse
				
				- paremalt keskele
				- paremalt välja
				- paremale sisse
				
			*/
			
			/*var left = $(images.get(nr)).data('left');
			var ew = $(images.get(nr)).data('width');
			var margin = (width * 40 / 500);
			//console.log('margin: '+margin);
			container.stop(true).animate({'margin-left': -1 * (left-margin) + 'px'});
			
			var setWidth = width - (margin * 2);
			if(last && curr != nr){
				$(last).stop(true).animate({'width' : last.data('width') + 'px'});
			}
			
			$(images.get(nr)).stop(true).animate({'width' : setWidth + 'px'}, 400);
			last = $(images.get(nr));
			curr = nr;	*/		
		};
		
		if (items.length > 0) {
			
			/* overlay setup */
			overlay = $('<div class="overlay"></div>')
				.css({
					'width' : '100%',
					'height' : '100%',
					'background-color' : '#000',
					'position' : 'absolute',
					'top' : '0px',
					'left' : '0px',
					'z-index' : '200',
					'display' : 'none'
				})
				.click(function(){
					hideGallery();
				});
			$('body').append(overlay);
			
			
			items.each(function(i){
				
				var x = i;
				$(this).click(function(e){
					e.preventDefault();
					showImage(x);
				});
				
				var image = new Image();
					$(image)
						.load(function(){
							
							var $this = this;
							var getHeight = function () {
								if ($($this).height() == 0) {
									setTimeout(getHeight, 1);
									return;
								}
								var h = $($this).height(),
								w = $($this).width();

								$($this).data('dimensions', {'h': h, 'w': w})
							};
							
							getHeight();
							
						})
						.attr('src', $(this).attr('href'))
						.addClass('galleryImage')
						.click(function(){
							if (lastKey == x) {
								hideGallery();
							} else {
								showImage(x);
							}
						})
						.css({
							'position' : 'absolute',
							'left' : '-2000px',
							'top' : '0px',
							'z-index' : '300',
							'border' : '10px solid #ffffff'
						}).mouseover(function(){
							$(this).css('border-color', '#eee809');
						}).mouseout(function(){
							$(this).css('border-color', '#ffffff');
						});	
					
				$('body').append(image);
			});
			
			items = $('img.galleryImage');
			
		}
		
	});
})(jQuery);
