﻿/* FRONT PAGE */

var _giftID = 0;
var _audioGiftID = 0;
var _lgnSuccessFunction = "";
var _loggedIn = false;
var _popups = 0;

function OnFPBox(boxID) {
	//show nav and stop animation
	document.getElementById(boxID + "nav").style.display = "block";
	eval(boxID + "active = false;");
}

function OffFPBox(boxID) {
	//hide nav and restart animation
	document.getElementById(boxID + "nav").style.display = "none";
	eval(boxID + "active = true;");
}

function AutoSwitchAnn(boxID, anns) {
	//schedule next switch,
	setTimeout("AutoSwitchAnn('" + boxID + "', " + boxID + "anns)", 10000);
	
	//switch, if the mouse isn't hovered
	if (eval(boxID + "active"))
		eval("NextAnn(boxID, anns, " + boxID + "index)");
}

function PrevAnn(boxID, anns, currIndex) {
	//calculate the new index in the array
	var newIndex = 0;
	if (currIndex > 0)
		newIndex = currIndex - 1;
	else
		newIndex = anns.length - 1;

	//make the change on the site
	document.getElementById(anns[currIndex]).style.display = "none";
	document.getElementById(anns[newIndex]).style.display = "block";

	//update the tracking variable
	eval(boxID + "index = " + newIndex);
}

function NextAnn(boxID, anns, currIndex) {
	//calculate the new index in the array
	var newIndex = 0;
	if (anns.length > (currIndex + 1))
		newIndex = currIndex + 1;
	else
		newIndex = 0;

	//make the change on the site
	document.getElementById(anns[currIndex]).style.display = "none";
	document.getElementById(anns[newIndex]).style.display = "block";

	//update the tracking variable
	eval(boxID + "index = " + newIndex);
}

/* VIEW ANNOUNCEMENT */

function SwitchIntTab(newTab) {
	if (getEl("annlinkcomments")) {
		getEl("annlinkcomments").className = "";
		getEl("ann-int-comments").style.display = "none";
	}
	getEl("annlinkgifts").className = "";
	getEl("ann-int-gifts").style.display = "none";
	getEl("annlinkphotos").className = "";
	getEl("ann-int-photos").style.display = "none";
	getEl("annlinkvideos").className = "";
	getEl("ann-int-videos").style.display = "none";
	getEl("annlinkphotostories").className = "";
	getEl("ann-int-photostories").style.display = "none";

	getEl("annlink" + newTab).className = "curr";
	getEl("ann-int-" + newTab).style.display = "block";

	return false;
}

function ShowPhotoPopup() {
	getEl("upl-head").innerText = "Upload A Photo";
	getEl(_uploadType).value = "photo";
	ShowIntPopup("popup-upload", 150);
}

function ShowVideoPopup() {
	getEl("upl-head").innerText = "Upload A Video";
	getEl(_uploadType).value = "video";
	ShowIntPopup("popup-upload", 150);
}

function UploadNewFile() {
	getEl("upload-photo-success").style.display = "none";
	getEl("upload-video-success").style.display = "none";
	getEl("upload-form").style.display = "block";
}

function UploadFile() {
	if (getEl(_uploadBox).value == "" || getEl(_uploadCaption).value == "") {
		ShowErrorMessage("You need to select a file to upload and provide a caption.\r\rPlease check the form and then re-submit.");
		return false;
	}
	else {
		if (_loggedIn) {
			DisplayProgressBar(_guid);
			return true;
		}
		else {
			MFA.FrontEndSite.InteractionService.IsUserSignedIn(UploadSignedInCallback, CommsFailure);
			return false;
		}
	}
}

function UploadSignedInCallback(status) {
	if (status == true) {
		_loggedIn = true;
		ClickUploadButton()
	}
	else {
		_lgnSuccessFunction = "ClickUploadButton()";
		SetRegisterText("Please register to upload this file.");
		ShowIntPopup("popup-register", 150);
	}
}

function ClickUploadButton() {
	document.getElementById(_uploadButton).click();
}

function UploadSuccess(el) {
	getEl("upload-form").style.display = "none";
	getEl("upload-" + el + "-success").style.display = "block";
}

