var reAbas = /planos\.|materias\.|coletaneas\.|videos\.|roteiros\.|jogos\.|animacoes\.|fotos\./;
if(qs.search(reAbas) == 0) abaTipo = qs.match(reAbas);
else if(!abaTipo || abaTipo =='') abaTipo = 'planos.';
var navTitulo = "";
var navLink = "";
$(function(){
	$("#Div_Indice .tabs li").click(function(){
		$("#Div_Indice .tabs li").each(function(i){
			$(this).removeClass('sel');
		});
		$(this).addClass('sel');
		abaTipo = $(this).attr('name');
		filtraLista(navTitulo,navLink);
	});
	
	$('.nivel2').each(function(index){
		$(this).children('li:last-child').addClass('last');
	});
});

// configura conteudo plano de aula
planos = {
	hits: 5,
	actualPage:0,
	mydivs: "<div class=\"pag[numero] divisao \">[pag]</div>",
	myPaginate: "<div class=\"paginacao\">"+
		        "<a href=\"javascript:void(0);\" class=\"primeira\" title=\"primeira página\" onclick=\"planos.showHide('first', '[divisao]')\">|&lt;</a> "+
		         "<a href=\"javascript:void(0);\"  class=\"anterior\" title=\"página anterior\" onclick=\"planos.showHide('prev', '[divisao]')\">&lt;</a>"+
		        "<span class=\"paginas\">Página"+
		        "<span class=\"atual\"> 1 </span>"+
		        "de [total]"+
				"</span>"+
		        "<a href=\"javascript:void(0);\"  class=\"proxima\" title=\"Próxima página\" onclick=\"planos.showHide('next', '[divisao]')\">&gt;</a>"+
		        "<a href=\"javascript:void(0);\" class=\"ultima\" title=\"Última página\" onclick=\"planos.showHide('last', '[divisao]')\">&gt;|</a>  "+
		    	"</div>",
	geraNumeros: function(){
		var myDiv = $(divisao + " .divisao");
		myDiv.hide();
		myDiv.eq(0).show();
		count = myDiv.size();
		container = "";
		temp = planos.myPaginate;
		container = temp	.replace(/\[total\]/g, count)
							.replace(/\[divisao\]/g, divisao);
		$("#display-files").append(container);
		
	},
	showHide: function(n, d){
		alvo = $(d + " .divisao");
		atual = $(d + " .divisao:visible");
		alvo.hide();
		pgAtual = parseInt(atual.attr("class").match(/pag[0-9][0-9]?/).toString().replace(/pag/,""));
		if(n == "next" && (pgAtual) < alvo.size()){
			pgAtual++;
		}else if(n == "prev" && (pgAtual) > 1){
			pgAtual--;
		}else if(n == "last"){
			pgAtual = alvo.size();	
		}else if(n == "first"){
			pgAtual = 1;
		};	
		$(d).find(".pag" + pgAtual.toString()).show();
		$(d).find(".atual").html(" " +pgAtual+ " ")
		
	},
	paginate: function(plan){
		plan = $(plan).find('.listagem-item');
		total = plan.size();
		countPage=0;
		txt = "";
		txtDiv ="";
		mypage = 0;
		hits = planos.hits;
//		divisao = inside.myclass;
		if(total>hits){
			$.each(plan, function(i , val){
				if(countPage < hits){
					txt +=  "<div class=\"listagem-item\">" + $(this).html() + "</div>";
					countPage++;
				} else{

					mypage++;
					txtDiv	+= planos.mydivs	.replace(/\[pag\]/g, txt)
												.replace(/\[numero\]/g, mypage)
										//		.replace(/\[divisao\]/g, divisao)
												;
					txt = "";
					countPage=0;
				}
			});			
		} else{
			$.each(plan, function(i , val){
				txt +=  "<div class=\"listagem-item\">" + $(this).html() + "</div>";
			});
			txtDiv	+= planos.mydivs	.replace(/\[pag\]/g, txt)
										.replace(/\[numero\]/g, mypage)
//										.replace(/\[divisao\]/g, divisao)
										;
		};
		divisao = "#display-files";
		$(divisao).html(txtDiv);
		planos.geraNumeros(divisao);
	}	
}

configurePlanos = function(){
	$("#display-files .listagem-item").mouseover(
		function(){
			$(this).css({"background-color":"#efefef"})	
		}
	).mouseout(
		function(){
			$(this).css({"background-color":"#fff"})				
		}
	);	
	
	n = $(".item-aba");
	$.each(n, function(){
		inside = $(this).find(".listagem-item");
		findId = $(this).attr("class").match(/id-.+\w/g).toString();
		inside.myclass = findId;
		if(inside.size() == 0){
			$("#"+findId).hide()
		}else{
			planos.paginate($('.listagem-item'));		
		};
	});
	// conmfigura o menu
	alvo = $("#Div_Indice .sel").attr("id");
	$(".item-aba").hide();
	$("."+alvo).show();
	atualBot = $("#Div_Indice .tabs li");
	atualBot.click(
		function(){
			atualBot.removeClass("sel");		
			$(this).addClass("sel");
			$(".item-aba").hide();
			actual = $(this).attr("id");
	
			$("."+actual).show();
		}
	)
}

var filtraLista = function(title,lnk){
	lnk = lnk.replace('/indice-','/lista-');
	auxLk = lnk.split('?');
	if(auxLk.length > 1)
		lnk = auxLk[0] +'?'+abaTipo+ auxLk[1];

	$.ajax({
	url: lnk,
	beforeSend: function() {
	$("div#display-files").html('<br/><br/><strong><center><img src="/css/imagens/loading.gif"/> Carregando</center></strong>');
	}, //beforeSend
	success: function(d) {

	$("#path-planos").html(title);
	if($("#Div_Indice").css("display") == "none") $("#Div_Indice").slideDown("slow");
	$("div#display-files").html(d);
//	$("div#display-files").html(d.find("#conteudo-centro .listagem-item").html());
	planos.paginate($("#display-files"));
	
	},//success
	dataType: "html",
	complete: function() {
		
	},//complete
	error: function() {
	$("div#display-files").html('<br/><br/><strong><center>Erro! Não foi possível carregar esta página</center></strong>');
	},//error
	timeout: function() {
	$("div#display-files").html('<br/><br/><strong><center>Tempo excedido! Tente novamente</center></strong>');
	}//timeout
	
	});//ajax

};

var toggleSubnivel = function(oLI){
	if($(oLI).parent("ul").attr("class") == "nivel1") {
		$(oLI).siblings("li").children(".nivel2 li").children(".nivel3").hide();
		$(oLI).siblings("li").children(".nivel2").hide();
		$(oLI).find(".nivel2").toggle();
		$(oLI).find(".nivel3").hide();
	} else if($(oLI).parent("ul").attr("class") == "nivel2") $(oLI).find(".nivel3").toggle();
};

var carregaQuery = function(query){
	if(query != "") {
		if(query.search(reAbas) == 0) query = query.replace(query.match(reAbas),"");
		var re = new RegExp(query+'$');
		$("#browser a").each(function(index){
			if($(this).attr('href').match(re)) {
				navTitulo = $(this).attr('name');
				navLink = $(this).attr('href');
				filtraLista(navTitulo, navLink);
				toggleSubnivel($(this).parent("li"));
				return false;
			}
		});
		$("#Div_Indice .tabs li").each(function(i){
			if($(this).attr("name") == abaTipo) $(this).addClass("sel");
			else $(this).removeClass('sel');
		});
	}
};

