(function($){

  $.fn.caSlider = function( oOpts ) {

    var iTimeout            = (oOpts.timeout != undefined) ? oOpts.timeout : 20000;
    var sPagerClass         = ( oOpts.pagerClass != undefined ) ? oOpts.pagerClass: 'slider-pager';
    var sPagerItemClass     = ( oOpts.pagerItemClass != undefined ) ? oOpts.pagerItemClass : 'slider-pager-item';
    var sPagerActiveClass   = ( oOpts.pagerActiveClass != undefined ) ? oOpts.pagerActiveClass : 'pager-active';
    
    var oContainer    = this;
    var oCurrent      = null;
    var iCurrentPage  = null;
    var timeOutFn     = null;
    var bSlideOpen    = false;
    var bIsMouseOver  = false;
    var aItems        = null;
    var aItemsText    = null;
    var oPager        = null

    var init = function() {
      var id = oContainer[0].id;
      aItems = $( "#" + id + "Content ." + id + "Image" );
      aItemsText = $( "#" + id + "Content ." + id + "Image span" );
      oPager = $( "#" + id ).append( '<div id="' +id + 'Pager"></div>' );
      $( "#" +id + "Pager" ).addClass( sPagerClass );
      makePager();
    }

    var makePager = function() {
      if( aItems.length > 1 ) {
        for( var i = 0 ; i < aItems.length; i ++ ) {
          $( '#'+oContainer[0].id+'Pager' ).append( '<a href="#" id="'+oContainer[0].id+'Pager-'+(i)+'">'+ ( i + 1 ) +'</a>' );
          $( '#' + oContainer[0].id + 'Pager-' + (i) ).addClass( sPagerItemClass );
        }
      }
    }

    init();

    var hideAll = function() {
      hideItems();
      clearPager();
    }

    var hideItems = function(){
     if( aItems.length > 1 ) {
       for( var i = 0 ; i < aItems.length; i ++ ) {
         $( aItems[ i ] ).stop();
         $( aItemsText[ i ] ).stop();
         $( aItems[ i ] ).hide();
         $( aItemsText[ i ] ).hide();
       }
     }
     bSlideOpen = false;
     clearTimeout( timeOutFn );
   }
   
    var clearPager = function(){
      if( aItems.length > 1 ) {
        for( var i = 0 ; i < aItems.length; i ++ ) {
          $( '#'+oContainer[0].id+'Pager-'+ i  ).removeClass( sPagerActiveClass );
        }
      }
    }

    var fadecontainer = function(isMouseOut) {
      var iThisTimeout = (!bSlideOpen) ? 10 : iTimeout;
      if(aItems.length > 0) {
        timeOutFn = setTimeout(makeSlider, iThisTimeout);
        
      } else {
        console.log("Poof..");
      }
    }

    aItems.each(function(i) {

      $(aItems[i]).mouseover(function() {
        bIsMouseOver = true;
      });

      $(aItems[i]).mouseout(function() {
        bIsMouseOver   = false;
        fadecontainer(true);
      });

    });

    var slideIn = function(index) {
        $(aItems[index]).fadeIn((iTimeout/6), function() {
          if($(aItemsText[index]).css('bottom') == 0) {
            $(aItemsText[index]).slideUp((iTimeout/6), function() {
              bSlideOpen = true;
              oCurrent = aItems[index];
              fadecontainer(false);
            });
          }
          else {
            $(aItemsText[index]).slideDown((iTimeout/10), function() {
              bSlideOpen = true;
              oCurrent = aItems[index];
              fadecontainer(false);
            });
          }
        });
    }

    var slideOut = function(index) {
        if($(aItemsText[index]).css('bottom') == 0) {
          $(aItemsText[index]).slideDown((iTimeout/26), function() {
            $(aItems[index]).fadeOut((iTimeout/6), function (){
              bSlideOpen = false;
              oCurrent = aItems[(index+1)];
              fadecontainer(false);
            });
          });
        }
        else {
          $(aItemsText[index]).slideUp((iTimeout/26), function() {
            $(aItems[index]).fadeOut((iTimeout/6), function(){
              bSlideOpen = false;
              oCurrent = aItems[(index+1)];
              fadecontainer(false);
            });
          });
        }
    }

    var makeSlider = function(  ) {
      
      oCurrent = (oCurrent != null) ? oCurrent : aItems[0];
      var iCurrentIndex      = jQuery.inArray(oCurrent, aItems);

        if( iCurrentPage != null ) {
          iCurrentIndex = iCurrentPage - 1;
          oCurrent = aItems[ iCurrentIndex ];
//          iCurrentPage = null;
//          clearTimeout( timeOutFn );
          bSlideOpen = false;
        }

        iCurrentIndex = (iCurrentIndex >= aItems.length) ? 0 : iCurrentIndex;

        clearPager();

        $( '#'+oContainer[0].id+'Pager-' + iCurrentIndex ).addClass( sPagerActiveClass );

      if(!bIsMouseOver) {
        if(bSlideOpen == false) {
          iCurrentPage = null;
          slideIn( iCurrentIndex );
        }
        else {
          if( iCurrentPage != null ) {
            hideItems();
          }
          slideOut( iCurrentIndex );
        }
      }
    }

    makeSlider();

    $( '.' + sPagerItemClass ).click( function(){
      iCurrentPage = parseInt( $(this).text() );

      hideAll();
      makeSlider();
    } );

  };

})(jQuery);