/*
Copyright (c) 2007-2008 the OTHER media Limited
Licensed under the BSD license, http://ojay.othermedia.org/license.html
Version: 0.4.1
Build:   min
*/

Ojay.HTTP=new JS.Singleton('Ojay.HTTP',{include:Ojay.Observable,READY_STATE:{UNINITIALIZED:0,LOADING:1,LOADED:2,INTERACTIVE:3,COMPLETE:4},VERBS:'GET POST PUT DELETE HEAD'.split(/\s+/)});Ojay.HTTP.VERBS.forEach(function(d){Ojay.HTTP[d]=function(a,b,c){var f=new Ojay.HTTP.Request(d,a,b,c);f._4();return f.chain}});Ojay.HTTP.Request=new JS.Class('Ojay.HTTP.Request',{initialize:function(a,b,c,f){this.verb=a.toUpperCase();if(Ojay.HTTP.VERBS.indexOf(this.verb)==-1)return;this._5=b;this._1=c||{};if(typeof f!='object')f={onSuccess:f};this._0=f||{};this.chain=new JS.MethodChain()},getURI:function(){if(this.uri)return this.uri;var a=(typeof this._1=='string')?{}:this._1;return this.uri=Ojay.URI.build(this._5,a)},_4:function(){var g=(this.verb=='POST'),h=this.getURI(),j=g?h._2():h.toString(),k=g?this._6(h):undefined;Ojay.HTTP.notifyObservers('request',{receiver:this});YAHOO.util.Connect.asyncRequest(this.verb,j,{scope:this,success:function(a){var b=new Ojay.HTTP.Response(this,a);var c=this._0.onSuccess;var f=this._0['on'+b.status];var d=this._0.onComplete;c&&Function.from(c)(b);f&&Function.from(f)(b);d&&Function.from(d)(b);this.chain.fire(b);Ojay.HTTP.notifyObservers('success',{receiver:b});Ojay.HTTP.notifyObservers(b.status,{receiver:b});Ojay.HTTP.notifyObservers('complete',{receiver:b})},failure:function(a){var b=new Ojay.HTTP.Response(this,a);var c=this._0.onFailure;var f=this._0['on'+b.status];var d=this._0.onComplete;c&&Function.from(c)(b);f&&Function.from(f)(b);d&&Function.from(d)(b);Ojay.HTTP.notifyObservers('failure',{receiver:b});Ojay.HTTP.notifyObservers(b.status,{receiver:b});Ojay.HTTP.notifyObservers('complete',{receiver:b})}},k)},_6:function(a){return(typeof this._1=='string')?this._1:a.getQueryString()}});Ojay.HTTP.Response=new JS.Class('Ojay.HTTP.Response',{initialize:function(b,c){'status statusText responseText responseXML readyState'.split(/\s+/).forEach(function(a){this[a]=c[a]},this);this.request=b;this.transport=c},insertInto:function(a,b){a=a.setContent?a:Ojay(a);var c=(this.responseText||'').stripScripts();if(!b)a.setContent(c);else a.insert(c,b);return this},evalScripts:function(){if(this.responseText)this.responseText.evalScripts();return this},parseJSON:function(){return(this.responseText||'').parseJSON()}});(function(){var i=Ojay.HTTP;var l={JS:/\.js$/i,CSS:/\.css$/i};var m='__ojay_cross_domain__',n='__ojay_jsonp_handler__',o=0;var p=function(){Ojay(document.body).insert('<iframe name="'+m+'" style="display: none;"></iframe>','top')}.runs(1);var q=function(){return n+(o++)};var r=function(a,b){var c=Array.from(arguments),a=c.shift();a.apply(null,c)};var s=function(a){window[a]=null;try{delete window[a]}catch(e){}}.curry();var t=function(a){switch(true){case l.JS.test(a):return'script';break;case l.CSS.test(a):return'css';break;default:return'script';break}};JS.extend(i,{GET:i.GET.wrap(function(a,b,c,f){if(Ojay.URI.parse(b).isLocal()||!YAHOO.util.Get)return a(b,c,f);this.load(b,c,f)}),POST:i.POST.wrap(function(a,b,c,f){if(Ojay.URI.parse(b).isLocal())return a(b,c,f);this.send(b,c)}),load:function(a,b,c){var f=Ojay.URI.parse(a).path,d=t(f),g=Ojay.URI.build(a,b),c=c||{};if(typeof c=='function')c={onSuccess:c};if(g.params.jsonp&&c.onSuccess){var h=q();g.setParam(g.params.jsonp,h);if(g.params.jsonp!=='jsonp')delete g.params.jsonp;window[h]=r.partial(c.onSuccess);c.onSuccess=s(h)}YAHOO.util.Get[d](g.toString(),c)},send:function(a,b){var c=this._3(a,b,true);p();Ojay(document.body).insert(c.node,'top');c.node.submit();c.remove()},_3:function(c,f,d){var g=Ojay.URI.build(c,f),h=g._2(),j=g.params;var k={action:h,method:'POST'};if(d)k.target=m;return Ojay(Ojay.HTML.form(k,function(a){for(var b in j)a.input({type:'hidden',name:b,value:String(j[b])})})).hide()}});i.GET.redirectTo=function(a,b){window.location.href=Ojay.URI.build(a,b).toString()};i.POST.redirectTo=function(a,b){var c=i._3(a,b,false).node;Ojay(document.body).insert(c,'top');c.submit()};JS.MethodChain.addMethods(i)})();Ojay.URI=new JS.Class('Ojay.URI',{extend:{sanitize:function(a){return String(a).trim().replace('&amp;','&').replace('&#38;','&')},parse:function(f){if(f instanceof this)return f;var d=new this;f=this.sanitize(f).replace(/^(\w+)(:\/+)/,function(a,b,c){d.protocol=b;return c}).replace(/^:\/+([^\:\/]+)/,function(a,b){d.domain=b;return''}).replace(/^:(\d+)/,function(a,b){d.port=b;return''}).replace(/^[^\?\#]+/,function(a,b){d.path=a;return''}).replace(/#(.*)$/,function(a,b){d.hash=b;return''});if(!d.port)d.port=(d.domain==this.local.domain)?this.local.port:this.DEFAULT_PORTS[d.protocol];if(d.path.charAt(0)!='/'&&d.domain==this.local.domain)d.path=this.local.directory+d.path;if(/^\?/.test(f))f.slice(1).split('&').forEach(function(pair){var g=pair.split('=');d.setParam(g[0],g[1].replace('+',' '))});return d},build:function(a,b){var c=this.parse(a),b=b||{},f;for(var d in b){f=(typeof b[d]=='function')?b[d]():b[d];c.setParam(d,f)}return c},compare:function(a,b){return this.parse(a).equals(b)},DEFAULT_PORTS:{http:'80',https:'443'}},initialize:function(){this.protocol=this.klass.local.protocol;this.domain=this.klass.local.domain;this.path='';this.keys=[];this.params={};this.toString=this._7},_7:function(){var a=this._2(),b=[];var c=this.getQueryString();if(c.length)a+='?'+c;if(typeof this.hash==='string')a+='#'+this.hash;return a},_2:function(){return this._8()+(this.domain||'')+this._9()+(this.path||'')},getQueryString:function(){return this.keys.sort().map(function(a){return encodeURIComponent(a)+'='+encodeURIComponent(this.params[a])},this).join('&')},_8:function(){return this.protocol?this.protocol+'://':''},_9:function(){if(!this.port||this.port==this.klass.DEFAULT_PORTS[this.protocol])return'';return':'+this.port},equals:function(a){a=this.klass.parse(a);if(this.domain!=a.domain||this.protocol!=a.protocol||this.port!=a.port||this.path!=a.path||this.hash!=a.hash)return false;if(!this.paramsEqual(a))return false;return true},setParam:function(a,b){var c=[a,b].map(decodeURIComponent).map('trim');if(this.keys.indexOf(c[0])==-1)this.keys.push(c[0]);this.params[c[0]]=c[1]},paramsEqual:function(a){a=this.klass.parse(a);for(var b in this.params){if(this.params[b]!=a.params[b])return false}for(b in a.params){if(this.params[b]!=a.params[b])return false}return true},isLocal:function(){return this.protocol==this.klass.local.protocol&&this.domain==this.klass.local.domain&&this.port==this.klass.local.port}});Ojay.URI.extend({local:{protocol:window.location.protocol.replace(/\W/g,''),domain:window.location.hostname,directory:window.location.pathname.replace(/[^\/]*$/,'')}});Ojay.URI.local.port=window.location.port||Ojay.URI.DEFAULT_PORTS[Ojay.URI.local.protocol||'http'];JS.extend(String.prototype,{parseURI:Ojay.URI.method('parse').methodize(),equalsURI:Ojay.URI.method('compare').methodize()});