var onBeforeOverride 	= new YAHOO.util.CustomEvent('');
var onAfterOverride 	= new YAHOO.util.CustomEvent('');
var onBeforeServerPost 	= new YAHOO.util.CustomEvent('');
var onAfterServerPost 	= new YAHOO.util.CustomEvent('');

$(document).ready(function() {
	$(document).override();
});

var request = {
	success:function(oResponse) {
		if (oResponse.responseText !== undefined) {
			$.process(JSON.parse(oResponse.responseText));
		}
		onAfterServerPost.fire()
	},
	falure:function(oResponse) {
		alert(oResponse);
	}
};

// AJAX submit methods

function ajax_submit(event) {
	if (event.type != 'change') {
		event.preventDefault();	
		event.stopPropagation();
	}
	var target = event ? event.target : this;
	if (target === undefined) target = event.srcElement;
	var returnval = $(target).stir();
	if (event.type != 'change') return returnval;
	return true;
};

// jQuery plugins

$.fn.override = function() {	
	return this.each(function() {
		onBeforeOverride.fire();
		$(this).find('form').ajaxify();
		$(this).find("*.click").bind("click", ajax_submit);
		$(this).find("*.change").bind("change", ajax_submit);
		onAfterOverride.fire();		
	});
};

$.getparent = function(item) {
	if (item.parentNode.id) return item.parentNode;
	if (item.parentNode) return parent_object(item.parentNode);
	else return item;
};

$.post = function(url, method, postitems, callback, async) {
	if (async === undefined) async = true;
	YAHOO.util.Connect.initHeader("Accept", "application/json");
	var objcall = YAHOO.util.Connect.asyncRequest(method, url, callback, "args="+postitems, async);
	return objcall;
}

$.fn.stir = function(url) {
	onBeforeServerPost.fire();	
	this.each(function() {
		var stir 	= new Object;
		stir.parent = this.id || this.name || this.parentNode.id || this.parentNode.name;	
		var action = ($(this).get()[0].getAttribute("action"));
		var method = ($(this).get()[0].getAttribute("method"));
		if (method == null) method = 'POST';		
		if (action == null) action = this.href || this.lang || this.parentNode.href || this.parentNode.action || this.parentNode.lang;
		if (action == this.src) action = this.parentNode.href;		
		$(this).gathervalues(stir);	
		var transaction = $.post(action, method, $.json(stir), request);
		return true;
	});
	return true;
};

$.call_ajax = function(url){
	$hold = url.split("/");
	$.post(url, 'POST', '', request);
}

$.json = function(target) {
	return JSON.stringify(target);
};

$.fn.ajaxify = function(callback) {
return this.each(function() {
if ($(this).get(0).enctype != 'multipart/form-data') {
$(this).bind("submit", ajax_submit);
}
});
};

$.fn.gathervalues = function(a) {
	this.each(function() {
		if (this.value) a[this.id || this.name || this.parentNode.id || this.parentNode.name] = this.value;
	});
	this.find("input,textarea,option")
		.filter(":enabled")
		.each(function() {
			if (((this.parentNode.tagName == 'OPTGROUP') || (this.parentNode.type == 'select-one') || (this.parentNode.type == 'select-multiple')) &&
				(!this.selected))
				return;
			if (((this.type == 'radio') || (this.type == 'checkbox')) &&
				(!this.checked))
				return;
				
			a[this.name || this.id || this.parentNode.name || this.parentNode.id || this.parentNode.parentNode.name || this.parentNode.parentNode.id] =this.value;
		});
	return a;
};

$.process = function(target) {
	for(var i in target) {
		var e = target[i];
		if (e.div && e.action != 'call') var el = document.getElementById(e.div.toString());
		switch(e.action) {
			case 'call':
				if(typeof e.string == 'object') {
					var callstr = false;
					for(var ai = 0; ai < e.string.length; ai++) {
						callstr = callstr ? callstr+', e.string['+ai+']' : 'e.string['+ai+']';
					}
				} else {
					var callstr = 'e.string';
				}
				eval(e.div+'('+callstr+');');
				break;
			case 'reload':
				window.location.reload();
				break;
			case 'location':
				document.location = e.string;
				break;
			case 'replace':
				if (el.tagName.toLowerCase() == 'div' && e.string) el.style.display = 'block';
				if (el.tagName.toLowerCase() == 'div' && !e.string) el.style.display = 'none';				
				$(el).html(e.string);
				break;
			case 'replace_outer':
				el.outerHTML = e.string;
				break;
			case 'append':
				$(el).after(e.string);
				break;
			case 'prepend':
				$(el).before(e.string);
				break;			
			case 'remove':		
				$(el).remove();
				break;
			case 'hide':
				$(el).hide();					
				break;
			case 'run':
				eval(e.string);
				break;
			case 'set_name':
				$(el).set('name', e.string);
				break;
			case 'concat':
				$(el).prepend(e.string);
				break;
			default:
				alert(e.string);
				break;
		}
		if(e.callback_class && e.callback_method && e.callback_args){
			e.callback_args.uclass = e.callback_class;
			e.callback_args.umethod = e.callback_method;
			$.post('/'+e.callback_class+'/'+e.callback_method, 'POST',  $.json(e.callback_args), request);
		}
	}
	$(document).override();	
};

$.fn.debug = function() {
  return this.each(function(){
    alert(this);
  });
};