(function ($) {

    // public variables
    var o = null;
    var self = null;
    var currentPos = 0;
    var totalLength = 0;
    var tileArrayPos = new Array();
    var tailOffset = 0;

    // public methods
    var methods = {
        init: function (options) {

            var defaults = {
                cmdPrev: null,
            cmdNext: null
          };      
            // combine input options with plugin defaults
            o = $.extend(defaults, options);
            currentPos = o.position;

          // adjust the width of the visible projectholder
            $('#projectHolder').width(o.visible * ($('.projectItem').outerWidth(true)));

            // this is the index of the first hidden project
            tailOffset = o.visible - 1;

            if (o.cmdPrev != null) {
              o.cmdPrev.click(function () {
                // checks carousel visibility is not at left limit
                if ((tailOffset - o.visible + 1) > 0){
                  self.animate({ left: '+='+($('.projectItem').outerWidth(true))+ 'px' }, "slow");                    
                  tailOffset--;
                }
              });
            }
            if (o.cmdNext != null) {
              o.cmdNext.click(function () {
                // checks carousel visibility is not at right limit
                if (tailOffset < (tileArrayPos.length - 1)){
                  tailOffset++;
                  self.animate({ left: '-='+($('.projectItem').outerWidth(true))+ 'px' }, "slow");                 
                }
              });
            }
          
            // build array containing px position of each project
            totalLength = 0;
            $('.projectItem').each(function () {
              tileArrayPos.push(totalLength);
              totalLength += $(this).outerWidth(true);
            });

            return this.each(function () {
            });
        },

        // go to a tile function
        go: function (pos) {
          currentPos = pos;
          // ensure we have valid index
          if (currentPos < 0)
            currentPos = 0;
          else if (currentPos > tileArrayPos.length - 1){
            currentPos = 0;
            tailOffset = o.visible - 1;
          }
          
          // only auto-scroll the carousel if current project is not visible
          if (currentPos > tailOffset){
            var newPos = "-" + (tileArrayPos[currentPos - o.visible + 1])+ 'px';
            tailOffset = currentPos;
            self.animate({"left": newPos}, "slow");
          }
          else if (currentPos == 0) {
            // reset to starting visible position
          var newPos ="-" + (tileArrayPos[currentPos])+'px'; 
            tailOffset = o.visible - 1;
            self.animate({"left": newPos}, "slow");
          }           
          // scroll the carousel to the new position         
         
      }
    };

    $.fn.theCarousel = function (method) {

        // self reference
        self = this;

        // public methods
    this.go = methods.go;

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        }
        else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };

})(jQuery);
