$(function() {
    
    $("body#tour").each(function() {
        
        $(".features").each(function() {
            $(this).children().each(function(index) {
                $(this).addClass("item" + (index + 1));
            });
        });
        
        // natural
        var imageWidth = $("#gallery img").width();
        
        // natural
        var minContentHeight = $("#content").height();
        
        // jQuery
        var allImages = $("#gallery img");
        
        // string boolean -> void
        function show(section, instant) {
            var imageWrapper = $("#" + section + "-image-wrapper");
            var image = imageWrapper.children("img");
            
            if(imageWrapper.length === 0) {
                show("dashboard", instant);
            } else {
                window.location.hash = "#" + section;

                var button   = $("#" + section + "-button");
                var features = $("#" + section + "-features");
            
                if(instant) {
                    $("#tour-nav li").removeClass("selected");
                    button.parent().addClass("selected");
                    
                    $("#gallery > div").css({ left: -1 * imageWrapper.position().left });
                                                  
                    allImages.css("opacity", 0.5);
                    image.css("opacity", 1.0);

                    $(".features").hide();
                    features.show();

                    $("#content").height(Math.max(minContentHeight,
                                                  features.height()));
                } else {
                    $("#gallery > div").animate({ 
                        left: -1 * imageWrapper.position().left
                    });

                    allImages.not(image).animate({ opacity: 0.5 });
                    image.animate({ opacity: 1.0 });

                    $(".features").fadeOut();
                    features.fadeIn();
            
                    $("#content").animate({
                        height: Math.max(minContentHeight,
                                         features.height())
                    }, function() {
                        $("#tour-nav li").removeClass("selected");
                        button.parent().addClass("selected");
                    });
                }
            }
            
            $("#gallery img").css("visibility", "visible");
        }
        
        // boolean -> void
        function rotate(positive) {
            
            // U(string false)
            var section = window.location.hash && window.location.hash.substring(1);
            
            // jQuery
            var imageWrapper = section && $("#" + section + "-image-wrapper");
            
            if(imageWrapper && imageWrapper.length > 0) {

                // natural
                var currentIndex = imageWrapper.index();
                var nextIndex = positive ?
                                (currentIndex >= imageWrapper.parent().children().length - 1 ? 0 : currentIndex + 1) :
                                (currentIndex === 0 ? imageWrapper.parent().children().length - 1 : currentIndex - 1);

                // string
                var nextSection = $("#gallery .image-wrapper:eq(" + nextIndex + ")")
                                  .attr("id")
                                  .match(/^(.*)-image-wrapper$/)[1];
                                 
                show(nextSection, false);
            }
        }
       
        $("#dashboard-button").click(function() { show("dashboard", false); });
        $("#bookings-button").click(function() { show("bookings", false); });
        $("#helpdesk-button").click(function() { show("helpdesk", false); });
        $("#setup-button").click(function() { show("setup", false); });
        
        $("#next-button").click(function() { rotate(true); });
        $("#prev-button").click(function() { rotate(false); });
        
        switch(window.location.hash) {
            case "#dashboard":
            case "#bookings":
            case "#helpdesk":
            case "#setup":
                show(window.location.hash.substring(1), true);
                break;
            default:
                show("dashboard", true);
        }
    });
    
    $("body#pricing").each(function() {
       var overlay = $("<div class=\"overlay\"></div>");
       $("body").append(overlay);
       
       var popups = $();
       
       $(".popup-wrapper").each(function() {
           var body = $("body");
           var self = $(this);
           var button = self.find(".popup-button");
           var popup = self.find(".popup");

           popups = popups.add(popup);

           body.append(popup.remove());
               popup.position({
       			of: button,
       			my: "middle top",
       			at: "middle bottom",
       			offset: "40 5"
       		});

           $(button).add(popup).click(function() {
               if(popup.is(":visible")) {
                   overlay.fadeOut("fast");
                   popup.fadeOut("fast");
               } else {
                   overlay.fadeIn("fast");
                   popup.fadeIn("fast");
               }
           });
       });

       overlay.click(function() {
           $(popups).fadeOut("fast");
           overlay.fadeOut("fast");
       });
   });
    
    $("body#faq .faq-section dt").click(function() {
       var self = $(this);
       if(self.hasClass("expanded")) {
         self.removeClass("expanded").nextUntil("dt").slideUp("fast");
       } else {
         self.addClass("expanded").nextUntil("dt").slideDown("fast");
       }
    });
    
    $("body#contactus form").submit(function(evt) {
      var valid = true;

      $("input:text,textarea").each(function() {
        var self = $(this);

        var value = $.trim(self.val() || "");
        self.val(value);

        if(self.hasClass("required") && value === "") {
          self.addClass("error");
          valid = false;
        } else {
          self.removeClass("error");
        }
      });

      return valid;
    });
    
    
});
