var ajaxizable=new Class({Implements:[Options,Events],options:{watcher:{hostname:location.hostname,pathname:"",actualHash:false,tempHash:false,start:true,periodical:1000,update:"",baseCheck:"",obviate:{"class":"noAjax",rel:"noAjax"},onRequest:function(){},onComplete:function(){},onCancel:function(){},onSuccess:function(){},onFailure:function(){},onException:function(){}},rQueue:{use:true,obj:null,base:"req_"},request:{ajaxFile:"/index.ajax.php?ajax=",method:"post",evalScripts:true,noCache:true,update:null,append:null,onRequest:function(){},onComplete:function(){},onCancel:function(){},onSuccess:function(){},onFailure:function(){},onException:function(){},replace:false,addAjaxNav:true,url:null,waiter:{use:true,baseHref:"/assets/",img:{styles:{width:"auto",height:"auto"}},containerPosition:{},containerProps:{},msg:false,msgProps:{},layer:{},fxOptions:{},useIframeShim:true,iframeShimOptions:{},delay:1000},toSend:{url:"",type:"",method:null,debug:false},sticky:{},form:{}}},initialize:function(a){this.setOptions(a);if(this.options.rQueue.use){this.options.rQueue.obj=new Request.Queue()}if(this.options.watcher.start){this.watcherInitialize(this.options.watcher.baseChek);this.watcher.periodical(this.options.watcher.periodical,this)}},watcherInitialize:function(){this.watcherAddAjaxNav()},watcherOptions:function(){var a={method:"get",onRequest:this.options.watcher.onRequest,onComplete:this.options.watcher.onComplete,onCancel:this.options.watcher.onCancel,onSuccess:this.options.watcher.onSuccess,onFailure:this.options.watcher.onFailure,onException:this.options.watcher.onException,replace:true,toSend:{type:"navigation"}};return a},watcherAddAjaxNav:function(a){a=$chk(a)?($type(a)=="element"?a:document.id(a)):document;var b="http://"+this.options.watcher.hostname+this.options.watcher.pathname;a.getElements("a").each(function(c){if(c.href.test(b)&&!this.watcherObviate(c)){c.removeEvents("click");c.addEvent("click",function(d){d.preventDefault();this.gotoUpdate(c.href,this.options.watcher.update,this.watcherOptions())}.bind(this))}}.bind(this))},watcherObviate:function(a){for(key in this.options.watcher.obviate){if(a.get(key)&&a.get(key).test(this.options.watcher.obviate[key])){return true}}return false},watcher:function(){switch(true){case (location.hash=="#/user/logout/"):location.hash="#";break;case (location.hash!=this.options.watcher.actualHash):if(!location.hash||location.hash=="#"){ret=location.href.replace("#","");ret=ret.replace("http://"+location.hostname,"")}else{ret=location.hash.replace("#","")}if(ret==this.options.watcher.tempHash){return true}this.options.watcher.tempHash=ret;this.gotoUpdate(ret,this.options.watcher.update,this.watcherOptions());break}},_goto:function(l){var f=l.element?l.element:(l.update?l.update:l.append);if(l.waiter.use){var i=""}if(!$chk(l.url)&&this.options.watcher.actualHash){l.url=l.toSend.url=rawurlencode(this.options.watcher.actualHash.replace("#",""))}if(!$type(l.validate)){l.validate=true}var b=(l.ajaxFile?l.ajaxFile:l.url)+JSON.encode(l.toSend);var d={url:b,method:l.method,evalScripts:l.evalScripts,noCache:l.noCache,onRequest:function(){if(l.waiter.use){i=new Waiter(f,l.waiter);i.start()}l.onRequest(l)}.bind(this),onComplete:function(n,e,o,m){l.onComplete(l)}.bind(this),onCancel:function(){l.onCancel(l);if(l.waiter.use){i.stop.delay(l.waiter.delay,i);document.getElements(".waitingDiv").destroy()}},onSuccess:function(r,n,s,p){if(l.replace&&l.url){var m=location.href;var q=location.hash;var o="http://"+this.options.watcher.hostname+this.options.watcher.pathname;if(!q){q=location.href.replace(o,"#")}location.hash="#"+l.url.replace(o,"");this.options.watcher.tempHash=this.options.watcher.actualHash=location.hash}if(l.addAjaxNav){$console("AddAjax al element "+f);this.watcherAddAjaxNav(f)}l.onSuccess(l,r,n,s,p);if(l.waiter.use){i.stop.delay(l.waiter.delay,i);var e=document.getElements(".waitingDiv");e.destroy.delay(l.waiter.delay,e)}}.bind(this),onFailure:function(){l.onFailure(l);if(l.waiter.use){i.stop.delay(l.waiter.delay,i)}},onException:function(e){l.onException(l,e);if(l.waiter.use){i.stop.delay(l.waiter.delay,i)}}};var c=false;switch(l.toSend.type){case"formUpdate":var g=$merge(this.options.form,l);g.onSuccess=function(){};g.requestOptions=$merge(d,g.requestOptions);$console("cargamos el metodo fUpdate de "+g.form+" "+g.element);if(g.validate){new Form.Validator.Tips(g.form,{pointyTipOptions:{point:12},onFormValidate:function(o,n,m){if(o){var e=new Form.Request(g.form,g.element,g);e.send()}m.preventDefault()}})}else{var j=new Form.Request(g.form,g.element,g)}break;case"formAppend":var k=$merge(this.options.form,l);k.requestOptions=$merge(d,k.requestOptions);$console("cargamos el metodo fAppend de "+k.form);var j=new Form.Request.Append(k.form,k.element,k);break;default:c=true;if($chk(l.update)){if($type(l.update)==="string"){d.update=document.id(l.update)}else{d.update=l.update}}if($chk(l.append)){d.append=l.append}var j=new Request.HTML(d)}if(this.options.rQueue.use&&j){var a=l.queueName?l.queueName:this.options.rQueue.base+$time();this.options.rQueue.obj.addRequest(a,j)}try{$console("ejecutamos el metodo Request.HTML");if(c&&j){j.send()}}catch(h){$console("fallo el AJAX "+h)}},gotoUpdate:function(c,d,b){var e={url:rawurlencode(c),type:"update"};if(!$chk(b)){b={}}b.toSend=$merge(e,b.toSend);var a=$merge(this.options.request,b);a.update=d;a.url=c;this._goto(a)},gotoAppend:function(c,d,b){var e={url:rawurlencode(c),type:"append"};if(!$chk(b)){b={}}b.toSend=$merge(e,b.toSend);var a=$merge(this.options.request,b);a.append=d;a.url=c;this._goto(a)},formUpdate:function(d,c,b){var e={type:"formUpdate",url:rawurlencode(d.get("action"))};if(!$chk(b)){b={}}b.toSend=$merge(e,b.toSend);var a=$merge(this.options.request,b);a.element=c;a.form=d;a.url=d.get("action");this._goto(a)},formAppend:function(d,c,b){var e={type:"formAppend"};if(!$chk(b)){b={}}b.toSend=$merge(e,b.toSend);var a=$merge(this.options.request,b);a.element=c;a.form=d;this._goto(a)},getScript:function(f,d,b){var e={id:"script_"+f,onload:function(){}};if(!$chk(b)){b={}}var a=$merge(e,b);if($type(d)=="function"){a.onload=d}if(document.id(a.id)&&!a.force){$console("el script "+f+" esta ya, ejecutamos la funcion");a.onload();return true}var c=new Asset.javascript(f,a)},getStyles:function(e,b){var d={id:"style_"+e,rel:"stylesheet",href:e};var a=$merge(d,b);if(document.id(a.id)&&!a.force){return false}var c=new Asset.css(e,b)},getStickyPoint:function(d,c){if(!$chk(c)){c={}}var a=c.onSuccess;c.onSuccess=function(h,k,g,l,i){var f=h.sticky.body?h.sticky.body:l;var j=new StickyWin.PointyTip(h.sticky.caption,f,h.sticky);if(h.addAjaxNav){$console("AddAjax al sticky "+j);this.watcherAddAjaxNav(j)}if($type(a)=="function"){a(h,k,g,l,i)}}.bind(this);var e={replace:false,addAjaxNav:false,toSend:{url:d,type:"sticky"}};var b=$merge(this.options.request,e,c);b.url=d;this._goto(b)}});function rawurlencode(f){if(!$chk(f)||$empty(f)){return""}var e={},b=[];var a=f.toString();var d=function(i,h,j){var g=[];g=j.split(i);return g.join(h)};e["'"]="%27";e["("]="%28";e[")"]="%29";e["*"]="%2A";e["~"]="%7E";e["!"]="%21";a=encodeURIComponent(a);a=d("%20"," ",a);for(var c in e){replace=e[c];a=d(c,replace,a)}return a.replace(/(\%([a-z0-9]{2}))/g,function(i,h,g){return"%"+g.toUpperCase()});return a};
