function ChangeTo(user, module) {
	if (module == '') { module = 'profile';	}
	$('#content').block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: user+'/'+module+'/xhr',
		success: function(msg){
			$('#content').unblock();
			$('#content').html(msg);
			UpdateMenuLink(module);
			UpdateInterface();
			UpdateMedia(module);
			document.title = winTitle +' > ' + ucfirst(module);
		}
	});
}

function GoPage(user, page) {
	$('#last_comments').block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: user+'/comments/page/'+page+'/xhr',
		success: function(msg){
			$('#last_comments').unblock();
			$('#last_comments').html(msg);
			UpdateInterface();
		}
	});
}

function SendWallMsg() {
	DeasactiveForm('#comment_form');
	$('#comment_form').submit();
}

function UpdatePostedForm(formType, msg, key) {
	if (formType == 'comment') {
		ActiveForm('#comment_form');
		if (msg.indexOf('error:') > -1) {
			DisplayError($('#comment_form_error'), msg);
		} else {
			var user = UrlParse(window.location.href, 1);
			$('#body').val('Write something...');
			GoPage(user, 1);
		}
	} else {
		alert(msg);
	}
}

function ShowMore(user, module, sub_cat, page) {
	$('#last_'+module).block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	if (typeof page != 'undefined') {
		$.ajax({
			url: '/',
			data: user+'/'+module+'/cat/'+sub_cat+'/page/'+page+'/xhr',
			success: function(msg){
				$('#last_'+module).unblock();
				$('#show_more_botton').hide();
				$('#last_'+module).append(msg);
			}
		});
	} else {
		$.ajax({
			url: '/',
			data: user+'/'+module+'/page/'+sub_cat+'/xhr',
			success: function(msg){
				$('#last_'+module).unblock();
				$('#last_'+module).html(msg);
			}
		});
	}
}

function DeleteMessage(user, id) {
	$.ajax({
		url: '/',
		data: user+'/delete/id/'+id+'/xhr',
		success: function(msg){
			GoPage(user, 1);
		}
	});
}

function SubCat(user, module, sub_cat) {
	$('#last_'+module).block({ message: null, overlayCSS: { backgroundColor: '#FFF' }  });
	$.ajax({
		url: '/',
		data: user+'/'+module+'/cat/'+sub_cat+'/xhr',
		success: function(msg){
			$('#last_'+module).unblock();
			$('#content').html(msg);
			UpdateInterface();
			UpdateMenuLink(module);
			UpdateMedia(sub_cat);
			document.title = winTitle+' > '+ ucfirst(module) + ' > ' + $('#media_link_'+sub_cat).text();
		}
	});
}

function UpdateMedia(module) {
	$('.content_control > a').removeClass("selected");
	if (typeof(module) != 'undefined') {
		if ($('#media_link_'+module).length) {
			$('#media_link_'+module).addClass("selected");
		} else {
			$('#media_link_all').addClass("selected");
		}
	} else {
		var result = UrlParse(window.location.href, 2);
		var ActivPage = '#media_link_'+result;
		if ($(ActivPage).length) {
			$(ActivPage).addClass("selected");
		} else {
			$('#media_link_all').addClass("selected");
		}
	}
}

function customHistory(hash) {
	var user = UrlParse(window.location.href, 1);
	if (user == 'profile') {
		user = UrlParse(window.location.href, 2);
	}
	user = Url.decode(user);
	user = unescape(user);
	user = user.replace(/\+/gi, " ");
	var tableau=hash.split('/');
	if (tableau.length >= 0) {
		if (tableau[1] == 'page' && tableau[0] !== null && tableau[2] !== null) {
			GoPage(user, tableau[2]);
		} else if (tableau[0] == 'more' && tableau[1] !== null && tableau[2] !== null && tableau[3] !== null) {
			ShowMore(user, tableau[2], tableau[3]);
		} else if (tableau[1] == 'cat' && tableau[2] !== null) {
			SubCat(user, tableau[0], tableau[2]);
		} else if (tableau[0] !== null) {
			ChangeTo(user, tableau[0]);
		}
	}
}
