$(document).ready(function ( ) {
	if ($("#details")[0]) {
		//
		// Google Maps
		//
		var address	=	$(".map .adres").text();
		gMaps($(".map"), address);
		
		$(".box .fullscreen").click(function ( ) {
			$.colorbox({
				"html":		$("<div class='fullscreenMap'></div>"),
				"onComplete":	function ( ) {
					gMaps($(".fullscreenMap"), address);
				}
			});
			
			return false;
		});
		
		$("a.inner").colorbox();
		
		//
		// Gallery
		//
		var gallery;
		
		$.fn.disableSelection	=	function ( ) {
			$(this).attr('unselectable', 'on')
				.css('-moz-user-select', 'none')
				.each(function ( ) { 
					this.onselectstart = function() { return false; };
				});
		};

		gallery		=	function ($el) {
			var methods, $topThumb, $bottomThumb, $activeThumb,
			    $photo		=	$el.find(".photo img"),
			    $prev		=	$el.find(".photo .prev"),
			    $next		=	$el.find(".photo .next"),
			    $fullscreen		=	$el.find(".photo .fullscreen"),
			    $current		=	$el.find(".photo .current"),
			    $slider		=	$el.find(".thumbs .slider"),
			    $thumbs		=	$slider.find(".thumb"),
			    $fsThumbs		=	$thumbs.clone().appendTo("body").hide(),
			    $firstThumb		=	$thumbs.first(),
			    $lastThumb		=	$thumbs.last(),
			    thumbHeight		=	$firstThumb.outerWidth(true),
			    position		=	parseInt($slider.css("left"), 10),
			    animationDuration	=	50;
			
			methods	=	{
				"init":		function ( ) {
					$topThumb	=	$firstThumb;
					$activeThumb	=	$firstThumb;
					
					if ($thumbs.length > 3) {
						$bottomThumb	=	$firstThumb.next().next().next();
					} else {
						$bottomThumb	=	$lastThumb;
					}
					
					$thumbs.click(methods.changePhoto);
					
					$prev.click(function ( ) {
						$activeThumb.prev().click();
						return false;
					});
					
					$next.click(function ( ) {
						$activeThumb.next().click();
						return false;
					});
					
					$prev.disableSelection();
					$next.disableSelection();
					$thumbs.find(".thumb").disableSelection();
					
					$fsThumbs.find("a").each(function ( ) {
						$(this).attr({"rel": "gal"});
					}).colorbox();
					$fullscreen.click(methods.fullscreen);
					
					return methods;
				},
				"changePhoto":	function ( ) {
					var $this	=	$(this);
					
					$activeThumb	=	$this;
					
					$current.text(+$activeThumb.data("i") + 1);
					
					if (this !== $firstThumb[0] && this !== $lastThumb[0]) {
						if (this === $topThumb[0]) {
							$topThumb	=	$topThumb.prev();
							$bottomThumb	=	$bottomThumb.prev();
							
							position	+=	thumbHeight;
						} else if (this === $bottomThumb[0]) {
							$topThumb	=	$topThumb.next();
							$bottomThumb	=	$bottomThumb.next();
							
							position	-=	thumbHeight;
						}
					}
						
					$slider.animate(
						{ "left": position + "px" },
						animationDuration
					);
					
					$photo.attr({
						"src":		$this.find("a").attr("href")
					});
					
					return false;
				},
				"fullscreen":	function ( ) {
					$fsThumbs.filter("[data-i=" + $activeThumb.data("i") + "]").find("a").click();
					
					return false;
				}
			};
			
			return methods.init();
		};
		
		gallery($(".gallery"));
	}
});

