/*

  America.gov JavaScript: Gallery Display
  
  America.gov Web Site
  United States Department of State
  Last Edited: Nov 2007 by Darren W Krape (krapedw@state.gov)

  Based on the "Accessible News scroll" plugin by Brian Reindel
  http://www.reindel.com/accessible_news_scroll/

*/

$.fn.accessscroll = function(settings) {
  settings = $.extend({
    scrollSpeed: "slow",
    scrollItemsPerSet: "2"
  }, settings);
  return this.each(function(i) {
    ascroll.init(settings,this);
  });
};

var scroll = function(s,p) {
  $(p).find(".container").stop().animate({ left: s.animateLeft }, s.scrollSpeed);
}

var ascroll = {
  init: function(s,p) {

    if($.browser.msie && $(p).hasClass("featured-items")) {
      var parentWidth = $(p).find(".box-body").width() - (parseInt($(p).find(".box-body").css("paddingLeft")) + parseInt($(p).find(".box-body").css("paddingRight")));
      $(p).find(".box-body").css({width:parentWidth + "px"});
    }

    var itemWidth = outerWidth($(p).find(".elements li:eq(0)"));

    var itemLength = $(p).find(".elements li").length;
    var itemSets = Math.ceil(itemLength / s.scrollItemsPerSet);
    var scrollContainerWidth = itemLength * itemWidth;
    
    $(p).find(".elements").wrap("<div class='container'></div>");
    $(p).find(".container").css("width",scrollContainerWidth + "px");
    
    if (itemSets > 1 ) {

      $(p).append("<div class='nav'><a class='button previous disabled' href='#'>Previous</a><a class='button next' href='#'>Next</a></div>");

      $(p).find(".nav").append('<ul class="numbers"></ul>');

      for ( var i = 0; i < itemSets; i++ ) {
         $(p).find(".numbers").append("<li><a href='#'>" + (i + 1) + "</a></li>");
      };

      $(p).find(".numbers li").slice(0,1).addClass("active");
      $(p).find(".numbers li:last").addClass("last");

// Next Button
      $(p).find(".next").click(function(){
      
        if(!$(p).find(".next").hasClass("disabled")) {

          var setWidth = itemWidth * s.scrollItemsPerSet;
          s.animateLeft = -(setWidth);
          scroll(s,p);
   
          $(p).find(".previous").removeClass("disabled");

          var currentItem = parseInt($(p).find(".numbers .active").text());
          $(p).find(".numbers li").removeClass("active");
          $(p).find(".numbers li:eq(" + currentItem + ")").addClass("active");
        }

        if ((parseInt($(p).find(".container").css("left")) + parseInt($(p).find(".container").css("width")) - setWidth) <= setWidth) {
          $(p).find(".next").addClass("disabled");
        }
        return false;
      });

  //Previous Button
      $(p).find(".previous").click(function(){
        
        if(!$(p).find(".previous").hasClass("disabled")) {

          s.animateLeft = "+" + (itemWidth * s.scrollItemsPerSet);
          scroll(s,p);

          $(p).find(".next").removeClass("disabled");

          var currentItem = parseInt($(p).find(".numbers .active").text());
          $(p).find(".numbers li").removeClass("active");
          $(p).find(".numbers li:eq(" + (currentItem - 2) + ")").addClass("active");
        }

        if (parseInt($(p).find(".container").css("left")) >= -(itemWidth * s.scrollItemsPerSet)) {
          $(p).find(".previous").addClass("disabled");
        }
        return false;
      });

  //Number Buttons
      $(p).find(".numbers li").click(function(){

        if(!$(this).hasClass("active")) {
          var currentItem = parseInt($(p).find(".numbers .active").text());
          var selectedItem = parseInt($(this).text());

          if(currentItem > selectedItem) var direction = "+"; else direction = "";
          s.animateLeft = direction + (itemWidth * s.scrollItemsPerSet) * (currentItem - selectedItem);
          scroll(s,p);

          $(p).find(".numbers li").removeClass("active");
          $(this).addClass("active");

          $(p).find(".nav a").removeClass("disabled")

          if($(this).text() == 1)
            $(p).find(".previous").addClass("disabled");
          else if($(this).text() >= ($(p).find(".elements li").length / s.scrollItemsPerSet))
            $(p).find(".next").addClass("disabled");
          else
            $(p).find(".next").removeClass("disabled");
        }

        return false;
      });
    }
  }
};

$(function() {
  $(".scroll").accessscroll({ scrollSpeed: "medium", scrollItemsPerSet: "2" });
  $(".featured-items").accessscroll({ scrollSpeed: "slow", scrollItemsPerSet: "3" });
});