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

Ojay.HTTP=new JS.Singleton({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 e=new Ojay.HTTP.Request(d,a,b,c);e._3();return e.chain}});Ojay.HTTP.Request=new JS.Class({initialize:function(a,b,c,e){this.verb=a.toUpperCase();if(Ojay.HTTP.VERBS.indexOf(this.verb)==-1)return;this._4=b;this._5=c||{};if(typeof e!='object')e={onSuccess:e};this._0=e||{};this.chain=new JS.MethodChain()},getURI:function(){if(this.uri)return this.uri;return this.uri=Ojay.URI.build(this._4,this._5)},_3:function(){var f=this.getURI();var i=(this.verb=='POST')?f._1():f.toString();var h=(this.verb=='POST')?f.getQueryString():undefined;Ojay.HTTP.notifyObservers('request',{receiver:this});YAHOO.util.Connect.asyncRequest(this.verb,i,{scope:this,success:function(a){var b=new Ojay.HTTP.Response(this,a);var c=this._0.onSuccess;var e=this._0['on'+b.status];var d=this._0.onComplete;c&&Function.from(c)(b);e&&Function.from(e)(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 e=this._0['on'+b.status];var d=this._0.onComplete;c&&Function.from(c)(b);e&&Function.from(e)(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})}},h)}});Ojay.HTTP.Response=new JS.Class({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 g=Ojay.HTTP;var k={JS:/\.js$/i,CSS:/\.css$/i};var l='__ojay_cross_domain__',j='__ojay_jsonp_handler__';var n=function(){Ojay(document.body).insert('<iframe name="'+l+'" style="display: none;"></iframe>','top')}.runs(1);var o=function(a,b){var c=Array.from(arguments),a=c.shift();a.apply(null,c)};var p=function(){delete window[j]};var q=function(a){switch(true){case k.JS.test(a):return'script';break;case k.CSS.test(a):return'css';break;default:return'script';break}};JS.extend(g,{GET:g.GET.wrap(function(a,b,c,e){if(Ojay.URI.parse(b).isLocal()||!YAHOO.util.Get)return a(b,c,e);this.load(b,c,e)}),POST:g.POST.wrap(function(a,b,c,e){if(Ojay.URI.parse(b).isLocal())return a(b,c,e);this.send(b,c)}),load:function(a,b,c){var e=Ojay.URI.parse(a).path,d=q(e),f=Ojay.URI.build(a,b),c=c||{};if(typeof c=='function')c={onSuccess:c};if(f.params.jsonp&&c.onSuccess){f.setParam(f.params.jsonp,j);delete f.params.jsonp;window[j]=o.partial(c.onSuccess);c.onSuccess=p}YAHOO.util.Get[d](f.toString(),c)},send:function(a,b){var c=this._2(a,b,true);n();Ojay(document.body).insert(c.node,'top');c.node.submit();c.remove()},_2:function(c,e,d){var f=Ojay.URI.build(c,e),i=f._1(),h=f.params;var m={action:i,method:'POST'};if(d)m.target=l;return Ojay(Ojay.HTML.form(m,function(a){for(var b in h)a.input({type:'hidden',name:b,value:String(h[b])})})).hide()}});g.GET.redirectTo=function(a,b){window.location.href=Ojay.URI.build(a,b).toString()};g.POST.redirectTo=function(a,b){var c=g._2(a,b,false).node;Ojay(document.body).insert(c,'top');c.submit()};JS.MethodChain.addMethods(g)})();Ojay.URI=new JS.Class({extend:{sanitize:function(a){return String(a).trim().replace('&amp;','&').replace('&#38;','&')},parse:function(e){if(e instanceof this)return e;var d=new this;e=this.sanitize(e).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(e))e.slice(1).split('&').forEach(function(pair){var f=pair.split('=');d.setParam(f[0],f[1])});return d},build:function(a,b){var c=this.parse(a),b=b||{},e;for(var d in b){e=(typeof b[d]=='function')?b[d]():b[d];c.setParam(d,e)}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._6},_6:function(){var a=this._1(),b=[];var c=this.getQueryString();if(c.length)a+='?'+c;if(this.hash)a+='#'+this.hash;return a},_1:function(){return this._7()+(this.domain||'')+this._8()+(this.path||'')},getQueryString:function(){return this.keys.sort().map(function(a){return encodeURIComponent(a)+'='+encodeURIComponent(this.params[a])},this).join('&')},_7:function(){return this.protocol?this.protocol+'://':''},_8: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()});