// global sl object allows calling in a sl.doFunction(x,y,z) fashion
var sl = {

	// Called when the 'Add to Shortlist' button is clicked on the map popup. Triggers the 'flying button' animation, and adds the tutor to the short list.
	startAjaxAddToShortList: function(id) {
		var details = tmap.detailsForMarkerId(id);
		if (details) {
			sl.moveBubble(details.id);
			sl.addToShortList(details.id, details.name, details.sub, details.smallImage, details.deleteLink, details.profileLink);
			new Ajax.Request('/ShortLists/add/'+details.id, {method:'post'});
		}
	},

	// Perform the 'flying button' animation, which animates the 'Add to Shortlist' button position to the shortlist menu location (with synchronised fade).
	// Also displays the yellow 'In Shortlist' button in the original location.
	moveBubble: function(id) {
		var sourceId = 'bubble-add-to-sl-'+id;
		
		var mover = document.createElement("img");
		mover.src             = '/img/button-add-to-sl.png';
		mover.border          = '0';
		mover.style.display   = 'none';
		mover.style.className = 'bubble-move-button';
		document.body.appendChild(mover);

		$(sourceId).src = '/img/button-in-sl.png';

		var startPos = $(sourceId).viewportOffset();
		var endPos   = $('nav-shortlist-li').viewportOffset();

		mover.setStyle({
			position: 'fixed',
			left:     startPos.left +'px',
			top:      startPos.top  +'px'
		});

		mover.show();

		new Effect.Parallel([
			new Effect.Morph(mover, { sync:true, style: 'left:' + endPos.left + 'px;top:' + endPos.top + 'px' }),
			new Effect.Fade(mover,  { sync:true })
		], { duration:0.75 });
	},
	
	// Add a new entry to the short list submenu. Also updates any related page elements (bubble, info area, etc).
	addToShortList: function(id, name, subinfo, image_location, olddeletelink, profilelink) {
		if ($('specific-tutor-holder-'+id)) Element.remove($('specific-tutor-holder-'+id));
		var theIndex = tmap.indexForMarkerTutorId(id);
		if (theIndex>-1) {
			sidebarTutors[theIndex].inSL = true;
			sl.generateAndInsertShortListItem(id, name, subinfo, image_location, olddeletelink, profilelink);
			sl.reloadPopupTextForId(id);
			setMarkerSelectedForId(id);
		}
	},
	
	// Add a new entry to the short list submenu. Does not associate it with a map marker. Useful for page launch, when sidebarTutors variable is not yet available.
	addToShortListSimple: function(id, name, subinfo, image_location, olddeletelink, profilelink) {
		sl.generateAndInsertShortListItem(id, name, subinfo, image_location, olddeletelink, profilelink);
	},
	
	// Physically generate and insert the HTML into the short list submenu. Called from addToShortList and addToShortListSimple, and provides a central HTML generation function to remove overlap.
	generateAndInsertShortListItem: function(id, name, subinfo, image_location, olddeletelink, profilelink) {
		var code = '<div class="shortlist-tutor-holder" id="specific-tutor-holder-' + id + '"><a href="' + olddeletelink + '" class="shortlist-preview-delete" onclick="sl.deleteFromShortList(' + id + ');return false;"><img src="/img/message-delete-clear.png" border=0 /></a><table cellspacing="0" cellpadding="0"><tr><td class="shortlist-preview-image"><img src="' + image_location + '" /></td><td class="shortlist-tutor-text"><a href="' + profilelink + '" onclick="simulateClickOnMarkerId(' + id + ');return false;">' + name + '<span>' + subinfo + '</span></a></td></tr></table></div>';
		Element.insert($('shortlist-preview'), {top:code});
		
		sl.updateShortListVisibility();
	},
	
	// Uses the cached sidebarTutors list to populate the short list submenu. Because the addToShortList function prevents duplicate entries, tutors will be added only if missing.
	redrawShortList: function() {
		if (sidebarTutors.length>0) {
			for (var i=0; i < sidebarTutors.length; i++) {
				var tut = sidebarTutors[i];
				if (tut.inSL) sl.addToShortList(tut.id, tut.name, tut.sub, tut.smallImage, tut.deleteLink, tut.profileLink);
			};
		}
		sl.updateShortListVisibility();
	},

	// Removes a tutor from the short list submenu, including animated slide out.
	deleteFromShortList: function(id) {
		new Ajax.Request('/ShortLists/delete/'+id, {
			asynchronous:true,
			evalScripts:true,
			onComplete:function() {
				sl.slideFromShortList(id);

				var theIndex = tmap.indexForMarkerTutorId(id);
				if (theIndex>-1) {
					sidebarTutors[theIndex].inSL = false;
					sl.reloadPopupTextForId(id);
					setMarkerSelectedForId(id, selectedId==id);
				}
			}
		});
	},
	
	// Re-generates and displays the popup bubble contents for the supplied tutor id, only if the supplied tutor id matches the currently displayed tutor id.
	reloadPopupTextForId: function(id) {
		if (currentInfoWin && selectedId==id) {
			currentInfoWin.html_ = tmap.popupTextForMarkerId(id);
			currentInfoWin.redraw(true);
		}
	},
	
	// Returns the number of tutor entries in the short list submenu. Assumes that previous tutor manipulation has prevented duplicates.
	shortListCount: function() {
		var items = $('nav').select('.shortlist-tutor-holder');
		if (items) return items.length;
		return 0;
	},
	
	// Displays/hides the short list submenu based on the availability of content.
	updateShortListVisibility: function() {
		var slCount = sl.shortListCount();
		if (slCount>0) {
			$('shortlist-invisible').show();
		} else {
			$('shortlist-invisible').hide();
		}

		var registeredNotification = $('short-list-notification');
		if (registeredNotification) registeredNotification.innerHTML = slCount;
	},
	
	// Visually removes a short list submenu entry by sliding it out. When removing the last entry, just fade out then entire list instead.
	slideFromShortList: function(id) {
		var element = $("specific-tutor-holder-"+id);

		if (sl.shortListCount()>1) {
			new Effect.BlindUp(element, {
				duration:0.3,
				afterFinish: function() {
					Element.remove(element);
					sl.updateShortListVisibility();
				}
			});
		} else {
			new Effect.Fade($('shortlist-invisible'), {
				duration:0.3,
				afterFinish: function() {
					Element.remove(element);
					sl.updateShortListVisibility();
				}
			});
		}
	}
	
};