$(function (){
	$('a.confirm').click(function (){
		var msg = $(this).attr('title');
		if (!msg){msg = 'Delete record';}
		return confirm(msg+'?');
	});
	$('.popup').popup();
	$('select.autosubmit').change(function (){
		var frm = $(this).parents('form');
		if (frm.length > 0){
			frm[0].submit();
		}
	});
});

/* POPUP Plugin */
/*
* jQuery POPUP Plugin v1.0
*
* easy window.open popups
* @requires metadata plugin
*/
;(function($){
	// plugin definition
	$.fn.popup = function (options){
		var options = $.extend({}, $.fn.popup.defaults, options);
		return this.each(function (){
			$(this).click(function (){
				$this = $(this);
				var features = $this.metadata() ? $.extend({}, options, $this.metadata()) : options;
				if (features['width'] == 'auto'){features['width'] = Math.floor(screen.width/2);}
				if (features['height'] == 'auto'){features['height'] = Math.floor(screen.height/2);}
				if (features['left'] == 'auto'){features['left'] = Math.floor((screen.width-features['width'])/2);}
				if (features['top'] == 'auto'){features['top'] = Math.floor((screen.height-features['height'])/2);}
				
				var url = this.href;
				var target = '_blank';
				var s_features = [];
				for (feature in features){
					if (feature == 'target'){target = features[feature];}
					else if (feature == 'url'){url = features[feature];}
					else{s_features[s_features.length] = feature+'='+features[feature];}
				}
				var win = window.open(url, target, s_features.join(','));
				if (win){win.focus();return false;}
				return true;
			});
		});
	};
	// plugin defaults
	$.fn.popup.defaults = {
		status:			1,
		toolbar:		0,
		resizable:		1,
		scrollbars:		1,
		width:			'auto',
		height:			'auto',
		top:			'auto',
		left:			'auto'
	};
})(jQuery);

jQuery.extend({
	first: function (object){
		if (object.length){
			return jQuery(object[0]);
		}
		return jQuery;
	},
	last: function (object){
		if (object.length){
			return jQuery(object[object.length-1]);
		}
		return jQuery;
	}
});
jQuery.fn.extend({
	first: function (){
		if (!this.length)
			return this;
		return jQuery.first(this);
	},
	last: function (){
		if (!this.length)
			return this;
		return jQuery.last(this);
	}
});
