/// <reference path="jquery-1.6.2-vsdoc.js" />
/// <reference path="jquery-ui-1.8.14.custom.min.js" />
/// <reference path="jquery-cookie-1.0.js" />
/// <reference path="develop.com-2-4.js" />
/// <reference path="develop.com-users-1-0.js" />

// Dependency: Depends on main develop.com-2-2.js, develop.com-users-1.0.js

// define this namespace object.
dm.webcasts = {};
dm.webcasts.REGISTERED_COOKIE_NAME = "develop_com_registered_for_webcasts";
dm.webcasts.webcastContentUrl = "";
dm.webcasts.hasWebcastAccessCookie = false;

////////////////////// $(document).ready() ///////////////////
$(document).ready(function () {
	try {
		dm.webcasts.enableWebcastGatedDownloads();
	} catch (e) {
		dm.core.logMessage("webcast error - enableWebcastGatedDownloads:" + e.message);
	}

	try {
		dm.webcasts.makeWebcastLinksNewTabs();
	} catch (e) {
		dm.core.logMessage("webcast error - makeWebcastLinksNewTabs:" + e.message); 
	}
});

dm.webcasts.makeWebcastLinksNewTabs = function () {
	//$("#webcastsSidebar a").attr("target", "_blank");
};

dm.webcasts.enableWebcastGatedDownloads = function () {

	dm.webcasts.hasWebcastAccessCookie = dm.webcasts.getRegisteredForWebcastContent();

	var webcastLinks$ = $("a").filter(function () {
		var link$ = $(this);
		var hrefAtt = link$.attr("href");
		if (!hrefAtt) {
			return false;
		}

		var href = hrefAtt.toLowerCase();

		var s3Link = "developmentor.s3.amazonaws.com/webcasts";
		var isWebcastLink = href.indexOf(s3Link) >= 0;

		return isWebcastLink;
	});

	webcastLinks$.click(function (e) {
		var hrefAtt = $(this).attr("href");
		dm.webcasts.webcastContentUrl = hrefAtt;

		if (dm.webcasts.hasWebcastAccessCookie) {
			dm.users.recordUserAction("WatchWebcast", hrefAtt);
			return true;
		}
		else {
			$("#webcast-download-dialog-form").dialog("open");
			e.preventDefault();
			return false;
		}
	});

	if (dm.webcasts.hasWebcastAccessCookie) {
		return;
	}

	var name = $("#name");
	var email = $("#email");
	var allFields = $([]).add(name).add(email);

	$("#webcast-download-dialog-form").dialog(
		{
			autoOpen: false,
			height: 'auto',
			width: 350,
			modal: true,
			resizable: false,
			buttons:
			{
				Submit: function () {
					var bValid = true;
					allFields.removeClass("ui-state-error");

					bValid = dm.webcasts.checkLength(name, "username", 3, 26) && bValid;
					bValid = dm.webcasts.checkLength(email, "email", 6, 80) && bValid;

					if (bValid) {
						dm.webcasts.submitNewUserRequestForWebcasts(name.val(), email.val());
					}
					else {
						// todo show error
					}
				},
				Cancel: function () {
					$(this).dialog("close");
				}
			},
			close: function () {
				allFields.val("").removeClass("ui-state-error");
			}
		});

};

dm.webcasts.submitNewUserRequestForWebcasts = function (name, email) {

	var webcastContentUrl = dm.webcasts.webcastContentUrl;

	$.post(
		"/Services/Develop_Com_Services/users/registerforwebcasts",
		{
			UserId: dm.users.user.Id,
			Name: name,
			Email: email
		},
		function (data) {
			if (data && data.Error == null) {
				dm.webcasts.setRegisteredForWebcastContent();
			}
			else {
				//alert(data.Error);
			}

			dm.users.recordUserAction("WatchWebcast", webcastContentUrl);
			$("#webcast-download-dialog-form").dialog("close");
			window.location = webcastContentUrl;
		},
		"JSON");
};

dm.webcasts.setRegisteredForWebcastContent = function () {
	$.cookie(dm.webcasts.REGISTERED_COOKIE_NAME, dm.users.user.Id, { expires: 365 });
	dm.webcasts.hasWebcastAccessCookie = true;
};

dm.webcasts.getRegisteredForWebcastContent = function () {
	if ($.cookie(dm.webcasts.REGISTERED_COOKIE_NAME)) {
		return true;
	}
	return false;
};

dm.webcasts.checkLength = function (o, n, min, max) {
	if (o.val().length > max || o.val().length < min) {
		o.addClass("ui-state-error");
		return false;
	} else {
		return true;
	}
};


