// Unicodé

function user(title, data, cfg) {
	cfg = array_merge({
		focuse : '',
		on_load : function() {}
	}, cfg);
	
	volatile(title, {
		url : 'user.ajax',
		data : data,
		focuse : cfg.focuse,
		on_load : cfg.on_load
	});
}


/*
	Bar
*/
var user_bar_updating = false;
var user_bar_relations_cache = '';

$(document).ready(function() {
	if(user_loggued === false) {
		if(typeof(at_home) == 'undefined') {
			$('#user_login_register').create().html('<a href="#null" onclick="user_login();">Identification</a> | <a href="#null" onclick="user_register();">Créer un compte</a>');
		}
		return false;
	}
	
	// User
	var items = [
		{
			str : 'Profil',
			on_click : 'user_profile();',
			ico : 'user'
		},
		{
			str : 'Photo / Avatar',
			on_click : 'user_picture();',
			ico : 'picture'
		},
		{
			str : 'Mot de passe',
			on_click : 'user_password();',
			ico : 'locked'
		},
		{
			str : 'Adresse e-mail',
			on_click : 'user_email();',
			ico : 'email'
		},
		{
			str : 'Status',
			on_click : 'user_status();',
			ico : 'smile'
		},
		{
			str : 'Crédits',
			on_click : 'user_credits();',
			ico : 'money'
		},
		{
			str : 'Gestionnaire de fichiers',
			on_click : 'fm_init();',
			ico : 'files'
		},
		{
			str : 'Echanges avec le support',
			on_click : 'user_support();',
			ico : 'help'
		},
		/*{
			str : 'Mes thèmes graphiques',
			on_click : 'user_themes();',
			ico : 'scissors'
		},*/
		{
			str : 'Retour sur Expressif',
			on_click : 'document.location.href = \'http://www.expressif.fr\';',
			ico : 'logo'
		},
		{
			str : 'Déconnecter',
			on_click : 'user_logout();',
			ico : 'off'
		}
	];
	
	var i = 0;
	var buffer = '<h2>Connecté en tant que '+user_ident+'</h2>';
	
	for(i = 0; i < items.length; i++) {
		buffer += '<div class="item" onclick="'+items[i].on_click+'"><div class="item_right"><img src="img/icos/'+items[i].ico+'.20.png" width="20" height="20" class="vam" /> '+items[i].str+'</div></div>';
	}
	
	$('#bar_user').create().hide().html(buffer);
	
	bar_element_add('user', user_ident, {
		id_content : 'bar_user',
		priority : 1
	});

	user_sites();
	
	bar_element_add('facebook', 'Partager cette page', {
		on_click : function() {
			window.open('http://www.facebook.com/share.php?u='+document.location.href);
		}
	});
});

/*
	Credits
*/
function user_credits() {
	user('Mes crédits', { a : 'credits' });
}
function user_credits_buy(method) {
	if(method == 'creditcard' || method == 'neosurf' || method == 'internetplus' || method == 'phone' || method == 'sms') {
		on_load = function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('user_credits()', 6000);
				},
				cancel : user_credits
			});
		}
	}
	else if(method == 'paypal') {
		on_load = function() {
			
		}
	}
	else if(method == 'moneybookers') {
		on_load = function() {
			
		}
	}
	
	user('Acheter des crédits', { a : 'credits', sa : method }, {
		 on_load : on_load
	});
}

/*
	Email
*/
function user_email() {
	user('Email', { a : 'email' }, {
		focuse : '#email',
		on_load : function() {
			volatile_form();
		}
	});
}

/*
	Login
*/
function user_login() {
	user('S\'identifier', { a : 'login' }, {
		 focuse : '#ident',
		 on_load : function() {
			 volatile_form({
				on_confirm : function() {
					volatile_timeout_set('go()', 3000);
				}
			});
		 }
	});
}

/*
	Logout
*/
function user_logout() {
	user('Se déconnecter', { a : 'logout' }, {
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('go(\'/\')', 3000);
				}
			});
		}
	});
}

/*
	Lost
*/
function user_lost() {
	user('Identifiants perdus', { a : 'lost' }, {
		focuse : '#ident',
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('user_login()', 6000);
				}
			});
		}
	});
}

/*
	Password
*/
function user_password() {
	user('Status', { a : 'password' }, {
		focuse : '#password',
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('go()', 3000);
				}
			});
		}
	});
}

