// Unicodé

var volatile_cfg = {
	data : {},
	focuse : '',
	html : '',
	on_close : function() {},
	on_load : function() {},
	url : '',
	width : 700
};
var volatile_timeout = null;
var volatile_timeout_func = 'volatile_close();';

function volatile(arg_1, arg_2) {
	if($('#volatile').size() == 0) {
		volatile_create();
		volatile_width(700);
	}
	
	$('#volatile .t .actions .reload').hide();
	
	clearTimeout(volatile_timeout);
	
	volatile_width(volatile_cfg.width);
	
	if(arg_1.substr(0, 1) == '@') {
		volatile_message(arg_1.substr(1));
		volatile_cfg.on_load();
		volatile_bind_closers();
	}
	else {
		if(isset(arg_2) === false) {
			arg_2 = {};
		}
		
		var title = arg_1;
		
		volatile_cfg = array_merge({
			data : {},
			focuse : '',
			html : '',
			on_close : function() {},
			on_load : function() {},
			url : '',
			width : 700
		}, arg_2);
		
		volatile('@loading');
		
		$('#volatile').zIndexize();
		
		$.post(volatile_cfg.url, volatile_cfg.data, function(resp) {
			if(resp.substr(0, 1) == '@') {
				volatile(resp);
			}
			else {			
				$('#volatile .t .actions .reload').show().unbind('click').click(function() {
					volatile_cfg.on_close();
					
					volatile(title, volatile_cfg);
				});
				
				$('#volatile .t .actions .close').show();
				
				volatile_title(title);	
				volatile_html(resp);
				volatile_bind_closers();
				
				volatile_cfg.on_load();
	
				$('#volatile').zIndexize();
				
				if(volatile_cfg.focuse != '') {
					setTimeout(function() {
						$(volatile_cfg.focuse).focus();
					}, 600);	
				}
			}
		});
	}
}

/*
	Create
*/
function volatile_create() {
	var buffer = '<div class="t">'
			   + '<h2></h2><div class="actions"><a href="#null" class="loading" title="Chargement en cours.."><img src="img/volatile/loading.gif" width="20" height="20" class="vam" /> <span style="padding-left:5px;">Chargement en cours, veuillez patienter..</span></a><a href="#null" class="reload closer" title="Recharger le contenu"><img src="img/volatile/reload.png" width="20" height="20" class="vam" /></a><a href="#null" class="close closer" title="Fermer"><img src="img/volatile/close.png" width="20" height="20" class="vam" /></a></div>'
			   + '</div>'
			   + '<div class="b">'
			   + '<div class="br"></div>'
			   + '</div>';	
	
	$('#volatile').create().html(buffer).draggable('destroy').draggable({
		cursor : 'move',
		delay : 100,
		handle : $('#volatile .t h2')
	}).unbind('click').click(function() {
		$('#volatile').zIndexize();
	});
	
	$('#volatile .t').disableSelection();
}

/*
	Close
*/
function volatile_close() {
	if($('#volatile').size() == 0) {
		return false;	
	}
	
	$('#volatile').fadeTo('fast', 0, function() {
		$(this).remove();
	});
	
	volatile_objects('restore');
}

/*
	Bind closers
*/
function volatile_bind_closers() {
	$('#volatile .closer').each(function() {
		var obj = $(this);
		
		if(obj.hasClass('closer_binded')) {
			return true;	
		}
		
		obj.addClass('closer_binded').click(function() {
			volatile_cfg.on_close();
			
			if($(this).hasClass('reload') === false) {
				volatile_close();
			}
		});
	});
}

/*
	Message
*/
function volatile_message(str) {
	var title = '';
	var message = '';
	var img = '';
	var type = '';
	
	if(str.substr(0, 7) == 'loading') {
		title = 'Chargement';
		img = '<img src="img/misc/ajax.light.35.gif" width="35" height="35" style="margin-top:7px; margin-left:7px;" />';
		type = 'message';
		str = 'Chargement en cours, veuillez patienter..';
	}
	else if(str.substr(0, 11) == 'maintenance') {
		title = 'Maintenance';
		img = '<img src="img/icos/stop.50.png" width="50" height="50" />';
		type = 'maintenance';
		str = 'Le site est actuellement en maintenance, vous ne pouvez pas accéder à cet espace.';
	}
	else if(str.substr(0, 7) == 'confirm') {
		title = 'Message de confirmation';
		img = '<img src="img/icos/confirm.50.png" width="50" height="50" />';
		type = 'confirm';
		str = str.substr(7);
	}
	else if(str.substr(0, 5) == 'error') {
		title = 'Message d\'erreur';
		img = '<img src="img/icos/error.50.png" width="50" height="50" />';
		type = 'error';
		str = str.substr(5);
	}
	else {
		type = 'message';
		title = 'Message';
		img = '<img src="img/icos/info.50.png" width="50" height="50" />';
	}
	
	var buffer = '<div class="section">'
		 	   + '<div class="_t"><h2>'+title+'</h2></div>'
		 	   + '<div class="_b">'
		 	   + '<div class="_br">'
		 	   + '<table border="0" cellpadding="0" cellspacing="0" width="100%" align="center">'
		 	   + '<tr>'
		 	   + '<td width="70" height="60" align="center">'+img+'</td>'
		 	   + '<td><div class="'+type+'">'+str+'</div></td>'
		 	   + '</tr>'
		 	   + '</table>';

	if(title != 'Chargement') {
		buffer += '<div align="center" class="mt10"><input type="button" class="button" value="Continuer" onclick="volatile_timeout_set(volatile_timeout_func, 1);" /></div>';
	}
	
	volatile_html(buffer);	
	volatile_title(title);
}

