var fieldError = 'field_error'; // red border

var errorState = 'ui-state-error ui-corner-all'; // round corners, red background
var successState = 'ui-state-highlight ui-corner-all'; // round corners, yellow background

var alertIcon = 'ui-icon ui-icon-alert'; // alert icon
var infoIcon = 'ui-icon ui-icon-info'; // info icon

var alertSpan = '<span class="'+ alertIcon +'" style="float: left; margin: 3px; margin-right: .3em;"></span>';
var infoSpan = '<span class="' + infoIcon + '" style="float: left; margin-right: .3em;"></span>';

// set defaults
var defaultMsgDiv = 'msg_div';
var defaultErr = 'Error occured while processing request. Please contact the administrator.';
var defaultFormError = 'form_error';

$.extend({
	Msgs: function(ndx, a, b) {
		var msgs = new Array();
		a = a || 'This field';
		b = b || '';
		
		msgs['required'] = a + ' is required.';
		msgs['unique'] = a + ' already exists.';
		msgs['match'] = a + ' does not match ' + b + '.';
		msgs['validDate'] = a + ' must contain a valid date.';
		msgs['dateFormat'] = a + ' must be in ' + b + ' format';
		msgs['validEmail'] = a + ' must contain a valid email address.';
		msgs['minLength'] = a + ' must be at least ' + b + ' characters in length.';
		msgs['maxLength'] = a + ' can not exceed ' + b + ' characters in length.';
		msgs['exactLength'] = a + ' must be exactly ' + b + ' characters in length.';
		msgs['max'] = a + ' can not exceed ' + b + '.';
		msgs['integer'] = a + ' must contain only integers.';
		msgs['image'] = a + ' must be an image.';
		msgs['afterDate'] = a + ' must be after ' + b + '.';
		msgs['validUrl'] = a + ' must contain a valid URL.';
		
		return msgs[ndx];
	},
	
	resetMsg: function(msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;
		
		$('#' + msgDiv).hide();
		$('#' + msgDiv).text('');
		
		$('#' + msgDiv).removeClass(errorState);
		$('#' + msgDiv).removeClass(alertIcon);
		
		$('#' + msgDiv).removeClass(successState);
		$('#' + msgDiv).removeClass(infoIcon);
	},
	
	showError: function(msg, msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;
		
		msg = msg || defaultErr;
			
		$('#' + msgDiv).removeClass(successState);
		$('#' + msgDiv).addClass(errorState);
		$('#' + msgDiv).html(alertSpan + msg);
		$('#' + msgDiv).css('color', '#444444'); 
		
		$('#' + msgDiv).fadeIn('slow');
	},
	
	showInfo: function(msg, msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;
		
		$('#' + msgDiv).removeClass(errorState);
		$('#' + msgDiv).addClass(successState);
		$('#' + msgDiv).html(infoSpan + msg);
		$('#' + msgDiv).css('color', '#444444');
		
		$('#' + msgDiv).fadeIn('slow');
	},
	
	/**
	 * For validation plugin error displaying
	 * Dirty hacks included
	 */
	
	appendError: function(error, element, div) {
		error.append('<br />');
		error.attr('id', 'error_' + element.attr('name'));
		error.appendTo(div);
		
		// dirty hack to align error list vertically
		div.children('.error').removeClass('error_3');
		div.children('.error').slice(1).each(function() {
			$(this).addClass('error_3');
		});
	},
	
	highlightFormElement: function(element, div) {
		var name = $(element).attr('name');
		
		// remove messages associated with element
		if ($('#error_'+name).length) 
		{
			$('#error_'+name).remove();
		}
		
		if ($(div + ':hidden').length) $(div + ':hidden').fadeIn('slow'); // show if hidden
		$(element).addClass(fieldError);
	},
	
	unhighlightFormElement: function(element, div) {
		var name = $(element).attr('name');
		
		// dirty hack to align error list vertically
		if (div)
		{
			div.children('.error').removeClass('error_3');
			div.children('.error').slice(2).each(function() {
				$(this).addClass('error_3');
			});
		}
		
		// remove messages associated with element
		if ($('#error_'+ name).length) 
		{
			$('#error_'+ name).remove();
			
			if ( ! $('.error').length) div.fadeOut('slow'); // hide error div
		}
		
		$(element).removeClass(fieldError);
	},
	
	unhighlightAll: function(objForm, div) {
		objForm.find('.' + fieldError).each(function() {
			$(this).removeClass(fieldError);
			var name = $(this).attr('name');
			
			if ($('#error_'+ name).length) {				
				$('#error_'+ name).remove();
			}
		});
		
		//if ( ! $('.error').length) div.fadeOut('slow'); // hide error div
	},
	
	oldHighlightFormElement: function(element) {
		// dirty hack to remove alert icon
		var sibling = $(element).nextAll('.' + defaultFormError);
		sibling.empty();
		
		// add red background
		var parent = $(element).parent(); 
		parent.addClass(errorState);
			
		// display alert icon
		var sibling = $(element).nextAll('.' + defaultFormError);
		sibling.prepend(alertSpan);
	},
	
	oldUnhighlightFormElement: function(element) {
		// remove red background
		var parent = $(element).parent(); 
		parent.removeClass(errorState);
		
		// remove alert icon
		var sibling = $(element).nextAll('.' + defaultFormError);
		sibling.empty();
	},
	
	/**
	 * Generic dialog boxes
	 */
	
	genericDialog: function(div, id, callback) {
		id = id || '';
		
		$('#' + div).dialog({
			bgiframe: true,
			resizable: false,
			height: 'auto',
			modal: true,
			buttons: {
				'No': function() {
					$(this).dialog('destroy');
				},
				'Yes': function() {
					eval(callback + '("' + id + '")');
					$(this).dialog('destroy');
				}
			},
			close: function() {
				$(this).dialog('destroy');
			},
			open: function() {
				$('#No').focus();
			}
		});
	},
	
	confirmDelete: function(id, callback, div) {
		div = div || 'dialog_delete';
		$.genericDialog(div, id, callback);
	},
	
	loading: function(title) {
		$('#loading').removeClass('display', 'block');
		$('#finished').addClass('hidden');
		
		$('#dialog_loading').attr('title', title).dialog({
			bgiframe: true,
			resizable: false,
			height: 'auto',
			modal: true,
			open: function() {
				$('a.ui-dialog-titlebar-close').addClass('hidden');
			},
			close: function() {
				$(this).dialog('destroy');
			}
		});
	},
	
	finished: function(html, iconClass, callback) {
		callback = callback || null;
			
		if (callback != null) eval(callback);
		else $('#dialog_loading').dialog('option', 'buttons', {
			'Close': function() {
				$(this).dialog('destroy');
			}
		});
			
		$('#loading').css('display', 'none');
		$('#ficon').addClass(iconClass);
		$('#fmsg').html(html);
		$('#finished').removeClass('hidden');
	}
});