function PickAudioGift(giftID, giftFile) {
	ClearAudioGiftBoxes();
	_audioGiftID = giftID;
	getEl("aggift" + giftID).className = "curr";
	getEl("audio-player-outer2").innerHTML = '<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" id="audio-player" autostart="1" showcontrols="1" src="/Audio/Gifts/' + giftFile + '">';
}

function GiveNewAudioGift() {
	ClearAudioGiftBoxes();
	getEl("audiogift-success").style.display = "none";
	getEl("audiogift-form").style.display = "block";
}

function GiveAudioGift() {
	if (_audioGiftID == 0) {
		ShowErrorMessage("You need to select a gift to give.\r\rPlease pick a gift and then re-submit.");
	}
	else {
		getEl("audio-player-outer2").innerHTML = "";
		MFA.FrontEndSite.InteractionService.AddAudioGift(_annID,  _audioGiftID, AudioGiftSuccess, CommsFailure);
	}
}

function AudioGiftSuccess(status) {
	switch (status) {
		case 1:
			getEl("audiogift-form").style.display = "none";
			getEl("audiogift-success").style.display = "block";
			ClearGiftBoxes();
			_audioGiftID = 0;
			getEl("audio-player-outer").innerHTML = "";
			break;
		case 2:
			_lgnSuccessFunction = "GiveAudioGift()";
			SetRegisterText("Please register to leave this gift");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error leaving your gift. Please try again later.");
			break;
	}
}

function ClearAudioGiftBoxes() {
	getEl("audio-player-outer2").innerHTML = "";
	for (var i = 0; i < _audioGiftBoxes.length; i++) {
		getEl("aggift" + _audioGiftBoxes[i]).className = "";
	}
}

function PickCar() { PickGift(26); }
function PickRose() { PickGift(27); }
function PickDaffodils() { PickGift(28); }
function PickCandle() { PickGift(30); }
function PickHeart() { PickGift(31); }

function PickGift(giftID) {
	ClearGiftBoxes();
	_giftID = giftID;
	getEl("ggift" + giftID).className = "gift-give-on";
}

function GiveNewGift() {
	getEl("gift-success").style.display = "none";
	getEl("gift-form").style.display = "block";
}

function GiveGift() {
	var dispName = getEl("gift-name").value;

	if (_giftID == 0) {
		ShowErrorMessage("You need to select a gift to give.\r\rPlease pick a gift and then re-submit.");
	}
	else if (dispName == "") {
		ShowErrorMessage("You need to enter your name to display with your gift.\r\rPlease enter your name and then re-submit.");
	}
	else {
		MFA.FrontEndSite.InteractionService.AddGift(_annID, dispName, _giftID, GiftSuccess, CommsFailure);
	}
}

function GiftSuccess(status) {
	switch (status) {
		case 1:
			getEl("gift-form").style.display = "none";
			getEl("gift-success").style.display = "block";
			ClearGiftBoxes();
			_giftID = 0;
			break;
		case 2:
			_lgnSuccessFunction = "GiveGift()";
			SetRegisterText("Please register to leave this gift");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error leaving your gift. Please try again later.");
			break;
	}
}

function ClearGiftBoxes() {
	for (var i = 0; i < _giftBoxes.length; i++) {
		getEl("ggift" + _giftBoxes[i]).className = "gift-give";
	}
}

function SaveWebCam() {
	getEl("cam-form").style.display = "none";
	getEl("cam-working").style.display = "block";
	MFA.FrontEndSite.InteractionService.UploadWebcamVideo(_annID, _guid, WebCamSuccess, CommsFailure);
}

function WebCamSuccess(status) {
	switch (status) {
		case 1:
			getEl("cam-working").style.display = "none";
			getEl("cam-success").style.display = "block";
			break;
		case 2:
			_lgnSuccessFunction = "SaveWebCam()";
			SetRegisterText("Please register to save this video");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error saving your video. Please try again later.");
			break;
	}
}

function PostNewComment() {
	getEl("msg-success").style.display = "none";
	getEl("msg-form").style.display = "block";
}

