/**
 * The Shadowbox (v3.x) ajax player class. jQuery ONLY!
 *
 * Retrieves and displays partial HTML from server via Ajax
 * (where 'partial' implies valid HTML, but not a full web page!)
 *
 * To use:
 *   Include 'ajax' as a player in the Shadowbox.init() call options
 *      eg. Shadowbox.init({players;['img','ajax']});
 *   Set the link's player to 'ajax'
 *     eg. < a href='ajaxpartial.php' rel='shadowbox;player=ajax'>Link< /a>
 *   By default, the expected response format is HTML
 *
 * The ajax() call options can be overriden by setting Shadowbox options.ajax
 * to an object containing Options as defined on http://docs.jquery.com/Ajax/jQuery.ajax#options
 * eg. to set a JSON data type (instead of HTML)
 *   var opts = { ajax : { dataType:'json', success:myAjaxHandler }
 *              , player: 'ajax'
 *              };
 *   var myAjaxHandler = function(data, responseText){
 *       this.html = '<div>'+data.text+'</div>';
 *       this.ready = true;
 *     };
 *   Shadowbox.init(opts);
 * Be warned : there are numerous options available for the jQuery.ajax() method, and
 *             experimenting with them could easily cause the call to fail!
 * NOTE : If overridden, the success, error and complete callbacks get scope of the Shadowbox.ajax
 *        instance, in place of the XMLHtmlRequest that is usually the context for jQuery.
 *        The callbacks - especially success - are expected/required to set this.html to
 *        hold the displayable HTML, and this.ready=true to indicate that the ajax call
 *        is complete and ready to be shown.
 */
if(typeof jQuery == 'undefined'){
  throw 'Unable to load Shadowbox Ajax player, jQuery library not found';
}else{
  (function(S){
    S.ajax = function(obj){ //constructor
        this.obj = obj; //cache object
        this.height = this.obj.height ? parseInt(this.obj.height, 10) : 300; //default to 300
        this.width = this.obj.width ? parseInt(this.obj.width, 10) : 500; //default to 500
        this.html = ''; //will be set from the ajax response
        this.ready = false; //make Shadowbox wait for the ajax all to return
        var me = this
          , opts = {}; //temporary object, only set if overrides are set in the cache's options.ajax object
        if(this.obj.options){
            jQuery.each(this.obj.options.ajax||{}, function(k,v){
                  if({complete:1,error:1,success:1}[k]){
                    //wrap function so as to change callback's context from the XMLHttpRequest to the Shadowbox.ajax instance...
      	            //NB success callback (at least) MUST assign this.html, and set this.ready=true
                    opts[k] = function(){ v.apply(me, arguments); };
                  }else{
                    opts[k] = v;
                  }
            });
        }
        var newurl = (obj.content.indexOf('?') > -1) ? '&ajax=1' : '?ajax=1';
        
        //apply overrides (if any) to defaults...
        var settings = jQuery.extend( { url:obj.content + newurl }
                                     , opts );
        jQuery.ajax(settings); //Go For It!
      };

    S.ajax.prototype = {
        /**
         * Appends this object to the document.
         *
         * @param   HTMLElement     body    The body element
         * @param   String          id      The content id
         * @param   Object          dims    The current Shadowbox dimensions
         * @return  void
         * @public
         */
        append: function(body, id, dims){
            this.id = id;
            var div = document.createElement('div');
            div.id = id;
            div.className = 'html html_ajax'; // give 'html' class to enable scrolling, plus 'html_ajax' just in case it's needed
            div.innerHTML = this.html;
            body.appendChild(div);
        } //end append()
        /**
         * Removes this object from the document.
         *
         * @return  void
         * @public
         */
      , remove: function(){
          var el = document.getElementById(this.id);
          if(el) S.lib.remove(el);
        } //end remove()
      }; //end prototype
  })(Shadowbox);
} //end of test for jQuery
