$(document).ready(function() {
	start_banner();
 	start_contact();
 	start_menu();
 	start_form();
});

/*#########################################
 * BANNER
 *########################################*/

/* Banner variables */
var current_banner = 0;
var banner_timeout;

function start_banner() {
	var banners = $(".banner-content img").length;
	
	tick_banner();
	
	$("#banner-navigator a").click(function () {
		var index = $("#banner-navigator a").index(this);
		move_banner(index);
		clearTimeout(banner_timeout);
	});
}

function move_banner(position) {
	$("#banner-image .selected").fadeOut(1000, function() {
		$("#banner-image div").eq(position).fadeIn(1000);
	});

	$("#banner-image .selected").removeClass("selected");
	$("#banner-image div").eq(position).addClass("selected");
	
	$("#banner-navigator a").html('<img src="images/square.png" alt="" />');
	$("#banner-navigator a").eq(position).html('<img src="images/square_selected.png" alt="" />');

	$("#banner-box .banner-info").hide();
	$("#banner-box .banner-info").eq(position).show();
}

function tick_banner() {
	banner_timeout = setTimeout(function() {
		var banners = $("#banner-image div").length;
	
		if (current_banner < banners - 1) {
			current_banner++;
		} else {
			current_banner = 0;
		}
			
		move_banner(current_banner);
		tick_banner();
	}, 8000);
}

/*#########################################
 * CONTACT
 *########################################*/

function start_contact(){
	$(".fale-conosco").click(function() {
		if ($("#content").css("top") != "137px") {
			$("#content").animate({
				top: "137px"
			}, 500);
		} else {
			$("#content").animate({
				top: "0px"
			}, 500);
		}
		
		if ($(this).closest("header").length > 0) {
			return false;
		}
	});
}

/*#########################################
 * FORM
 *########################################*/

var last_val = "";

function start_form() {
	$('#contact input[type="text"]').focusin(function() {
		if ($(this).val() == "Nome" || $(this).val() == "Email") {
			last_val = $(this).val();
			$(this).val("");
		}
	});
	
	$('#contact input[type="text"]').focusout(function() {
		if ($(this).val() == "") {
			$(this).val(last_val);
		}
	});
	
	$('#contact textarea').focusin(function() {
		if ($(this).html() == "Comentário") {
			last_val = $(this).html();
			$(this).html("");
		}
	});
	
	$('#contact textarea').focusout(function() {
		if ($(this).html() == "") {
			$(this).html(last_val);
		}
	});
}

/*#########################################
 * MENU
 *########################################*/

function start_menu(){
	$("#menu").click(function(e) {
		if(e.pageY >= 65) {
			if ($("#menu-expanded").css("top") != "0px") {
				$("#menu-expanded").animate({
					top: "0px"
				}, 500);
				
				$("#menu-expanded ul").fadeIn("slow");
			} else {
				$("#menu-expanded").animate({
					top: "-250px"
				}, 500);
				
				$("#menu-expanded ul").fadeOut("fast");
			}
			return false;
		}
	});
	
	$("#menu-expanded ul li").mouseenter(function() {
		$("#menu-expanded ul li").removeClass("selected");
		$(this).addClass("selected");
	});
}

/*#########################################
 * UTIL FUNCTIONS
 *########################################*/
function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

function isWebkit(){
	return (navigator.userAgent.indexOf("Chrome") != -1 || navigator.userAgent.indexOf("Safari") != -1);
}

function isSafari(){
	return (navigator.userAgent.indexOf("Safari") != -1);
}