function PostComment() {
	var heading = getEl("msg-subject").value;
	var comment = getEl("msg-content").value;

	if (heading == "" || comment == "") {
		ShowErrorMessage("You need to supply a headline and a message in order to post.\r\rPlease check the form and then re-submit.");
	}
	else {
		//no errors, continue
		MFA.FrontEndSite.InteractionService.AddComment(_annID, heading, comment, CommentSuccess, CommsFailure);
	}
}

function CommentSuccess(status) {
	switch (status) {
		case 1:
			getEl("msg-form").style.display = "none";
			getEl("msg-success").style.display = "block";
			getEl("msg-subject").value = "";
			getEl("msg-content").value = "";
			getEl("MsgCharsRemaining").innerText = " ";
			break;
		case 2:
			_lgnSuccessFunction = "PostComment()";
			SetRegisterText("Please register to leave this message");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error posting your comment. Please try again later.");
			break;
	}
}

function ECShowCard(cardID) {
	var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
	if (is_chrome)
		window.open("/eCards/PreviewCard.aspx?cd=" + cardID, "eCardWin", "width=600,height=532");
	else
		window.open("/eCards/PreviewCard.aspx?cd=" + cardID, "eCardWin", "width=600,height=510");
}

function ECSubmitCard() {
	MFA.FrontEndSite.InteractionService.SubmitECard(ECardSuccess, CommsFailure);
}

function ECCancelCard() {
	HideIntPopup("popup-ecard");
	window.frames["eCardFrame"].location = "/EmptyIFrame.htm";
}

function ECardSuccess(status) {
	switch (status) {
		case 1:
			window.frames["eCardFrame"].location = "/eCards/Success.aspx";
			break;
		case 2:
			_lgnSuccessFunction = "ECSubmitCard()";
			SetRegisterText("Please register to save this eCard");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error submitting your eCard. Please try again later.");
			break;
	}
}

function ClaimAnnouncement() {
	var urn = getEl("clm-urn").value;

	if (urn == "")
		ShowErrorMessage("You need to enter a URN in order to claim this announcement.\r\rPlease enter your URN and re-submit.");
	else
		MFA.FrontEndSite.InteractionService.ClaimAnnouncement(_annID, urn, ClaimSuccess, CommsFailure);
}

function ClaimSuccess(status) {
	switch (status) {
		case 1:
			getEl("clm-form").style.display = "none";
			getEl("clm-success").style.display = "block";
			break;
		case 2:
			_lgnSuccessFunction = "ClaimAnnouncement()";
			SetRegisterText("Please register to claim this announcement");
			ShowIntPopup("popup-register", 150);
			break;
		case 3:
			ShowErrorMessage("There was an error claiming this announcement. Please try again later.");
			break;
		case 4:
			ShowErrorMessage("The URN supplied was invalid.\r\rPlease check the URN and try again.");
			break;
	}
}

function CommsFailure() {
	ShowErrorMessage("There was a communication error. Please try again later.");
}

function SetRegisterText(newText) {
	getEl("reg-head").innerText = newText;
}

function DoRegister() {
	var email = getEl("reg-email").value;
	var pwd1 = getEl("reg-pwd1").value;
	var pwd2 = getEl("reg-pwd2").value;
	var realname = getEl("reg-realname").value;

	if (email == "" || pwd1 == "" || pwd2 == "" || realname == "") {
		ShowErrorMessage("You need to complete all 4 fields in order to register.\r\rPlease check the form and then re-submit.");
	}
	else if (pwd1 != pwd2) {
		ShowErrorMessage("The two password fields do not match.\r\rPlease correct this error and re-submit the form.")
	}
	else {
		MFA.FrontEndSite.InteractionService.Register(email, pwd1, realname, RegisterSuccess, CommsFailure);
	}
}

function RegisterSuccess(status) {
	switch (status) {
		case 1:
			//todo:perform further login actions
			_loggedIn = true;
			HideIntPopup("popup-register");
			eval(_lgnSuccessFunction);
			break;
		case 4:
			ShowErrorMessage("The e-mail address supplied has already been registered.");
			break;
		case 5:
			ShowErrorMessage("The password chosen does not meet the minimum password length of 6 characters.\r\rPlease choose a longer password and try again.");
			break;
	}
}

