var Remco = {
	
	profileDump: null,
	
	init: function() {
		$(".textinput").each(function(){
			
			$(this).val($(this).attr("default")).attr("class", "textinput defaultvalue");
			
			$(this).focus(function(){
				if ($(this).val() == $(this).attr("default")) {
					$(this).val("").attr("class", "textinput");
				}
			});
			
			$(this).change(function(){
				Remco.setName();
				$("#title h2").hide();
				$("#contact").hide();
			});
			
			$(this).blur(function(){
				if (!$(this).val()) {
					$(this).val($(this).attr("default")).attr("class", "textinput defaultvalue");
				} else {
					$(this).attr("class", "textinput");
				}
			});
			
		});
		
		$("#icon-save").animate({
			opacity: .2
		});
		// $(window).resize(function(){Remco.resize();});
		// Remco.resize();
		// setTimeout(function(){
		// 	if (!Remco.currentBalloon) {
		// 		Remco.showBalloon("icon-info");
		// 	}
		// }, 1500);
		setTimeout(function(){
			$("h1, h2, h3").fadeIn(250);
		}, 250);
		
		$(window).bind("resize", function(){
			Remco.resize();
		}).trigger("resize");
		
	},
	
	currentBalloon: null,
	balloonTimeout: null,
	showBalloon: function(id) {
		if (id) {
			clearTimeout(this.balloonTimeout);
			if (this.currentBalloon != id) {
				$("#"+this.currentBalloon+" .balloon").fadeOut(250);
				this.currentBalloon = id;
				$("#"+id+" .balloon").fadeIn(250);
			}
		} else {
			this.balloonTimeout = setTimeout(function(){
				// Remco.hideCurrentBalloon();
				$("#"+Remco.currentBalloon+" .balloon").fadeOut(250);
			}, 1000);
		}
	},
	
	// hideCurrentBalloon: function() {
	// 	$("#"+this.currentBalloon+" .balloon").fadeOut(250);
	// },
	
	resize: function() {
		// var windowHeight = $(window).height();
		// var imageHeight = windowHeight - 380;
		// $("#image").css("height", imageHeight);
		// $("#image img").css("margin-top", -((600 - imageHeight) / 2));
		var img = $("#flash img");
		if (img.length == 1) {
			var w = $(window).width();
			var h = $(window).height();
			var d = w > h ? h : w;
			var mt = w > h ? 0 : (h - d) / 2;
			var ml = w > h ? (w - d) / 2 : 0;
			img.css({
				"width": d,
				"height": d,
				"margin-top": mt,
				"margin-left": ml
			});
		}
	},
	
	imageVisible: false,
	toggleImage: function() {
		if (Remco.imageVisible) {
			$("#image").animate({
				height: 120
			}, 500);
			$("#who p, #who-header").animate({
				height: 'show',
				opacity: 1
			}, 500);
			Remco.imageVisible = false;
		} else {
			var windowHeight = $(window).height();
			var imageHeight = windowHeight - 300;
			$("#image").animate({
				height: imageHeight
			}, 500);
			$("#who p, #who-header").animate({
				height: 'hide',
				opacity: 0
			}, 500);
			Remco.imageVisible = true;
		}
	},
	
	saveProfile: function() {
		
		var firstname = $("#form-firstname");
		var lastname = $("#form-lastname");
		var email = $("#form-email");
		
		if (firstname.val() == firstname.attr("default")) {
			firstname = "";
		} else {
			firstname = firstname.val();
		}
		
		if (lastname.val() == lastname.attr("default")) {
			lastname = "";
		} else {
			lastname = lastname.val();
		}
		
		if (email.val() != email.attr("default") && email.val()) {
			email = email.val();
		} else {
			email = false;
		}

		if (email && Remco.profileDump) {

			var data = {
				firstname: firstname,
				lastname: lastname,
				email: email,
				dump: Remco.profileDump
			}
			
			$.ajax({
				cache: false,
				data: data,
				dataType: "json",
				error: function(xhr, status, error) {
					alert("Het profiel kan op dit moment niet worden opgeslagen.\nProbeer het later nog eens.");
				},
				success: function(data, status, xhr) {
					alert("Je profiel is opgeslagen!\nJe ontvangt binnen enkele minuten een e-mail met hierin de unieke link naar je profiel.");
					$("#save-profile").hide();
				},
				url: "save/"
			});

		} else {
			alert("Het huidige profiel is niet geldig. Pas het profiel naar wens aan, en vul tenminste een geldig e-mailadres in.");
		}

		return false;
						
	},
	
	toggle: function(id) {
		$("div.content").each(function(){
			var div = $(this);
			if (div.attr("id") == id) {
				if (div.css("display") == "block") {
					div.slideUp();
					$("#"+id+"-header").removeClass("open");
				} else {
					div.slideDown();
					$("#"+id+"-header").addClass("open");
				}
			} else {
				$("#"+div.attr("id")+"-header").removeClass("open");
				div.slideUp();
			}
		});
	},
	
	setName: function() {
		var firstname = $("#form-firstname");
		if (firstname.val() != firstname.attr("default")) {
			$("#firstname").text($("#form-firstname").val());
		} else {
			$("#firstname").text("");
		}
		var lastname = $("#form-lastname");
		if (lastname.val() != lastname.attr("default")) {
			$("#lastname").text($("#form-lastname").val());
		} else {
			$("#lastname").text("");
		}
		Cufon.refresh("#firstname, #lastname");
	},
	
	setFirstname: function(name) {
		$("#firstname").text(name);
		Cufon.refresh("#firstname, #lastname");
	},
	
	setLastname: function(name) {
		$("#lastname").text(name);
		Cufon.refresh("#firstname, #lastname");
	},
	
	toggleSaveForm: function() {
		if (Remco.profileDump) {
			$("#save-profile").toggle();
		} else {
			alert("Je kunt je profiel pas opslaan zodra je het naar eigen inzicht hebt aangepast.");
		}
	},

	toggleInfo: function() {
		$("#website-info").toggle();
	},
	
	// getDumpFromFlash: function() {
		// document.getElementById('RemcoSchreuder').sendDumpFromHtml("");
	// },
	
	receiveDumpFromFlash: function(data) {
		Remco.profileDump = data;
		$("#icon-save").animate({
			opacity: 1
		}, 250);
		Remco.showBalloon();
	}
	
};

