// JavaScript Document
jQuery.fn.fadeToggle = function(speed, easing, callback) { 
   return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};


jQuery.fn.filterList = function(opts) { 

	opts = jQuery.extend({
		recent:10,
		filterTab:".filterTab",
		filterItems:".toFilter",
		startWith:"recent",
		attribute:"name",
		fadeIn:true,
		callback:function(){return false;}
	},opts||{});
	

	var $items=$(this).children(opts.filterItems);
	var tgt = $(this).attr("id");

	//alert(opts['recent']+" "+opts['filterTab']+" "+opts['filterItems']+" "+opts['startWith']+" "+opts['attribute']+" "+tgt); // Alerts 2,22,30
	

	$(opts.filterTab).click(function(){
	//$(".filterTabPrevious").click(function(){
		//alert(opts['recent']+" "+opts.filterTab+" "+opts['filterItems']+" "+opts['startWith']+" "+opts['attribute']+" "+tgt); // Alerts 2,22,30

		$(opts.filterTab).removeClass("activeFilter");
		$(this).addClass("activeFilter");
 		//alert("? "+$(this).attr(opts.attribute));
 		showItems($(this).attr(opts.attribute));
		return false;
	});
	
	function showItems(toShow){
		$items.hide();
		
		if(toShow=="recent"||toShow=="previous"){
			 
			$("#"+tgt).children(opts.filterItems+":lt("+opts.recent+")").fadeIn(200);
	
		}else if(toShow=="all"){
			$items.fadeIn(200);
 
		}else{
 			$("#"+tgt).children(opts.filterItems+ "[name="+toShow+"]").fadeIn(200);
		}
		//alert(toShow);
		
		if(opts.fadeIn){
		}else{
		}
		
		return false;
	}
	
	showItems(opts.startWith);
	return false;
		
};



jQuery.fn.paginateList = function(opts) { 

	opts = jQuery.extend({
		itemsPerPage:10,
		btnHolder:"#btnHolder",
		startPage:0,
		btnLabel:"page",
		callback:function(){return false;}
	},opts||{});
 		
		var currentPage = opts.startPage;
		//opts.startPage = (!opts.startPage || opts.startPage < 0)?1:opts.startPage;
		opts.itemsPerPage = (!opts.itemsPerPage || opts.itemsPerPage < 0)?1:opts.itemsPerPage;
		opts.btnLabel = (!opts.btnLabel)?1:opts.btnLabel;

		var tgt="#"+$(this).attr("id");
		var items = $(this).children(".toPage");
		var totChildren = items.size();
		var totPages = numPages();
		
 		$(tgt).children(".toPage").hide();
 		$(tgt).children(".toPage:lt("+(opts.itemsPerPage)+")").show();
		
		//$("#feedback").append("<li>"+tgt+" "+totChildren+" "+totPages+"</li>");

		var pageButtons = "";
		
		var i;
		
		for(i=0;i<totPages;i++){
			pageButtons +="/ <a class=\"pageIcon\" href=\"#\" ><span>"+(i+1)+"</span> </a>";
		}
		
		if(numPages() > 1){
	
	//		pageButtons +="<a class=\"changePagePrev\" title=\"p\" href=\"#\" >/ <span>prev</span> </a>";
		//	pageButtons +="<a class=\"changePageNext\" title=\"n\" href=\"#\" >/ <span>next</span> </a>";
			
		
			$(opts.btnHolder).html(opts.btnLabel+pageButtons);		
			$(opts.btnHolder).children("a:eq("+(opts.startPage)+")").addClass("activeBlogFilter");
		}

//alert(tgt);

		$(opts.btnHolder).children("a.pageIcon").click(function(){
		 	var gotoPage = $(this).children("span").text()-1;
			var s = (opts.itemsPerPage*gotoPage)-1;
			var e = (s+opts.itemsPerPage)+1;
			showRange(s,e);
			$(opts.btnHolder).children("a").removeClass("activeBlogFilter");
			$(this).addClass("activeBlogFilter");
			currentPage = gotoPage;
		//	alert(s+" "+e);
			return false;
		});
		
		$(opts.btnHolder).children("a.changePageNext").click(function(){
				currentPage++;
				if(currentPage==totPages){
					currentPage=0;
				}
				var gotoPage = currentPage;
				var s = (opts.itemsPerPage*gotoPage)-1;
				var e = (s+opts.itemsPerPage)+1;
				showRange(s,e);
				//alert((gotoPage+1));
				$(opts.btnHolder).children("a").removeClass("activeBlogFilter");
				$(opts.btnHolder).children('*:contains("'+(gotoPage+1)+'")').addClass("activeBlogFilter");
				return false;
		});

		
		$(opts.btnHolder).children("a.changePagePrev").click(function(){
				currentPage--;
				if(currentPage<0){
					currentPage=totPages-1;
				}
				var gotoPage = currentPage;
				var s = (opts.itemsPerPage*gotoPage)-1;
				var e = (s+opts.itemsPerPage)+1;
				showRange(s,e);
				//alert((gotoPage+1));
				$(opts.btnHolder).children("a").removeClass("activeBlogFilter");
				$(opts.btnHolder).children('*:contains("'+(gotoPage+1)+'")').addClass("activeBlogFilter");
				return false;
		});
		
		function numPages() {
			return Math.ceil(totChildren/opts.itemsPerPage);
		}
		
		function showRange(a,b) {
			$(tgt).children(".toPage").hide();
		//	$(tgt).children(".toPage:lt("+b+")").filter(":gt("+a+")").show();
			if(a<0){
		 		$(tgt).children(".toPage:lt("+(opts.itemsPerPage)+")").fadeIn(200);
			}else{
				$(tgt).children(".toPage:lt("+b+")").filter(":gt("+a+")").fadeIn(200);
			}
		}
		
		
	 
};








//