/*
	Picture
*/
function user_picture() {
	user('Photo / Avatar', { a : 'picture' }, {
		on_load : function() {
			volatile_form();
		}
	});
}
function user_picture_choose(file) {
	if(isset(file.path) === false) {
		file = file[0];
	}
	
	var scaled = scale(file.width, file.height, 100, 100);
	
	$('#picture').val(file.path);
	$('#picture_preview').html('<img src="'+file.path+'" width="'+scaled.width+'" height="'+scaled.height+'" class="vam" style="padding:4px;" />');
}

/*
	Profile
*/
function user_profile() {
	user('Profil', { a : 'profile' }, {
		focuse : '#ident',
		on_load : function() {
			volatile_form();
		}
	});
}

/*
	Register
*/
function user_register() {
	user('Créer un compte', { a : 'register' }, {
		focuse : '#ident',
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('go(\'redirect?url=/#user_site_create\')', 3000);
				}
			});
		}
	});
}

/*
	Site create
*/
function user_site_create(step) {
	user('Créer un site', { a : 'site_create', step : step }, {
		focuse : '',
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					var next = $('#step_next').val();
					
					volatile_timeout_set('user_site_create('+next+')', (next == 6 ? 500 : 0));
				}
			});
			
			$('#volatile .back').click(function() {
				user_site_create(step - 1);
			});
		}
	});
}

/*
	Sites
*/
function user_sites() {
	bar_element_delete('mes_sites');
	
	$.post('user.ajax', { a : 'sites' }, function(resp) {
		var json = eval('('+resp+')');
		var buffer = '<h2 style="min-width:200px;">Mes sites</h2>';
		
		if(json.length > 0) {		
			var k = '';
			
			for(k in json) {
				buffer += '<div class="item" onclick="window.open(\'http://'+json[k].subdomain_primary+'.'+json[k].domain_primary+'\');"><div class="item_right"><img src="'+(json[k].image == 'no' ? 'data/users/site.image.default.png' : json[k].image)+'" width="20" height="20" class="vam" /> '+json[k].title+'</div></div>';
			}
		}
		else {
			buffer += '<div class="item"><div class="item_right"><img src="img/icos/info.20.png" width="20" height="20" class="vam" /> Aucun site</div></div>';
		}
		
		buffer += '<div class="item"><div class="item_right" onclick="user_site_create();"><img src="img/icos/more.20.png" width="20" height="20" class="vam" /> Créer un site</div></div>';
		
		$('#bar_sites').create().hide().html(buffer);
		
		bar_element_add('earth', 'Mes sites', {
			id_content : 'bar_sites',
			priority : 2
		});
	});
}

/*
	Status
*/
function user_status() {
	user('Status', { a : 'status' }, {
		focuse : '#status',
		on_load : function() {
			volatile_form();
		}
	});
}

/*
	Support
*/
function user_support() {
	user('Echanges avec le support', { a : 'support' });
}
function user_support_view(support_id) {
	user('Voir un échange', { a : 'support', sa : 'view', support_id : support_id }, {
		on_load : function() {
			$('.support_close').click(function() {
				user_support_close(support_id);
			});
			
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('user_support()', 3000);
				},
				cancel : user_support
			});
		}
	});
}
function user_support_close(support_id) {
	user('Clôturer un échange', { a : 'support', sa : 'close', support_id : support_id }, {
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('user_support()', 3000);
				},
				cancel : function() {
					user_support_view(support_id);
				}
			});
		}
	});
}
function user_support_write(subject) {
	var data = { a : 'support', sa : 'write' };
	
	if(isset(subject)) {
		data.subject = subject;	
	}
	
	user('Lancer un nouvel échange', data, {
		focuse : '#subject',
		on_load : function() {
			volatile_form({
				cancel : user_support
			});
		}
	});
}

/*
	Themes
*/
/*function user_themes() {
	user('Mes thèmes graphiques', { a : 'themes' }, {
		on_load : function() {
			volatile_width(830);
			
			volatile_form();
		}
	});
}
function user_theme_sheet(theme_id) {
	user('Fiche d\'un thème graphique', { a : 'theme', sa : 'sheet', theme_id : theme_id }, {
		on_load : function() {

		}
	});
}
function user_theme_delete(theme_id) {
	user('Supprimer un thème graphique', { a : 'theme', sa : 'delete', theme_id : theme_id }, {
		on_load : function() {
			volatile_form({
				on_confirm : function() {
					volatile_timeout_set('user_themes()', 3000);
				},
				cancel : function() {
					user_theme_sheet(theme_id);	
				}
			});
		}
	});
}*/