/**
 * jPageNav - add page navigation to a page
 * 
 * The navigation is composed of prev and next buttons and a drop-down menu
 * Usage: 
 * $("#links").jPageNav();
 * where 'links' is the ID of an UL, OL, or a DIV with an UL or an OL.
 * The html list must contain items with an A tags and 
 * one of the items should have a 'selected' class assigned to it.
 *
 * markup for example:
 * <div id="links">
 *   <ol>
 *       <li><a href="#1">link 1</a></li>
 *       <li class="selected"><a href="#2">link 2</a></li>
 *       <li><a href="#3">link 3</a></li>
 *       <li><a href="#4">link 4</a></li>
 *       <li><a href="#5">link 5</a></li>
 *   </ol>
 * </div> 
 *
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * @author: Gilad
 */
//should find a better solution for this global var...
var pageNavTimer = null;
(function($) {                                          // Compliant with jquery.noConflict()
    $.fn.jPageNav = function(o) {
        o = $.extend({
            effect: 'slide'
        }, o || {});
        
        return this.each(function() {                   // Returns the element collection. Chainable.
            this.container  = null;
            this.curr       = null;
            this.list       = null;
            this.buttonNext = null;
            this.buttonPrev = null;
            
            if ($(this).is('UL') || $(this).is('OL')) {
                this.list = $(this);
            }
            else {
                this.list = $(this).find('>ul,>ol,div>ul,div>ol');
            }
            this.container = this.list.wrap('<div class="pages-nav"></div>').parent();            
            
            //this.list = $(this).find('>ul,>ol,div>ul,div>ol');
            
            //find selected item index
            //--------------------------
            var listItems = $("li", this.list);
            var listSize = listItems.size();
            var selIndex=0;
            while (!listItems.eq(selIndex).hasClass('selected') && selIndex<listSize){
                selIndex++;
            }
            if (selIndex == listSize) selIndex = 0;
                        
            //create and add navigation
            //-------------------------
            var dropDiv = this.list.wrap('<div class="drop"></div>').parent();
            
            //selected item
            this.curr = this.list.before('<div class="selection"></div>').prev();
            if ($("li.selected", this.list).children().is('A'))
                this.curr.html($("li.selected", this.list).html());
            else
                this.curr.html($("li.selected", this.list).html());
            
            //prev button
            var prevBtnHtml = '';
            if (selIndex == 0) prevBtnHtml = "<div class=\"prev disabled\">&nbsp;</div>\n";
            else {
                var prevLink = $('li'+':eq('+(selIndex-1)+') a', this.list).attr('href');
                prevBtnHtml = "<div class=\"prev\"><a href=\""+prevLink+"\" title=\"Previouse\">&nbsp;</a></div>\n"; 
            }
            this.container.prepend(prevBtnHtml);
            this.buttonPrev = $(".prev", this.container);
            
            //next button
            var listItems = $("li", this.list).size();
            var nextBtnHtml = '';
            if (selIndex == listItems-1) nextBtnHtml = "<div class=\"next disabled\">&nbsp;</div>\n";
            else {
                var nextLink = $('li'+':eq('+(selIndex+1)+') a', this.list).attr('href');
                nextBtnHtml = "<div class=\"next\"><a href=\""+nextLink+"\" title=\"Next\">&nbsp;</a></div>\n"; 
            }
            this.container.append(nextBtnHtml);
            this.buttonPrev = $(".next", this.container);

            //hide the original list
            this.list.css('display', 'none');
            
            //add hover events
            //----------------
            dropDiv.hover(
			    function() {},
			    function() { $(this).find('>ul,>ol').slideUp('fast'); }
		    );
		    this.curr.hover(
                function() { $(this).parent().find('>ul,>ol').slideDown('fast'); },
                function(){}
		    )
        });
    };
})(jQuery);
