/*
 *
 * Preferences
 *
 */
var DEBUG = 0;
if( location.host == "localhost"){
	DEBUG = 1;
}

var baseurl = "http://tsuyuguchi.com/masaaki/Flickr/";

if( DEBUG ){
		baseurl = "http://localhost/~mtsuyugu/masaaki/Flickr/";
}

/*
 *
 * Main function
 *
 */
function photoset_init(){
	request_photoset_list();
}

/*
 *
 * send request function
 *
 */
function request_photoset_list(){
	var url = baseurl + "flickrPhotoSet.cgi";
	var pars = "fname=processPhotosetsGetList";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: showResponse
		});
}

function request_photoicon( photoset_id ){
	var url = baseurl + "flickrSquarePhoto.cgi";
	var pars = "photoset_id=" + photoset_id;
	pars += "&fname=processSquarePhotoInfo";
	var myAjax = new Ajax.Request(
		url, 
		{
			method: 'get', 
			parameters: pars, 
			onComplete: showResponse
		});
}


/*
 *
 *  Call back functions
 *
 */

/* Called directly by Ajax.Request, this just calls callback function */
function showResponse( originalRequest ){
	/* execute call back function */
	eval( originalRequest.responseText );
}

function processPhotosetsGetList( obj ){

	if( obj["stat"] != "ok" ){
		var ep = new ErrorPrinter();
		ep.put( stat, obj["code"], obj["message"] );
		ep.print( "photoset" );
		return;
	}

	var pp = new PhotosetPrinter( "photoset" );
	pp.clear();
	var ps = obj["photosets"]["photoset"];
	for( i = 0; i < ps.length; i++ ){
		pp.put( ps[i]["title"]["_content"], 
				  ps[i]["description"]["_content"],
				  ps[i]["id"] );
	}
	pp.print();

	for( i = 0; i< ps.length; i++ ){
		request_photoicon( ps[i]["id"] );
	}

}

function processSquarePhotoInfo( obj ){
	$( "ps" + obj["photoset_id"] ).setAttribute("src", obj["source"]);
}


/*
 *
 *  Utility functions
 *
 */

/*
 *  Error Printer
 */
function ErrorPrinter(){
	this.message = "";
}
ErrorPrinter.prototype.put = function( stat, code, message ){
	this.message += "Stat: " + stat + "<br />\n";
	this.message += "Code: " + code + "<br />\n";
	this.message += "Message: " + message + "<br />\n";
}
ErrorPrinter.prototype.print = function( id ){
	$(id).innerHTML += "Error has occured.<br/ >\n" + this.message;
}

/*
 *  Photoset Printer
 */
function PhotosetPrinter( target_id ){
	this.list = "";
	this.id = target_id ;
}
PhotosetPrinter.prototype.clear = function(){
	$(this.id).innerHTML = "";
};
PhotosetPrinter.prototype.put = function( title, desc, id ){
	this.list += '<div class="photoset">'
			+ '<a href="http://www.flickr.com/photos/mtsuyugu/sets/' + id + '/">'
			+ '<img id="ps' + id + '" height="75" width="75" class="photoseticon" '
			+ 'src="' + baseurl + 'blank.jpg" /></a>'
			+  '■ <a href="http://www.flickr.com/photos/mtsuyugu/sets/' + id + '/">' + title + "</a><br />"
			+   desc + "</div>\n";
};
PhotosetPrinter.prototype.print = function(){
	$(this.id).innerHTML +=  this.list ;
};

