/*
Script: jsSmug v0.0.1
	SmugMug REST/JSON API implemenation for SmugMug.

License:
	BSD license.

jsSmug Copyright:
	copyright (c) 2007 Jesse Foster | jf26028, <jesse.foster@gmail.com>, <http://gravitycube.net/>

jsSmug Credits:
	* Developed with consultation from CodePoint Labs, <http://www.codepointlabs.com/>
*/

var jsSmug = new Class(
{
	initialize: function(smugLuvId, apiKey, context, albumsCallback, imagesCallback)
	{
		this._id = smugLuvId;
		this._apiKey = apiKey;
		this._sessionId = null;
		this._urlStack = new Array();
		this._albumsCallback = albumsCallback == null ? null : albumsCallback.bind(context);
		this._imagesCallback = imagesCallback == null ? null : imagesCallback.bind(context);
		this._albums = null;
		this._imageUrls = null;
		
		this._apiGetAlbums = "smugmug.albums.get";
		this._apiGetImagesIds = "smugmug.images.get";
		this._apiLoginAnonymously = "smugmug.login.anonymously";
		this._apiLogout = "smugmug.logout";
	}, 
	
	//
	// Properties
	//
	GetId: function()
	{
		return this._id;
	},
	
	GetCurrentAlbums: function()
	{
		return this._albums;
	}, 
	
	GetCurrentImageUrls:  function()
	{
		return this._imageUrls;
	},

	//
	// Methods
	//
	GetAlbums: function(nickname)
	{
		//
		// Login, GetAlbums, Logout, _albumsCallback
		//
		this._urlStack = new Array(); // reset the array.

		var url;

		//
		// 20071012 - Anonymous logout does not seem to work.
		// Anonymous logout fails so badly that it does not put the callback wrapper around it.
		// Hopefully, it is something I an doing incorrectly.  Try http://api.smugmug.com/services/api/json/1.2.1/?/services/api/json/1.2.1/?method=smugmug.logout&APIKey={key}&SessionID={anonymous session id}
		//
		// For now, just do not call it.
		//
		//url = this._GetUrl(this._apiLogout, "");
		//this._urlStack.push(url);
		
		url = this._GetUrl(this._apiGetAlbums, "&NickName=" + nickname);
		this._urlStack.push(url);
		
		this._Login();
	},
	
	GetImages: function(albumID)
	{
		//
		// Login, GetImages, GetImageUrls, Logout, _imagesCallback
		//
		this._urlStack = new Array(); // reset the array.
		this._imageUrls = new Array(); // reset the image urls.

		var url;

		//
		// 20071012 - Anonymous logout does not seem to work.
		// Anonymous logout fails so badly that it does not put the callback wrapper around it.
		// Hopefully, it is something I an doing incorrectly.  Try http://api.smugmug.com/services/api/json/1.2.1/?/services/api/json/1.2.1/?method=smugmug.logout&APIKey={key}&SessionID={anonymous session id}
		//
		// For now, just do not call it.
		//
		//url = this._GetUrl(this._apiLogout, "");
		//this._urlStack.push(url);
		
		url = this._GetUrl(this._apiGetImagesIds, "&AlbumID=" + albumID + "&Heavy=1");
		this._urlStack.push(url);
		
		this._Login();
	}, 

	//
	// Private Methods
	//	
	
	_Login: function()
	{
		var url = this._GetUrl(this._apiLoginAnonymously, "");
		this._GetJSONAsync(url);
	}, 
	
	_GetUrl: function(remoteMethod, qsParameters)
	{		
		var smugmugApiUrl = "http://api.smugmug.com/services/api/json/1.2.1/?";
		var apiMethod = "method=" + remoteMethod;
		var apiKey = "&APIKey=" + this._apiKey;
		var jsonCallback = "";

		//
		// Dont callback after logoff because that api is broken as of 20071012.
		//
		if (remoteMethod != this._apiLogout)
		{
			jsonCallback = "&JSONCallback=window." + this._id + ".smugMugApi.ProcessResponse";
		}
		
		var sessionId = this._sessionId == null ? "" : "SessionID=" + this._sessionId;

		return smugmugApiUrl + apiMethod + apiKey + jsonCallback + qsParameters;
	},
	
	_GetJSONAsync: function(url)
	{
		var script = document.createElement("script");
		script.type = "text/javascript";
		script.src = url;
		document.getElementsByTagName("head")[0].appendChild(script);
	}, 
	
	ProcessResponse: function(response)
	{
		if (response.stat == "ok")
		{
			switch (response.method)
			{
				//
				// Logged in successfully.
				//
				case this._apiLoginAnonymously:
					this._sessionId = response.Login.Session.id;
					
					var url = this._urlStack.pop() + "&SessionID=" + this._sessionId;

					this._GetJSONAsync(url);
					break;
				
				//
				// Albums returned successfully.
				//
				case this._apiGetAlbums:
					//
					// Save the albums
					//
					this._albums = response.Albums;
					
					//
					// If there is an album callback, call it here.
					//
					if (this._albumsCallback)
					{
						try
						{
							this._albumsCallback(this, response.Albums);
						}
						catch (exception)
						{
							// This script does not care about this exception, but another developer may.  throw here if you are debugging.
						}
					}
					
					//
					// If there are more urls in the queue, call them here.
					//
					var url = this._urlStack.pop();
					if (url)
					{
						url += "&SessionID=" + this._sessionId;
						this._GetJSONAsync(url);
					}
					
					break;
				
				//
				// Image Ids Returned Successfully
				//
				case this._apiGetImagesIds:
					//
					// Save the images
					//
					this._imageUrls = response.Images;
					
					if (this._imagesCallback)
					{
						try
						{
							this._imagesCallback(this, this._imageUrls);
						}
						catch (exception)
						{
							// This script does not care about this exception, but another developer may.  throw here if you are debugging.
						}
					}

					break;
				
				default:
					alert("Unhandled response.  Method:  " + response.method);
					break;
			}
		}
		else
		{
			if (response.method == this._apiLogout)
			{
			}
			else
			{
				alert("Error processing album:  " + response.stat + "\n[" + response.code + "] " + response.message);
			}
		}
	}
});

