;var gallery = {

    init: function(tabsDisabled, tabSelected) {
    
        tabsDisabled = tabsDisabled || [];
        tabSelected = tabSelected || 0;

        $("#galleryTabs").tabs({
            create: application.evtTabsLoaded,
            select: gallery.evtTabSelect,            
            selected: tabSelected,
            disabled: tabsDisabled
        });
        
        gallery.slideshow.init();
        gallery.panorama.init();
        gallery.video.init();
        
    },

    destroy: function() {
        $('#gallerySlideshow').cycle('destroy');
    },
    
    evtTabSelect: function(event, ui) {
    
        $('#gallerySidebar div.similarContent>div').hide();
        
        if (ui.index != 0) {
            $('#gallerySlideshow').cycle('pause');
        } else {
            $('#gallerySlideshow').cycle('resume');
        }
        
        switch (ui.index) {
            case 0: $('#relatedImage').show(); break;
            case 1: $('#relatedVideo').show(); break;
            case 2: $('#related360').show(); break;
            case 3: $('#relatedAudio').show(); break;
            case 4: $('#related3d').show(); break;
            case 5: $('#relatedWebcam').show(); break;
        }
        return true;
    },
    


    slideshow: {
    
        init: function() {
        
			var that = this;
        
            $("ul#gallerySlideshow li img.first").load(function() {
                $(this).css("visibility", "visible");
                $("#gallerySlideshow").cycle({
                    fx: "fadeResize",
                    containerResize: 0,
                    slideResize: 0,
                    fit: 1,
                    fastOnEvent: 1,
                    after: gallery.slideshow.evtAfter
                });    
            });
            
            $("#tabImage").keyup(function(e) {
				if (e.keyCode === 27 && $(this).hasClass("fullscreen")) {
					that.fullscreenToggle();
				}
            });
            
            $('#tabImage a.back').click(gallery.slideshow.prev);
            $('#tabImage a.break').click(gallery.slideshow.pause);
            $('#tabImage a.play').click(gallery.slideshow.resume);
            $('#tabImage a.next').click(gallery.slideshow.next);
            
            $('#tabImage a.buttonFullscreen').click(function() {
				that.fullscreenToggle();
			});
            
        },
        
        fullscreenToggle: function() {
            $("body").toggleClass('fullscreen');
            $("#tabImage").toggleClass('fullscreen');
            $("#tabImage a.buttonFullscreen").toggleClass('buttonFullscreenClose');
            
            this.resize();
            return false;
        },
        
        resize: function() {
            $("#gallerySlideshow img.full:visible").each(function(i, v) {
                if ($(v).height() > $(v).width()) {
                    $(v).css("height", "100%")
                        .css("width", "auto")
                        .css("top", "0")
                        .css("left", ($("#gallerySlideshow").width() - $(v).width()) / 2);
                } else {
                    $(v).css("height", "auto")
                        .css("width", "100%")
                        .css("top", ($("#gallerySlideshow").height() - $(v).height()) / 2)
                        .css("left", "0");
                }
            });
            return true;
        },
        
        next: function() {
            $('#gallerySlideshow').cycle('next');
            return false;
        },
        
        prev: function() {
            $('#gallerySlideshow').cycle('prev');
            return false;
        },
        
        pause: function() {
            $('#gallerySlideshow').cycle('pause');
            return false;
        },
        
        resume: function() {
            $('#gallerySlideshow').cycle('resume');
            return false;
        },
        
        evtAfter: function(curr, next, opts) {
            $('#imageCounter').text((opts.currSlide+1) +" / "+ opts.slideCount);
        }
    
    },
    
    
    panorama: {
    
        init: function() {

            $(".dreiSechzigGrad").click(gallery.panorama.overview);
            $("#gallery_360_overview a.gallery_panorama_a").click(gallery.panorama.detail);
            
            $('#tab360 a.back').click(gallery.panorama.prev);
            $('#tab360 a.next').click(gallery.panorama.next);
            $('#tab360 a.buttonFullscreen').click(gallery.panorama.fullscreenToggle);
        },
        
        
        fullscreenToggle: function() {
        
            $("body").toggleClass('fullscreen');
            $("#tab360").toggleClass('fullscreen');
            $("#tab360 a.buttonFullscreen").toggleClass('buttonFullscreenClose');
            
            $pano = $("#tab360 div.panorama").toggleClass("panoramaFullscreen");
            
            if ($("#tab360").hasClass('fullscreen')) {
                $pano.children(".krpano").css("width", "100%").css("height", "100%");
                gallery.panoramaResizeHandler = $(window).resize(gallery.panorama.resize);
                gallery.panorama.resize();
                $("body").css("overflow","hidden");
                $("#gallery_panorama_bg").css({display: "block"});
            } else {
                $(window).unbind("resize", gallery.panorama.resize);
                $pano.css("height", "auto");
                $pano.children(".krpano").css("width", 656).css("height", 492);
                $("body").css("overflow","auto");
                $("#gallery_panorama_bg").css({display: "none"});
            }
            return false;
        },
        
        resize: function() {
            $("#tab360 div.panorama").filter(":visible").css("height", $(window).height() - 35);
        },
        
        overview: function() {
            if ($("#tab360>div.panorama").length > 1) {
                $("#tab360>div").hide();
                $('#gallery_360_overview').show();
            }
        },
        
        detail: function(e) {
        
            if (e && e.currentTarget) {
            
                $a = $(e.currentTarget);
                
                $('#gallery_360_overview').hide();
                $("div#"+$a.attr("rel")).show();
            }
        },
        
        next: function() {
            $panorama = $("#tab360>div.panorama").filter(":visible");
            $next = $panorama.next(".panorama");
            if ($next.length == 0) {
                $next = $panorama.siblings(".panorama:first");
            }
            
            if ($next.length > 0) {
				$panorama.hide();
				$next.show();
            }
            return false;
        },
        
        prev: function() {
            $panorama = $("#tab360>div.panorama").filter(":visible");
            $prev = $panorama.prev(".panorama");
            if ($prev.length == 0) {
                $prev = $panorama.siblings(".panorama:last");
            }
            
            if ($prev.length > 0) {
				$panorama.hide();
				$prev.show();
            }
            return false;
        }
        
    },
    
    
    
    
    
    
    
    
    video: {
    
        init: function() {
            $(".galleryNavigation .video").click(gallery.video.overview);
            $("#gallery_video_overview a.gallery_video_a").click(gallery.video.detail);
            
            $('#tabVideo a.back').click(gallery.video.prev);
            $('#tabVideo a.next').click(gallery.video.next);
        },
        
        overview: function() {
             if ($("#tabVideo>div.video_c").length > 1) {
                $("#tabVideo .video_c").hide();
                $('#gallery_video_overview').show();
             }
        },
        
        detail: function(e) {
        
            if (e && e.currentTarget) {
            
                $a = $(e.currentTarget);
                
                $('#gallery_video_overview').hide();
                $("div#"+$a.attr("rel")).show();
            }
        },
        
        next: function() {
            $video = $("#tabVideo>div.video").filter(":visible");
            $next = $video.next(".video");
            if ($next.length == 0) {
                $next = $video.siblings(".video:first");
            }
            
            if ($next.length > 0) {
				$video.hide();
				$next.show();
            }
            return false;
        },
        
        prev: function() {
            $video = $("#tabVideo>div.video").filter(":visible");
            $prev = $video.prev(".video");
            if ($prev.length == 0) {
                $prev = $video.siblings(".video:last");
            }
            
            if ($prev.length > 0) {
				$video.hide();
				$prev.show();
            }
            return false;
        }
        
    }        
            
};
