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

/**
 * Project handler
 */
var project = {
	keyword: "",
	
	getNew: function(id) {
		$.getJSON('/catalog/project/masculinity/product-list-item/' + id, function(data) {
			project.loadFeatured(data);
		});
	},
	
	init: function() {
		var keyword = ("undefined" != typeof(projectOpts)) ? projectOpts.keyword : "";
		if (keyword != "") {
			project.setKeyword(keyword);
			
			$.getJSON('/catalog/project/' + keyword + '/product-list-item', function(data) {
				project.loadFeatured(data);
			});
		}
		
		$("#landing-featured").show();
		
		$("body").addClass("project project-masculinity");
		
		/* Edit Profile form control */
		$("#btn-edit-profile").click(function() {
			$("#wrapper-profile-form").fadeIn("fast");
			$("#btn-profile-cancel").click(function() {
				$("#wrapper-profile-form").fadeOut("fast");
			});
			var form = $("#wrapper-profile-form form");
			form.submit(function() {
				$("#btn-profile-submit").val("Please wait...");
			});
		});
		
		setTimeout(function() {
			$(".wrapper-feedback").fadeOut(2000);
		}, 4000);
		
		/* "Add to favorites" button on media detail page */
		$("#btn-add-favorites").click(function() {
			if (typeof(bpmVideo) != "undefined") {
				$.getJSON('/project/' + bpmVideo.keyword + '/addfavorite/' + bpmVideo.id, function(data) {
					$("#btn-add-favorites").after(data);
					$("#btn-add-favorites").hide();
				});
			}
		});
		
		/* "Add to friends" button on profile page */
		$("#btn-add-friend").click(function() {
			if (typeof(bpmProfile) != "undefined") {
				$.getJSON('/project/' + bpmProfile.keyword + '/addfriend/' + bpmProfile.id, function(data) {
					$("#btn-add-friend").after(data);
					$("#btn-add-friend").hide();
				});
			}
		});
		
		/* Add Your Friends form control on Get Involved page */
		$(".page-involved #link-add-friends").click(function() {
			$("#spread-the-word").hide();
			$("#link-add-friends").hide();
			$("#wrapper-form-add-friends").show();
			
			$("#form-add-friends").submit(function() {
				$.post("/project/" + bpmProfile.keyword + "/getinvolved/addfriends", {
					recip_email: $("#textarea-recip-email").val(),
					recip_msg: $("#textarea-recip-msg").val(),
					sender_name: $("#text-sender-name").val(),
					sender_email: $("#text-sender-email").val()
				}, function(data) {
					$("#form-add-friends").after("<div class=\"feedback\">" + data.data + "</div>");
					$("#form-add-friends").hide();
				}, "json");
				return false;
			});
			
			$("#form-add-friends-cancel").click(function() {
				$("#wrapper-form-add-friends").hide();
				$("#spread-the-word").show();
				$("#link-add-friends").show();
				return false;
			});
			return false;
		});
	},
	
	initTimelineForm: function() {
		var form = $("#form-timeline");
	},
	
	loadFeatured: function(data) {
		$(".project-landing-product-list").html(data.content).show();
		$("#project-product-list-nav-prev").unbind('click');
		$("#project-product-list-nav-prev").click(function() {
			project.getNew(data.id_prev);
			return false;
		});
		$("#project-product-list-nav-next").unbind('click');
		$("#project-product-list-nav-next").click(function() {
			project.getNew(data.id_next);
			return false;
		});
	},
	
	setKeyword: function(kw) {
		this.keyword = kw;
	}
};
