$(function() {
	if($.url.param("oauth_token") && $.url.param("oauth_token") != "") {
		chamarFollow("action=follow&oauth_token=" + $.url.param("oauth_token"));
	}
	$("a.btFollow").click(function(e) {
		e.preventDefault();
		chamarFollow("action=follow");
	});
	
	$().ajaxStart(function() { $('#divBotaoFollow a').hide(); $('#divBotaoFollow').addClass("loading"); });
	
	$("select#txtAssunto").change(function(e) {
		if($("option:selected", this).val() == "Trabalhe conosco") {
			$("label.file").show();
		} else {
			$("label.file").html($("label.file").html()).hide();
		}
	});
	
	function chamarFollow(dados) {
		$.ajax({
			url: ajaxURL,
			type: "POST",
			data: dados,
			dataType: "json",
			success: function(ret){
				switch(ret.status) {
					case 0:
						$(window).attr("location",ret.url);
						break;
					default:
						$("#divBotaoFollow").removeClass("loading").html("<p>Agora voc&ecirc; est&aacute; nos seguindo.</p>");
						break;
				}
			}
		});
	}
	
	$(".divImagensAdicionais ul li a").click(function(e) {
		e.preventDefault();
		var ni = $(this).attr("href");
		var type = $(this).hasClass("video") ? "video" : "image";
		$("#imgPrincipal").fadeOut(function() {
			switch(type) {
				case "video":
						$("#divViewVideo")
							.html("")
							.append($('<a></a>')
								.attr('href',ni));
						$.fn.media.defaults.flvPlayer = $("#templateURL").val() + "/swf/player.swf";
						$.fn.media.defaults.autoplay = 1;
						
						$("#divViewVideo a").media({ width: 738 });
						
						$("#divViewVideo").width($("#divViewVideo div:eq(0)").css("width")).show();
					break;
				case "image":
					$("#divViewVideo").html("");
					$(".divImagemPrincipal #imgLoadingImage").hide();
					var a = $(this).attr("src", ni).load(function() {
						$("#imgPrincipal").fadeIn();
					});
					break;
			}
		});
	});	
	
	$(".divContato form").validationEngine({
		success :  function() {
			
			var action = $("input#txtAction").val();
			
			var assunto = $("select#txtAssunto option:selected").val();
			var destinatario = $("input#txtDestinatario").val();
			
			if(assunto.toLowerCase().replace(" ","") == "trabalheconosco")
				//destinatario = "otavio@urbansummer.com.br"
				destinatario = "rh@urbansummer.com.br";

			$.ajax({
				url: action, 
				type: "POST",
				global : false,
				data : ({
					action : "contato",
					txtNome : $("input#txtNome").val(),
					txtEmail : $("input#txtEmail").val(),
					txtMensagem : $("textarea#txtMensagem").val(),
					txtFile: $("input#txtFile").val(),
					txtAssunto : assunto,
					destinatario : destinatario
				}),
				dataType : "text",
				success: function(ret){
					switch(ret) {
						case "0": 
							alert('Ocorreu um erro ao tentar enviar o e-mail. Tente novamente.');
							break;
						case "1":
							$(".divContato").html("<h3>O email foi enviado com sucesso.</h3>");
							break;
					}
				}
			});
		}
	});
	
	$(".divImagensAdicionais ul li").each(function(e) {
		var img = jQuery("<img>").attr("src", $("a",this).attr('href'));
	});
	
	$('ul.tweets a').attr('target','_blank');

});