/***
 *
 *  @author Małgorzata Wierzbicka biedronne@gmail.com
 *  @usage: $(_selector_css_).paginate();
 *  @desc plugin jQuery - paginowanie artykułów:  w miejscu oznaczonym <ins></ins> wewnatrz DOMElementu odpowiadajacemu _selector_css_ dzieli strony i generuje odpowiednio paginację (do własnego ostylowania CSSami) 
 *   
 ***/  
(function($){

    $.fn.paginate = function(){
		return this.each(function(){
			var root = $(this);
			if (!root.is("[paged]")){
				var playground = $("<div style='position:absolute;display:none'></div>").appendTo("body"),
					pageHTML = '<div class="tab_page"></div>',
					pCont = '<ul class="pagination"></ul>',
					pElem = '<li><a href="#" tab="',
					pArrowNext = '<li><a href="#">&raquo;</a></li>',
					pArrowPrev = '<li><a href="#">&laquo;</a></li>',
					pArrowNext2 = '<a class="right" href="#"></a>',
					pArrowPrev2 = '<a class="left" href="#"></a>'
				
				root.children('ins').each(function(i){
					$(this).attr("id","ins_"+i);
				});
				var pages_total = root.children('ins').length; 
				if (!root.children().filter(':not(ins)').filter(':not(br)').filter(':not(hr)').filter(':not(img)').length) {
                    // no valid children elements (plain text probably) - we only work with html nodes
                    try {
                        console.log('paginate fail: no valid children elements (plain text probably) - we only work with html nodes')
                    }catch(e){}
                    return false; 
                }
                if (!pages_total) { return false; }
				try {
					for (var i=pages_total; i>0; i--){
						var page = $(pageHTML).prependTo(playground);
						
						page.html($("#ins_"+(i-1)+" ~ *")).attr('tab',i);
					}// treat the leftovers as first page
					var page = $(pageHTML).prependTo(playground);
					page.html(root.children()).attr('tab',0);
					root.html(playground.html())
					playground.remove();
				}
				catch(e){}
				
				var allPages = $(".tab_page", root).hide(), curPage = allPages.eq(0).show();
				
				// add pagination
				var pagination = $(pCont).appendTo(root), curTab = 0;
				for (var i=0; i<=pages_total; i++){
					$(pElem+i+'">'+(i+1)+'</a></li>').appendTo(pagination);
				}
				
				// make it work
				pElems = $("a", pagination).click(function(e){
						e.preventDefault();
						var elem = $(this);							
						if (!elem.parent().is('.active')) {
							var tab = elem.attr('tab');
							changePage(tab);
						}
					});
				pElems.eq(0).parent().addClass('active');
				
				// add fucking arrows 
				var next = $(pArrowNext).appendTo(pagination).click(function(e){
					e.preventDefault();
					changePage(parseInt(curTab,10)+1);
				}), 
					prev = $(pArrowPrev).prependTo(pagination).hide().click(function(e){
					e.preventDefault();
					changePage(curTab-1);
				});
					
					// add more fucking arrows 
				var next2 = $(pArrowNext2).prependTo(root).click(function(e){
					e.preventDefault();
					changePage(parseInt(curTab,10)+1);
				}), 
					prev2 = $(pArrowPrev2).prependTo(root).hide().click(function(e){
					e.preventDefault();
					changePage(curTab-1);
				});
				
				var changePage = function(tab){
					
					pElems.parent().removeClass('active');
					pElems.eq(tab).parent().addClass('active');
					allPages.hide().filter("[tab='"+tab+"']").eq(0).show();
					curTab = tab;
					
					if (tab == 0) {
						// hide 'prev'
						prev.hide();
						prev2.hide();
					}
					else {
						prev.show();
						prev2.show();	
					}
					if (tab == pages_total) {
						// hide 'next'
						next.hide();
						next2.hide();
					}
					else {
						next.show();
						next2.show();	
					}
					
				};
				root.attr('paged',true);
			}
		});
	};
	$(document).ready(function(){
        $('.paginate').paginate();
    })
})(window.jQuery);

