/**
 * myConfirm ( http://supercanard.phpnet.org/jquery-test/myconfirm/ )
 * 
 * Version 1.1
 *
 * Auteur : Jonathan Coulet ( j.coulet@gmail.com )
 * 
 **/
(function($){
	$.fn.myConfirm = function(option){
		// Global var
		var param = jQuery.extend({
			popupWidth: 300, // @param : int (px)
			fadeSpeed: 150, // @param : int
			txtConfirm: 'OK', // @param : string
			txtExit: 'Annuler', // @param : string
			containerColor: '#ccc', // @param : color code
			popupColor: '#f1f1f1', // @param : color code
			popupBorderColor: '#888', // @param : color code
			textColor: '#888' // @param : color code
		}, option);
		var arrQuestion = Array();
		var marginTopContainer = (document.body.clientHeight/2)-100;
		// Fonctions
		function idForAll(cible, value){
			// Fonction générique : applique des identifiants uniques à une liste d'éléments ciblés
			$(cible).each(function(i){
				$(this).attr('id', value+'-'+i);
			});
		}
		function getAllQuestion(){
			// Stocke les attributs title de la liste d'éléments .myConfirm dans un tableau et les suppriment des éléments
			$('.myConfirm').each(function(i){
				arrQuestion[$(this).attr('id')] = $(this).attr('title');
				$(this).removeAttr('title');
			});
		}
		function toggleContainer(){
			// Affiche ou cache le container
			if($("#myConfirm-container").css('display') == 'none'){
				$('#myConfirm-container').show('fast', function(){
					$('#myConfirm-container').animate({
						opacity: '1'
					}, option.fadeSpeed);
				});
			}
			else{
				$('#myConfirm-container').css({ opacity: '0' });
				$('#myConfirm-container').hide();
			}
		}
		// Init
		idForAll($(this), $(this).attr('class'));
		getAllQuestion();
		$('body').append('<div id="myConfirm-container"><div id="myConfirm-popup"></div></div>');
		$('#myConfirm-container').css({
			display: 'none',
			opacity: '0',
			position: 'absolute',
			top: '0',
			left: '0',
			zIndex: '1500',
			width: '100%',
			height: '100%',
			backgroundColor: option.containerColor
		});
		$('#myConfirm-popup').css({
			width: option.popupWidth+'px',
			overflow: 'hidden',
			marginTop: marginTopContainer+'px',
			marginRight: 'auto',
			marginLeft: 'auto',
			padding: '15px',
			color: option.textColor,
			backgroundColor: option.popupColor,
			border: '1px solid '+option.popupBorderColor
		});
		$('#myConfirm-popup*').css({
			fontFamily: 'Verdana, Geneva, sans-serif',
			fontSize: '12px'
		});
		// Actions
		$(this).each(function(){
			$($(this)).click(function(){
				var question = arrQuestion[$(this).attr('id')];
				var url = $(this).attr('href');
				var elem = $(this).attr('id');
				$(this).removeAttr('href');
				toggleContainer();
				$('#myConfirm-popup').append('<p>'+question+'<div style="clear:both;"></div><a href="javascript:" class="bouton" id="myConfirm-confirm">'+option.txtConfirm+'</a><a href="javascript:" class="bouton" id="myConfirm-exit">'+option.txtExit+'</a></p>');
				$('#myConfirm-popup *').css({
					margin: '0'
				});
				$('#myConfirm-popup a.bouton').css({
					display: 'block',
					float: 'left',
					padding: '5px',
					marginTop: '15px',
					marginRight: '5px',
					cursor: 'pointer',
					color: '#fff',
					textDecoration: 'none',
					backgroundColor: option.textColor
				});
				$('#myConfirm-popup a.bouton').hover(function(){
					$(this).css({
						backgroundColor: '#000'
					});
				});
				$('#myConfirm-popup a.bouton').mouseout(function(){
					$(this).css({
						backgroundColor: option.textColor
					});
				});
				$('#myConfirm-popup a#myConfirm-confirm').click(function(){
					$('#'+elem).attr('href', url);
					$('#myConfirm-popup').empty();
					toggleContainer();
					location.href = url;
				});
				$('#myConfirm-popup a#myConfirm-exit').click(function(){
					$('#'+elem).attr('href', url);
					$('#myConfirm-popup').empty();
					toggleContainer();
				});

			});
		});
	}
})(jQuery);
