$(document).ready(function ( ) {
	if ($("#details")[0]) {
		//
		// 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"));
		
		//
		// Tabs
		//
		$(".tabs").each(function ( ) {
			var $tabs	=	$(this),
			    $buttons	=	$tabs.find(".buttons > li > a"),
			    $contents	=	$tabs.find(".contents > li");
			
			$buttons.click(function ( ) {
				var $this	=	$(this),
				    $target	=	$($this.attr("href"));
				
				$buttons.removeClass("active");
				$this.addClass("active");
				$contents.hide();
				$target.show();
				
				if ($target.data("has-map") && !$target.data("map-initialized")) {
					var address	=	$target.find(".adres").text();
					gMaps($target.find(".map"), address);
					
					$target.data({
						"map-initialized":	true
					})
				}
				
				if ($target.data("has-streetview") && !$target.data("streetview-initialized")) {
					var address	=	$target.find(".adres").text();
					gStreetview($target.find(".streetview"), address);
					
					$target.data({
						"streetview-initialized":	true
					})
				}
				
				return false;
			});
			
			$contents.hide();
			if (document.location.hash && $contents.filter(document.location.hash)[0]) {
				$buttons.filter("[href=" + document.location.hash + "]").click();
			} else {
				$buttons.filter(":first").click();
			}
		});
	}
});

