var expandPanel = function(panelId, contentType, requestPath)
{
	// html references
	this.panelId = (panelId) ? $(panelId) : $("my_bookmarks");
	// Defaults to My Bookmarks.

	// request path
	this.requestPath = (requestPath) ? requestPath : "/ajax/morecontent";
	// One script to serve them all.
	
	this.contentType = (contentType != "" ? contentType : "bookmarks");
	this.requestPath += "?content=" + this.contentType;
	// Defaults to bookmarks.

	// dataloader 
	this.dataLoader = new contentLoader(); // requires dataLoader.js
	this.dataLoader.onload = this.onDataLoad;
	this.dataLoader.onerror = this.onDataError;
	this.dataLoader.parent = this;	
}
expandPanel.prototype =
{
	initialise:function()
	{
		if(this.dataLoader.isSupported()) // check we have http request functionality
		{
			// Handle all the strip-mining/replacement/actually-getting-stuff later.
			// Here, we just need to set up the clicky-so-do-stuff behaviour.
			if (this.panelId)
			{
				// Stuff only happens if the specified panel is there for it to happen to.
				// Apply event to the "Show all" link.
				// We need certain info there. Erm... the token. And... I dunno. Name?
				// Name will be passed if necessary: blank = person's own profile. Backend script handles that sensibly.
				// And the token can be pulled from the link.
				// There we go! That was easy enough. Lets code it now...
				var myShowAllLink = $ES(".show_all_link", this.panelId);
				myManageLink = $ES(".manage", this.panelId);
				if (myManageLink.length > 0) { this.myDuplicateManageLink = myManageLink.clone(); } else { this.myDuplicateManageLink = null; }
				
				if (typeof myShowAllLink != "undefined") {
					theHref = myShowAllLink.getProperty("href").toString();
					this.token = theHref.split("token=")[1].split("&")[0];
					if (theHref.indexOf("/profiles/") != -1) {
						this.profileName = theHref.split("/profiles/")[1].split("?")[0].split("/")[0];
					} else {
						this.profileName = "";
					}
					var objRef = this;
					// That covers it - definitely captures only the chars between "token=" and the "&" following the value.
					myShowAllLink.addEvent('click',function(e){
						evt = new Event(e);
						evt.stop();
						objRef.handleClick(objRef);
					});
				}
			}
		}
	},
	handleClick:function(theEl)
	{
		var theToken = theEl.token;
		var profileName = theEl.profileName;
		//var contentType = theEl.contentType;
		// These values are always the same. See if Dan can figure out a way to preserve their values between anchors.
		this.dataLoader.url = this.requestPath + "&token=" + theToken + (profileName != "" ? ("&profile=" + profileName) : "");
		this.dataLoader.getData();
		// Need some kind of "Doing stuff!@#" thing to show to the user while we're working in the background.
		// How about a "Working" panel that sits atop the three buttons? The buttons can be toggled display:none
		// style, and the "Doing stuff!@#" panel can toggle back to the three buttons when the call is complete.
		// Could write "Please wait..." over the "Show now" text in the link.
	},
	onDataLoad:function()
	{
		this.parent.handleResponseData(this.loadedData); // pass data into parent object
	},
	onDataError:function()
	{
		this.parent.handleError();
	},
	handleError:function()
	{
		return false;
	},
	handleResponseData:function(data)
	{
		var data = data;
		try
		{
			eval (data);
			this.moreContentJSON = showContentOutcome;
		}
		catch (e)
		{
			alert("There is a problem with the system; please try again later.");
			// This needs to be better.
			return false;
		}

		if (this.moreContentJSON["error"])
		{
			alert(this.moreContentJSON["error"]);
			return false;
		} else
		{
			var panelId = this.rebuildPanel();
		}
	},
	rebuildPanel:function()
	{
		if (this.panelId)
		{
			if (this.panelId.getElements('dd').length > 0)
			{
				var thePanelList = this.panelId.getElements('dd');
				// Several products in the list.
				for (i=thePanelList.length; i>0; i--)
				{
					thePanelList[i-1].remove();
				}
			}
		}
		// All gone now.
		// Now we rebuild it. We have the technoloblah...
		var comma = "";
		for (var i in this.moreContentJSON['content'])
		{
			myNewDD = new Element('dd');
			myNewAnchor = new Element('a', {
					'title': i,
					'href': this.moreContentJSON['content'][i]
				}
			);
			myNewAnchor.setText(i);
			myNewDD.adopt(myNewAnchor);
			this.panelId.adopt(myNewDD);
			myNewDD.getPrevious().appendText(comma);
			comma = ", ";
		}
		if (this.myDuplicateManageLink != null) {
			myNewDD = new Element('dd', { "class": "last" });
			possibleHrefs = { "bookmarks" : "/profile/bookmarks", "owned" : "/profile/wizard/3", "wishlist" : "/profile/wizard/3", "posts" : "" };
			// None for the Posts panel since it's not called for anyway.
			theHref = possibleHrefs[this.contentType];
			myNewAnchor = new Element('a', {
					'title': "Manage",
					'href': theHref
				}
			);
			myNewAnchor.setText("Manage");
			myNewDD.adopt(myNewAnchor);
			this.panelId.adopt(myNewDD);
		}
	},
	addClickHandler:function(theEl,token)
	{
		var objRef = this;
		theEl.token = token;
		theEl.onclick = function(theEl)
		{
			objRef.handleClick(this);
			return false;
		}
	}
}

// set up the Show All/Manage links.
function setupBookmarksPanelLinks()
{
	var myBookmarksUpgrade = new expandPanel("my_bookmarks", "bookmarks");
	myBookmarksUpgrade.initialise();
}
function setupPostsPanelLinks()
{
	var myPostsUpgrade = new expandPanel("my_posts", "posts");
	myPostsUpgrade.initialise();
}
function setupGearPanelLinks()
{
	var myGearUpgrade = new expandPanel("what_i_own", "owned");
	myGearUpgrade.initialise();
}
function setupWishlistPanelLinks()
{
	var myWishlistUpgrade = new expandPanel("my_wishlist", "wishlist");
	myWishlistUpgrade.initialise();
}
addWindowOnLoadEvent(setupBookmarksPanelLinks,"profile_container");
//addWindowOnLoadEvent(setupPostsPanelLinks,"profile_container");
addWindowOnLoadEvent(setupGearPanelLinks,"profile_container");
addWindowOnLoadEvent(setupWishlistPanelLinks,"profile_container");
