$(function() {
	setTimeout(function() {
		$("#logo_icon > img").css({
			top : "-214px"
		});
		setTimeout(function() {
			$("#logo_icon > img").css({
				top : "-441px"
			});
			setTimeout(function() {
				$("#logo_icon > img").css({
					top : "-667px"
				});
				$("#logo_icon_underline").css({
				    opacity : 0
				});
				$("#logo_tagline").css({
					opacity : 1,
					display: "block"
				});
			}, 3000);

		}, 3000);

	}, 3000);

	if ($.browser.msie) {

		// Fake placeholder values
		$("textarea,input").each(function(i, input) {
			$input = $(input);
			if ($input.attr("placeholder") != undefined && $input.attr("placeholder") != "") {
				$input.val($input.attr("placeholder"));
				$input.bind("focus", $.proxy(function() {
					if (this.val() == this.attr("placeholder"))
						this.val("");
				}, $input));
				$input.bind("blur", $.proxy(function() {
					if (this.val() == "") {
						this.val(this.attr("placeholder"));
					}
				}, $input));
			}
		});
	}
	
	// Load latest blog posts
	// '<div class="post"><h1>The Truth Behind See, Believe, Think, Act</h1><p>By Keith Yamashita</p></div>'
	window.processBlogs = function(resp) {
		if (resp.response.posts.length < 1)
			return;
		if (resp.response.posts.length > 1) {
			var author = "";
			for (i = 0; i < resp.response.posts[1].tags.length; i++) {
				if (resp.response.posts[1].tags[i].search("by ") == 0)
					author = resp.response.posts[1].tags[i].substring(3);
			}
			$('<div class="post"><h1><a href="' + resp.response.posts[1].post_url + '">' + resp.response.posts[1].title + '</a></h1><p>By ' + author + '</p></div>').insertAfter("#blog > h2");
		}
		var author = "";
		for (i = 0; i < resp.response.posts[0].tags.length; i++) {
			if (resp.response.posts[0].tags[i].search("by ") == 0)
				author = resp.response.posts[0].tags[i].substring(3);
		}
		$('<div class="post"><h1><a href="' + resp.response.posts[0].post_url + '">' + resp.response.posts[0].title + '</a></h1><p>By ' + author + '</p></div>').insertAfter("#blog > h2");
	}

	var TUMBLR_API_KEY = "661ZnDpE5iftqwVtjwpodicKCF0gpfdmqgk7hl1WFUchD2z9LX";
	$.ajax({
		dataType : "jsonp",
		url : "http://api.tumblr.com/v2/blog/unstuckcommunity.tumblr.com/posts/text?jsonp=processBlogs&api_key=" + TUMBLR_API_KEY
	});

	

	$("#videolink").bind("click", function(e) {
		e.preventDefault();
		e.stopPropagation();
		
		var video_link = "<iframe src='http://player.vimeo.com/video/31056501?title=0&amp;byline=0&amp;portrait=0' \
			width='640' height='360' frameborder='0' webkitAllowFullScreen \
			allowFullScreen></iframe>";
		
		$('#videoModal').html(video_link);
		$("#videoModal").dialog({
			autoOpen : false,
			modal : true,
			closeOnEscape: true,
			resizable : false,
			draggable : false,
			title : "Introducing Unstuck",
			width : 710,
			height : 470
		});
		$("#videoModal").dialog("open");
	});

	$("#videoModal").bind( "dialogclose", function(event, ui) {
		$("#videoModal").dialog("destroy");
		});
	
	
	$("#tool_scroller").css({
		left : "0px"
	});

	$("#tool_scroller_right").bind("click", function(e) {
		e.preventDefault();
		e.stopPropagation();
		$("#tool_scroller").css({
			left : Math.max($("#tool_scroller").position().left - 395, -$("#tool_scroller").outerWidth() + $("#tool_scroller_wr").width())
		});
	});

	$("#tool_scroller_left").bind("click", function(e) {
		e.preventDefault();
		e.stopPropagation();
		$("#tool_scroller").css({
			left : Math.min($("#tool_scroller").position().left + 395, 0)
		});
	});

	$("#stuckmoments > .left > .thumb").each(function(i, thumb) {
		$(thumb).bind("mouseenter", function() {
			// $($("#stuckmoments > .left > .full_wr")).css({"background-color":"#dfe21b"});
			$("#stuckmoments > .left > .full_wr > .full.selected").removeClass("selected");
			$($("#stuckmoments > .left > .full_wr > .full")[i]).addClass("selected");

			$("#stuckmoments > .left > .thumb.selected").removeClass("selected");
			$($("#stuckmoments > .left > .thumb")[i]).addClass("selected");

		});
	});

	$("#stuckmoments > .left").bind("mouseleave", function() {
		// $($("#stuckmoments > .left > .full_wr")).css({"background-color":"transparent"});
		// $("#stuckmoments > .left > .full_wr > .full.selected").removeClass("selected");
		// $($("#stuckmoments > .left > .full_wr > .full")[0]).addClass("selected");
		//
		// $("#stuckmoments > .left > .thumb.selected").removeClass("selected");
		// $($("#stuckmoments > .left > .thumb")[0]).addClass("selected");

	});

	$(".signup").bind("click", function(e) {
		e.stopPropagation();
		openSubscribe();
	});

	$("body").bind("click", function() {
		closeSubscribe();
	});

	$("#subscribe").bind("click", function(e) {
		e.stopPropagation();
	});

	$("#mc-embedded-subscribe").bind("click", function(e) {
		$("#mc-embedded-subscribe-form").submit();
	});

	$balloon = $($(".balloon")[0]);
	var balloonWidth = $balloon.outerWidth();
	var balloonCount = $(".balloon").length;
	$("#balloons").css("width", balloonWidth * balloonCount + "px");
	var balloonIndex = 0;
	setInterval(function() {
		balloonIndex += 3;
		balloonIndex = balloonIndex % balloonCount;
		$("#balloons").css({
			left : -balloonWidth * balloonIndex + "px"
		});
	}, 5000);

	$("#maildialog").dialog({
		autoOpen : false,
		modal : true,
		draggable : false,
		resizable : false,
		width : 491,
		height : 461,
		title : "Contact Us"
	});

	$(".contact").bind("click", function(e) {
		e.preventDefault();
		$("#maildialog").dialog("open");
		$(":focus").blur();
	});

	$("#mail_submit").bind("click", function(e) {
		e.preventDefault();
		// alert("This will be implemented once the server is ready");
		// curl -u info@unstuck.com:believe1 -H "Content-Type: application/xml" -d "<ticket><description>Test ticket</description><priority-id>2</priority-id><requester-name>Seth Samuel</requester-name><requester-email>seth@visere.com</requester-email></ticket>" -X POST http://unstuck.zendesk.com/tickets.xml
		// return false;
		if ($("#mail_name").val() == "") {
			alert("Please enter your name.");
			return false;
		}
		if ($("#mail_from").val() == "") {
			alert("Please enter your email.");
			return false;
		}
		if ($("#mail_message").val() == "") {
			alert("Please enter your message.");
			return false;
		}
		$.ajax({
			type : "GET",
			dataType : "jsonp",
			jsonpCallback : "zdCb",
			data : {
				description : $("#mail_message").val(),
				name : $("#mail_name").val(),
				email : $("#mail_from").val()
			},
			url : "http://api.unstuck.com/zendesk",
			success : function(resp) {
				alert("Thank you for your request. You should receive email confirmation and one of our representatives will be in touch soon.");
				$("#maildialog").dialog("close");
			},
			error : function(resp, status, error) {
				alert("Sorry, there appears to have been a problem submitting your ticket. Please try again, or go to http://unstuck.zendesk.com");
				console.log(error);
			}

		});
	});

});

function openSubscribe() {
	$("#subscribe").addClass("open");
}

function closeSubscribe() {
	$("#subscribe").removeClass("open");
}