function DoLogin() {
	var email = getEl("lgn-email").value;
	var pwd = getEl("lgn-pwd").value;

	if (email == "" || pwd == "") {
		ShowErrorMessage("You need to supply your e-mail address and password in order to log in.\r\rPlease check the form and then re-submit.");
	}
	else {
		MFA.FrontEndSite.InteractionService.LogIn(email, pwd, false, LoginSuccess, CommsFailure);
	}
}

function LoginSuccess(status) {
	if (status == 1) {
		//todo:perform further login actions
		_loggedIn = true;
		HideIntPopup("popup-login");
		eval(_lgnSuccessFunction);
	}
	else {
		ShowErrorMessage("There was an error logging you in.\r\rPlease check your e-mail address and password and try again.");
	}
}

function ShowECardPopup() {
	window.frames["eCardFrame"].location = _eCardLocation;
	ShowIntPopup("popup-ecard", 50);
}

function ShowIntPopup(popupID, fromTop) {
	var giftBox = getEl("ann-int-gifts");
	if (giftBox)
		giftBox.style.display = "none";
	
	var pop = getEl(popupID);

	pop.style.top = f_scrollTop() + fromTop + "px";
	pop.style.display = "block";
	_popups += 1;
}

function HideIntPopup(popupID) {
	getEl(popupID).style.display = "none";

	if (_popups > 0) _popups -= 1;

	if (_popups == 0 && getEl("annlinkgifts").className == "curr") getEl("ann-int-gifts").style.display = "block";
}

function ShowErrorMessage(msg) {
	alert(msg);
}


/* SHARE ANNOUNCEMENT */


function ShowTwitterBox() {
	var twitBox = getEl("twt-message");
	if (twitBox.value == "") twitBox.value = _twitText;

	ShowIntPopup("popup-twitter", 150);
}

function SendTweet() {
	var twitUser = getEl("twt-username").value;
	var twitPass = getEl("twt-password").value;
	var twitMessage = getEl("twt-message").value;
	if (twitUser == "" || twitPass == "" || twitMessage == "") {
		ShowErrorMessage("Your need to enter your twitter username and password and enter a message in order to tweet.\r\rPlease check the form and then re-submit.");
	}
	else {
		getEl("twt-progress").style.display = "block";
		MFA.FrontEndSite.InteractionService.SendTweet(twitUser, twitPass, twitMessage, TweetSuccess, CommsFailure);
	}
}

function TweetSuccess(status) {
	switch (status) {
		case 1:
			getEl("twit-form").style.display = "none";
			getEl("twit-success").style.display = "block";
			break;
		case 2:
			ShowErrorMessage("Your twitter username and password has been declined.\r\rPlease check them carefully and try again.");
			break;
		case 3:
			ShowErrorMessage("There was an error tweeting this announcement. Please try again later.");
			break;
	}
	getEl("twt-progress").style.display = "none";
}

function SendEmail() {
	var fromName = getEl("eml-fromname").value;
	var fromEmail = getEl("eml-fromemail").value;
	var recipient = getEl("eml-recipients").value;
	var message = getEl("eml-message").value;
	if (fromName == "" || fromEmail == "" || recipient == "") {
		ShowErrorMessage("You have not completed the entire e-mail form. Please complete all required fields and re-submit.");
	}
	else {
		getEl("eml-progress").style.display = "block";
		MFA.FrontEndSite.InteractionService.EmailFriends(_announcementID, fromName, fromEmail, recipient, message, EmailSuccess, CommsFailure);
	}
}

function SendNewEmail() {
	getEl("eml-recipients").value = "";

	getEl("eml-form").style.display = "block";
	getEl("eml-success").style.display = "none";
}

function EmailSuccess(status) {
	switch (status) {
		case 1:
			getEl("eml-form").style.display = "none";
			getEl("eml-success").style.display = "block";
			break;
		case 3:
			ShowErrorMessage("There was an error sending your e-mail.\r\rPlease check the e-mail addresses that you supplied, as invalid e-mail addresses are a common cause of errors.");
			break;
	}
	getEl("eml-progress").style.display = "none";
}

function PublishToFacebook() {
	FB.ensureInit(function() {
		FB.Connect.streamPublish('', _fbObj, null);
	});
}


/* USEFUL FUNCTIONS */

//http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
function f_scrollTop() {
	return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}
