  	var numMsg=10;
  	var dontReload=false;
	var currentFilter="";
	var currentFilterID="tag_";
	var currGroup = 0;
	var backgroundImage = '';
	var groupsList = new Array();
	var lastMsgValue = '';
	var userHasWritePermission = true;
  	
  	$(document).ready(function() {
		$("#messagearea").val('');
		$("#messagearea").focus();
		highlightCurrentTag();
		highlightCurrentGroup();
		onActivePeriodicRefresh();
	});


	function onActivePeriodicRefresh(){
		if (!dontReload) {
			getNewMessageCount();
			setTimeout("onActivePeriodicRefresh()", 120000);
		}
		else{
			setTimeout("onActivePeriodicRefresh()", 120000);
		}
	}
	
	function refreshDynamicParts(){
		loadMotherWrapper();		
		//loadTags();
		//loadGroups();
	}

	function loadMotherWrapper(){
		url = 'loadmotherwrapper?' + '&group='+currGroup + '&msgtype='+escape(currentFilter) + '&nummsgs='+numMsg;
		$('#motherWrapper').load(url,function(){
			highlightCurrentTag();
			highlightCurrentGroup();
			if(currentFilter!='self' && currentFilter!=''){
				$("#messagearea").val(currentFilter + " ");
				}
			else{$("#messagearea").val(''); }
			$("#messagearea").focus();
		});
	}
	
	function loadTags(){
		//alert("loading tags");
		url = 'loadtags?' + '&group='+currGroup;
		$('#rightBarContent').load(url,function(){
			highlightCurrentTag();
			//Remove any loading class associated if present
			$("#SaveUnsavePreloadDiv").removeClass("loadingSaveUnsave");
		}
		);
	}

	function highlightCurrentTag(){
		$('.rightSidebarMenu').css("background-color","");
		$('#'+currentFilterID).css("background-color","#FFFFFF");		
		$('.tagsClass').css("font-weight","normal");
		$('#'+currentFilterID).css("font-weight","bold");
	}

	function loadGroups(){
		url = 'loadgroups?group='+currGroup;
		$('#groupsList').load(url,function(){
			highlightCurrentGroup();
			
		});
	}

	function highlightCurrentGroup(){
		$('.groupsListMenu').css("background-color","");
		$('#group_'+currGroup).css("background-color","#FFFFFF");
		$('.groupsListMenu').css("font-weight","normal");
		$('#group_'+currGroup).css("font-weight","bold");
	}

	function loadMessages(){
		//if (dontReload){return}
		url = 'loadmessages?msgtype=' + escape(currentFilter) + '&nummsgs=' + numMsg+'&group='+currGroup;		
		$("#newMessageNotificationDiv").slideUp(); //To ensure when you go to a new group you dont see information from a diff group dangling in the bottom	
		$('#section').load(url,function(){
			if(currentFilter!='self' && currentFilter!=''){
				$("#messagearea").val(currentFilter + " ");
				}
			else{$("#messagearea").val(''); }
			//alert(backgroundImage);
			if(backgroundImage != ''){
				backgroundStr = "#DCE9F6 url(" + backgroundImage + ") repeat scroll 0 0";
				$("body").css('background',backgroundStr);
			}

			else{
				//alert("in the else statement now");
				backgroundStr = "#DCE9F6 url(/static/images/blue-gradient.png) repeat-x scroll 0 0";
				$("body").css('background',backgroundStr);
			}

		});

	}

	function saveFilter(filter){
		//alert("came here");
		url = "/savetag?group=" + currGroup + "&msgtype=" + escape(filter);
		$('#SaveUnsavePreloadDiv').attr('class','loadingSaveUnsave');
		$('#SaveActionLink').css('display','none');
		$('#UnsaveActionLink').css('display','block');
		$.get(url, function(data){
			loadTags();
		});
	}

	function unSaveFilter(filter){
		//alert("came here");
		url = "/unsavetag?group=" + currGroup + "&msgtype=" + escape(filter);
		$('#SaveUnsavePreloadDiv').attr('class','loadingSaveUnsave');
		$('#UnsaveActionLink').css('display','none');
		$('#SaveActionLink').css('display','block');
		$.get(url, function(data){
			loadTags();	
		});
	}

	function deleteMessage(msgID)
	{
		//alert(msgID)
		if (confirm("Are You sure? This action cannot be undone") == true)
		{
			$('.' + msgID).slideUp();
			url = 'deletemessage?msgid=' + msgID + '&group='+currGroup;
			$('.' + msgID).load(url,'',function()
			{
				//loadMessages();
				//loadTags();
				loadMotherWrapper();
			});
		}
	}
	function addrss()
	{
		urlToAdd = $("#rssurlbox").val();
		alert("hello" + urlToAdd);
		alert(currGroup);
		$.post("addrss", 
			{ rssurl: urlToAdd,group:currGroup },
			function(data){
				$("#rssurlbox").val('');
						}
			, "text");
	}

	function replyToMessage(msgID)
	{	
		dontReload = true;
		$('#' + msgID + "Reply").css('display','block');
		$('#'+msgID+'Txt').focus();
		return false
	}

	function cancelReplyToMessage(msgID)
	{	//alert(msgID)
		dontReload = false;
		$('#' + msgID).slideUp();
	}
	
	function postReplyMessage(parentMsgKey,msgID,groupID){
		dontReload = false;
		//alert(groupID);
		message = $("#"+msgID+"Txt").val();
		$("#"+msgID+"Txt").val('');
		$('#' + msgID+"Reply").slideUp();
		$.post("write", 
			{ content: message,parentMessageKey:parentMsgKey,group:groupID },
			function(data){
				//loadMessages();
				//loadTags();
				loadMotherWrapper();
						}
			, "text");
	}

	function chooseGroup(group){
		//alert(group);
		numMsg = 10;
		dontReload = false;
		$('#group_'+group).attr('class','loadingGroups');
		currentFilter = '';
		currentFilterID = 'tag_';
		currGroup =  group;
		//loadMessages();
		//loadGroups();
		//loadTags();
		loadMotherWrapper();
		//$('#postingBox').css('display','block');
		$("#messagearea").focus();
	}

	function chooseFilter(filterName,filterValue){
		//alert(group);
		$('#'+filterName).attr('class','loadingTags');
		currentFilter =  filterValue;
		currentFilterID = filterName;
		//loadMessages();
		//loadTags();
		loadMotherWrapper();
		$("#messagearea").focus();
	}

	function showInvisibleMessages(id){
		$('.invisibleMessage_'+id).toggle();
		if($('.expandInvisbleMessage_'+id).html()=='&nbsp;&nbsp;Click to see more comments'){
			$('.expandInvisbleMessage_'+id).html('&nbsp;&nbsp;Collapse to hide some comments')
		}
		else{
			$('.expandInvisbleMessage_'+id).html('&nbsp;&nbsp;Click to see more comments')
		}
	}	

	function getNewMessageCount(){
		url = "/newmessagecount?json=True";
		$.getJSON(url, function(json){
			newMessageCounts = json.newMessageCounts;
			for (i=0; i < newMessageCounts.length; i++){
				if(newMessageCounts[i].id == currGroup){
					$("#newMessageNotification").text("Refresh to see new messages");
					$("#newMessageNotificationDiv").slideDown();
				}
				else {
					$("#grouplink_" + newMessageCounts[i].id).text(newMessageCounts[i].grpName + "*");
				}
			}

		});
	}

	function msgNotificationClick(){
		currentFilter = "";
		currentFilterID="tag_"
		//loadMessages();
		//loadGroups();
		//loadTags();
		loadMotherWrapper();
		$("#newMessageNotificationDiv").slideUp();
		//ideally reset counter #Todo
	}

	function postNewMessage(parentMsgKey)
	{	

		$('#post-button-preload-div').css('display','block');
		writeGroup = currGroup;
		dontReload = false;
		if (currGroup == 0) {
			alert("Please choose a group to post in");
			return
		}
		txtMsg = $("#messagearea").val();
		lastMsgValue = txtMsg;
		$("#messagearea").val('');
		$.ajax({
			type: "POST",
			url: "write",
			cache: false,
			data: ({ content: txtMsg,parentMessageKey:parentMsgKey,group:writeGroup }),
			success: function(msg){
				//loadMessages();
				//loadTags();
				loadMotherWrapper();
				$('#post-button-preload-div').css('display','none');
				//$("#messagearea").focus();
			},
			error: function(msg){
				$('#post-button-preload-div').css('display','none');
				$("#messagearea").val(lastMsgValue);
				$('#notificationArea').slideDown();
				$('#notificationArea').html('Scribble Failed');
				$("#messagearea").focus();
				setTimeout("$('#notificationArea').slideUp();", 5000);
				$("#messagearea").focus();
			}
		});
	}

