﻿var currentlyDragging = null;
var becomeMemberSuccess = false;
function Navigate(url) { window.location = url; }
function RefreshHeight() {
	$("#selection").height(size);
	$("#savedItems").height(size);
}

$(document).ready(function () {

	$("a#setHomeRegion").fancybox({
		'width': 250,
		'height': 170,
		'autoScale': false,
		'transitionIn': 'none',
		'transitionOut': 'none',
		'type': 'iframe',
		'hideOnContentClick': false,
		'onClosed': function () {
			if (becomeMemberSuccess) {
				setLoveApplicationIconState(true, appId);
			}
		}
	});

	if (!loggedOn)
	{
		$('.applications-selection').hide();
	}

	hideActivitiesTheUserShouldNotSee();
	initLoveUnLoveButtons();


	function initLoveUnLoveButtons() {
		$(".applications-selection .application").click(toggleApplicationLove);
	}


	function toggleApplicationLove() {

		var applicationIsLoved = $(this).hasClass('app-selected');
		var appId = $(this).attr("applicationId");

		$(this).addClass('app-changing');
		$(this).removeClass('app-selected');

		if (applicationIsLoved)
		{
			//UnLove the application...
			$.post(
				root + 'home/removeuserapplication', 
				{ applicationID: appId }, 
				function () { 
					//UnLove successful.
					setLoveApplicationIconState(false, appId);
				});
		}
		else
		{
			//Love the application (via the home region selection dialog)...
			$("a#setHomeRegion").attr('href', root + "home/selecthomeregion/" + appId);
			$("a#setHomeRegion").trigger('click');
		}
	}

	
	function setLoveApplicationIconState(set, appId){
		var $appButton = $('.applications-selection .application[applicationId="' + appId + '"]');
		if ($appButton.length == 0)
		{
			return;
		}

		if (set)
		{
			$appButton.addClass('app-selected');
		}
		else
		{
			$appButton.removeClass('app-selected');
		}

		$appButton.removeClass('app-changing');
		hideActivitiesTheUserShouldNotSee();
	}


	function hideActivitiesTheUserShouldNotSee() {
		var activeApplicationIds = getActiveApplicationIds();
		$('.applications-container .activity').show();

		if (activeApplicationIds.length == 0)
		{
			//don't hide anything. Rather show everything as opposed to nothing.
			return;
		}

		var activitiesToHide = $('#applications-container .activity'); //start with a full list. We'll subtract from this.

		for(var i = 0; i < activeApplicationIds.length; i++) {
			var appId = activeApplicationIds[i];
			activitiesToHide = activitiesToHide.not('[applicationId="' + appId + '"]');
		}

		activitiesToHide.hide();
	}

	
	function getActiveApplicationIds() {
		var activeApplications = $('.applications-selection .application.app-selected');
		return $.map(activeApplications, function(btn) {
			return $(btn).attr('applicationId');
		});
	}

});






