$(function (){
	$('.confirm').click(function (){
		var msg = $(this).attr('title');
		if (!msg){msg = 'Are you sure';}
		return confirm(msg+'?');
	});
	$('.popup').popup();
	$('select.autosubmit').change(function (){
		$(this.form).trigger('submit');
	});
	$('input.autosubmit')
		.focus(function (){
			var $t = $(this);
			$t.data('originalValue', $t.val());
		})
		.blur(function (){
			var $t = $(this);
			if ($t.data('originalValue') != $t.val()){
				$(this.form).trigger('submit');
			}
		});
	$('.autofocus').trigger('focus').trigger('select');
	$('.calendar').each(function (){
		addCalendar.apply(this);
	});
	$('#debug_mail').click(function (){
		var $t = $(this);
		$t.parents('form').attr('target', '_blank');
		setTimeout(function (){
			$t.parents('form').attr('target', '');
		}, 100);
	});
});

function addCalendar(){
	var $this = $(this), curdate = new Date();
	var settings = {
		dateFormat: 'd/m/yy',
		minDate: null,
		maxDate: null,
		firstDay: 1,
		// gotoCurrent: true,
		showOtherMonths: true,
		changeMonth: true,
		// changeYear: true,
		// yearRange: '1970:2100',
		duration: ''
	};
	if ($this.is('.calendar-future')){
		settings['beforeShowDay'] = function (d){return [d >= curdate];};
	}
	if ($this.metadata()){
		settings = $.extend({}, settings, $this.metadata());
	}
	if (this.id && $('#button_'+this.id).length){
		settings.buttonImage = $('#button_'+this.id).attr('src');
		settings.showOn = 'button';
		settings.buttonImageOnly = true;
		$('#button_'+this.id).remove();
	}
	$(this).datepicker(settings);
}

/* POPUP Plugin */
/*
* jQuery POPUP Plugin v1.0
*
* easy window.open popups
* @requires metadata plugin
*/
/* POPUP 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;
				features.url = this.href;
				var win = $.fn.popup.open(features);
				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'
	};
	$.fn.popup.open = function (features){
		if (features['width'] == 'auto' || !features['width']){features['width'] = Math.floor(screen.width/2);}
		if (features['height'] == 'auto' || !features['height']){features['height'] = Math.floor(screen.height/2);}
		if (features['left'] == 'auto' || !features['left']){features['left'] = Math.floor((screen.width-features['width'])/2);}
		if (features['top'] == 'auto' || !features['top']){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];}
		}
		return window.open(url, target, s_features.join(','));
	};
})(jQuery);

/* if(!Array.indexOf){
	Array.prototype.indexOf = function(needle, offset){
		var start = Math.max(0, offset);
		for(i=start; i<this.length; i++){
			if (this[i] == needle){return i;}
		}
		return -1;
	}
} */
function ucFirst(i){
	return i.substr(0,1).toUpperCase() + i.substr(1,i.length);
};

if ($.browser.msie){
	jQuery.browser.version = navigator.userAgent.toLowerCase().match(/MSIE ([\d+]\.[\d+])/i)[1];
}
