/*
 *
 * jQuery pager Plugin
 * http://delicious-factory.com
 *
 */
 

(function($) {

   $.extend({

       deliciousPager : new function(){


		this.construct = function(options) {
	
			var defaults = {
				pageSize: 10,
				currentPage: 1
			};
			
			var options = $.extend(defaults, options);

			$rand = Math.ceil( Math.random() * 10000 );
			
			this.after('<div class="jQ-delicious-pager" id="pager-' + $rand + '"></div>');
			
			$pager = $('#pager-' + $rand );
		    
			$pageSize = options.pageSize;
			$page     = 1;
			$i        = 0;
			
			this.children('*').each(function(e){
				
				$i++;
				
				$this = $(this);
												
				$this.addClass('pager-' + $rand + '-' + Math.ceil( $i / $pageSize ) + ' pager-' + $rand );
								
				if( $i > $pageSize ){
				
					$this.hide();
				
				}
				
				
			 });
			 
			 
			 $pages = Math.ceil( $i / $pageSize );
				
			 $pager.text('');
			 
			 for( $p = 1 ; $p <= $pages ; $p++ ){
			 
				link = $('<a href="javascript:void()">' + $p + '</a>').data({
				
					'p' : $p,
					'rand' : $rand
					
				}).click(function(){
				
					showPage( $(this).data('p') , $(this).data('rand') );
					
				});
				
				$pager.append( link );
				$pager.append('&nbsp;');
				
			 
			 }
			 		 
			 
			 return this;

		};
		
		
		function showPage( $pa , $rand ){ 
						
			$('.pager-' + $rand).hide();
			$('.pager-' + $rand + '-' + $pa ).show();
			
			
		}

          return this;

       }

   });

   /**

     * Extending jQuery fn

    */

   $.fn.extend({

       deliciousPager: $.deliciousPager.construct

   });

})(jQuery);
