jQuery(function(){
		$("#footer").css({'top': parseInt($(document).height() - 60)}).fadeIn('fast');
		
        var page = $('#page').val();
	    if(page == undefined) {
            showNews();
        }
		tabLink();
		startCycle();
		photos = $("#news-photo").find("img");
		
		$("#news-tabs, #news").bind("mouseenter",function(){
			stopCycle();
		}).bind("mouseleave", function(){
			startCycle();
		});
		
		tab();
		emp(); // Empresas
		neg(); // Negócios
		ae(); // Indicadores
		
	});
	
	var timer;
	var i = 0;
	var t = parseInt($(".news-tab",$("#news-tabs")).length - 1);
	var photos = new Array();
	var f = true;
    var page = $('#page').val();

	if(page == undefined) {
        r = false
    } else {
        r = true;
    }
	
	function stopCycle () {
		clearInterval(timer);
	}
	
	function startCycle () {
		if(r == true){
			timer = setInterval(function(){
						if(i >= t){
							i = 0;
						} else {
							i++;
						}
						if(f){ i = 1; f = false; }
						showNews(i);
					}, 5000);
			
		} else {
			stopCycle();
		}
	}
	
	function tabLink () {
		$(".news-link").click(function(){
			var a  		 = $(this);
			var li 		 = a.parent("li");
			var tabbox	 = $("#news-tabs");
			var tabs	 = tabbox.find("a");
			var id		 = null;
			
			if(li.hasClass("on")){
				return false;
			}
			
			for (var i = tabs.length - 1; i >= 0; i--){
				if($(tabs[i]).attr("href") == a.attr("href")){
					id = i;
				}
			};
			tabbox.find("li").removeClass("on");
			li.addClass("on");
			showNews(id)
			
			return false;
		});
	}
	
	function showNews (index) {
		
		var id = (index != undefined) ? index : 0;
		
		var photobox = $("#news-photo");
		var newsbox  = $("#news");
		var tabbox	 = $("#news-tabs");
		var textbox	 = $("#news-content");
		var imgbox	 = $("#news-photo-content");
		
		var text	 = newsbox.find("li").eq(id).html();
		var tab 	 = tabbox.find("li").eq(id);

		if (textbox.is(":visible")) {
			textbox.fadeOut('fast', function(){
				$(this).html(text);
				$(this).fadeIn("fast", function(){
					imgbox.fadeOut("fast", function(){
						$(this).html(photos[id]);
						$(this).fadeIn("slow");
					});
				});
				tabbox.find("li").removeClass("on");
				tab.addClass("on");
			});
		} else {
			newsbox.width(0).animate({ 'width' : '+=400px' }, 1000, function(){
				imgbox.html(photos[0]).fadeIn("slow");
				tabbox.find("li").removeClass("on");
				tab.addClass("on");
				tabbox.slideDown("fast");
				textbox.addClass("esc").html(text).fadeIn("slow");
			});
		}
	}
	
	function tab () {
		$("a.tab").each(function(){
			$(this).click(function(){
				var a = $(this);
				var pg = a.attr("href");
				var img = a.find("img");
				var src = img.attr("src");
				var int = $("#int");
				
				stopCycle();
				r = false;

				$("a.tab").removeClass("on");

				a.addClass("on");
				img.attr("src", "templates/i/loading-o.gif");
				var ts = pg;

				$.ajax({
					// HENRIQUE - ALTERAR AQUI
					url: 'response.php?page='+ts,
					type: 'GET',
					data: {  }, // se quiseres passar parametros para o arquivo, o formato e: { 'param': 'valor', 'param2' : 'valor2' }
					dataType: 'text',
					success: function(data){

						if($("#news").is(":visible")){
							$("#news").css("display", "none");
							$("#news-photo").css("display", "none");
							$("#news-tabs").css("display", "none");

							int.height(0).animate({ opacity: 'show', height: '+=433px' }, 500).append('<div id="int-text" />')
							$("#int-text").css({ height: 0 }).html(data);
							int.scrollTop(0);
							$("#int-text").fadeIn("fast");

							img.attr("src", src);

						} else {
							img.attr("src", src);
							$("#int-text").fadeOut("fast", function(){
								$(this).empty();
								$("#int-text").html(data);
								int.scrollTop(0);
								$(this).fadeIn("slow");
							});
						}
					}
				});
				return false;
			});
		});
	}
	
function emp () {
	$("a.emp").each(function(){
		$(this).click(function(){
			var a = $(this);
			var pg = a.attr("href");
			var img = a.find("img");
			var src = img.attr("src");
			var int = $("#int");
			
			stopCycle();
			r = false;

			$("a.emp").removeClass("on");

			a.addClass("on");
			img.attr("src", "templates/i/loading-o.gif");
			var ts = pg;

			$.ajax({
				// HENRIQUE - ALTERAR AQUI
				url: 'response.php?page='+ts,
				type: 'GET',
				data: {  }, // se quiseres passar parametros para o arquivo, o formato e: { 'param': 'valor', 'param2' : 'valor2' }
				dataType: 'text',
				success: function(data){

					if($("#news").is(":visible")){
						$("#news").css("display", "none");
						$("#news-photo").css("display", "none");
						$("#news-tabs").css("display", "none");

						int.height(0).animate({ opacity: 'show', height: '+=433px' }, 500).append('<div id="int-text" />')
						$("#int-text").css({ height: 0 }).html(data);
						int.scrollTop(0);
						$("#int-text").fadeIn("fast");

						img.attr("src", src);

					} else {
						img.attr("src", src);
						$("#int-text").fadeOut("fast", function(){
							$(this).empty();
							$("#int-text").html(data);
							int.scrollTop(0);
							$(this).fadeIn("slow");
						});
					}
				}
			});
			return false;
		});
	});
}	
	
