kikGallery_options = {
  scripts_path:   'js/kikGallery/',
  css_path:       'css/kikGallery/',
  playing:        true
} ;

(function($) {
  $.kikGallery = function( id, el ){
    var that                  = this ;
    this.id                   = id ;
    this.options              = $.kikGallery.options ;
    this.playing              = $.kikGallery.options.playing ;
    this.options.image_path   = el.attr( 'href' ) ;
    var css_id                = el.attr( 'id' ) ;
    this.gallery              = $( '<ul class="kikGallery" id="' + css_id + '"></ul>' ) ;
    this.index                = 0 ;
    this.count ;

    el.replaceWith( this.gallery ) ;

    this.gallery.load( this.options.scripts_path + 'populate.php', this.options, function(resp, st){
      if ( st == 'success' )
      {
        that.count = $( '.kikGalleryItem', that.gallery ).length ;
        that.build_interface() ;
        $( '.kikGalleryItem:first', that.gallery ).show() ;
        if ( that.playing )
          that.loop() ;
        that.setPlayButtonBg() ;
      } else 
        that.gallery.hide() ;
      } ) ;
  } ;

  $.kikGallery.prototype.build_interface = function()
  {
    this.gallery.prepend( '<li class="leftArrow"><a href="javascript://"></a></li>' ) ;
    this.gallery.prepend( '<li class="rightArrow"><a href="javascript://"></a></li>' ) ;
    this.gallery.prepend( '<li class="playButton"><a href="javascript://"></a></li>' ) ;

    $( '.leftArrow', this.gallery ).click( function(){ 
      var i = $( '.kikGallery').index( $(this).parent() ) ; 
      window.kikGalleries[i].showPrev() ;
      return false ;
    } ) ;

    $( '.rightArrow', this.gallery ).click( function(){
      var i = $( '.kikGallery').index( $(this).parent() ) ; 
      window.kikGalleries[i].showNext() ;
      return false ;
    } ) ;

    $( '.playButton', this.gallery ).click( function(){
      var i = $( '.kikGallery').index( $(this).parent() ) ; 
      window.kikGalleries[i].togglePlay() ;
      return false ;
    } ) ;
  }

  $.kikGallery.prototype.showPrev = function()
  {
    $( '.kikGalleryItem:eq(' + this.index + ')', this.gallery ).hide() ;
    this.index -= 1 ;
    if ( this.index < 0 )
      this.index = this.count - 1 ;
    $( '.kikGalleryItem:eq(' + this.index + ')', this.gallery ).fadeIn( 'slow' ) ;
  }

  $.kikGallery.prototype.showNext = function()
  {
    $( '.kikGalleryItem:eq(' + this.index + ')', this.gallery ).hide() ;
    this.index += 1 ;
    if ( this.index >= this.count )
      this.index = 0 ;
    $( '.kikGalleryItem:eq(' + this.index + ')', this.gallery ).fadeIn( 'slow' ) ;
  }

  $.kikGallery.prototype.loop = function()
  {
    if ( this.playing )
    {
      $( '.rightArrow', this.gallery ).trigger( 'click' ) ;
      i = $( '.kikGallery' ).index( this.gallery ) ;
      this.timeoutID = window.setTimeout( 'window.kikGalleries[' + i + '].loop()', 3000 ) ;
    } else
      window.clearTimeout( this.timeoutID ) ;
  }

  $.kikGallery.prototype.setPlayButtonBg = function()
  {
    if ( this.playing )
    {
      $( '.playButton a', this.gallery ).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/stop.png)' ) ;
      $( '.playButton a', this.gallery ).hover(
        function() { $(this).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/stop_hover.png)' ) },
        function() { $(this).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/stop.png)' ) ; }
      ) ;
    } else {
      $( '.playButton a', this.gallery ).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/start.png)' ) ;
      $( '.playButton a', this.gallery ).hover(
        function() { $(this).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/start_hover.png)' ) },
        function() { $(this).css( 'background-image', 'url(' + $.kikGallery.options.css_path + '/images/start.png)' ) ; }
      ) ;
    }
  }

  $.kikGallery.prototype.togglePlay = function()
  {
    this.playing = ( this.playing ? false : true ) ;
    if ( this.playing )
      this.loop() ;
    this.setPlayButtonBg() ;
  } 

  $.fn.kikGallery = function( options )
  {
    $.kikGallery.options = options ;
    $( 'head' ).append( '<link href="' + options.css_path + '/kikGallery.css" rel="stylesheet" type="text/css" />' ) ;
    if ( undefined === window.kikGalleries )
      window.kikGalleries = [] ;

    return this.each( function()
    {
      var el = $( this ) ;
      if ( el.is( 'a' ) )
      {
        i = window.kikGalleries.length ;
        window.kikGalleries.push( new $.kikGallery( i, el, options ) ) ;
      }
    } ) ;
  }

  $(document).ready( function(){ 
      $( '.doKikGallery' ).kikGallery( kikGallery_options ) 
      $( 'ul > li:first' ).show() ;
  } ) ;
})(jQuery);
