$().ready( function() {
	
	$(".lightbox").lightbox();
	
	//menu();
	confirmacao();
	printing();
	target();
	zebra();
	ieFlickerFix();
	masks();
});

function confirmacao() {
	$('a.confirmacao').bind('click', function() {
		var title = $(this).attr('title');
		
		if (!confirm(title)) {
			return false;
		}
		else return true;

		return false;
	});
}


function printing() {
	$('.printit').click( function() {
		window.print();
	});
}


function target() {
	$("a[rel$='external'], .external").each( function(){
		$(this).attr('target','_blank');
		$(this).attr('title', $(this).attr('title')+' (este link abre em nova janela)');
	});
}


function zebra() {
	$('table:not(.nozebra) tr:nth-child(odd)').addClass('par');
}


function ieFlickerFix() {
	try {
	  document.execCommand("BackgroundImageCache", false, true);
	} catch(err) {}
}


function masks() {

	$('.mask-date').mask('99/99/9999',{placeholder:' '});
	
	$('.mask-phone').mask('(99) 9999-9999',{placeholder:' '});
	
	$(".mask-cep").mask('99999-999',{placeholder:' '});

	//$('.mask-cpf').mask('999.999.999-99',{placeholder:" "});
	//$('.mask-cnpj').mask('99.999.999/9999-99',{placeholder:" "});
	$('.mask-rg').mask('999.999.9999',{placeholder:" "});

	$('.mask-inscr').mask('999/9999999',{placeholder:" "});
}


function affectedFields(form,el) {
	var classname = 'affected';
	$(form+' *').each( function() {
		if (el!='') {
			if ($(this).attr('id')==el) {
				$(this).focus().addClass(classname);
			} else {
				$(this).removeClass(classname);
			}
		} else {
			$(this).removeClass(classname);
			$(form).resetForm();
		}
	});
	return false;
}


function get(el) {
  return document.getElementById(el);
}


function limiter(el, max) {
	$(el).bind('keypress', function() {
		var valor = $(this).attr('value');
		valor = valor.substr(0,max);
		if (valor > max) {
			$(this).attr('value',valor);
		}
	});
}

////////////////// MENU ///////////////////////////////////////////////////////////////////////////

/* Preload Menu */
/*
var preloaded = new Array();
function preload_images() {
    for (var i = 0; i < arguments.length; i++){
		preloaded[i] = document.createElement('img');
		preloaded[i].setAttribute('src',arguments[i]);
    };
};

preload_images(
	'themes/default/images/menu_links.gif',
	'themes/default/images/menu_hover_cidade.gif',
	'themes/default/images/menu_hover_governo.gif',
	'themes/default/images/menu_hover_secretarias.gif',
	'themes/default/images/menu_hover_cultura.gif',
	'themes/default/images/menu_hover_polo.gif',
	'themes/default/images/menu_hover_publicacoes.gif',
	'themes/default/images/menu_seta.gif'
);
*/

/* Menu */

function menu() {
	
	$('#menu>ul>li').hover(
		
		function start() {

			// aplica o hover, aplicando a classe, e procura um submenu
			$(this).addClass('affected').find('ul.sub').each( function() {
				
				/*
				$(this).animate({
					opacity: 0
				}, 0 );
				*/
				
				// iniciando o hover...
				$(this).hover( 
					
					function() {

						/*
						$(this).animate({
							opacity: 1
						}, 250 );
						*/

						// primeiro exibe o submenu através da classe, e então busca os itens da lista
						$(this).addClass('affected').find('>li').each( function() {

							// se os sub itens estiverem em hover, então...
							$(this).hover(

								function() {

									/////////////////////////////////////////////////////////////////////////////////////////////////
									// aplica o hover, aplicando a classe, e procura um submenu
									$(this).addClass('affected').find('ul.sub2').each( function() {
										
										// iniciando o hover...
										$(this).hover( 
											
											function() {

												// primeiro exibe o submenu através da classe, e então busca as itens da lista
												$(this).addClass('affected').find('>li').each( function() {

													// se os sub itens estiverem em hover, então...
													$(this).hover(

														function() {

															// ... vê se ele item um submenu derivado e exibe
															$(this).find('ul').each( function() {
																$(this).css('display','block');
															});								
															
														},

														function() {

															$(this).find('ul').each( function() {
																$(this).css('display','none');
															});

														}

													);
												
												});
												
											}, 
											
											function() {
												
											}
										
										);
									
									});
									///////////////////////////////////////////////////////////////////////////////////////////////

									// ... vê se ele item um submenu derivado e exibe
									$(this).find('ul').each( function() {
										$(this).css('display','block');
									});
									
								},

								function() {

									$(this).find('ul').each( function() {
										$(this).css('display','none');
									});

								}

							);
						
						});
						
					}, 
					
					function() {
						
						/*
						$(this).animate({
							opacity: 0
						}, 250 );
						*/
					}
				
				);
			
			});
		
		},
		
		// faz alguma coisa quando o menu sair de hover
		function end() {
			$(this).removeClass('affected');
		}
	
	);
	return false;
}

///////////////////////////////////////////////////////////////////////////
