(function($) {
  $.scroller = function(element, options) {
    var defaults = {
      speed: 500,
      nav: true,
      navPosition: "before",
      auto: false,
      autoTimeout: 5000,
      transition: "slide"
    }
    var plugin = this;
    plugin.settings = {}
    
    var $element = $(element),
        element = element;
    
    plugin.init = function() {
    
      plugin.settings = $.extend({}, defaults, options);
      
      plugin.settings.panels = $element.find(".scroll_panel");
      plugin.settings.outerWidth = $element.find(".scroll_panel").outerWidth();
      plugin.settings.width = $element.find(".scroll_panel").width();
     
      // Setting up default styles
      $element.css("overflow","hidden");
      $element.find(".scroll_container").css({
        //width: plugin.settings.panels.length*plugin.settings.outerWidth,
        height: $element.find(".scroll_panel").first().height()
      });
      plugin.settings.panels.css({
        //float: "left",
        width: plugin.settings.width,
        position: "absolute",
        top: 0,
        left: plugin.settings.outerWidth
      });
      plugin.settings.panels.first().addClass("active").css("left",0);
      
      // Add a navigation
      if(plugin.settings.nav === true) {
        var nav = $("<ul class='scroller_nav'></ul>");
        $element.find(".scroll_panel").each(function(i) {
        
          if($(this).attr("data-ajax")) {
            link = $(this).attr("data-ajax");
            $(this).addClass("loading").html("");
          } else {
            link = "#"+$(this).attr("id");
          }
          var name = $(this).attr("data-name") ? $(this).attr("data-name") : link;
          var link_class = (i==0) ? "active" : "";

          nav.append("<li><a href='"+link+"' class='"+link_class+"'>"+name+"</a></li>");
          
        });
        
        if(plugin.settings.navPosition == "before") {
          $element.find(".scroll_container").before(nav);
        } else {
          $element.find(".scroll_container").after(nav);
        }
      }
      $(".scroller_nav a",$element).live("click", function() {
        plugin.settings.auto = false;
        var href = $(this).attr("href");
        href = href.split("#");
        href = "#"+href[1];
        
        var current = parseInt($(".scroll_panel.active").index(),10);
        var next = parseInt($(href).index(),10);
        
        if(current-next > 0) {
          plugin.moveTo(href,"left");
        } else {
          plugin.moveTo(href);
        }
        return false;
      });
      
      $("a.scroller_next",$element).live("click", function() {
        plugin.get("next");
        return false;
      });
      
      $("a.scroller_prev",$element).live("click", function() {
        plugin.get("prev","left");
        return false;
      });
      
      // Initiate automove
      if(plugin.settings.auto === true) {
        autoNext();
      }
      
    }
    
    var autoNext = function() {
      setTimeout(function() {
        if(plugin.settings.auto === true) {
          plugin.get("next");
          autoNext();
        }
      },plugin.settings.autoTimeout);
    }

    plugin.moveTo = function(panel,direction,callback) {

      var next,
          ajax,
          current = $(".scroll_panel.active",$element);

      // Remove all active classes, set active navigation
      $element.find("a",".scroller_nav").removeClass("active").end()
        .find("a[href='"+panel+"']").addClass("active").end()
        .find(".scroll_panel.active").removeClass("active");
      
      if($("html").hasClass("ie7")) {
        $element.find("a[href='"+window.location.href+panel+"']").addClass("active");
      }
      if(panel.indexOf("#") == 0) {
        next = $(panel);
        ajax = 0;
      } else {
        next = $(".scroll_panel[data-ajax='"+panel+"']");
        ajax = 1;
      }
      
      next.addClass("active");
      var distance = next.position();
      distance = distance.left;

      // If we're sliding (default)
      if(plugin.settings.transition == "slide") {
        
        var next_distance;
        if(direction && direction === "left") {
          next.css("left",-plugin.settings.outerWidth);
          move_current = plugin.settings.outerWidth;
        } else {
          next.css("left",plugin.settings.outerWidth);
          move_current = -plugin.settings.outerWidth;
        }
        
        current.dequeue().animate({
          left: move_current
        },plugin.settings.speed, "easeOutCubic");
        
        next.dequeue().animate({
          left: 0
        },plugin.settings.speed, "easeOutCubic");
        
        $element.find(".scroll_container").dequeue().animate({
          height: next.outerHeight()
        },plugin.settings.speed, "easeOutCubic", function() {
          if(ajax != 1) {
            doCallback();
          }
        });

      }
      // If we're fading
      else if(plugin.settings.transition == "fade") {
        var speed = plugin.settings.speed/2;
        
        plugin.settings.panels.hide();
        current.show().fadeOut(plugin.settings.speed);
        next.css("left",0).fadeIn(plugin.settings.speed, function() {
          plugin.settings.panels.show();
          current.css("left",plugin.settings.outerWidth);
        });
        
        if(ajax != 1) {
          $element.find(".scroll_container").dequeue().animate({
            height: next.height()
          },plugin.settings.speed, "easeOutCubic", doCallback);
        }
        
      }
      
      if(ajax == 1) {
        $.get(next.data("ajax"), function(data) {
          next.html(data).removeClass("loading");
          next.find(".carousel").carousel({
            navPosition: "after"
          });
          $element.find(".scroll_container").dequeue().animate({
            height: next.height()
          },plugin.settings.speed, "easeOutCubic", doCallback);
        });
      }
      
      var doCallback = function() {
        if(typeof callback == 'function') {
          callback.apply(next.position());
        }
        if(typeof plugin.settings.callback == 'function') {
          plugin.settings.callback.apply(next.position());
        }
      }
    }

    plugin.get = function(type) {
      var new_panel;
      if(type === "next") {
        new_panel = $element.find(".active").next();
        if(new_panel.length === 0) {
          new_panel = $element.find(".scroll_panel").first();
        }
      } else if(type === "prev") {
        new_panel = $element.find(".active").prev();
        if(new_panel.length === 0) {
          new_panel = $element.find(".scroll_panel").last();
        }
      }
      
      if(new_panel.attr("data-ajax")) {
        new_panel = new_panel.attr("data-ajax");
      } else {
        new_panel = "#"+new_panel.attr("id");
      }
      plugin.moveTo(new_panel);
    }

    plugin.init();

  }

  $.fn.scroller = function(options) {

    return this.each(function() {
      if (undefined == $(this).data('scroller')) {
        var plugin = new $.scroller(this, options);
        $(this).data('scroller', plugin);
      }
    });

  }

})(jQuery);
