(function($){
	
	$(document).ready(function(){
		
		/* navigation height */
		var nav = $('header nav');
		var submenu = $('ul ul', nav);
		if ( submenu.length ) {
			nav.css({height : '+=' + submenu.outerHeight() + 'px'});		
		}
		
		/* video */
		if ($("#jquery_jplayer_1").length > 0) {
			$("#jquery_jplayer_1").jPlayer({
				ready: function () {
					$(this).jPlayer("setMedia", {
						m4v: $("#video_src").attr("value")
					}).jPlayer("play");
	
				},
				swfPath: "http://goliath.divid.ee/extensions/goliath/jplayer.2.1.0/",
				supplied: "m4v",
				errorAlerts: true,
				solution:"html, flash",
				loop: true,
				backgroundColor: '#F7F7F7',
				size: {
					width: "970px",
					height: "500px",
					cssClass: "jp-video-360p"
				}
			});
		}
		
		/* gallery images */
		var fade = typeof pngFade != "undefined" ?  pngFade : true;
		$('.gallery a')
			.each(function(){
				var info = $('.info', this).outerHeight();
				$(this).data('move', info);
				$('.zoom', this).css({'opacity': '0', 'display':'block', 'height': (135 - info) + 'px'});
			})
			.live({
				  mouseenter : function() {
					  var move = $(this).data('move') + 2;
					  $('.info', this).stop(true).animate({top: (135 - move) + 'px'}, 300);
					  if (fade) {
					      $('.zoom', this).stop(true).animate({opacity: '1'}, 300);
					  } else {
					      $('.zoom', this).css({opacity: '1'});  
					  }
				  },
				  mouseleave : function() {
					  var move = $(this).data('move') + 2;
					  $('.info', this).stop(true).animate({top: '135px'}, 300);
					  if (fade) {
					      $('.zoom', this).stop(true).animate({opacity: '0'}, 300);
					  } else {
					  	  $('.zoom', this).css({opacity: '0'}); 	
					  }
				  },
				  click : function (e) {
					  e.preventDefault();
				  	  overlay.show( $(this).attr('href'), $('.description', this).html() );	
				  }
			});
		
		/* overlay */
		var overlay = (function(){
			var overlay = $('<div></div>').css({
				'position':'fixed',
				'background':'#000',
				'opacity':'0',
				'left':'0px', 
				'top':'0px', 
				'width':'100%', 
				'height':'100%',
				'z-index':'100',
				'display' :'none'
			}).click(function(){ hide(); });
			$('body').append(overlay);
			
			var imageContainer = overlay.clone().addClass('imageOverlay').css({'overflow':'auto','overflow-y':'scroll','position' : 'fixed','background':'transparent','z-index':'101','opacity':'1','display':'none'}).click(function(){ hide(); });
			var titleContainer = $('<div class="title"></div>').css({'position':'relative'});
			
			imageContainer.append(titleContainer);
			var closeLink = $('<a href="#" class="close"></a>');
			titleContainer.append(closeLink);
			
			$('body').append(imageContainer);
			closeLink.click(function(e){
				e.preventDefault();
				hide();
			});
			var activeImage = null;
			var description = '';
			
			var show = function () {
				overlay.css({'opacity':'0','display':'block'}).animate({'opacity' : '0.5'});
				imageContainer.show();
				$('html,body').css({'overflow':'hidden'});
				imageContainer.css({'overflow' : 'auto','overflow-y':'scroll'});
			};
			
			var hide = function () {
				overlay.animate({'opacity' : '0'}, function(){ $(this).hide(); });
				imageContainer.css({'overflow' : 'hidden'});
				$(imageContainer).fadeOut(function(){ $(activeImage).remove(); activeImage = null; });
				$('html,body').css({'overflow':'auto'});
			};
			
			var setPosition = function(){
				var h = titleContainer.outerHeight();
					var dH = imageContainer.outerHeight();
					
					if (dH > h) {
						var padding = Math.round((dH - h) / 2);
						titleContainer.css({'top' : padding + 'px'});
					} else {
						titleContainer.css({'top' : '40px'});
					}
			};
			$(window).resize(function(){
				setPosition();
			});
			
			var setContainerWidth = function () {
				var w = $(activeImage).outerWidth();
				if ( w < 10) {
					setTimeout(setContainerWidth, 10);
				} else {
					titleContainer.css({'width' : w + 'px'});
					
					var h = titleContainer.outerHeight();
					var dH = imageContainer.outerHeight();
					
					if (dH > h) {
						var padding = Math.round((dH - h) / 2);
						titleContainer.css({'top' : padding + 'px'});
					} else {
						titleContainer.css({'top' : '40px'});
					}
				}
			};
			
			var loadImage = function (url, callback) {
				var img = new Image();
				$(img).load(function(){
					
					setContainerWidth();
					
					if (typeof callback == "function") {
						callback();
					}
				}).css({'visibility' : 'hidden','display':'block'}).attr('src', url);
				activeImage = img;
				titleContainer.prepend(img);
			};
			
			return {
				show: function ( url, description ) {
					titleContainer.html('<div class="description">' + description + '</div>');
					titleContainer.append(closeLink);
					loadImage( url , function () {
						show();
						$(activeImage).css({'visibility' : 'visible'});
					});
					
				}
			};
			
		})();
		
	}); //$(document).ready

})(jQuery);
