var currentIndex = 0;

$(document).ready(function() {
	init();
});

function init() {

	$("#content #projects .project .overlay").hide();
	$("#content #projects .project .alt").hide();
	
	$("#content #projects .project").mouseover(function() {
		$(".overlay", this).show();
		$(".alt", this).show();
	});
	
	$("#content #projects .project").mouseout(function() {
		$(".overlay", this).hide();
		$(".alt", this).hide();
	});
	
	$("#content #projects .project").click(function() {
		currentIndex = $(this).index();
		$("#header #slider").animate({
			left: -(currentIndex*980)
		}, 400);
	});
	
	initMaps();
	formBehaviour();
	
}

function navigatePrevious() {
	currentIndex--;
	if(currentIndex < 0) {
		currentIndex = $("#content #projects .project").size()-1;
	}
	$("#header #slider").animate({
		left: -(currentIndex*980)
	}, 400);
}

function navigateNext() {
	currentIndex++;
	if(currentIndex >= $("#content #projects .project").size()) {
		currentIndex = 0;
	}
	$("#header #slider").animate({
		left: -(currentIndex*980)
	}, 400);
}

function navigateContact() {
	currentIndex = 8;
	$("#header #slider").animate({
		left: -(currentIndex*980)
	}, 400);
}

function formBehaviour() {
	var formFields = [
		["contact-company",	"Company"],
		["contact-name",	"Name"],
		["contact-phone",	"Phone number"],
		["contact-email",	"E-mail address"],
		["contact-message",	"Message"]
	];
	for(var i = 0; i < formFields.length; i++) {
		attachFormBehaviour(formFields[i][0], formFields[i][1]);
	}
}

function attachFormBehaviour(id, value) {
	$("#"+id).focus(function() {
		if($(this).val() == value) {
			$(this).val("");
			$(this).css("color", "#333333");
		}
	});
	$("#"+id).blur(function() {
		if($(this).val() == "") {
			$(this).val(value);
			$(this).css("color", "#aaaaaa");
		}
	});
}

function checkForm() {
	if(isEmpty("contact-company") || isEmpty("contact-name") || isEmpty("contact-phone") || isEmpty("contact-email") || isEmpty("contact-message")) {
		alert("Please fill out all the fields.");
	} else {
		$.post("mail.php", {
			company:	$("#contact-company").val(),
			name:		$("#contact-name").val(),
			phone:		$("#contact-phone").val(),
			email:		$("#contact-email").val(),
			message:	$("#contact-message").val(),
		}, function(r) {
			alert("Thanks for contacting us!");
			currentIndex = 0;
			$("#header #slider").animate({
				left: -(currentIndex*980)
			}, 400);
		});
	}
}

function isEmpty(id) {
	var formFields = [
		["contact-company",	"Company"],
		["contact-name",	"Name"],
		["contact-phone",	"Phone number"],
		["contact-email",	"E-mail address"],
		["contact-message",	"Message"]
	];
	for(var i = 0; i < formFields.length; i++) {
		if(formFields[i][0] == id) {
			if($("#"+id).val() == "" || $("#"+id).val() == formFields[i][1]) {
				return true;
			} else {
				return false;
			}
		}
		break;
	}
}

function initMaps() {
	var map = new google.maps.Map($("#map").get(0), {
		zoom: 14,
		center: new google.maps.LatLng(52.376228,4.923785),
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var marker = new google.maps.Marker({
		position: new google.maps.LatLng(52.376228,4.923785),
		map: map,
		icon: "static/img/map-icon.png",
	});
}
