$(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();
		}
	});
	$('.zp-cal').each(function (){
		AddCalendar(this);
	});
});

/* 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];}
					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);

function AddCalendar(o){
	// console.log('#button_'+o.id, $('#button_'+o.id), $('#button_'+o.id).length);
	if ($('#button_'+o.id).length){
		Zapatec.Calendar.setup({
			firstDay          : 1,
			electric          : false,
			showOthers        : true,
			inputField        : o.id,
			button            : 'button_'+o.id,
			ifFormat          : "%d/%m/%Y",
			daFormat          : "%d/%m/%Y"
		});
	}
	else{
		Zapatec.Calendar.setup({
			firstDay          : 1,
			electric          : false,
			showOthers        : true,
			inputField        : o.id,
			ifFormat          : "%d/%m/%Y",
			daFormat          : "%d/%m/%Y"
		});
	}
}

