(function($){

	$(document).ready(function(){
		
		
		//Main menu
		var i = 0;
		var step = 3;
		var show = function() {
			if(this._top + step < 28) {
				this._top += step;
				$(this).css('background-position', this._left + 'px -' + this._top +'px')
				return true;
			} else {
				$(this).css('background-position', this._left + 'px -28px')
			}
		}
		
		var hide = function() {
			if(this._top - step > 0) {
				this._top -= step - 1;
				$(this).css('background-position', this._left + 'px -' + this._top +'px')
				return true;
			} else {
				$(this).css('background-position', this._left + 'px 0px')
			}
		}
			
		$('#header li a').not('.current').each(function(){
			$(this).css('background-position', $(this).css('background-position'));
			this._left = -$(this).parent().position().left;
			this._top = 0;
			this._id = i++;
		}).hover(
			function(){
				ijs.worker.add('item'+ this._id, show, this);
			},
			function(){
				ijs.worker.add('item'+ this._id, hide, this);
			}
		);
		//end main menu
		
		
	
	
		
		$(".js-gallery a").each(function(){
			$('<img>').attr('src', this.href);
			$(this).css('cursor', 'default');
		}).click(function(){return false});
		
			
		$(".js-gallery li").hover(
			function() {
				var win_width = document.documentElement.clientWidth;
				var win_height = document.documentElement.clientHeight;
				var win_scroll_x = (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
				var win_scroll_y = (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
				var top = $(this).find("img").offset().top;
				
				$("body").append(
					$("<div>").attr("id", "displayed-photo-frame").css({
						"position": "absolute",
						"z-index": 666
					})
				);
				
				
				$("<img>").load(function(){
					var w = this.width;
					var h = this.height;
					var x = parseInt(win_width/2 - w/2);
					var y = parseInt(top - h - 20);

					//alert(w + ' '+ h + ' '+ x + ' '+ y)
					
					$("#displayed-photo-frame").css({
						"height": h + "px",
						"left": x + "px",
						"top": y + "px",
						"width": w + "px"
					});
				}).attr(
					"src", $('a', this).attr("href")
				).attr(
					"id", "displayed-photo"
				).appendTo("#displayed-photo-frame");
				
				
			},

			function() {
				$("#displayed-photo").remove();
				$("#displayed-photo-frame").remove();
			}
		);

			
			
		
		
		
		
	});

})(jQuery);