
var buzz_dialogs = new Array();

YUI_config = {
  insertBefore: 'styleoverrides',
  sync: true
};


var Y = new YUI();

var handleSuccess2 = function(o){
	var ioId = '';
	var args = new Array();
	handleSuccess(ioId, o, args);
}

var handleFailure2 = function(o){
	var ioId = '';
	var args = new Array();
	handleFailure(ioId, o, args);
}

var found_handelers = new Array(); 
var found_handelers2 = new Array(); 

function runHandler(success_action_handler, data, args) {

	buzzDebug("success handeler: "+success_action_handler);

	var fullpath = url+"/framework/javascript/success_action_handlers/"+success_action_handler+".js";
	if (success_action_handler in found_handelers) {
		Y.use(success_action_handler, function(Y) {
			buzzDebug("success line 34: "+success_action_handler);
			eval("Y."+success_action_handler+'(data, args)');
		});
	} else {
	
		Y.Get.script(fullpath, {
			onSuccess: function() {
				Y.use(success_action_handler, function(Y) {
					buzzDebug("success line 42: "+success_action_handler);
					eval("Y."+success_action_handler+'(data, args)');
					found_handelers[success_action_handler] = fullpath;
				});
			}
		});
		
		
	}
}
function runHandlerSuccess(handler_success) {

	if (handler_success!=undefined) {
		handler_success();
	}

}

function runHandlerChain(elements, i, html) {
	
	if (elements[i] == undefined) return;
	
	var element = elements[i];
	var success_action_handler = element.element_type;

	//alert(success_action_handler);
	//alert(element.toSource());
	var data = element.args;
	if (html!=undefined) {
		data.module_html = html;
		if (data.html==undefined) {
			data.html = html;
		}
	}
	
	var args = data;
	var fullpath = url+"/framework/javascript/success_action_handlers/"+success_action_handler+".js";
	if (success_action_handler in found_handelers) {
		Y.use(success_action_handler, function(Y) {
			eval("Y."+success_action_handler+'(data, args)');
			buzzDebug("success line 76: "+success_action_handler);
			runHandlerChain(elements, i+1, html);
		});
	} else {
		Y.Get.script(fullpath, {
			onSuccess: function() {
				Y.use(success_action_handler, function(Y) {
					var handler_success = function () {
						runHandlerChain(elements, i+1, html);
						found_handelers[success_action_handler] = fullpath;
					};
					buzzDebug("success line 87: "+success_action_handler);
					eval("Y."+success_action_handler+'(data, args, handler_success)');
				});
			}
		});

		//alert("hi2");
		
	}
}

// the handleSuccess function will include the proper buzz module and hand args off to it
var handleSuccess = function(ioId, o, args) {
	buzzDebug("handle success");
	Y.use('json', function(Y) {
		try {
			
			if (o.responseText.substring(o.responseText.length-6)=='</pre>') {
				
				o.responseText = o.responseText.substring(0, o.responseText.length-6);
				buzzDebug(o.responseText);
			}
			
			var data = Y.JSON.parse(o.responseText);
			//alert (data.toSource());
			
		} catch (e) {
			buzzDebug("Invalid json data");
			buzzDebug(o.responseText);
			
			var lastChars = o.responseText.substring(o.responseText.length-6);
			alert(o.responseText);	
			return false;
		}
		if (args==undefined) args = new Array();

		if (args.action!=undefined) {
			buzzDebug("has action");
			// this is the name of the buzz module we will use
			var success_action_handler = args.action;
			buzzDebug("success line 133: "+success_action_handler);
			args.action = '';
			runHandler(success_action_handler, data, args);

		} else if (data.html!=undefined) {
		
		}
		
		if (data.elements!=undefined) {
			buzzDebug("has elements");
			runHandlerChain(data.elements, 0, data.html);		
		}
	});
}

//  just spit out the error
var handleFailure = function(ioId, o){
	if(o.responseText !== undefined){
		buzzDebug("handle falure");
	}
}

// make an async request and hand it off the the success handler
Y.use("io", function(Y) {
	Y.on('io:success', handleSuccess);
	Y.on('io:failure', handleFailure);	
});

var current_request = null;
function makeRequest(args) {
	//url = args["request"];
	var this_url = url+args["request"]
	get_args = args["get_args"];
	buzzDebug("Make Request");
	Y.use("io", function(Y) {
			// create an actual get string from our array
			var get_data = '';
			for(var field in get_args){
				get_data = get_data+"&"+field+"="+get_args[field];
			}
			
			var cfg = {
				method: "POST",
				data: get_data,

				arguments: args
			};
			
			var sUrl = this_url+"?view=json";
			current_request = Y.io(sUrl, cfg);
			//buzzDebug(request);
			
		}
	);	
}

function submitForm(form_id) {
	Y.use("io-form", function(Y) {		
		var this_form =  Y.one('#'+form_id);
		
		var cfg = {
			method: 'POST',
			form: {
				id: this_form,
				useDisabled: false
			}
		};
	 
		var action = this_form.get('action');
		
		// Define a function to handle the response data.
		function complete(id, o, args) {
		  var id = id; // Transaction ID.
		  var data = o.responseText; // Response data.
		  var args = args[1]; // 'ipsum'.
		};
	 
		// Subscribe to event "io:complete", and pass an array
		// as an argument to the event handler "complete".
		//Y.on('io:complete',  handleSuccess, Y, { 'foo':'bar' });
	 
		// Start the transaction.
		var sUrl = action+"?view=json";
		//alert(sUrl);
		var request = Y.io(sUrl, cfg);
	});
}


function buzzFunction(function_name, data) {
	runHandler(function_name, data, data);
}

function buzzDebug(this_message) {
	try {
		console.log(this_message);
	} catch(e) {
		//var element = document.getElementById("debug_messages");
		//element.innerHTML = element.innerHTML+"<br>"+this_message; 
		//alert("You don't have Firebug!\nFor shame...");
	}   

}

function disableClick(this_el) {
	this_el.onclick=disabledClick;
}

function disabledClick(this_el) {
	alert("Please click only once.");
	return false;
}


function buzzFormCheck(form_id) {
	Y.use("node", function(Y) {		
		var required_items =  Y.one('#'+form_id).all('.buzz_required');
		
		var do_submit = true;
		
		required_items.each(function(v, k){
			
			if (v.get('value')=='') {
				v.get('parentNode').addClass('field-error');
				do_submit = false;
			} else {
				v.get('parentNode').removeClass('field-error');
			}
		});
		if (do_submit) {
			Y.one('#'+form_id).submit();
		}
	});
	return false;
}


