/**
* Arquivo contendo as configura??es iniciais dos recursos do site.
* 
* @author    Luiz R. Junior <billjr07@gmail.com>
*/


$(document).ready(function() {

	// URL do site
	var baseURL = 'http://www.tvaesul.com.br/';
	
	
	/**
	 * Busca
	 */
	$('#txtSearch').focus(function() {
		if ($(this).val() == "pesquisar vídeos") {
			$(this).val("");
			$(this).addClass("active");
		}
	});
	
	$('#txtSearch').focusout(function() {
		if ($(this).val() == "") {
			$(this).val("pesquisar vídeos");
			$(this).removeClass("active");
		}
	});
	
	
	//----------------------------------------------------------------------------------
			
	/**
	 * Mega DropDown menu
	 */
	//On Hover Over - Mega DropDowwn
	function megaHoverOver() {
		$(this).find(".submenu-div").stop().fadeTo('fast', 1).show();
		$(this).addClass("submenu_active");
	}
	
	//On Hover Out - Mega DropDowwn
	function megaHoverOut() {
		$(this).removeClass("submenu_active");
		$(this).find(".submenu-div").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
			$(this).hide();  //after fading, hide it
		});
	}
	
	var config = {
		sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		interval: 100, // number = milliseconds for onMouseOver polling interval
		over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		timeout: 100, // number = milliseconds delay before onMouseOut
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("#menu_nav ul li .submenu-div").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#menu_nav ul li").hoverIntent(config); //Trigger Hover intent with custom configurations
	
	
	//----------------------------------------------------------------------------------
	
	/**
	 * Twitter News
	 */
	$('#twitter_timeline').cycle({ 
    	fx:     'scrollUp', 
		cssFirst : { zIndex: 1 },
		cssBefore: { zIndex: 1 }, 
		cssAfter: { zIndex: 0 }, 
    	timeout: 15000, 
    	delay:  -2000 
	});
	
	
	//----------------------------------------------------------------------------------
	
	/**
	 * ADS Sidebar
	 */
	$(function() {
		$(".ads-btn").hover (
			function () { $(".ads-btn").stop().animate({width: 180}, 1000); },
			function () { $(".ads-btn").stop().animate({width: 60}, 1000); }
		);
	});
	
	
	//----------------------------------------------------------------------------------
	
	/**
	 * Vertical Tabs
	 */
	 
	 //When page loads...
	$("#video_tools .tab_content").hide(); //Hide all content
	$("#video_tools ul.tabs li:first a").addClass("active").show(); //Activate first tab
	$("#video_tools .tab_content:first").show(); //Show first tab content

	//On Click Event
	$("#video_tools ul.tabs li").click(function() {

		$("#video_tools ul.tabs li a").removeClass("active"); //Remove any "active" class
		$(this).find("a").addClass("active"); //Add "active" class to selected tab
		$("#video_tools .tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	$(".enviar_comentario").click(function() {

		$("#video_tools .tab_content").hide(); //Hide all tab content

		//var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		var activeTab = $(this).attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	$("#btnComentarioCancel").click(function() {
		$("#video_tools .tab_content").hide(); //Hide all tab content
		var activeTab = '#video-comentarios'; //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	
	//----------------------------------------------------------------------------------
	
	/**
	 * Horizontal Tabs
	 */
	 
	 //When page loads...
	$("#related_info .tab_content").hide(); //Hide all content
	$("#related_info ul.tabs li:first a").addClass("active").show(); //Activate first tab
	$("#related_info .tab_content:first").show(); //Show first tab content

	//On Click Event
	$("#related_info ul.tabs li").click(function() {

		$("#related_info ul.tabs li a").removeClass("active"); //Remove any "active" class
		$(this).find("a").addClass("active"); //Add "active" class to selected tab
		$("#related_info .tab_content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		return false;
	});
	
	
	//----------------------------------------------------------------------------------
	
	/**
	 * Form Comentários
	 */
	$("#frmComentario").validate(
    { 
    	rules:
            { 	comentarioNome: { required: true, maxlength: 35 },
              	comentarioEmail: { required: true, email: true, maxlength: 200 },
				comentarios: { required: true, maxlength: 16384 }
            },
            
        messages:
            { 
				comentarioNome: { required: "Nome obrigatório" },
              	comentarioEmail: { required: "Email obrigatório", email: "Email inválido" },
				comentarios: { required: "Comentário obrigatório" }
            }
    });	
    
    
    $("#frmComentario").submit(function() {  
    	// we want to store the values from the form input box, then send via ajax below  
		var idvideo = $('#hidIdVideo').attr('value');
		var nome = $('#txtComentarioNome').attr('value');
		var email = $('#txtComentarioEmail').attr('value');
		var newsletter = $('#chkComentarioEmail').attr('value');
		var comentarios = $('#txtComentarios').val();
		
		$.ajax({
				type: "POST",
				url: "/comentarios/enviar",
				data: "idvideo=" + idvideo + "&nome="+ nome + "&email=" + email + "&newsletter=" + newsletter + "&comentarios=" + comentarios,
				success: function(msg){
					if (msg.length > 0) {
						alert("Erro: " + msg);
					} else {
						$("#video_tools .tab_content").hide(); //Hide all tab content
						$('#video-comentarios-success').fadeIn(); //Fade in the active ID content
					}
				}
		});
		
		return false;
	});
	
	
	$('#btnComentarioSubmit').click(function() {
		var validation = false;
		
		if (isValidEmailAddress($("#txtEmail").val()) == 0) {
			
		}
		
		if (isValidEmailAddress($("#txtEmail").val()) != 0) {
			$.ajax({
				type: 'post',
				cache: false,
				url: 'http://www.tvaesul.com.br/site/newsletters/assinar',
				data: ({email : $('#txtEmail').val() }),
				dataType: 'text',
				success: function(msg){
					$("#txtEmail").fadeOut(1000).hide();
					$("#btnSubmit").fadeOut(1000).hide();
					$("#success").text(msg).fadeIn(1000).show();
					return false;
				}
			});
			
			return false;
		}
		else {
			$("#error").text("Informe um endereço de e-mail válido!").fadeIn(1000).show();
			return false;
		}
	});

	$("#video-form-comentarios form").submit(function() {
		$('#bbtnComentarioSubmit').click();
    });

    
    
    //----------------------------------------------------------------------------------
	
	/**
	 * News dialog
	 */
    
	//Inicializa a caixa de notícias
	$("#dialog").dialog("destroy");
    $("#news-modal").dialog({
    	autoOpen: false,
		height: 420,
		modal: true,
		resizable: false,
		width: 640
	});
	
	$('a[rel=overlay]').each(function() {
		$(this).click(function(e) {
	    	//Realiza a requisição do conteúdo
	    	$.ajax({
	    		type: 'get',
	    		cache: false,
	    		url: $(this).attr('href') + '/true',
	    		dataType: 'html',
	    		success: function(msg) {
	    			//$("#news-modal").attr('title', $(this).attr('title'));
	    			$("#news-modal").find(".dialog-content").html(msg);
	    			$("#news-modal").dialog('open');
                    pageTracker._trackPageView($(this).attr('href'));
	    		}
	    	});
	    	
	    	e.preventDefault();
	    });
	});


   /* $("a[rel=overlay]").click(function(e) {
    	//Realiza a requisição do conteúdo
    	$.ajax({
    		type: 'get',
    		cache: false,
    		url: $(this).attr('href') + '/true',
    		dataType: 'html',
    		success: function(msg) {
    			//$("#news-modal").attr('title', $(this).attr('title'));
    			$("#news-modal").find(".dialog-content").html(msg);
    			$("#news-modal").dialog('open');
    		}
    	});
    	
    	e.preventDefault();
    });*/

});