var FuncAjaxLoad = {};
var openPopup = {};

$(document).ready(function(){
	//user menu
	$('.tbUserH').live('click', function(event) {
		var obj = $('.tbMenu');
		if(obj.css('display')=='none')
		{
			$('.tbMenu').slideDown();
		}
		else
		{
			$('.tbMenu').slideUp();
		}
	});

	//do an ajax post call to the server
	FuncAjaxLoad = function(dataurl,vars){
		$.ajax({
			type: "POST",
			url: dataurl,
			data: vars,
			success: function(retmsg){
				if(retmsg.indexOf('<b>Parse error</b>')>0)
				{
					alert("ERROR" + retmsg);
				}
				else
				{
					try{
						ret=JSON.parse(retmsg);
					} catch (e){
						alert('Server Script error : ' + retmsg);
					}
				}
			}
			,async: false
		});
		return ret;
	};

	openPopup = function(html){
		$("#PopupContent").html(html);
		$('.hPopup').css("display","block");
	};

	//close popup
	$('.closePopup').live('click', function(event) {
		$('.hPopup').hide();
	});


	//Dofollow
	$('.DoFollow').live('click', function(event) {
		var uid= $(this).attr('id').replace(/dfuid/i, "");
		var ret = FuncAjaxLoad("/json/follow.php",{action:'follow',uid:uid})
		if(ret.loadstatus=='OK')
		{
			$(this).fadeOut();
		}
		else
		{
			alert(ret.loadstatus);
		}
	});

	//Stopfollow
	$('.StopFollow').live('click', function(event) {
		var uid= $(this).attr('id').replace(/sfuid/i, "");
		var ret = FuncAjaxLoad("/json/follow.php",{action:'stopfollow',uid:uid})
		if(ret.loadstatus=='OK')
		{
			$(this).fadeOut();
		}
		else
		{
			alert(ret.loadstatus);
		}
	});
	
	//Activity tab
	$('.ActTabs').live('click', function(event) {
		var type= $(this).attr('id');
		var uid = $(this).closest('.Tabs').attr('id').replace(/user/i, "");
		$('.ActTabs').removeClass('selected');
		$(this).addClass('selected');
		$("#ActContent").html('<div align="center"><Br><img src="/img/ajax-loader.gif"></div>');
		var ret = FuncAjaxLoad("/json/user_activity.php",{type:type,uid:uid});
		if(ret.loadstatus=='OK')
		{
			$("#ActContent").html(ret.html).slideDown();
		}
		else
		{
			alert(ret.loadstatus);
		}
	});

	function FitToContent(id, maxHeight)
	{
		var text = id && id.style ? id : document.getElementById(id);
		if ( !text )
		return;

		var adjustedHeight = text.clientHeight;
		if ( !maxHeight || maxHeight > adjustedHeight )
		{
		adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
		if ( maxHeight )
			adjustedHeight = Math.min(maxHeight, adjustedHeight);
		if ( adjustedHeight > text.clientHeight )
			text.style.height = adjustedHeight + "px";
		}
	}

});
