$(document).ready(function(){
	$(".personTab:eq(0)").addClass('current');

	$(".personPanel").not(".personPanel:eq(0)").hide();

	$("div.personTab").click(function(){
		if(! $(this).is(".current")){
			var ind = $(".personTab").index(this);
			
			$(".personPanel").hide();
			$(".personPanel:eq("+ind+")").fadeIn();
			$("div.personTab").removeClass('current');
			$(this).addClass('current')
		}
		return true;
	});

	try{
		$("#roleSelect option:eq(0)").attr({'selected':'selected'});
	}
	catch(err){ }

	$("#roleSelect").bind('change',function(){
		var optInd = $(this).val();
		$(".rolePanel").hide();
		$(".rolePanel:eq("+optInd+")").fadeIn();
		return true;
	});
	
	
	
	//pagination
	$("div.rolePanel").each(function(){
		var 
			o = $(this),
			rowSelector = "div.indWork",
			rowCount = o.find(rowSelector).size(),
			perPage = 20,
			numPages = Math.ceil(rowCount/perPage),
			divLinks = [];
		
		(function(){ //INIT PAGINATION
			for(var i=0;i<numPages;i++){
				divLinks.push("<a href='#' class='pageNum'>"+(i+1)+"</a>");
			}

			o.find("div.currRows").html("1 - 10 of "+rowCount);	

			o.find("div.pages").html(divLinks.join(""));
			
			o.find(rowSelector).hide().filter(":lt("+perPage+")").kAnim("appear");

			o.find("a.pageNum:eq(0)").addClass("current");

			return true;
		})();
		
		//binding
		o.find("a.pageNum").click(function(){
			var 
				aInd = o.find("a.pageNum").index(this),
				gte = aInd * perPage,
				lt = gte + perPage,
				page = $.trim( $(this).text() ) - 1;
			
			
			if(lt > rowCount){
				lt = rowCount;
			}
			if(! o.find(rowSelector).eq(gte).is(":visible")){
				o.find(rowSelector).hide().slice(gte,lt).fadeIn();
			}
			$(this).siblings("a.pageNum").removeClass("current");
			$(this).addClass("current");
			$("div.currRows").html( (gte+1)+" - "+lt+" of "+rowCount );
			
			(function excessPageNumClear(){
				var 
					nPageNums = 5,
					startPageNums = (function(){
						var 
							p = (page) - Math.floor(nPageNums / 2);
						
						if(p < 0){
							p = 0;
						}
						
						return p;
					})(),
					endPageNums = startPageNums + nPageNums - 1 ;

				if( endPageNums >= numPages){
					endPageNums = numPages - 1;
					startPageNums = endPageNums - nPageNums + 1;
				}
				o.find("a.pageNum").hide().filter(":lt("+(endPageNums + 1)+")"+ ( startPageNums  <= 0 ? "" : ":gt("+ (startPageNums-1) +")")  ).show();
				if(numPages == 1){
					o.find("a.pageNum").hide();
				}
				
			})();
			
			return false;
		});
		
		o.find("a.pageNum:eq(0)").trigger("click");
		
	});
	
	
	$("input.workFilter").preVal({"inputVal":"Filter Works"}).keyup(function(){
		var 
			currPanel = $(this).parents(".rolePanel"),
			rows = currPanel.find("div.indWork"),
			str = $(this).val(),
			lStr = str.toLowerCase(),
			counter = 0,
			currTop = currPanel.find("rolePanelTop"),
			hideAbles = currPanel.find("div.currRows").add( currPanel.find("div.pages") );
			
		
		if(str == ""){
			hideAbles.css({"visibility":"visible"});
			currPanel.find("a.pageNum").eq(0).trigger("click");
			
			return false;
		}

		//make nav gone		
		hideAbles.css({"visibility":"hidden"});
		
		rows.each(function(i){
			var 
				o = $(this);
			if( o.text().toLowerCase().indexOf(lStr) == -1 ){
				o.hide();
			} else{
				o.show();
				counter++;
			}
		});
		
		return true;
	});
	
});