// Thanks to: http://n2.nabble.com/player%3Aajax%2C-your-starter-for-10%2C-jQuery-ONLY.html
/**
 * The Shadowbox 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:
 *   Set 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 and 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 can easily cause the call to fail!
 * NOTE : If overridden, the success, error and complete callbacks get context 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(){
    var SL = Shadowbox.lib;
    //id is 'shadowbox_content'; obj is the cache object...
    Shadowbox.ajax = function(id, obj){ //constructor
        this.id = id;
        this.obj = obj;
        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
          
        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;
            }
          });
        //apply overrides (if any) to defaults...
        this.options = jQuery.extend( { type:'GET', url:obj.content, success:function(data){ me.html=jQuery.trim(data); me.ready=true; } }, opts );
        jQuery.ajax(this.options); //Go For It!
    };
    Shadowbox.ajax.prototype = {
        markup: function(dims){
            return { tag:    'div'
                   , id:     this.id
                   , cls:    'html' //keep html class
                   , html:   this.html
                   };
        } //end markup()
      , remove: function(){ //kept adapter calls here...
            var el = SL.get(this.id);
            if(el) SL.remove(el);
        } //end remove()
      }; //end prototype
})();
} //end of test for jQuery 