/*!
* hAjax - jQuery prototype
* Version: 0.1
* Maneja las peticiones ajax con historial
*
* Copyright (c) 2010 SOLEMTI (http://www.solemti.com)
* Autor: Efrain Rochin Aramburo
* efrainrochin@solemti.com
*
* http://jquery.com
*
*/

$.hAjax=function(opc){
	var self=$.hAjax;
	self.settings= $.extend({}, self.settings, opc || {});
	self.selector=self.settings.selector;
	$.History.bind(function(hash){
		self.loadPage(hash);
	});
	$(self.selector).click(self.linkClick);
	$.hAjax.afterRequest.push(self.settings.callbacks.afterRequest);
};

$.hAjax.div="";
$.hAjax.selector="";
$.hAjax.id="";
$.hAjax.afterRequest=[];

$.hAjax.loadPage=function(hash){
	var self=$.hAjax;
	var pieces=hash.match(/(.+)\|(.+)/);
	if(pieces && $('#'+pieces[1]).length){
		$(self.settings.indicator).fadeIn('slow');
		$.ajax({
			url:pieces[2],
			success: function(html){
				$("#"+pieces[1]).html(html);
				$(self.settings.indicator).fadeOut('slow');
				self.refresh($("#"+pieces[1]));
				self.execAfterRequest(pieces[1]);
				if(self.settings.callbacks[pieces[1]]){
					self.settings.callbacks[pieces[1]](pieces[1]);
				}
			},
			beforeSend:function(http){
				http.setRequestHeader("X-Update",pieces[1]);
			}
		});
		self.div=pieces[1];
	}else if(!hash && self.div){ // si no llega nada en el hash
		$(self.settings.indicator).fadeIn('slow');
		$.ajax({
			url:window.location.pathname,
			success: function(html){
				$("#"+self.div).html(html);
				$(self.settings.indicator).fadeOut('slow');
				self.refresh($(self.div));
				self.execAfterRequest(self.div.replace("#",""));
				if(self.settings.callbacks[self.div.replace("#","")]){
					self.settings.callbacks[self.div.replace("#","")](self.div.replace("#",""));
				}
			},
			beforeSend:function(http){
				http.setRequestHeader("X-Update",self.div);
			}
		});
	}
};

$.hAjax.linkClick=function(){
	var self=$.hAjax;
	id=$(this).attr("rev").match(/#(.+)/);
	if(id){
		modal.options.buttonCancel=true;
		modal.setButtonAcceptText('{:si}');
		modal.setButtonCancelText('{:no}');
		modal.setImg('/img/icons/48/help.png');
		modal.setText($(this).attr("rel"));
		modal.options.hideOnAccept=false;
		modal.options.doAfterAccept=true;
		var $this=$(this);
		if(!$this.hasClass("action")){
			if($this.attr("rel")!=""){
				modal.options.afterAccept=$.proxy(self.go,$this);
				modal.show();
			}else{
				$.proxy(self.go,$this)();
			}
		}else{
			if($this.attr("rel")!=""){
				modal.options.afterAccept=function(){
					modal.loading();
					var url=$this.attr('href');
					$.ajax({
						url:url,
						success:function(html,status,http){
							eval('var notice = '+http.getResponseHeader('Notice')+';');
							$(id[0]).html(html);
							self.execAfterRequest();
							if(self.settings.callbacks[id[1]]){
								self.settings.callbacks[id[1]](id[1]);
							}
							if(notice){
								var img='/img/icons/48/accept.png';
								if(notice.type!="success"){
									img='/img/icons/48/cancel.png'
								}
								modal.alert(notice.message,img);
							}else{
								modal.cancel();
							}
							self.refresh($(id[0]));
						},
						beforeSend:function(http){
							http.setRequestHeader("X-Update",id[1]);
						}
					});
				};
				modal.show();
			}
		}
		return false;
	}
	return true;
};

$.hAjax.go=function(){
	var self=$.hAjax;
	if($.History.getHash!=""){
		self.div=this.attr("rev");
	}
	self.setHash("#"+id[1]+"|"+this.attr("href"));
};

$.hAjax.setHash=function(hash,callback){
	var pieces=hash.match(/(.+)\|(.+)/);
	if(pieces && callback){
		$.hAjax.settings.callbacks[pieces[1]]=callback;
	}
	$.History.setHash(hash);
}
$.hAjax.getHash=function(){
	return $.History.getHash();
}

$.hAjax.refresh=function(element){
	$($.hAjax.selector,element).click($.hAjax.linkClick);
};

$.hAjax.execAfterRequest=function(id){
	$.each($.hAjax.afterRequest,function(key,afterRequestCallback){
		if(typeof(afterRequestCallback) == 'function'){
			afterRequestCallback(id);
		}
	});
}

$.hAjax.setCallback=function(id,callback){

	if(id && id=="afterRequest"){
		$.hAjax.afterRequest.push(callback);
	}else{
		$.hAjax.settings.callbacks[id]=callback;
	}
}

$.hAjax.settings={
	selector:'',
	indicator:'#Loading',
	callbacks:[]
};

