/*---------------------------------------------------[ other ajax ]------------------------------------------*/
function followUser(userId) {
	var link = $('#followLink');
	link.html('Please wait...');
	
	if (link.attr('rel') == 'follow') {

		$.post("/followers/follow/"+userId+"?call=ajax", {}, function(response) {
			if (response.result == 'success') {
				link.attr('rel', 'unfollow').attr('title', response.title);
			}
			link.html(response.message);
		}, 'json');
		
	} else if (link.attr('rel') == 'unfollow') {
		
		$.post("/followers/unfollow/"+userId+"?call=ajax", {}, function(response) {
			if (response.result == 'success') {
				link.attr('rel', 'follow').attr('title', response.title);
			}
			link.html(response.message);
		}, 'json');
		
	}

	return false;
}
	


function showFollowers(followedUserId) {
	if ($('#followersDiv').length > 0) {
		$('#followersDiv').show();
	} else {
		$.get('/feeds/followers/'+followedUserId+'?call=ajax&_='+Math.random(), function(response){
			$('.followmenu li:first').append(response);
			$('#followersDiv').show();
		});
	}
}
function showFollowed(followerId) {
	if ($('#followedDiv').length > 0) {
		$('#followedDiv').show();
	} else {
		$.get('/feeds/followed/'+followerId+'?call=ajax&_='+Math.random(), function(response){
			$('.followmenu li:eq(1)').append(response);
			$('#followedDiv').show();
		});
	}
}
function changeImageSet(setSelect) {
	var imageId = $(setSelect).attr('id').split('_')[1];
	var setId = $(setSelect).val();
	$(setSelect).after('<img src="http://'+siteTopDomain+'/img/working.gif" class="ajaxWorking" />');
	$.post('/images/changeSet?call=ajax&_='+Math.random(), {'data[Image][id]':imageId,'data[Image][image_set_id]':setId}, function(response){
		$('.ajaxWorking').remove();
		if (response == 'forbidden') {
			alert('This action is forbidden. Please log in.');
		} else if (response == 'error') {
			alert('Error while saving');
		} else {
			//success
		}
	});
}
function switch_theme(new_theme) {
	$.post('/systems/switchTheme/?call=ajax', {'theme':new_theme}, function(response){
		window.location.reload(false);
	});
}

function saveStatus() {
	var newStatus = $('div.editsubmenu div.statusbox input').val();
	$('div.editsubmenu div.statusbox img[alt="Ok"]').attr('src', '/img/load_dark.gif')
	$.post('/profiles/saveStatus/?call=ajax', {'data[Profile][status]':newStatus}, function(response){
		$('div.editsubmenu div.statusbox img[alt="Ok"]').attr('src', '/img/statusok.gif');
		if ($('.savedSpan').length == 0) {
			$('div.editsubmenu div.statusbox img[alt="Ok"]').after('<span class="savedSpan"><img src="/img/saved.gif" class="floatleft" style="padding-left:4px;" alt="Saved" /></span>');
		}
		setTimeout("$('div.editsubmenu div.statusbox .savedSpan').remove()", 5000);
	});
}

function showFavorites(model, parentId) {
	var favoriteByDiv = $('#favoriteByDiv');
	
	if (favoriteByDiv.is(':visible')) {
		
		favoriteByDiv.hide();
		
	} else if (favoriteByDiv.html().trim() == '') {
		
		favoriteByDiv.html('<img src="/img/load_dark.gif">');
		
		$.ajax({
			'url':'/favorites/getUsersByFavorite/'+model+'/'+parentId,
			'type':'GET',
			'cache':true,
			'success':function(response){
				favoriteByDiv.html(response);
			}
		});
		
		favoriteByDiv.slideDown('fast');
	} else {
		favoriteByDiv.slideDown('fast');
	}
	
	return false;
}


function changeMosaicType(link, direction) {
	var mosaicInfo =  $('#mosaicUl').attr('title');
	if (mosaicInfo) {
		$('#mosaicUl').data('mosaicInfo', mosaicInfo).attr('title', '');
	} else {
		mosaicInfo = $('#mosaicUl').data('mosaicInfo');
	}

	var mosaicGroupCurrent = mosaicInfo.split(':')[0];
	var mosaicTypeCurrent = mosaicInfo.split(':')[1];
	var mNum = parseInt(mosaicInfo.split(':')[2]);
	var rand = mosaicInfo.split(':')[3];

	if ($(link).attr('rel')) {
		if ($(link).attr('rel').split('=')[0] == 'group') { //like group:NEWEST
			var mosaicGroupNew = $(link).attr('rel').split('=')[1];
			var mosaicTypeNew = mosaicTypeCurrent;
		} else if ($(link).attr('rel').split('=')[0] == 'type') { //like type:ALL
			var mosaicTypeNew = $(link).attr('rel').split('=')[1];
			var mosaicGroupNew = mosaicGroupCurrent;
		}
	} else {
		var mosaicGroupNew = mosaicGroupCurrent;
		var mosaicTypeNew = mosaicTypeCurrent;
	}

	var mNumNew;

	var url = '/pages/changeMosaicType/'+mosaicGroupNew+'/'+mosaicTypeNew+'/';

	if (typeof direction != 'undefined') {
		if (direction == 'prev') {
			if (mNum == 0) {
				mNumNew = 7;
			} else {
				mNumNew = mNum - 1;
			}
		} else if (direction == 'next') {
			if (mNum == 7) {
				mNumNew = 0;
			} else {
				mNumNew = mNum + 1;
			}
		}
	}

	if (typeof mNumNew != 'undefined') {
		url += mNumNew+'/';
	}
	
	$.ajax({
		'url':url,
		'type':'GET',
		'cache':true,
		'dataType':'html',
		'success':function(resp){
			var tmpUl = $('<ul></ul>');

			$('#mosaicUl').replaceWith(tmpUl);
			setTimeout(function(){
				tmpUl.replaceWith(resp);
			}, 50);

			$('#mosaicLinks a').removeClass('current');
			$('#mosaicLinks a[rel="type='+mosaicTypeNew+'"]').addClass('current');
			$('#mosaicLinks a[rel="group='+mosaicGroupNew+'"]').addClass('current');

			var mtStr = mosaicTypeNew;
			if (mtStr.length > 2) {
				mtStr = ucfirst(mtStr.toLowerCase());
			}

			$('#currentMosaicGroupSpan').html(ucwords(mosaicGroupNew.replace('_', ' ', 'g').toLowerCase()));
			$('#currentMosaicTypeSpan').html(mtStr);

			setTimeout("mosaicTooltip()", 500);
			setTimeout("removeMosaicTitle()", 1500);
		}
	});

	return false;
}
