var YackTrack = {};
YackTrack.dataReady = new YAHOO.util.CustomEvent("dataReady");
YackTrack.getDialog = function(action) {
	var ytDialog = null;
	if (action == "login") {
		ytDialog = YackTrack.Login;
	} else if (action == "join") {
		ytDialog = YackTrack.Register;
	} else if (action == "editaccount") {
		ytDialog = YackTrack.Account;
	} else if (action == "changepassword") {
		ytDialog = YackTrack.ChangePassword;
	} else if (action == "add") {
		ytDialog = YackTrack.Query;
	} else if (action == "edit") {
		ytDialog = YackTrack.Query;
	} else if (action == "editblog") {
		ytDialog = YackTrack.Blog;
	} else if (action == "addblog") {
		ytDialog = YackTrack.Blog;
	}
	YackTrack.dataReady.subscribe(function(type, args, me) {
		ytDialog.setBody(ytDialog.getForm());
		ytDialog.render(document.body);
		ytDialog.show();
	}, this);
	return ytDialog;
}
YackTrack.Util = {};
YackTrack.Util.addError = function(id, msg) {
	var label = YAHOO.util.Dom.getPreviousSibling(YAHOO.util.Dom.getPreviousSibling(id));
	if (!YAHOO.util.Dom.hasClass(label, 'error')) {
		YAHOO.util.Dom.addClass(label, 'error');
	}
	var errorId = 'error_' + id;
	if (!document.getElementById(errorId)) {
		var br = document.createElement('br');
		YAHOO.util.Dom.insertAfter(br, id);
		var err = document.createElement('span');
		err.setAttribute('class', 'error');
		err.setAttribute('id', errorId);
		err.innerHTML = msg;
		YAHOO.util.Dom.insertAfter(err, br);
	}
}
YackTrack.Util.removeError = function(id) {
	var label = YAHOO.util.Dom.getPreviousSibling(YAHOO.util.Dom.getPreviousSibling(id));
	if (YAHOO.util.Dom.hasClass(label, 'error')) {
		YAHOO.util.Dom.removeClass(label, 'error');
	}
	var errorId = 'error_' + id;
	var errorElem = document.getElementById(errorId); 
	if (errorElem) {
		var br = YAHOO.util.Dom.getPreviousSibling(errorElem);
		br.parentNode.removeChild(br);
		errorElem.parentNode.removeChild(errorElem);
	}
}
YackTrack.Util.removeAllErrors = function(fields) {
	for (var i=0; i<fields.length; i++) {
		var done = false;
		while (!done) {
			var element = fields[i];
			if (document.getElementById('error_' + element)) {
				YackTrack.Util.removeError(element);
			} else {
				done = true;
			}
		}
	}
}
YackTrack.Util.addFormError = function(id, msg) {
	var child = YAHOO.util.Dom.getFirstChild(id);
	var errorId = 'error_' + id;
	if (!document.getElementById(errorId)) {
		var err = document.createElement('span');
		err.setAttribute('class', 'error');
		err.setAttribute('id', errorId);
		err.innerHTML = msg;
		YAHOO.util.Dom.insertBefore(err, child);
		var br = document.createElement('br');
		YAHOO.util.Dom.insertAfter(br, err);
	}
}
YackTrack.Util.removeFormError = function(id) {
	var errorId = 'error_' + id;
	var errorElem = document.getElementById(errorId); 
	if (errorElem) {
		var br = YAHOO.util.Dom.getNextSibling(errorElem);
		br.parentNode.removeChild(br);
		errorElem.parentNode.removeChild(errorElem);
	}
}

function executeAction(action) {
	var ytDialog = YackTrack.getDialog(action);
	ytDialog.getDialogData();
}

function executeEditAction(action, id) {
	var ytDialog = YackTrack.getDialog(action);
	ytDialog.setId(id);
	ytDialog.getDialogData();
}
