//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').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);
					$("#VIDEO").append('<div class="video-show-more"><ul><li rel="16">Show more</li></ul></div>');
					get_video();			
				}
				
				if(o.callBack) o.callBack();
				function get_video(){
					$(".video-show-more li").click(function(){
						var page = parseInt($(this).attr("rel"));
						$("#VIDEO").empty();
						$.get("/2012/templates/ajax_sup2011_v0/render_video_list.ajax.php", { db: "sup", limit: "100" },
						function(data){
							$("#VIDEO").append(data).livePaging({
								items_per_page:page,
								image_prev_button:'/2011/img/arrow_back.gif',
								image_next_button:'/2011/img/arrow_readmore.gif',
								callBack:function(){
									$("#VIDEO .video-show-more ul").empty().append('<li rel="'+(page+8)+'">Show more</li>|<li><a href="http://star-board-sup.com/2012/news/" target="_blank" > Go to Archive</a></li>');
									get_video();
									$("#VIDEO .item_list ul").highLight();
									$("#VIDEO .item_list").css("height","100%");
								}
							});
						});
					});	
				}
				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);

