var ajax_call;

function base()
{
	$('.date_picker').datepicker({
		showOn: 'button',
		buttonImage: 'css/img/calendar.png',
		buttonImageOnly: true,
		duration: 'fast',
		dateFormat: 'mm/dd/yy'
	});
	$('.date_picker:disabled,.date_picker[readonly]').datepicker("disable");
}

$(function(){
	// ajax loading gif...
	$('body').append('<div id="loading" style="width: 32px; height: 32px; left: 50%; z-index: 9999;">' +
	'<img src="img/ajax-loader.gif" style="padding-top: 9px;"></div>');
	$('#loading').hide();
	$("#loading").ajaxSend(function(){
		$(this).vCenter();
		$(this).show();
	});
	$("#loading").ajaxStop(function(){
		$(this).hide();
	});
});

(function () {
//ajax_load...
$.fn.ajax_load = function(url, data, callback) {
		return this.each(function() {
			var obj = $(this);
			ajax(url, data, function(json) {
  				var off = url.indexOf(" ");
  				if ( off >= 0 ) {
  					var selector = url.slice(off, url.length);
  					url = url.slice(0, off);
  				}
  				// See if a selector was specified
  				obj.html( selector ?
						// Create a dummy div to hold the results
						$("<div/>").append(json.html).find(selector) :

						// If not, just inject the full result
						json.html );
  				if (callback)
  					callback(json);

			});
	});
	};

	// ajax_submit...
	$.fn.ajax_submit = function(callback) {
	return this.each(function() {
        var formdata = $(this).serialize();
		$.ajax({
		    type: $(this).attr('method'),
		    url: $(this).attr('action'),
		    dataType: 'json',
		    data: formdata,
		    success: callback
		});
	});
};

$.fn.dialog_cancel = function() {
	ajax_call.abort();
	this.dialog("close");
};
})();

// manage error msgs
var AlertMgr = function (msgs, duration, dialog) {
		this.msgs = msgs,
		this.duration = duration || 3000,
		this.dialogID = dialog || "alertMgrDialog",
		this.$dialog = $('#'+this.dialogID)
	if (!this.msgs) return;
	this.checkUI();
	return this;
};

AlertMgr.prototype = {
	checkUI : function () {
		var duration = this.duration;
		if (this.$dialog.length <= 0) {
			$("body").append("<div id='" + this.dialogID + "'></div>");
			this.$dialog = $('#'+this.dialogID);
			this.$dialog.dialog({
				resizable : false,
				stack : true,
				hide : "fade",
				dialogClass : "alertMgr",
				open: function () { $(this).delay(duration).queue(function () { $(this).dialog('close').clearQueue(); }); }
			});
		}
		return this;
	},
	showAlert : function () {
		this.build().open();
		return this;
	},
	build : function () {
		html = '';
		$.map(this.msgs, function (msg) {
			var msg = "<p class='alert msg'>" + msg.msg + "</p>";
			html = html + msg;
		});
		this.$dialog.html(html);
		return this;
	},
	open : function () {
		this.$dialog.dialog('open');
		return this;
	}
};

function ajax(url, data, callback)
{
	ajax_call = $.getJSON(url, data, function(json) {
		// no longer logged in...
		if (json.result == -1)
			window.location = 'signin.php';
		// insufficient security...
		else if (json.result == -2)
			window.location = 'index.php';
		else if (json.result == 0 && json.errors) {
			errors = new AlertMgr(json.errors);
			errors.showAlert();
			if (callback)
				callback(json);
		}
		else if (callback)
			callback(json);
	});
}

function submit_save(obj)
{
	$('form', obj).append("<input type='hidden' name='save' value='Save'>");
	$('form', obj).submit();
}

function submit_update(obj)
{
	$('form', obj).append("<input type='hidden' name='update' value='Update'>");
	$('form', obj).submit();
}

function submit_delete(obj)
{
	$('form', obj).append("<input type='hidden' name='delete' value='Delete'>");
	$('form', obj).submit();
}

$.fn.setOffsetData = function () {
	var $this = $(this),
		offset = $this.offset();
	$this.data('offset', {'top':Math.round(offset.top), 'left':Math.round(offset.left)});
	return this;
};

if (!Date.now) {
  Date.now = function now() {
    return +new Date();
  };
}

Date.prototype.isSameDate = function (date) {
  try {
    return this.toDateString() === date.toDateString();
  }
  catch (e) {
    return;
  }
};

/* Range is assumed to be in minutes
Date.prototype.isAfterInRange = function(date, range) {
    var endDate = new Date(this.getTime() + range * 60 * 1000);
    return date >= this && date <= endDate;
}
*/

