$(function()
{
	$("#contact .titleContact").bind("click", function() { openContact(this); });
	
	$("#lnkMandeSeuCurriculo").bind("click", function() { openModal(); return false; });
	
	$(".btCloseModal").bind("click", function() { closeModal(); return false; });
	
	$("#makingOfAnterior, #makingOfProximo").bind("click", function() { mankingOfNavigation(this); return false; });
	
	$(".mandeSeuCurriculo div fieldset input[type='button']").bind("click", function() { sendCV(); return false; });
	
	fixPortfolioHeight();
	$(window).resize(function() {fixPortfolioHeight()});
});

function openContact(obj)
{
	var contact = $("#addresses div");
	
	$(obj).toggleClass("contactOpened");
	if($(obj).hasClass("contactOpened"))
	{
		contact.show();
		window.location = "#addresses";
	}
	else
	{
		contact.hide();
	}
}

function openModal()
{
	$("#modalShadow").css({"height":$(window).height(), "display":"block"}).animate({"opacity":0.95}, 800);
	$("#modal").fadeIn(1000);
}

function closeModal()
{
	if(window.location.toString().indexOf("cv") > 0)
	{
		var w = window.location.toString();
		window.location = "index.php";
	}
	
	$("#modal").fadeOut(500);
	
	$("#modalShadow")
    .animate({"opacity" : 0}, 1000,
    function() {$(this).hide();});
}

var currentMakingOf = 0;
function mankingOfNavigation(obj)
{
	var makingOfs = $(".embedsMakingOf");
	var div = $("#makingOf_" + currentMakingOf);
	var objMakingOf = $(obj);
	
	makingOfs.hide();
	var vid = div.find("iframe");
	div.find("iframe").remove();
	div.append(vid);
	
	if(objMakingOf.attr("id") == "makingOfAnterior")
	{
		if(currentMakingOf == 0)
			currentMakingOf = makingOfs.length-1;
		else
			currentMakingOf--;
	};
		
	if(objMakingOf.attr("id") == "makingOfProximo")
	{
		if(currentMakingOf == makingOfs.length-1)
			currentMakingOf = 0;
		else	
			currentMakingOf++;
	};
	
	$("#makingOf_" + currentMakingOf).show();
}

function callProjectVid(idVid)
{
	var embed = $("#embedProject");
	embed.find("iframe").remove();
	embed.html("<iframe src='http://player.vimeo.com/video/" + idVid + "?color=c9ff23' width='755' height='500' frameborder='0'></iframe>");
	
	openModal();
}

function sendCV()
{
	var name	= $("#txtName");
	var tel		= $("#txtTelephone");
	var email	= $("#txtEmail");
	var site	= $("#txtSite");
	var message	= $("#txtMessage");
	var file	= $("#cvRealUpload");
	var msg		= $("#msg");
	
	name.removeAttr("style");
	tel.removeAttr("style");
	email.removeAttr("style");
	site.removeAttr("style");
	message.removeAttr("style");
	msg.html("");
	
	if(name.val() == "")
	{
		name.css("border", "1px solid #F00");
		name.focus();
		msg.html("Preencha o nome");
	}
	else if(tel.val() == "")
	{
		tel.css("border", "1px solid #F00");
		tel.focus();
		msg.html("Preencha o telefone");
	}
	else if(email.val() == "")
	{
		email.css("border", "1px solid #F00");
		email.focus();
		msg.html("Preencha o email");
	}
	else if(site.val() == "")
	{
		site.css("border", "1px solid #F00");
		site.focus();
		msg.html("Preencha o site");
	}
	else if(message.val() == "")
	{
		message.css("border", "1px solid #F00");
		message.focus();
		msg.html("Preencha a mensagem");
	}
	else
	{
		$.ajax
		({
			type: "POST",
			contentType: "application/x-www-form-urlencoded; charset=UTF-8",
			url: "_php/curriculo.php",
			data: "txtName=" + name.val() + "&txtTelephone=" + tel.val() + "&txtEmail=" + email.val() + "&txtSite=" + site.val() + "&txtMessage=" + message.val(),
			error: function(request, status, error)
			{
				msg.html(error);
			},
			success: function(data, status, request)
			{
				msg.html(data);
			}
		});
	}
}

function fixPortfolioHeight()
{
	$("#portfolioMain").css("height", ($(window).height() > 820) ? $(window).height() : 820);
}
