var formElementError = 'form_element_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 occurred while processing request. Please contact the administrator';

$.extend({
	Msgs: function(ndx, a, b) {
		var msgs = new Array();
		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['afterDate'] = a + ' must be after ' + b + '.';
		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.';
		
		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);
	},
	
	showMsg: function(msg, msgDiv) {
		msg = msg || null;
		msgDiv = msgDiv || defaultMsgDiv;
		
		if (msg == null) return false;
		
		var aMsg = msg.split('|');
		
		if (aMsg[0] == 'I') $.showInfo(aMsg[1], msgDiv);
		else $.showError(aMsg[1], msgDiv);
	},
	
	showError: function(msg, msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;
		
		msg = msg || defaultErr;
			
		$('#' + msgDiv).removeClass(successState);
		$('#' + msgDiv).addClass(errorState);
		$('#' + msgDiv).html(alertSpan + msg);
		
		$('#' + msgDiv).fadeIn('slow');
	},
	
	showInfo: function(msg, msgDiv) {
		msgDiv = msgDiv || defaultMsgDiv;
		
		$('#' + msgDiv).removeClass(errorState);
		$('#' + msgDiv).addClass(successState);
		$('#' + msgDiv).html(infoSpan + msg);
		
		$('#' + 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(formElementError);
		
		$('#msg_div').fadeOut('slow');
	},
	
	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(1).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(formElementError);
	},
	
	unhighlightAll: function(objForm, div) {
		objForm.find('.' + formElementError).each(function() {
			$(this).removeClass(formElementError);
			var name = $(this).attr('name');
			
			if ($('#error_'+ name).length) {				
				$('#error_'+ name).remove();
			}
		});

		if ( ! $('.error').length) div.fadeOut('slow'); // hide error div
	},
	
	/**
	 * Generic dialog boxes
	 */
	
	genericDialog: function(div, id, callback) {
		id = id || '';
		
		$('#' + div).dialog({
			bgiframe: true,
			resizable: false,
			height: 'auto',
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			},
			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 || 'confirm_delete_dialog';
		$.genericDialog(div, id, callback);
	},
	
	confirmDeleteImg: function(id, callback, div, txt) {
		div = div || 'confirm_delete_dialog';
		txt = txt || 'image';
		
		$('#' + div).attr('title', 'Delete Image');
		
		$('#to_del').html(txt);
		$.genericDialog(div, id, callback);
	}
});