/*
	Html
*/
function volatile_html(html) {
	if($('#volatile').size() == 0) {
		return false;	
	}
	
	$('#volatile .br').html(html);	
	
	volatile_coords();
}

/*
	Width
*/
function volatile_width(width) {
	if($('#volatile').size() == 0) {
		return false;	
	}
	
	$('#volatile').css('width', width);	
	
	volatile_coords();
}

/*
	Title
*/
function volatile_title(str) {
	$('#volatile .t h2').html(str);
}

/*
	Coords
*/
function volatile_coords() {
	if($('#volatile').size() == 0) {
		return false;	
	}
	
	$('#volatile').coords(-1, -1, true);
}

/*
	Objects
*/
function volatile_objects(a) {
	if(a == 'restore') {
		$('object.hidden_by_volatile').css('visibility', 'visible');
	}
	else {
		$('object:visible').each(function() {
			var obj = $(this);
			
			if(obj.hasClass('hidden_by_volatile') === false) {
				obj.addClass('hidden_by_volatile').hide();
			}
		});
	}
}

/*
	Loading
*/
function volatile_loading(loading) {
	if(loading) {
		$('#volatile .t .actions .loading').show();
	}
	else {
		$('#volatile .t .actions .loading').hide();
	}
}

/*
	Timeout set
*/
function volatile_timeout_set(func, delay) {
	volatile_timeout_func = func;
	
	clearTimeout(volatile_timeout);
	
	volatile_timeout = setTimeout(func, delay);
}

/*
	Form
*/
function volatile_form(cfg) {
	if(isset(cfg) === false) {
		cfg = {};	
	}
	
	cfg = array_merge({
		form : '#volatile_form',
		before_confirm : function() {},
		on_submit : function() {},
		on_confirm : function() {
			volatile_timeout_set('volatile_close()', 3000);
		},
		cancel : volatile_close
	}, cfg);

	$(cfg.form+' .closer').click(function() {
		 setTimeout(cfg.cancel, 300);
	});

	$(cfg.form).submit(function() {
		cfg.on_submit();
		
		return volatile_form_submit(cfg);
	});
}
function volatile_form_submit(cfg) {
	var form = $(cfg.form);
	var id = form.attr('id');
	var action = form.attr('action');
	var data = {};
	
	volatile_loading(true);

	$('#'+id+' .submit').attr('disabled', 'true');
	$('#volatile .closer').hide();

	$('#'+id+' input, #'+id+' select, #'+id+' textarea').each(function() {
		if($(this).attr('id') == '') {
			return true;	
		}
		
		if($(this).attr('type') == 'checkbox') {
			$(this).val($(this).attr('checked'));
		}
		
		data[$(this).attr('id')] = $(this).val();
	});

	$.post(action, data, function(resp) {
		if(strpos(resp, '@confirm') !== false || strpos(resp, '@error') !== false || strpos(resp, '@maintenance') !== false) {
			resp = resp.substr(strpos(resp, '@'));
			
			cfg.before_confirm();
			
			volatile(resp);
			
			cfg.on_confirm(resp);
		}
		else {
			$('#'+id+' .submit').removeAttr('disabled');
			$('#volatile .closer').show();
			$('#'+id+' .status').html('<div class="mt10"><img src="img/icos/warning.20.png" width="20" height="20" class="vam" /> '+resp+'</div>');
		}

		volatile_loading(false);
		volatile_coords();
	});
	
	return false;
}

/*
	Resize
*/
$(window).resize(function() {
	var obj = $('#volatile');
	var coords = obj.coords();
	
	if(coords.left > (client('width') - 20) || coords.top > (client('height') - 20)) {
		obj.coords(-1, -1, true);
	}
});