/******************
 * Please read the license agreement.
 * You may not remove or change this notice.
 * Do not sell this as your own work or remove this copyright notice.
 * This script is freely distributable under the terms of 
 *    an MIT-style license (http://www.opensource.org/licenses/mit-license.php)
 * Copyright Tim Tully 2006. All rights reserved.
 * 
 * FlickrAPI is a class to talk to the Flickr backend w/o 
 * using any middle tier logic. 
 * @author Tim Tully(tully_tim@yahoo.com)
 * @version 1.0
 * @constructor
 * @param {String} key This is the application key
 * @param {String} shared_secret This is the shared secret for the user
 *******/
function FlickrAPI (key, shared_secret)
{
	var auth_url = 'http://flickr.com/services/auth/?';
	var rest_url = '/proxy.php?yws_path=';
	var key = key;
	var shared_secret = shared_secret;
	var xmlhttp = null;

  this.getLoginURL = function (perms)
	{
		var url = auth_url;
		var sig;
		url += "api_key=" + key + "&perms=" + perms + "&api_sig=" + getSig(perms);
		return url;
  }

/******************
 * This method calls a flickr api method using the passed hash params
 *****************/
  this.callMethodXML = function (method, params)
	{
		return _call (method, params);
  }

  this.callMethodJSON = function (method, params)
	{
		params['format'] = 'json';
		params['nojsoncallback'] = 1; 
		return _call (method, params);
  }

  function _call (method, params)
	{
		var url = rest_url;
		var tmp_url = "http://api.flickr.com/services/rest?method=" + method + "&api_key=" + key;
		for (key in params)
		{
			tmp_url += "&" + key + "=" + params[key]; 
		}
		url += encodeURIComponent (tmp_url);
		xmlhttp = xmlHttpCreate ();
		xmlhttp.open ("GET", url, false);
		xmlhttp.send (""); 
		if (params['format'] = 'json')
		{	
	  	return xmlhttp.responseText;
		}
		return xmlhttp.responseXML;
	}

/*************************
 * Right now, return only medium sized photos. 
 * We can easily change this later. 
 *************************/
  this.getPhotoURL = function (photo)
	{
		return "http://static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + "_b.jpg";
  }

	

  function getSig (perms)
	{
		var sig = shared_secret + "api_key" + key +  "perms" + perms; 
		return hex_md5 (sig);
	}

  function xmlHttpCreate ()
	{
		var req = null;
		try
		{
    	req = new ActiveXObject ("Msxml2.XMLHTTP");
    }
    catch (e)
		{
    	try
			{
      	req = new ActiveXObject ("Microsoft.XMLHTTP");
      }
      catch (sc)
			{
      	req=null;
      }
    }
    if (!req && typeof XMLHttpRequest != "undefined")
		{
    	req = new XMLHttpRequest ();
    }
		return req; 
	}
}

function get_random_picture ()
{
	var flickr = new FlickrAPI ('f5da66aa9aad6feeacf4850fe0694ef9', '');
	var params = new Array ();
	params['user_id'] = '39305496@N04';
	params['tags'] = 'CCUA';
	params['api_key'] = 'f5da66aa9aad6feeacf4850fe0694ef9';
	var json = flickr.callMethodJSON ('flickr.photos.search', params);
		var obj = eval ('(' + json + ')');
		var photos = obj.photos.photo;
		var i = Math.floor (Math.random () * photos.length);
		return photos[i];	
}

function write_random_picture ()
{
	var flickr = new FlickrAPI ('f5da66aa9aad6feeacf4850fe0694ef9', '');
	var photo = get_random_picture ();
	var url = flickr.getPhotoURL (photo);

	var img = document.createElement ('img');
	img.setAttribute ('src', url);

	var link = document.createElement ('a');
	link.setAttribute ('href', '/pictures/slideshow.shtml?photoid=' + photo.id);
	link.appendChild (img);

	document.getElementById ('picture').appendChild (link); 
}

function write_slideshow_pictures ()
{
	// assuming only one parameter is passed to this page
	var flickr = new FlickrAPI ('f5da66aa9aad6feeacf4850fe0694ef9', '');
	var params = new Array ();
	params['user_id'] = '39305496@N04';
	params['tags'] = 'CCUA';
	params['api_key'] = 'f5da66aa9aad6feeacf4850fe0694ef9';

	var photoID;
	// check if photoid is passed
	if (location.search.search ('photoid=') > 0 && location.search.search ('&') < 0) {
		photoID = location.search.slice (9, location.search.length);
	} else if (location.search.length == 0) {
		var photo = get_random_picture ();
		photoID = photo.id;
	}
	params['photo_id'] = photoID;
	
	var json = flickr.callMethodJSON ('flickr.photos.getInfo', params);
	var obj = eval ('(' + json + ')');
	var currentURL = flickr.getPhotoURL (obj.photo);

	json = flickr.callMethodJSON ('flickr.photos.getContext', params);
	obj = eval ('(' + json + ')');
	var previousID = obj.prevphoto.id;
	var previousURL = obj.prevphoto.thumb;
	var nextID = obj.nextphoto.id;
	var nextURL = obj.nextphoto.thumb;

	document.getElementById ('slideshow').innerHTML = '';

	if (previousID != 0)
	{
		document.getElementById ('slideshow').innerHTML = '<a href="/pictures/slideshow.shtml?photoid=' + previousID + '"><img title="Previous picture in slideshow" src="' + previousURL +'" /></a>';
	}

	document.getElementById ('slideshow').innerHTML += '<img src="' + currentURL + '" />';

	if (nextID != 0)
	{
		document.getElementById ('slideshow').innerHTML += '<a href="/pictures/slideshow.shtml?photoid=' + nextID + '"><img title="Next picture in slideshow" src="' + nextURL + '" /></a>';
	}
}
