( function($) {
	var settings;
	
	jQuery.fn.b2f_carnetVoyage = function(action, options) {
		switch (action) {
		case 'store':
			store(options, this);
			break;
		case 'afficheFormLogin':
			afficheFormLogin(options, this);
			break;
		case 'setSetting':
			setSetting(options, this);
			break;
		case 'init':
		default:
				init(options, this);
		}
		return this;
	}
	
	function setSetting(newSetting, self) {
		settings = jQuery.extend(settings, newSetting);
	}
	
	function afficheFormLogin(options, self) {

		var params = jQuery.extend( {
			 dataType: 'html'
			, type: 'get'
			, data: {render_mode:'ajax'}
			, url: ''
				
		}, options);
		
		if ($('#carnet-voyage-dialog').length == 0) {
			$('<div id="carnet-voyage-dialog" ></div>').appendTo(document.body);
		}
		
		
		$.ajax({
			dataType : params.dataType
			, type : params.type
			, data : params.data
			, url : params.url
			, cache : false
			, success : function(htmlResponse) {
				$('#carnet-voyage-dialog')
					.html(htmlResponse)
					.dialog({
						bgiframe 	: true
						, width		: 640
						, modal		: true
						, close		: function() {
							$('#carnet-voyage-dialog').dialog('destroy').remove();
						}
					})
					.find('.entrieDelete a')
						.bind('click', function() {
							$.ajax({
									dataType: 'html'
									, type: 'get'
									, data: {render_mode:'ajax'}
									, url : $(this).attr('href')
								, success: function(htmlResponse) {
									$(settings.formDomId).find('.cv-selection').trigger('click');
								}
							});
							
							return false;
						})
						.end()
					.find('.cv-modifier-compte')
						.bind('click', function() {
							afficheFormLogin({url:$(this).attr('href')}, this);
							
							return false;
						})
				;
			}
		});
	}
	
	function store(options, self) {

		var params = jQuery.extend( {
			cont_id		: ''
			, titre 	: ''
			, url 		: ''
		}, options);
			
		if((params.cont_id>0 || settings.cont_id>0) && (settings.isLoged == null || settings.isLoged == true)){
			jQuery.ajax({
				   type: "GET",
				   url: settings.store,
				   data: {cont_id:params.cont_id,titre:params.titre,url:params.url, render_mode:'ajax'},
				   dataType : "html",
				   cache : false,
				   success: function(texte){
					   var toaster = $('div', texte)
					   		.dialog({ show: 'explode' });
					   setTimeout(function() {
						   toaster.dialog('close');
					   }, 5000);
					   $('.cvhome').change();
				   }   
			});
		}else{
			afficheFormLogin({url:settings.login+'?titre='+escape(params.titre)+'&url='+escape(params.url)}, this);
		}
	}
	
	function init(options, self) {
		settings = jQuery.extend( {
			store		: ""
			, login 	: ""
			, isLoged 	: null
			, formDomId	: '#cvhome'
		}, options);
		
		$(settings.formDomId)
			.live('change', function() {
				var self = this;
				$.ajax({
					data: $(this).serialize()
					, dataType: 'html'
					, type: 'get'
					, data: {render_mode:'ajax'}
					, url: $(this).attr('action')
					, success: function(htmlResponse) {
							$(settings.formDomId).html($(settings.formDomId, htmlResponse).html());
					}
				});//fin $.ajax()
				return false;
			})
			.find('.cv-logout, .cv-selection')
				.live('click', function() {
					//console.debug(afficheFormLogin, {url:$(this).attr('href')}, afficheFormLogin({url:$(this).attr('href')}, this)); alert('test');
					afficheFormLogin({url:$(this).attr('href')}, this);
					
					return false;
				})
		;
	}
	
	
	
})(jQuery);