/*
+---------------------------------------------------------------------------
|   MyNetwork Profile System
|   =============================================
|   by Tom Moore (www.xekko.co.uk)
|   Copyright 2011 Mooseypx Design / Xekko
|   =============================================
+---------------------------------------------------------------------------
*/

var Profile = {
	init: function()
	{
		var uid = 0;
		var page = 0;
		var view = '';
	},
	
	loadPage: function(uid, view, page, action)
	{
		// More content link
		this.uid = uid;
		this.page = page;
		this.view = view;

		this.spinner = new ActivityIndicator("body", {image: imagepath + "/spinner_big.gif"});
		new Ajax.Request("member.php",
		{
			parameters: { action: "profile", uid: uid, ajax: "1", view: view, page: page, activity: action, my_post_key: my_post_key },
			onComplete: function(r)
			{
				Profile.updateContent(r, view, action);
			}
		});
	},

	updateContent: function(data, action, type)
	{
		if(data.responseText.match(/<error>(.*)<\/error>/))
		{
			var message = data.responseText.match(/<error>(.*)<\/error>/);
			if(!message[1])
			{
				message[1] = "An unknown error occurred.";
			}

			if(this.spinner)
			{
				this.spinner.destroy();
				this.spinner = '';
			}

			alert('There was an error fetching profile information.\n\n'+message[1]);
		}
		else
		{
			if(this.spinner)
			{
				this.spinner.destroy();
				this.spinner = '';
			}

			if(type == "update")
			{
				if(MyBB.browser == "ie" || MyBB.browser == "opera" || MyBB.browser == "safari" || MyBB.browser == "chrome")
				{
					data.responseText.evalScripts();
				}

				$("nav_tabs").update(data.responseText);
			}
			else
			{
				// We're adding content to the end
				if(data.responseText.indexOf("<!-- Remove -->") != -1)
				{
					$("more_content_button_container").innerHTML = '';
				}
				else if($("more_content_button_container") != null)
				{
					this.page = parseInt(this.page) + 1;					
					$("more_content_button").setAttribute("onclick", "Profile.loadPage('" + this.uid + "', '" + this.view + "', '" + this.page + "', 'append');");
				}

				Element.insert("dynamic_content", { 'bottom': data.responseText }); // Inserts content to the end of the list*/
				$("dynamic_content").evalScripts();
			}
		}
	}
};

Profile.init();
