/******************
 * 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
 *******/
var reqs = new Array();
function FlickrAPI(key, shared_secret){
   var auth_url = 'http://flickr.com/services/auth/?';
   //var rest_url = '/review2/callout/flickrproxy.php?yws_path=';
   var rest_url = '/callout/flickrproxy.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;
   }
   
   
   this.asyncMethodXML = function(method, params, outputfunction, target){
      this._async(method, params, outputfunction, target);
   }

   this.asyncMethodJSON = function(method, params, outputfunction, target){
      params['format'] = 'json'; 
      params['nojsoncallback'] = 1; 
      this._async(method, params, outputfunction, target);
   }
   this._async = function(method, params, outputfunction, target) {
      var asyncHttp;
      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);
      asyncHttp = this.asyncXmlHttpCreate();
      asyncHttp.target = target;
      asyncHttp.processed = false;
      asyncHttp.xhr.onreadystatechange = eval(outputfunction);
      asyncHttp.xhr.open("GET", url, true);
      asyncHttp.xhr.send(""); 
   }
   
/*************************
 * Right now, return only medium sized photos. 
 * We can easily change this later. 
 *************************/
   this.getPhotoURL = function(photo,res){
   var suffix;
   switch(res) {
   case 'high': {
      suffix = '.jpg';
   }break;
   case 'med': {
      suffix = '_m.jpg';
   }break;
   case 'low': {
      suffix = '_t.jpg';
   }break;
   case 'square': {
      suffix = '_s.jpg';
   }break;
   default: {
      suffix = '.jpg';
   }break;
   }
	return "http://static.flickr.com/" + photo.server + "/" + photo.id + "_" + photo.secret + suffix;
   }

   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; 
   }
   this.asyncXmlHttpCreate = function() {
      var req = null;
      var obj = new Object();
      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();
      }
      
      /* find the first empty position in the array and fill it with the new 
         request */
      var c = 0;
      var creq = reqs[c];
      while (creq != null) creq = reqs[++c];
      obj.xhr = req;
      reqs[c] = obj;
      return obj; 
   }
}
