/***************************************************************************************
    jQuery Cross Domain Ajax Plugin
    (c)2010 Martin Boynton - work in progress
    
    if the response is XML, use another plugin:
    $.loadXML(o.responseText)
    
    currently accepts:
        url:"http://www.google.com/"
        success:function(o){}
        error:function(o){}
        timeout:1000
        contentType:"text/xml"
        type:"POST"
        data:string
*/
jQuery.xdajax_ready=jQuery.xdajax_loading=false;
jQuery.xdajax_setup=function(){
    jQuery.xdajax_loading=true;
    var obj={
        inc:0,
        files:[
            "http://yui.yahooapis.com/2.8.1/build/yahoo-dom-event/yahoo-dom-event.js",
            "http://yui.yahooapis.com/2.8.1/build/connection/connection-min.js"
        ],
        load:function(){
            var _self=this;
            jQuery.getScript(this.files[this.inc], function(){_self.loadCallback()});
        },
        loadCallback:function(){
            this.inc++;
            if(this.inc==this.files.length) {
                this.prepare();
            }else{
                this.load();
            }
        },
        prepare:function(){
            var _self=this;
            try{
                YAHOO.util.Connect.transport('/connection.swf?t=' + new Date().valueOf().toString());
                YAHOO.util.Connect.xdrReadyEvent.subscribe(function() {
                    jQuery.xdajax_ready=true;
                    jQuery.xdajax_loading=false;
                });
            } catch(e) {
                var _self=this;
                setTimeout(function(){_self.prepare()},50);
            }
        }
    }
    
    obj.load();
}
jQuery.xdajax=function(s){
    if(!jQuery.xdajax_ready){
        if(!jQuery.xdajax_loading)jQuery.xdajax_setup();
        setTimeout(function(){jQuery.xdajax(s)},50);
        return false;
    }
    
    var callback={}
    if(typeof s.success != "undefined")callback.success=s.success;
    if(typeof s.error != "undefined")callback.failure=s.error;
    if(typeof s.timeout != "undefined")callback.timeout=s.timeout;

    if(s.contentType) {
        YAHOO.util.Connect._use_default_post_header = false;
        YAHOO.util.Connect.initHeader( 'Content-Type', s.contentType, false );
    }
    callback.xdr = true;
    s.type=s.type||'POST';
    s.data=s.data||null;
    var service_call = YAHOO.util.Connect.asyncRequest(s.type, s.url, callback, s.data);
}

jQuery.loadXML=function(s){
    var d;
    if(jQuery.browser.msie){
        d=new ActiveXObject('Microsoft.XMLDOM');
        d.async='false'
        d.loadXML(s);
    }else{
        d=(new DOMParser()).parseFromString(s, 'text/xml');
    }
    return jQuery(d);
}

