var app = '';

$(document).ready(function(){ 
	$('a.external').click(function() {
		self.open(this.href);
		return(false);
	});

	if($('form.ajax').html() != undefined) {
		$('form.ajax').ajaxForm({
			dataType: 'json',
			beforeSubmit: disableAll,
			success: process
		});
	}
}); 
function disableAll(formData, jqForm, options) {
	jqFormInstance = jqForm;
	$(":input", jqForm).attr('disabled', 'disabled');
	$(".error", jqForm).removeClass('error');
	$("ul.errors", jqForm).remove();
	if(options.url.search(/\?/) != -1) {
		options.url = options.url + '&format=json';
	} else {
		options.url = options.url + '?format=json';
	}
}
function errors(obj, text) {
	//obj.parent().addClass('error');
	obj.parent().append('<ul class="errors"><li>' + text + '</li></ul>');
}
function process(data) {
	if(data.isError != undefined) {
		for(id in data.messages) {
			var qid = id;
			var error = data.messages[id];
			if(typeof(data.messages[id]) == 'object') {
				for(key in data.messages[id]) {
					if(key.length > 1) {
						qid = id + '-' + key;
					}
					error = data.messages[id][key];
					if(typeof(error) == 'object') {
						for(subkey in error) {
							var sid = qid;
							if(subkey.length > 1 && typeof(error[subkey]) != 'string') {
								sid = qid + '-' + subkey;
							}
							var serror = error[subkey];
							var obj = $('label[for=' + sid + ']', jqFormInstance).text() ? $('label[for=' + sid + ']', jqFormInstance) : $('#' + sid, jqFormInstance)
							errors(obj, serror);
						}
					} else {
						var obj = $('label[for=' + sid + ']', jqFormInstance).text() ? $('label[for=' + sid + ']', jqFormInstance) : $('#' + sid, jqFormInstance)
						errors(obj, error);
					}
				}
			} else {
				var obj = $('label[for=' + sid + ']', jqFormInstance).text() ? $('label[for=' + sid + ']', jqFormInstance) : $('#' + sid, jqFormInstance)
				errors(obj, error);
			}
		}		
		$(':input', jqFormInstance).removeAttr('disabled');
	} else if(data.isSuccess != undefined){
		switch(data.action) {
			case 'replace':
				jqFormInstance.fadeOut(100, function(){
					$(jqFormInstance).remove();
					$('#' + data.id).html(data.html);
				});
				break;
			case 'redirect':
				if($('#overlayContainer').html() != undefined) {
					closeOverlay();
					constructOverlay(data.location);
				} else {
					self.location.href = data.location;
				}
				break;
			case 'force_redirect':
				self.location.href = data.location;
				break;
			case 'replaceWith':
				$('#' + data.id).replaceWith(data.html);
				callbacks();
				break;
			case 'redirect':
				self.location.href = data.location;
				break;
			case 'highlight':
				alert(data.html);
			case 'remove':
				jqFormInstance.after(data.html).remove();
				break;
		}
	}
}
$.fn.inline = function() {
	$(this).each(function(){
		$(this).prev().hide();
		$(this).val($(this).prev().html());
		$(this).focus(function(){
			if($(this).prev().html() == $(this).val()){
				$(this).val('');
				$(this).addClass("focus");
			}
		});
		$(this).keypress(function(){
			$(this).addClass("typing");
		});
		$(this).blur(function(){
			$(this).removeClass("focus").removeClass("typing");
			if($(this).val() == ""){
				$(this).val($(this).prev().html());
			}
		});
	});
};

