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

}

$(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();
	});
	$('#content_in').append("<div id='confirm_dialog' title='Confirm'></div>");
	// edit/add client dialog
	$('#confirm_dialog').dialog({
		autoOpen: false,
		resizable: false,
		closeOnEscape: false,
		height: 210,
		width: 350,
		modal: true
	});
});

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

// ajax_load...
(function($){
 	$.fn.ajax_load = function(url, data) {
 		return this.each(function() {
 			var obj = $(this);
  			ajax(url, data, function(json) {
					obj.html(json.html);
  			});
		});
 	}
})(jQuery);

// ajax_submit...
(function($){
	$.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
			});
		});
	}
})(jQuery);

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();
}

function hotspot_start(show_msg)
{
  	if (arguments.length == 0)
  		show_msg = 1;
	$('.hotspot').each( function() {
		var content = $(this).attr('id') + '_content';
		$('#' + content).hide();
		$(this).click( function() {
			$('#' + content).hide_show();
		});
	});
	$('.hotcheck').each( function() {
		var content = $(this).attr('id') + '_content';
		$('#' + content).hide_show($(this).attr('checked'));
		$(this).change( function() {
			$('#' + content).hide_show($(this).attr('checked'));
		});
	});
	if (show_msg)
	{
		$('span.msg:not(:empty)').each( function() {
			$(this).parents('.hotspot_content').hide_show(1);
		});
	}
}

(function($){

  	$.fn.vCenter = function(options) {
	    var pos = {
	      sTop : function() {
	        return window.pageYOffset || document.documentElement && document.documentElement.scrollTop ||	document.body.scrollTop;
	      },
	      wHeight : function() {
	        return window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.body.clientHeight;
	      }
	    };
	    return this.each(function(index) {
	      if (index == 0) {
	        var $this = $(this);
	        var elHeight = $this.height();
			    var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
	        $this.css({
	          position: 'absolute',
	          marginTop: '0',
	          top: elTop
	        });
	      }
	    });
	};

  	$.fn.hide_show = function(show) {
  		if (arguments.length == 0)
	  		var show = $(this).is(':hidden');
		if (show)
			$(this).fadeIn()
		else
			$(this).hide();
	};

})(jQuery);

// article carousel
(function($){
 	$.fn.carousel = function(url, data) {
 		return this.each(function() {
 			var carousel = {};
			carousel.main = $(this);
			carousel.content = $(this).children().css({position: "absolute", "top": 0});
			carousel.wrap = carousel.content.wrap('<div class="carousel_wrap"></div>').parent().css({overflow: "hidden", position: "relative"});
			carousel.steps = {
				first: 0, // first step
				count: carousel.content.find("li").length // items count
			};
			// next, previous buttons...
			carousel.previous = $("<div></div>").prependTo(carousel.main)
				.addClass("carousel_control carousel_previous").data("first_step", carousel.steps.count-1);
			carousel.next = $("<div></div>").appendTo(carousel.main)
				.addClass("carousel_control carousel_next").data("first_step", 1);
			// Bind click event on next / prev buttons
			carousel.next.add(carousel.previous).bind("click", function(e){
				slide(e, this, carousel);
			}).hover(
				function(){
					$(this).addClass("hover");
				},
				function(){
					$(this).removeClass("hover");
				}
			);
			carousel.pages = $('.carousel_pages');
			carousel.pages.children().each(function(i) {
				$(this).data("first_step", i);
				$(this).bind("click", function(e) {
					slide(e, this, carousel);
				})
			});
			$(function(){
				carousel.item_width = carousel.content.find("li").css("width");
				carousel.item_width = carousel.item_width.replace("px", "");
				carousel.content.width(carousel.item_width * carousel.steps.count);
				$(".carousel img").css({display: "block"});
				// autoslide...
				window.setTimeout(function(){
					carousel.autoSlideInterval = window.setInterval(function(){
						carousel.next.click();
					}, 7000);
				}, 1000);
				state(0, carousel);
			});
		});
 	}

	function slide(e, btn, carousel)
	{
		var first_step = $(btn).data("first_step");
		// Stop autoslide on user click...
		if (!!e.clientX && carousel.autoSlideInterval)
			window.clearInterval(carousel.autoSlideInterval);
		state(first_step, carousel);
		carousel.content.stop().animate({
			left : -(carousel.item_width * first_step) + "px"
			}, 'slow', 'linear');
	};

	function state(first_step, carousel)
	{
		carousel.previous.data("first_step", first_step - 1);
		carousel.next.data("first_step", first_step + 1);
		if (carousel.previous.data("first_step") < 0 && carousel.steps.count > 1)
			carousel.previous.data("first_step", carousel.steps.count - 1);
		if (carousel.next.data("first_step") >= carousel.steps.count && carousel.steps.count > 1)
			carousel.next.data("first_step", 0);
		carousel.pages.children().each(function(i) {
			if (i == first_step)
				$(this).addClass('carousel_active_page')
			else
				$(this).removeClass('carousel_active_page')
		})
	};

})(jQuery);

