$(document).ready(function(){
	
	// LINKS EXTERNOS
	$("a[rel=externo]").attr('target','_blank');
	
	// USE HAND CURSOR
	$("input[type=submit]").css("cursor","pointer");
	$("input[type=button]").css("cursor","pointer");
	
	// AREAS DE ATIVACAO DO LIGHTBOX
	$('a[rel*=lightbox]').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: 	'img/loading.gif',
		imageBtnClose: 	'img/close.gif',
		imageBtnPrev: 	'img/prev.gif',
		imageBtnNext: 	'img/next.gif',
		containerResizeSpeed: 300,
		txtImage: 'Imagem',
		txtOf: 'de'
	});
	
	// TIKER NEWS INDEX
	$('#destaques-home > ul').cycle({ 
	    speed:       600, 
	    timeout:     5000, 
	    pager:      '#nav-destaques-home', 
	    pagerEvent: 'mouseover', 
	    pauseOnPagerHover: true
	});
	
	//CONTROLADOR DO TAMANHO DAS FONTES
	if($('#font')){
		$("a#font-small").click(function(){
			$('.texto-editor').css('font-size', '10px');
		});
		$("a#font-normal").click(function(){
			$('.texto-editor').css('font-size', '11px');
		});
		$("a#font-big").click(function(){
			$('.texto-editor').css('font-size', '16px');
		});
		$("a#font-up").click(function(){
			var currentFontSize = $('.texto-editor').css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize, 10);
			if(currentFontSizeNum<18){
				var newFontSize = currentFontSizeNum*1.1;
				$('.texto-editor').css('font-size', newFontSize);
			}
		});
		$("a#font-down").click(function(){
			var currentFontSize = $(".texto-editor").css('font-size');
			var currentFontSizeNum = parseFloat(currentFontSize);
			if(currentFontSizeNum>10){
				var newFontSize = currentFontSizeNum*0.9;
				$('.texto-editor').css('font-size', newFontSize);
			}
		});
	}
	
	// BT BACK 
	$(".bt-back").click(function(){
		history.back();
	});
	// BT TOPO
	$(".bt-topo").click(function(){
		$(document).scrollTo( {top:'0px',left:'0px'}, 200 );
	});
	
	// FLASH HEADER
	$('#header').flash({
	    src: 'swf/header.swf',
	    width: '100%',
	    height: 327,
		wmode: 'transparent',
		expressInstall: true,
		flashvars: { pagina:$('#header').attr('rel')}
	});
	
	// ATIVAS JANELAS MODAL
	$(".dialog-box").dialog({
		autoOpen: false,
	    modal: true,
		resizable: false,
		width: 450,
		height: 410,
	    overlay: {
	        opacity: 0.5, 
	        background: "black"
	    }
	});
	
	// BOTOES PARA JANELAS MODAL
	$(".dialog-bt").click(function(){
		var rel 	= $(this).attr('rel');
		var Title 	= $(this).attr('title');
		$(rel).dialog({ title: Title});
		$(rel).dialog("open");
		$(".ui-dialog-overlay").click(function(){
			$(rel).dialog("close");
		});
	});	

	
	// MOSTRA VIDEOS
	$("#ativa-historia-banda").click(function(){		
		$.ajax( {
			type: "POST",
			url: "incs/historia-banda.php",
			beforeSend: function() {
				$("#alvo-ajax").html("<img src=\"img/loader.gif\" />");
			},
			success: function(txt) {
				$("#alvo-ajax").html(txt);
			},		
			error: function(txt) {
				alert('Erro: '+txt);
			}
		});
	});
	
	//DIALOGS
	$(".close-dialog").click(function(){
		$(".dialog").fadeOut(500);
	});
	$("#form-banda-cadastrada").submit(function(){
		$(".dialog").fadeOut(500);
	});

	$("#ativa-cadastro-bandas-dialog").click(function(){		
		
	});
	
	$("#ativa-dialog-recado").click(function(){		
		$("#dialog-recado").fadeIn(500);
	});
	$("#ativa-dialog-banda-cadastrada").click(function(){		
		$("#dialog-banda-cadastrada").fadeIn(500);
	});
});

/**
   * Valida form
   **/
function validaForm(form) {
    for (var i = 0; i < document.getElementById(form).elements.length; i++) {
        
        var nomeObj = document.getElementById(form).elements[i].name;
        var idObj 	= document.getElementById(form).elements[i].id;
		var relObj 	= document.getElementById(form).elements[i].getAttribute('rel');
				
		if ((relObj == "email")) {
			expressao = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/i;
			if(!expressao.test(document.getElementById(form).elements[i].value)){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].value = "";
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}else if (relObj == "required") {
			if((document.getElementById(form).elements[i].value == null) || (document.getElementById(form).elements[i].value == "")){
				alert(document.getElementById(form).elements[i].title);
				document.getElementById(form).elements[i].style.backgroundColor = "#f1f1f1";
				document.getElementById(form).elements[i].focus();
				return false;
			}
		}
    }
    return true;
}

function mostrarVideo(id) {
	$.ajax( {
		type: "POST",
		url: "incs/mostra-video.php",
		data: "id=" + id,
		beforeSend: function() {
			$("#alvo-video").html("<img src=\"img/loader.gif\" />");
		},
		success: function(txt) {
			$("#alvo-video").html(txt);
		},		
		error: function(txt) {
			alert('Erro: '+txt);
		}
	});
}
