//20110711:Tony:v0.01 Change next/prev arrow to text.
//20110608:Tony:v0.00a:Fix bug on total page calculation (by adding 0.5 to the round function)
//20110408:Tony:Add call back method
//20110408:Tony:Changed name from li_paging to livePaging
(function($){
    $.fn.extend({
        //plugin name - animatemenu
        livePaging: function(options) {
 
            var defaults = {
                items_per_page: 8,
				index_id: 'page_index',
            };
             
            var options = $.extend(defaults, options);
         
            return this.each(function(){
				var o = options; //alert(o.select_ex);
				var panel = $(this); //alert(panel.attr('id'));
				var items = panel.find('div > ul > li'); //alert(items.length);
				///////////Prepare page index
				//if(total_pages>1){
				if(items.length>o.items_per_page){
					//Add page id to each item
					var total_pages = Math.round(items.length/o.items_per_page+0.5); //alert(total_pages);
					items.each(function(){
						var index = $(this).index();
						var page_id = Math.round(index/o.items_per_page-0.5);
						var pos_id = index % o.items_per_page;
						$(this).addClass('id-'+index + ' page-' + page_id + ' pos-'+pos_id);
					});
					var st='';
					for(i=0;i<total_pages;i++){
						st+="<li class='page-" + i + "' rel='" + i + "'>" + (i+1) + '</li>';
					}
					st = "<li id='prev'></li>"+st+"<li id='next'></li>";
					/////////Generating page index
					var div = document.createElement('div');
					var page_index = $(div);
					page_index.attr('id',o.index_id);
					panel.append(page_index);
					page_index.html('<ul>'+st+'</ul>');
					//page_index.find('ul > li#prev').html("<img src='"+o.image_prev_button+"'/>");
					//page_index.find('ul > li#next').html("<img src='"+o.image_next_button+"'/>");
					page_index.find('ul > li#prev').html('<');
					page_index.find('ul > li#next').html('>');
					page_index.find('ul > li').each(function(){
						//$(this).attr('xx','xx');
						$(this).click(function(){
							set_active_page($(this).attr('rel')); //alert(selected_page_id);
						});
					});
					//page_index.find('ul > li#prev').click(function(){
					//	var active_page = parseInt(page_index.find('ul > li.active').attr('rel')); alert(active_page);
					//});
					//page_index.find('ul > li#next').click(function(){
					//});
					/////////Set default active page to 0
					//fix_ie_middle();
					set_active_page(0);
				}
				
				if(o.callBack) o.callBack();
				
				function set_active_page(page_no){ page_no = parseInt(page_no);
					var li_prev = page_index.find('ul > li#prev'); if(page_no==0) li_prev.hide(); else li_prev.show();
					var li_next = page_index.find('ul > li#next'); if(page_no==total_pages-1) li_next.css('opacity',0); else li_next.css('opacity',1);
					items.hide();
					panel.find('div > ul > li.page-'+page_no).show();
					page_index.find('ul > li').removeClass('active');
					page_index.find('ul > li.page-'+page_no).addClass('active');
					li_prev.attr('rel',page_no-1);
					li_next.attr('rel',page_no+1);
				}
				/*
				function fix_ie_middle(){
					var li_prev = page_index.find('ul > li#prev');
					var li_next = page_index.find('ul > li#next');
					var li_height = li_prev.height();
					var img_height = li_next.children('img').height();
					var top = (li_height - img_height)/2;
					//alert(li_next.height());
					//alert(li_next.children('img').height());
					//alert('xx');
					//li_prev.children('img').css({position:'relative', top:top+'px'});
					//li_next.children('img').css({position:'relative', top:top+'px'});
					li_next.css('padding-top',top+'px');
				}
				*/
            });
        }
    });
})(jQuery);