function neg () {
	$("a.neg").each(function(){
		$(this).click(function(){
			var a = $(this);
			var pg = a.attr("href");
			var img = a.find("img");
			var src = img.attr("src");
			var int = $("#int");
			
			stopCycle();
			r = false;

			a.addClass("on");
			
			urlParts = pg.split('/');
			
			var ts = urlParts[urlParts.length - 1];

			$.ajax({
				url: 'response.php?page='+ts,
				type: 'GET',
				data: {  }, // se quiseres passar parametros para o arquivo, o formato e: { 'param': 'valor', 'param2' : 'valor2' }
				dataType: 'text',
				success: function(data){

					if($("#news").is(":visible")){
						$("#news").css("display", "none");
						$("#news-photo").css("display", "none");
						$("#news-tabs").css("display", "none");

						int.height(0).animate({ opacity: 'show', height: '+=433px' }, 500).append('<div id="int-text" />')
						$("#int-text").css({ height: 0 }).html(data);
						int.scrollTop(0);
						$("#int-text").fadeIn("fast");


					} else {

						$("#int-text").fadeOut("fast", function(){
							$(this).empty();
							$("#int-text").html(data);
							int.scrollTop(0);
							$(this).fadeIn("slow");
						});
					}
				}
			});
			return false;
		});
	});
}	

function ae () {
	$("a.ae").each(function(){
		$(this).click(function(){
			var a = $(this);
			var pg = a.attr("href");
			var img = a.find("img");
			var src = img.attr("src");
			var int = $("#int");
			
			stopCycle();
			r = false;

			a.addClass("on");
			
			var ts = pg;

			$.ajax({
				// HENRIQUE - ALTERAR AQUI
				url: 'response.php?page='+ts,
				type: 'GET',
				data: {  }, // se quiseres passar parametros para o arquivo, o formato e: { 'param': 'valor', 'param2' : 'valor2' }
				dataType: 'text',
				success: function(data){

					if($("#news").is(":visible")){
						$("#news").css("display", "none");
						$("#news-photo").css("display", "none");
						$("#news-tabs").css("display", "none");

						int.height(0).animate({ opacity: 'show', height: '+=433px' }, 500).append('<div id="int-text" />')
						$("#int-text").css({ height: 0 }).html(data);
						int.scrollTop(0);
						$("#int-text").fadeIn("fast");


					} else {

						$("#int-text").fadeOut("fast", function(){
							$(this).empty();
							$("#int-text").html(data);
							int.scrollTop(0);
							$(this).fadeIn("slow");
						});
					}
				}
			});
			return false;
		});
	});
}	


function fale_conosco_submit() {
    var errmsg = ""
    //Verificar campos obrigatórios
    if($('#fale_conosco_assunto').val() == "") {
        errmsg += "Campo assunto é obrigatório e está vazio\n";
    }
    if($('#fale_conosco_nome').val() == "") {
        errmsg += "Campo nome é obrigatório e deve ser preenchido\n";
    }
    if($('#fale_conosco_email').val() == "") {
        errmsg += "Campo e-mail é obrigatório, mas está vazio\n";
    }
    if($('#fale_conosco_mensagem').val() == "") {
        errmsg += "Sua mensagem está vazia e queremos que você entre em contato!\n";
    }

    if(errmsg.length > 0) {
        errmsg = "Ocorreram os seguintes problemas:\n" + errmsg;
        alert(errmsg);
        return false;
    }

    $.ajax({
        url: 'send_msg.php',
        type: 'GET',
        data: { 
            'assunto': $('#fale_conosco_assunto').val(),    
            'nome': $('#fale_conosco_nome').val(),
            'email': $('#fale_conosco_email').val(),
            'empresa': $('#fale_conosco_empresa').val(),
            'telefone': $('#fale_conosco_telefone').val(),
            'mensagem': $('#fale_conosco_mensagem').val()
        },
        dataType: 'text',
        success: function(data) {
            $('#int-text').fadeOut("fast", function() {
                $("#int-text").html(data);
                $(this).fadeIn("slow");
            })
        }
    });
}

function banco_ideias_submit() {
    var errmsg = ""
    //Verificar campos obrigatórios
    if($('#banco_ideias_topico').val() == "") {
        errmsg += "Campo assunto é obrigatório e está vazio\n";
    }
    if($('#banco_ideias_nome').val() == "") {
        errmsg += "Campo nome é obrigatório e deve ser preenchido\n";
    }
    if($('#banco_ideias_arquivo').val() == "") {
        errmsg += "Arquivo do projeto é obrigatório, por favor nos envie ele\n";
    }
    if($('#banco_ideias_email').val() == "") {
        errmsg += "Campo e-mail é obrigatório, mas está vazio\n";
    }
    if($('#banco_ideias_cpfcnpj').val() == "") {
        errmsg += "CPNF ou CNPJ também precisa ser preenchido\n";
    }
    if($('#banco_ideias_mensagem').val() == "") {
        errmsg += "Sua mensagem está vazia e queremos que você entre em contato!\n";
    }

    if(errmsg.length > 0) {
        errmsg = "Ocorreram os seguintes problemas:\n" + errmsg;
        alert(errmsg);
        return false;
    }
    $('#banco_ideias_form').submit();
}

function change_language() {
    var i18n = change_language.arguments[0];
    $.cookie('i18n', i18n);
    location.reload();
}

