// JavaScript Document
$(document).ready( function(){
	
	$("#galeria a").lightBox();
	
	// Books
	$(".book").click( function(){
		$('.details').css('display','none');
		$(this).parent().find('.details').slideDown('fast');
		return false;
	});
	
	// Scroll Home
	interval();
	$(".prevNext .next").click( function(){
		nextHighlight(this);
		clearInterval(intervalId);
		interval();
		return false;
	});
	$(".prevNext .prev").click( function(){
		var ul = $(this).parent().parent().find("ul:first");
		if ( ul.find("li:last").is(':visible') ){
			ul.find("li:visible").css('display','none').prev().fadeIn('slow');
		} else {			
			ul.find("li:visible").css('display','none');
			ul.find("li:last").fadeIn('slow');
		}
		clearInterval(intervalId);
		interval();
		return false;
	});
							
	// Inputs
	$("#cnpj").mask('99.999.999/9999-99');
	$("#cep").mask('99.999-999');
	$("#tels, #tel, #tel2, #tel3, #tel4").mask("(99)9999-9999");
	
	// Lightbox
	$(".subBoxContent a").lightBox({
		imageBtnPrev:	'img/left-arrow-2.jpg',
		imageBtnNext:	'img/right-arrow-2.jpg',
		txtImage: 'Imagem',
		txtOf: 'de'
	});
	
	// Boxes
	$(".boxHead").click( function(){
		if ( $(this).find('a:first').text() == 'Expandir' ){
			$(this).find('a:first').parent().parent().nextAll('.boxContent').slideDown('fast');
			$(this).find('a:first').text('Recolher');
		} else {
			$(this).find('a:first').parent().parent().nextAll('.boxContent').slideUp('fast');
			$(this).find('a:first').text('Expandir');
		}
	});
	$(".boxContent p").click( function(){
		if ( $(this).find('.expand:first').text() == 'Expandir' ){
			$(this).find('.expand:first').parent().parent().nextAll('.subBoxContent').slideDown('fast');
			$(this).find('.expand:first').text('Recolher');
		} else {
			$(this).find('.expand:first').parent().parent().nextAll('.subBoxContent').slideUp('fast');
			$(this).find('.expand:first').text('Expandir');
		}
	});
	$("#expandAll").click( function(){
		if ( $(this).text() == 'Expandir tudo' ){
			$('.boxContent').slideDown('fast');
			$(".boxExpand a").text('Recolher');
			$('.subBoxContent').slideDown('fast');
			$(".expand").text('Recolher');
			$(this).text('Recolher tudo');
		} else {
			$('.boxContent').slideUp('fast');
			$(".boxExpand a").text('Expandir');
			$('.subBoxContent').slideUp('fast');
			$(".expand").text('Expandir');
			$(this).text('Expandir tudo');
		}
	});
							
	// Scrolls
	$(".scrollList .prev a").click( function(){
		$(".scrollList .scroller li:last").remove().prependTo(".scrollList .scroller ul");
		$(".scrollList .scroller li:first").animate({ 
			marginLeft : '+=125px'
		} , 'fast' , '' , function(){ $(this).css('margin-left','15px') });
	});
	$(".scrollList .next a").click( function(){
		$(".scrollList .scroller li:first").animate({ 
			marginLeft : '-=125px'
		} , 'fast' , '' , function(){
			$(this).remove().css('margin-left','15px').appendTo(".scrollList .scroller ul");
		});
	});
	
	// Tab index
	// $("a").each( function(i){ $(this).attr('tabindex',i+1); });
	
	// Bug Fix
	if($.browser.webkit){
		$("#searchInput").css('margin-top','0px');
	}
	
	// Accesskey
	$("*[accesskey]").each(function(i){
		title = $(this).attr('title');
		if ( $.browser.msie || $.browser.webkit ){
			$(this).attr('title', title + ' [alt+' + $(this).attr('accesskey') + ']');
		} else if ( $.browser.opera ){
			$(this).attr('title', title + ' [shift+esc+' + $(this).attr('accesskey') + ']');
		} else if ( $.browser.mozilla ){
			$(this).attr('title', title + ' [alt+shift+' + $(this).attr('accesskey') + ']');
		}
	});
});

function nextHighlight(element){
	var ul = $(element).parent().parent().find("ul:first");
	if ( $(element).parent().parent().attr('id') === 'homeHighlight' ){
		if ( $("#homeHighlight li:last").is(':visible') ){
			$("#homeHighlight li:visible").css('display','none');
			$("#homeHighlight li:first").fadeIn('slow');
		} else {
			$("#homeHighlight li:visible").css('display','none').next().fadeIn('slow');
		}
	} else {
		if ( ul.find("li:last").is(':visible') ){
			ul.find("li:visible").css('display','none');
			ul.find("li:first").fadeIn('slow');
		} else {
			ul.find("li:visible").css('display','none').next().fadeIn('slow');
		}
	}
}

function interval(){
	intervalId = setInterval(function(){
		nextHighlight( $(".prevNext .next") );
	} , 7000 );
}
