/*
*
* Usage:
*
*   settings = {
*       interval: 20, // in seconds
*   }
*
*   $('.showcase_item').showcase(settings)
*
*
*/

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(searchElement /*, fromIndex */)
  {
    "use strict";
 
    if (this === void 0 || this === null)
      throw new TypeError();
 
    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;
 
    var n = 0;
    if (arguments.length > 0)
    {
      n = Number(arguments[1]);
      if (n !== n) // shortcut for verifying if it's NaN
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }
 
    if (n >= len)
      return -1;
 
    var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);
 
    for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
}


(function($) {

/* default settings: */
var defaults = {
    interval: 8
}

function group_objects(objects) {
    /* Group objects which are next to each other */
    groups = [];
    var last = null;
    var current_group = null;

    for(var i=0, current; current=objects[i]; i++){

        if (last == null) {
            last = current;
            current_group = [ current ];
            continue;
        }

        var $last = $(last);
        var $current = $(current);

        if( $last.parent()[0] == $current.parent()[0] &&
            $last.index() + 1 == $current.index()
        ) {
            current_group.push( current );
        } else {
            groups.push(current_group);
            current_group = [ current ];
        }
        last = current;
    }

    groups.push(current_group);
    return groups;
}

/* Register plugin in jQuery namespace: */
$.fn.showcase = function(options) {

    groups = group_objects(this);
    if(!groups)
        return this;

    for(i=0; i<groups.length; i++){
        group = groups[i];
        if(!group) continue;
        wrapper = $('<ul>').addClass('showcase-wrapper');
        $(group[0]).before(wrapper);
        $(group).each(function(){
            $('<li>').append(this).appendTo(wrapper);
        });
        showcase = new ShowCase(wrapper[0], options);
    }

    return this;
}


/* Constructor */
function ShowCase(el, options){
    if( el.tagName != 'UL')
        return false;

    this.options = defaults

    if(typeof options != 'undefined')
        this.options = $.extend(this.options, options)

    this.ul = $(el);

    /* initial sequence of li's: */
    this.ls = [];

    /* page slectors ul: */
    this.page_selector = 0;

    /* height of bigest li element: */
    this.height = 0;

    /* initialize: */
    this.init();
}

ShowCase.prototype = {
    init: function() {
        var self = this;
        this.ul.children().each(function(){
            /* find max-height: */
            if($(this).height() > self.height)
                self.height = $(this).height();

            /* set negative margin: */
            //$(this).css('margin-bottom', -$(this).height());
            $(this).css('position', 'absolute');

            /* append to initial sequence: */
            self.ls.push(this);
        });

        /* set ul's height: */
        this.ul.css('height', this.height);

        this.create_page_selector();

        /* hide all */
        this.ul.children().hide()
        /* show first */
        this.show(0)

        this.restart_interval();
    },

    restart_interval: function() {
        if('interval' in this)
            clearInterval(this.interval);

        var self = this;

        /* init interval */
        this.interval = setInterval(function(){
            self.show_next();
        }, this.options.interval*1000);
    },

    create_page_selector: function(){
        var self = this;
        if(this.page_selector)
           return; 
        var ul = $('<ul>');
        ul.addClass('paginator');
        for(i=1;i<=this.ls.length;i++){
            a = $('<a>').attr('href', 'javascript:void(0)').text(i);
            a.data('page_index', i-1);
            a.click(function(){
                self.show( $(this).data('page_index') );
                self.restart_interval();
            });
            li = $('<li>').append(a);
            if(i == this.selected)
                li.addClass('selected');
            ul.append(li);
        }
        this.page_selector = ul;
        this.ul.parent().append(ul);
    },

    show: function(index) {
        /* display item: */
        var current = this.ul.children().last();
        var next = $(this.ls[index]);
        if(current[0] == next[0])
            return;
        current.fadeOut('slow');
        next.detach().appendTo(this.ul).fadeIn('slow');

        /* update page selector: */
        this.page_selector.children().removeClass('sel');
        this.page_selector.children().eq(index).addClass('sel');

        page_placeholder = next.find('.paginator_holder');
        if(page_placeholder[0])
            this.page_selector.detach().appendTo(page_placeholder[0]);
    },

    show_next: function() {
        current = this.ul.children().last()[0];
        next = this.ls.indexOf(current) + 1;
        if(next >= this.ls.length)
            next= 0;
        this.show(next);
    }
}

})(jQuery);



