/**
 * Labyes application entry point and resources.
 *
 * @author Matias Mirabelli <lumen.night@gmail.com>
 * @since 1.0.0
 */
(function($) {

  $(document).ready(function () {
    $("a.print").click(function() {
      window.print();
    });
    $("a.video").click(function() {
      window.open("http://www.youtube.com/user/labyeschannel");
    });
    var height = 0;
    $("div.info-box").each(function() {
      var currentHeight = $(this).height();
      if (currentHeight > height) {
        height = currentHeight;
      }
    });
    $("div.info-box").height(height);
    $("#terms-and-conditions").click(function() {
      window.open(Labyes.termsAndConditions, "legal");
    });
    $("#privacy-policies").click(function() {
      window.open(Labyes.privacyPolicies, "legal");
    });
    var select = $("select#country")
        .prepend("<option value=\"select\">...</option>");
    if (select.length === 1) {
      select[0].selectedIndex = 0;
    }
  });

  /**
   * Top-level namespace for the application.
   */
  Labyes = {
    /**
     * Base web url. It's never null.
     * @type String
     */
    baseUrl : null,

    /**
     * Current section (i.g.: news, products, etc.). Default is "generic".
     * @type String
     */
    section : null
  };

  Labyes.Navigation = {
    initialize : function() {
      (function(self) {
        $("div.bottom > div.aside li").each(function() {
          if (!$(this).hasClass("child-item")) {
            $(this).click(function() {
              self.show($(this));
            });
          } else if($(this).hasClass("selected")) {
            var item = $(this).prev();

            while (item.hasClass("child-item")) {
              item = item.prev();
            }

            self.show(item);
          }
        });
      }(this));
    },
    show : function(parentEl) {
      var item = parentEl;

      parentEl.siblings("li.child-item").hide();

      while (item.next().hasClass("child-item")) {
        item.next().show();
        item = item.next();
      }
    }
  };

  /**
   * Automatic rolling of header background image.
   */
  Labyes.RollingSlideShow = {
    /**
     * Number of total frames or background images for the header. It cannot
     * change after initialization. It's never null.
     * @constant
     */
    MAX_FRAMES : null,

    /**
     * Interval, in milliseconds, between header background will change.
     * @constant
     */
    ROLLING_DELAY : 10000,

    /**
     * List of pictures for this page.
     * @type String[]
     */
    pictures : null,

    /**
     * Index of the image currently displayed as header background.
     * @type Number
     */
    _currentFrame : 1,

    /**
     * List of bullets (IMG elements) that indicates the current frame. Cannot
     * be null.
     * @type Element[]
     */
    _bullets : null,

    /**
     * Attaches the markup to this component and initializes image cache.
     */
    initialize : function() {
      this._bullets = $(".slideshow-wrapper img");
      //TODO: move to Labyes object.
      this.imagePath = this.imagePath.substr(0,
        this.imagePath.lastIndexOf("/"));
      this.MAX_FRAMES = this.pictures.length;

      if (this.MAX_FRAMES > 1) {
        setInterval($.proxy(this.displayNext, this), this.ROLLING_DELAY);
      }

      this.display(1);

      (function(self) {
        $.each(self._bullets, function(index, bullet) {
          // Little sandbox to save the index.
          (function() {
            var frame = index;

            $(bullet).click(function(event) {
              self.display(frame);
            });
          }());
        });
      }(this));
    },

    /**
     * Display specified slideshow frame.
     *
     * @param {Number} frame Index of frame to display. Must be greater than
     *    0 and less than MAX_FRAMES.
     */
    display : function(frame) {
      var index = frame;
      var imagePath = this.imagePath;

      if (frame > this.MAX_FRAMES) {
        index = 1;
      }

      $.each(this._bullets, function(index, bullet) {
        bullet.src = imagePath + "/bullet.png";
      });

      var imageUrl = this.getPictureUri(index);

      $(".slideshow-transition").fadeIn("slow", function() {
        $("div.top").css({
          backgroundImage : "url(" + imageUrl + ")"
        });

        $(this).fadeOut("slow");
      });

      this._currentFrame = index;
      this._bullets[index - 1].src = imagePath + "/bullet-active.png";
    },

    /**
     * Displays the next image in the slideshow.
     */
    displayNext : function() {
      this.display(this._currentFrame + 1);
    },

    /**
     * Returns full picture URI for the specified index.
     * @param {Number} index Frame index. Cannot be null.
     *
     * @returns {String} A valid URI. Never returns null.
     */
    getPictureUri : function(index) {
      var picture = this.pictures[index - 1];
      var uri = this.imagePath + "/slideshow/" + picture;

      if (Labyes.baseUrl !== "/") {
        uri = Labyes.baseUrl + uri;
      }

      return uri;
    }
  };
}(jQuery));

