﻿jQuery.sortArgs = function (args, sorts) {
	var object = {};
	for (var i=0; i<args.length; i++) {
		var type = typeof args[ i ];
		if (sorts[ type ]) {
			for (var j=0; j<sorts[ type ].length; j++) {
				if (typeof object[ sorts[ type ][ j ] ] == 'undefined') {
					object[ sorts[ type ][ j ] ] = args[ i ];
					break;
				}
			}
		}
	}
	for (var i in sorts) {
		for (var j=0; j<sorts[ i ].length; j++) {
			if (typeof object[ sorts[ i ][ j ] ] == 'undefined') {
				object[ sorts[ i ][ j ] ] = i == "function" ? function(){} : i == 'number' ? 0 : i == 'object' ? {} : i == 'array' ? [] : i == 'boolean' ? false : '';
			}
		}
	}
	return object;
};

jQuery.alert = function (option) {
	if (typeof option == 'object') {
		option = jQuery.extend({}, jQuery.alert._default, option);
	} else {
		option = jQuery.sortArgs(arguments, {
			'string' : ['title', 'content'],
			'function': ['callback']
		});
	}
	var win = jQuery.windows({
		title : option.title,
		callback : option.callback,
		content : option.content + '<div class="jquery-buttons"><a class="jquery-button ok">ok</a></div>'
	});
	win.elem.find('a').click(function () {
		win.close(true, 'ok');
	});
};
jQuery.alert._default = {
	title : '',
	content : '',
	callback : function(){}
};


jQuery.confirm = function (option) {
	if (typeof option == 'object') {
		option = jQuery.extend({}, jQuery.confirm._default, option);
	} else {
		option = jQuery.sortArgs(arguments, {
			'string' : ['title', 'content'],
			'function': ['callback']
		});
	}
	var win = jQuery.windows({
		title : option.title,
		callback : option.callback,
		content : option.content + '<div class="jquery-buttons"><a class="jquery-button yes">oui</a> <a class="jquery-button no">non</a></div>'
	});
	win.elem.find('a').click(function () {
		if (jQuery(this).is('.yes'))
			win.close(true, 'yes');
		else
			win.close(false, 'no');
	});
};
jQuery.confirm._default = {
	title : '',
	content : '',
	callback : function(){}
};

jQuery.prompt = function (option) {
	if (typeof option == 'object') {
		option = jQuery.extend({}, jQuery.prompt._default, option);
	} else {
		option = jQuery.sortArgs(arguments, {
			'string' : ['title', 'content', 'value'],
			'boolean': ['multi'],
			'function': ['callback']
		});
	}
	var input = option.multi
					? '<textarea class="jquery-input jquery-width-full">' + option.value + '</textarea>'
					: '<input class="jquery-input jquery-width-full" type="text" value="' + option.value + '" />';
	var win = jQuery.windows({
		title : option.title,
		callback : option.callback,
		content : option.content + input + '<div class="jquery-buttons"><a class="jquery-button ok">ok</a> <a class="jquery-button cancel">annuler</a></div>'
	});
	win.elem.find('a').click(function () {
		if (jQuery(this).is('.ok'))
			win.close(win.elem.find(option.multi ? 'textarea' : 'input').val(), 'ok');
		else
			win.close(false, 'cancel');
	});
};
jQuery.prompt._default = {
	title : '',
	content : '',
	value : '',
	multi : false,
	callback : function(){}
};

jQuery.windows = function (option) {
	if (typeof option == 'object') {
		option = jQuery.extend({}, jQuery.windows._default, option);
	} else {
		option = jQuery.sortArgs(arguments, {
			'string' : ['title', 'content'],
			'boolean': ['modal', 'draggable'],
			'function': ['callback']
		});
	}
	var elem = jQuery('<div class="jquery-windows">' +
						'<div class="jquery-windows-title">' +
							option.title +
							'<div class="jquery-close">×</div>' +
						'</div>' +
						'<div class="jquery-windows-content">' + option.content + '</div>' +
					'</div>').appendTo('body');
	if (option.modal)
		jQuery('body').mask();
	var object = {
		elem : elem,
		option : option,
		close : function (ret, button) {
			ret = typeof ret == "undefined" ? false : ret
			this.elem.remove();
			if (option.modal)
				jQuery('body').unmask();
			this.option.callback(ret, button || 'close');
		}
	};
	if ($.browser.msie && $.browser.version == "6.0")
		elem.css("width", "450px");
	elem.css({
		top : jQuery(window).scrollTop() + (jQuery(window).height() - elem.height()) / 2,
		left : jQuery(window).scrollLeft() + (jQuery(window).width() - elem.width()) / 2
	}).find('.jquery-close').click(function(){
		object.close();
	});
	if ($.browser.msie && $.browser.version == "7.0")
		elem.find('.jquery-windows-title').css("width", elem.width() - 10);
	if (option.draggable) {
		jQuery('.jquery-windows-title').mousedown(function(e){
			var start	= e,
				self	= this;
			jQuery(this).mousemove(function(e){
				e.preventDefault();
			});
			jQuery(document).bind("mousemove", function(e){
				e.preventDefault();
				jQuery(self).parent().css({
					top: e.pageY - (start.layerY || start.offsetY) +'px',
					left: e.pageX - (start.layerX || start.offsetX) +'px'
				});
			}).mouseup(function(){
				jQuery(document).unbind('mousemove');
				jQuery(self).unbind('mousemove');
			});
		}).addClass('draggable');
	}
	return object;
};

jQuery.windows._default = {
	title : '',
	content : '',
	draggable : true,
	callback : function () {},
	modal : true
};

$(function(){
	$(".jquery-button").bind("click", function(e){
		if ($(this).hasClass('disable') || $(this).is(":disabled")) {
			e.preventDefault();
			e.stopImmediatePropagation();
			e.stopPropagation();
			return false;
		}
	});
	$(".jquery-button").live("click", function(e){
		if ($(this).hasClass('disable') || $(this).is(":disabled")) {
			e.preventDefault();
			e.stopImmediatePropagation();
			e.stopPropagation();
			return false;
		}
	});
	if ($.browser.msie && $.browser.version == "6.0") {
		$(".jquery-button").live("mouseover", function(){
			$(this).addClass('hover');
		}).live("mouseout", function(){
			$(this).removeClass('hover');
			$(this).removeClass('focus');
		});
		$(".jquery-input").live("mouseover", function(){
			$(this).addClass('hover');
		}).live("mouseout", function(){
			$(this).removeClass('hover');
		}).live("focus", function(){
			$(this).addClass('focus');
		}).live("blur", function(){
			$(this).removeClass('focus');
		});
	} else {
		if ($.browser.msie && $.browser.version == "7.0") {
			$(".jquery-input").live("focus", function(){
				$(this).addClass('focus');
			}).live("blur", function(){
				$(this).removeClass('focus');
			});
		}
		$(".jquery-button").live("mouseout", function(){
			$(this).removeClass('focus');
		});
	}
	$(".jquery-button").live("mousedown", function(){
		$(this).addClass('focus');
	});
	$('body').bind("mouseup", function(){
		$(".jquery-button").removeClass('focus');
	});
});
