// JavaScript Document
$(document).ready(function() {

    //Funcões Básicas
	paginate();
	
	$('a.print').click(function() {
		imprime('container');
		return false;
    });
	
	
    $('a.blank').click(function() {
        $(this).attr("target", "_blank");
    });

    $('a.self').click(function() {
        $(this).attr("target", "_self");
    });

    $('a.parent').click(function() {
        $(this).attr("target", "_parent");
    });

	/* Limpa o Input ao clicar */
    $('input:text').focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
            if ($(this).hasClass("password")) {
                $(this).attr("type", "password");
            }
            $(this).bind("blur", function() {
                if ($(this).val() == '') {
                    if ($(this).hasClass("password")) {
                        $(this).attr("type", "text");
                    }
                    $(this).val($(this).attr("title"));
                }
            });
        }
    });
	
	/* edições anteriores */
	$('a.edicoesAnteriores').click(function() {
		$('#comboEdAnt').slideToggle(400);
	});
});

function paginate() {
	
	var pages = $('.text-container');
	if (pages.length > 0) { 
		init();
	}
	
	function init() {
		
		var htmlNav = nav(0);
		
		pages.hide();
		pages.filter(':first').append(htmlNav).show();
		
		pages.find('.prev').live('click', function() {
			var page = $(this).attr('page');
			page = parseInt(page) - 1;
			goTo(page);
		});
		
		pages.find('.next').live('click', function() {
			var page = $(this).attr('page');
			page = parseInt(page) + 1;
			goTo(page);
		});
	
	}
	
	function nav(page) {
	
		pages.find('.pagination').remove();
		
		var html = '';
		var htmlPrev = '<a page="' + page + '" class="prev" href="#main">' + prev + '</a>';
		var htmlNext = '<a page="' + page + '" class="next" href="#main">' + next + '</a>';
		
		if (page > 0) html += htmlPrev;
		if (page < (pages.length-1)) html += htmlNext;
		
		htmlWrapped = '<div class="pagination">' + html + '</div>';
		return htmlWrapped;
	
	}
	
	function goTo(page) {
		pages.hide();
		var htmlNav = nav(page);
		pages.filter(':eq(' + page + ')').append(htmlNav).show();
	}
	
}