/**
 *	Common JavaScript object
 *	@authors Nirahiel, RasCarlito
 *	@version 0.1
 */
 
 
 
/*


common.dialogs.confirm({
	title: '',
	msg: '',
	icon: 'alert',
	buttons: {
		'Ok': function() {
			// code ici
			
			
			$(this).dialog('close');
		},
		'Annuler': function() {
			
			
			$(this).dialog('close');
		}
	}
});

*/

var common = {

	ie: navigator.appName == "Microsoft Internet Explorer",

	init: function() {
		$("#confirm-dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			resizable: false,
			modal: true,
			overlay: {
				backgroundColor: '#000',
				opacity: 0.5
			}
		});
		
		$(window).resize(function() {
			common.adjust();
		});
		this.adjust();
	},
	
	
	dialogs: {
		/**
		 *	@return boolean true=tout va bien normalement, false=erreur
		 */
		confirm: function(options) {
			if (typeof options == 'undefined') {
				$.log('Il manque la liste des parametres')
				return false;
			}
			
			var dialog = $('#confirm-dialog');
			if (!dialog.length) {
				$.log('La div#confirm-dialog est introuvable');
				return false;
			}
			
			options = $.extend({
				title: '',
				msg: '',
				icon: 'alert',
				buttons: {
					'Ok': function() {
						$(this).dialog('close');
					},
					'Annuler': function() {
						$(this).dialog('close');
					}
				}
			}, options);
			
			
			$('span.ctnr', dialog).empty().html(options.msg);
			$('span.ui-icon', dialog).addClass('ui-icon-' + options.icon);
			
			delete options.msg;
			delete options.icon;
			
			for (key in options) {
				dialog.dialog('option', key, options[key]);
			}
			
			dialog.dialog('open');
			
			return dialog;
		}
	},
	
	
	forms: {
		hoverButtons: function() {
			$('.button.ui-state-default').hover(
				function() { $(this).addClass('ui-state-hover'); },
				function() { $(this).removeClass('ui-state-hover') }
			)
		},
		
		hoverLabels: function() {
			$('label.hover+input')
			.wrap('<div class="hover-wrap ui-widget-content"></div>')
			.focus(function() { $(this).prev().hide(); })
			.change(function() { $(this).prev().hide(); })
			.blur(function() { if (!this.value) { $(this).prev().show(); } })
			.each(function() {
				$(this).before($(this).parent().prev());
				if (this.value) { $(this).prev().hide(); }
			});
			
			if (common.ie) { $('label.hover').css('top', 3); }
		}
	},
	
	showMsg: function(type, msg, delay) {
		if (type == 'error') {
			msg = msg.split('~')[1];
		}
		
		if (typeof delay == 'undefined') {
			delay = 3000;
		}
		
		
		type += 'Ctnr';
		$('#' + type + ' span.inner').html(msg);
		
		this.adjust();
		
		$('#' + type).fadeIn('fast');
		$('#' + type)[0].timer = setTimeout(function() {
			$('#' + type).fadeOut('fast');
		}, delay);
	},
	
	adjust: function() {
		var width = $(window).width();
		var height = $(window).height();
		
		var notice = $('#noticeCtnr');
		notice.css({
			left: (width - notice.width()) / 2,
			top: (height - notice.height()) / 2
		});
		
		var error = $('#errorCtnr');
		error.css({
			left: (width - error.width()) / 2,
			top: (height - error.height()) / 2
		});
	}
};

$(document).ready(function() {
	common.init();
});






/**
 *	jQuery extensions
 */
(function($) {

// links an ajax call to a forms submit
// plus serializes the form fields automatically into the postdata
$.fn.addsubmit = function(options) {
	$(this).each(function() {
		options = $.extend({
			type: 'post',
			url: '',
			success: function() {}
		}, options);
		
		$(this).submit(function() {
			$.ajax($.extend({ data: $(this).serialize() },options));
			return false;
		});
	});
	return $(this);
};


$.log = function(msg) {
	if (console && console.log) {
		console.log(msg);
	}
}

})(jQuery)
