if( !communityModule ) var communityModule = {
	
	userInfo : function(userName, regDate, ratingCount,reviewsCount,movieCircleCount,followersCount,powerScale,isFollower,isFollowed){
	
				this.userName 		= userName;
				this.regDate		= regDate;
				this.numRating		= ratingCount;
				this.numReviews		= reviewsCount;
				this.numMovieCircle	= movieCircleCount;
				this.numFollowers	= followersCount;
				this.powerScale		= powerScale;
				this.similarity		= null;
				this.isFollower		= isFollower;
				this.isFollowed		= isFollowed;
				},
	usersInfoHash		: new utils.Hash(),
	usersInfoArray 		: [],
	matchOmatDictionary : ["Can't tell yet", "Polar opposites","Culture clash","Unlikely pair","Opposites attract","Room for debate","Common ground","There's a spark","Lots to discuss","In harmony","Long lost twins"],
	rolloverUserId		: 0,
	pageWatcherId		: -1,
	rolloverInterval	: null,
	pulseArray			: new Array(),
	pendingPuliseArray	: new Array(),
	pulseInterval		: null,
	fatchPulseTimeout	: 15000,
	pulseIntervalHendler: null
	
	
};


communityModule.showToolTip = function( message )
{
	
}

communityModule.updateMovieCircle = function (follower, followee, feedbackRefObject, context)
{
	if (generalFunctions.goToPrivatePage() == false ) return;
	
	
	var actionClicked = feedbackRefObject.id;
	var alternateAction = "";
	var addAction = true;
	if( feedbackRefObject.id.endsWith("remove") )
	{
		addAction = false;
		var addButton = feedbackRefObject.id.substring(0,feedbackRefObject.id.lastIndexOf("remove")) + "add";
		alternateAction = document.getElementById(addButton);
	}
	else if( feedbackRefObject.id.endsWith("add") )
	{
		addAction = true;
		var removeButton = feedbackRefObject.id.substring(0,feedbackRefObject.id.lastIndexOf("add")) + "remove";
		alternateAction = document.getElementById(removeButton);
	}
	
	
	if( followee == '' ) followee = communityModule.rolloverUserId;
	
	communityAjax.updateMovieCircle(follower, followee, addAction, function( isFollower ){
	
		if( context == "publicCommunityContainer" )
		{
			feedbackRefObject.style.display = "none";
			alternateAction.style.display = "inline-block";
			
			
			if( communityModule.usersInfoHash.get("user"+followee) != null )
				communityModule.usersInfoHash.get("user"+followee).isFollowed = addAction;
//			if( communityModule.usersInfoArray[followee] )
//				communityModule.usersInfoArray[followee].isFollowed = addAction;
		}
		else if( context == "noFollowerNotes")
		{
			var hash = document.location.href.indexOf("#") > -1 ? "" :"#followers";
			window.location.reload(true);
		}
		else if( context == "privateCommunityContainer" )
		{
			if( document.location.pathname.indexOf("neighbors") > -1 || document.location.pathname.indexOf("movie-circle") > -1 )
			{
				communityModule.userContainerRollout( null, null);
				communityModule.usersInfoHash.get("user"+followee).isFollowed = addAction;
				communityModule.usersInfoHash.remove("user" + followee);
				//communityModule.usersInfoArray.splice( followee, 1 );
				communityModule.removeUserCommunityContainer( 'communityUserContainer-' + followee );
			}
			else if( document.location.pathname.indexOf("followers") > -1 )
			{
				feedbackRefObject.style.display = "none";
				alternateAction.style.display = "inline-block";
				communityModule.usersInfoHash.get("user"+followee).isFollowed = addAction;
				
				var currentArrow = "";
				if( isFollower && addAction ) 	currentArrow = "relationshipArrow mutualArrow"
				if( isFollower && !addAction )	currentArrow = "relationshipArrow followerArrow"
				if( !isFollower && addAction )	currentArrow = "relationshipArrow followedArrow"
				if( !isFollower && !addAction )	currentArrow = "relationshipArrow neighborArrow"
			
				document.getElementById('relationShipArrows-communityUserContainer-' + followee).className = currentArrow;
				document.getElementById('rolloverRelationshipArrows').className = currentArrow;
				
			}
		}
		else if ( context == "publicRightSide" )
		{
			feedbackRefObject.style.display = "none";
			alternateAction.style.display = "inline-block";
			
			var currentArrow = "";
			if( isFollower && addAction ) 	currentArrow = "relationshipArrow mutualArrow"
			if( isFollower && !addAction )	currentArrow = "relationshipArrow followerArrow"
			if( !isFollower && addAction )	currentArrow = "relationshipArrow followedArrow"
			if( !isFollower && !addAction )	currentArrow = "relationshipArrow neighborArrow"
			
			document.getElementById('relationShipArrows').className = currentArrow;
			
		
		}
		else if( context == "teaser")
		{
			var alternateActionSuffix = alternateAction.id.split("-")[2];
			var clickedOnActionSuffix = feedbackRefObject.id.split("-")[2];
			var teaserElements = "";
			
			teaserElements = document.getElementsByTagName("div");
			if(!teaserElements || teaserElements.length == 0) 
				teaserElements = document.getElementsByTagName("DIV");
			
			for(var index=0; index<teaserElements.length; index++) {
				
				if(teaserElements[index].id && teaserElements[index].id.endsWith( alternateActionSuffix )){
					
					teaserElements[index].style.display = "inline-block";
				}
				if( teaserElements[index].id && teaserElements[index].id.endsWith( clickedOnActionSuffix ) ){
					
					teaserElements[index].style.display = "none";
				}
				
			}
		}
		
		
	});
}

communityModule.removeUserCommunityContainer = function( containerId )
{
	document.getElementById( containerId ).style.display = 'none';
	
	if( communityModule.usersInfoHash.length == 0 )
	{
		document.location.href = document.location.href;
	}
	
}


communityModule.userContainerRollover = function ( containerId )
{
	
	communityModule.rolloverInterval = window.setTimeout(function(){
	
		var userId = parseInt( containerId.split("-")[1] );
		communityModule.rolloverUserId = userId;
		var userObject = communityModule.usersInfoHash.get("user"+userId);
		//var userObject = communityModule.usersInfoArray[ userId ];
		
		var regDate = new Date();
		regDate.setTime( userObject.regDate );
		
		document.getElementById("rolloverUserName").innerHTML = userObject.userName;
		document.getElementById("rolloverPowerScale").className = "powerScale scale" + userObject.powerScale;
		document.getElementById("rolloverDate").innerHTML = regDate.getDate() + "/" + (regDate.getMonth() + 1) + "/" + (regDate.getFullYear() + "").substr(2);
		document.getElementById("rolloverRateReview").innerHTML = userObject.numRating + " Ratings | " + userObject.numReviews + " Reviews";
		document.getElementById("rolloverCircleFollowers").innerHTML = userObject.numMovieCircle + " Following | " + userObject.numFollowers + " Followers";
		document.getElementById("matchometerrolloverMask").style.width = userObject.similarity;
		document.getElementById('rolloverRelationshipArrows').className = document.getElementById("relationShipArrows-" + containerId).className;
		
		if( userObject.isFollowed )
		{
			document.getElementById('rolloverCommunityAddRemoveActionadd').style.display = 'none';
			document.getElementById('rolloverCommunityAddRemoveActionremove').style.display = 'inline-block';
		}
		else
		{
			document.getElementById('rolloverCommunityAddRemoveActionadd').style.display = 'inline-block';
			document.getElementById('rolloverCommunityAddRemoveActionremove').style.display = 'none';
		}
		
		if( userId == communityModule.pageWatcherId )
		{
			document.getElementById('rolloverCommunityAddRemoveActionadd').style.display = 'none';
			document.getElementById('rolloverCommunityAddRemoveActionremove').style.display = 'none';
			document.getElementById('rolloverMatchometer').style.visibility = 'hidden';
		}
		else
		{
			var follower = -1;
			if (communityModule.pageWatcherId == -1 && document.pageWatcherId)
				follower = document.pageWatcherId;
			else
				follower = communityModule.pageWatcherId;
			if( communityModule.usersInfoHash.get("user"+userId) != null && communityModule.usersInfoHash.get("user"+userId).similarity == null)
				communityModule.setSimilarity(follower , userId);
			
			var maskWidth = "0%";
			if( (communityModule.usersInfoHash.get("user"+userId).similarity) >=0 )
				maskWidth = ((communityModule.usersInfoHash.get("user"+userId).similarity)*100) + "%";
				
			document.getElementById('matchometerrolloverMask').style.width = maskWidth;
			document.getElementById('matchometerrolloverTextualDescription').innerHTML = communityModule.getMatchTextualRate( communityModule.usersInfoHash.get("user"+userId).similarity );
			document.getElementById('rolloverMatchometer').style.visibility = 'visible';
			
			
		}
		
		var containerChilds = document.getElementById(containerId).childNodes;
		for(var index=0; index<containerChilds.length; index++) {
			
			if(containerChilds[index] && containerChilds[index].tagName && containerChilds[index].tagName.toLowerCase() == 'img')
			{
				document.getElementById('communityRolloverImg').src = containerChilds[index].src;
				break;
			}
			
		} 
		
		
		document.getElementById('communityRollover').style.left = getPosX(document.getElementById(containerId)) + "px";
		document.getElementById('communityRollover').style.top = getPosY(document.getElementById(containerId)) + "px";
		document.getElementById('communityRollover').style.display = 'block';
		var rolloverTweenIn = new OpacityTween(document.getElementById('communityRollover'), Tween.regularEaseIn, 0, 100, 0.5);
		rolloverTweenIn.start();
		
		
	},300);
	
}

communityModule.userContainerRollout = function( containerObj, e)
{
	var goingToObj = null;
	
	
	//containerObj=null and e=null : forced hide
	
	if( e ){
		if( e.relatedTarget) goingToObj = e.relatedTarget;
		else goingToObj = e.toElement;
	}
	if(( e == null && containerObj == null ) || !isChildOf( goingToObj, document.getElementById('communityRollover')) )
	{
		document.getElementById('communityRollover').className = "transparent communityRollover"; 
		document.getElementById('communityRollover').style.display = 'none';
		

		window.clearTimeout( communityModule.rolloverInterval);
	}
}
communityModule.getMatchTextualRate = function( match )
{
	var text = communityModule.matchOmatDictionary[0];
	
	if( match < 0 )
		text = communityModule.matchOmatDictionary[0];
	else if( match <= 0.09 )
		text = communityModule.matchOmatDictionary[1];
	else if( match <= 0.19 )
		text = communityModule.matchOmatDictionary[2];
	else if( match <= 0.29)
		text = communityModule.matchOmatDictionary[3];
	else if( match <= 0.39)
		text = communityModule.matchOmatDictionary[4];
	else if( match <= 0.49)
		text = communityModule.matchOmatDictionary[5];
	else if( match <= 0.59)
		text = communityModule.matchOmatDictionary[6];
	else if( match <= 0.69)
		text = communityModule.matchOmatDictionary[7];
	else if( match <= 0.79)
		text = communityModule.matchOmatDictionary[8];
	else if( match <= 0.89)
		text = communityModule.matchOmatDictionary[9];										
	else if( match <= 1)
		text = communityModule.matchOmatDictionary[10];

	return text;

}
communityModule.setSimilarity = function( follower, followee )
{
	var similarity = 0;
	
	communityAjax.getSimiliarity(follower, followee,{
		
		async	: false,
		callback: function(match){
			
			similarity=match;
			if(similarity == null || similarity == undefined)
				similarity = -1;
			}
			
		});
	
	communityModule.usersInfoHash.get("user"+followee).similarity = similarity;
	//communityModule.usersInfoArray[followee].similarity = similarity;
}
communityModule.updateSettingsButtons = function( radio )
{
	if( radio.value == 'hide')
	{
		document.getElementsByName("profileForm:searchesSettings")[0].disabled = true;
		document.getElementsByName("profileForm:searchesSettings")[1].disabled = true;
		document.getElementsByName("profileForm:wishListSettings")[0].disabled = true;
		document.getElementsByName("profileForm:wishListSettings")[1].disabled = true;	
		document.getElementsByName("profileForm:favoriteSettings")[0].disabled = true;
		document.getElementsByName("profileForm:favoriteSettings")[1].disabled = true;
	}
	else
	{
		document.getElementsByName("profileForm:searchesSettings")[0].disabled = false;
		document.getElementsByName("profileForm:searchesSettings")[1].disabled = false;
		document.getElementsByName("profileForm:wishListSettings")[0].disabled = false;
		document.getElementsByName("profileForm:wishListSettings")[1].disabled = false;
		document.getElementsByName("profileForm:favoriteSettings")[0].disabled = false;
		document.getElementsByName("profileForm:favoriteSettings")[1].disabled = false;
	}
	
}
communityModule.goToPublicPage = function()
{
	var userName = document.getElementById('rolloverUserName').innerHTML;
	document.location.href = "/user/" + userName + "/public/";
}
communityModule.toggleRecentRecommendations = function()
{
	var extensionList = document.getElementById('recentRecExtension');
	var link = document.getElementById('recToggleLink');
	if( extensionList.className.indexOf("invisible") > -1 )
	{
			extensionList.className = extensionList.className.replace(/invisible/,"visible");
			link.innerHTML = "Less";
	}
	else
	{
			 extensionList.className = extensionList.className.replace(/visible/,"invisible");
			 link.innerHTML = "More";
	} 
	
}
communityModule.toggleRecentActvities = function()
{
	var extensionList = document.getElementById('recentActExtension');
	var link = document.getElementById('actToggleLink');
	if( extensionList.className.indexOf("invisible") > -1 )
	{
			extensionList.className = extensionList.className.replace(/invisible/,"visible");
			link.innerHTML = "Less";
	}
	else
	{
			 extensionList.className = extensionList.className.replace(/visible/,"invisible");
			 link.innerHTML = "More";
	} 
	
}
communityModule.toggleTopFavorites = function()
{
	var extensionList = document.getElementById('recentFavExtension');
	var link = document.getElementById('favToggleLink');
	if( extensionList.className.indexOf("invisible") > -1 )
	{
			extensionList.className = extensionList.className.replace(/invisible/,"visible");
			link.innerHTML = "Less";
	}
	else
	{
			 extensionList.className = extensionList.className.replace(/visible/,"invisible");
			 link.innerHTML = "More";
	} 
}
communityModule.exposeCloud = function()
{
	document.getElementById('cloudHideLink').className = "cloudToggleLink";
	document.getElementById('userCloudExtension').className = "";
	document.getElementById('cloudExposeLink').className = "invisible cloudToggleLink";
	
}
communityModule.hideCloud = function()
{
	document.getElementById('cloudHideLink').className = "invisible cloudToggleLink";
	document.getElementById('userCloudExtension').className = "invisible";
	document.getElementById('cloudExposeLink').className = "cloudToggleLink";
}
communityModule.getPulse = function()
{
	var feedsContext = ""
	if( location.href.indexOf("pulse.html") > -1 ) feedsContext = "all_feeds";
	else 									feedsContext = "search_feeds";
	
	
	ajaxCommunityPulse.getFeeds(feedsContext, {
		
		errorHandler: function(error){
			
			var feedError = new utils.AjaxError( document.location.href, error, "ajaxCommunityPulse")
			
			ajaxErrorReporter.logError(feedError.ajaxError, {errorHandler: function(){}});
			
			window.clearInterval(communityModule.pulseIntervalHendler);
			window.clearTimeout(communityModule.fatchPulseTimeout);
			window.setTimeout(communityModule.getPulse, communityModule.fatchPulseTimeout);
			
		},
		async:false,
		callback: function( data ){
		
				var feedsCount = 0;
				xml = utils.getXMLDocObject( data );
				communityModule.pendingPuliseArray = new Array();
				
				data = null;
				
				if( xml != null)
				{
					feedsCount = xml.getElementsByTagName("div").length;
					if( feedsCount == 0)
					{
						window.setTimeout(communityModule.getPulse, communityModule.fatchPulseTimeout);
						return;
					}
					
					
					var element = xml.getElementsByTagName("div")[0];
					
					while( element  )
					{
						if( element.tagName == "div")
						{
							communityModule.pendingPuliseArray.push( element );
						}
						element = element.nextSibling;
					}
					xml = null;
					/* show the first one immediately */
					
					if(communityModule.pendingPuliseArray.length > 0)
						communityModule.pulseHandler();
					
					communityModule.pulseIntervalHendler =  window.setInterval(communityModule.pulseHandler,communityModule.pulseInterval)
					
					
					
				}
			}
	
		})

}


communityModule.pulseHandler = function()
{
	var feed = null;
	
	if( communityModule.pendingPuliseArray.length == 0 ) 
	{
			window.clearInterval(communityModule.pulseIntervalHendler);
			communityModule.getPulse();
			
	}
	else
	{
		feed = 	communityModule.pendingPuliseArray[0];
		
		var feedAsString = "";
		if( document.all )
			feedAsString = feed.xml;
		else
			feedAsString = (new XMLSerializer()).serializeToString(feed);
			
		var newDiv = feed;
		
	
		//alert('new div: ' + feed);
		communityModule.pendingPuliseArray.splice(0,1);
		/* add the new pulse to the pulse array */
		
		var tempArray = new Array();
	//	alert( "tempArray b4 push: " + printArray(tempArray ))
	//	alert( "pulseArray b4 push: " + printArray(communityModule.pulseArray ))
		//tempArray.push( feed );
		
	//	alert( "tempArray after push: " + printArray(tempArray ))
		//tempArray = tempArray.concat( communityModule.pulseArray);
		//	tempArray = communityModule.concatArrays(tempArray, communityModule.pulseArray);
	//	alert( "tempArray after concat: " + printArray(tempArray ))
		//communityModule.pulseArray = tempArray;
		communityModule.pulseArray = communityModule.addFeedToArray(newDiv,communityModule.pulseArray )
	//	alert( "pulseArray after concat: " + printArray(communityModule.pulseArray ))
		tempArray = null;
		
		
		
		/*****************************************/
		
		var feedsContainer = document.getElementById('pulse');


		var firstChild = feedsContainer.getElementsByTagName("div")[0];
		
		
		
		
		if(document.all)
		{
		//	alert('************* ' + feed.getAttribute("class"))
			var div = document.createElement("div");
			div.setAttribute("className",feed.getAttribute("class"));
			div.innerHTML = feedAsString;
			/* remove the wrapper */
			var wrapper = div.getElementsByTagName("div")[0];
			var divWeNeed = div.getElementsByTagName("div")[1];
			div.removeChild(wrapper);
			div.appendChild(divWeNeed);
			/**********************/
			div.setAttribute("style","visibility:hidden;position:absolute;");
			document.body.appendChild(div);
			var newDivHeight = div.clientHeight;
			document.body.removeChild(div);
			div.setAttribute("style","visibility:visible;position:static; height:0;");
			newDiv = div;
			div = null;
		}
		else
		{
			newDiv.style.visibility = "hidden";
			newDiv.style.position = "absolute"
			document.body.appendChild(newDiv);
			var newDivHeight = newDiv.clientHeight;
			document.body.removeChild(newDiv);
			newDiv.style.visibility = "visible";
			newDiv.style.position = "static"
			newDiv.style.height = 0;
		}
		
		
		
		 var currentlyFirstFeed = communityModule.pulseArray.length > 1 ? communityModule.pulseArray[1] : undefined;
		 
	
		 if( currentlyFirstFeed )
		 {
		 	var firstFeedClass =  currentlyFirstFeed.getAttribute("class");
		 	if( !firstFeedClass) firstFeedClass =  currentlyFirstFeed.className;
		 		
		 	
		 	var evenStrip = firstFeedClass.indexOf("evenStrip") > -1 ? true : false;
		 	
		 	//alert( "first in list evenStrip? " + evenStrip + " new feed even? " + (newDiv.className.indexOf("evenStrip") > -1));
		 	
		 	
		 	if( evenStrip && newDiv.className.indexOf("evenStrip") > -1 )
		 	{
		 		//newDiv.setAttribute("className",firstFeedClass.replace(/evenStrip/,'oddStrip'));
		 		
		 		newDiv.setAttribute("class",newDiv.className.replace(/evenStrip/,'oddStrip'));
		 		newDiv.className = newDiv.className.replace(/evenStrip/,'oddStrip');
		 	}
		 	if( !evenStrip && newDiv.className.indexOf("oddStrip") > -1 )
		 	{
		 		//newDiv.setAttribute("className",firstFeedClass.replace(/oddStrip/,'evenStrip'));
		 		newDiv.setAttribute("class",newDiv.className.replace(/oddStrip/,'evenStrip'));
		 		newDiv.className = newDiv.className.replace(/oddStrip/,'evenStrip');
		 	}	 	
		 }
		
		
		
		if( firstChild)
			firstChild.parentNode.insertBefore(newDiv,firstChild);
		else
			feedsContainer.appendChild(newDiv);
			
		
		
	
		
		
		
		var addFeedtween = new Tween(newDiv.style, 'height', Tween.regularEaseIn, 0, newDivHeight, 1,'px');
		try{
			addFeedtween.start();
		}
		catch(e) {
		//	alert('error when adding feed ' + e)
			
		}
		if(communityModule.pulseArray.length > 30 )
		{
			//var lastElement = communityModule.pulseArray.pop();
			var lastElement = communityModule.findLastFeed();
				//alert('new last element ' + lastElement.tagName);
			
		//	alert( "pulseArray when removing: " + printArray(communityModule.pulseArray ))
			
			//alert(lastElement == newDiv)
			if( /*lastElement != newDiv*/ true )
			{
				var removeFeedtween = new Tween(lastElement.style, 'height', Tween.regularEaseIn, lastElement.clientHeight, 0, 1,'px');
				
				removeFeedtween.onMotionFinished = function(){
					try{
						
						lastElement.parentNode.removeChild(lastElement);
					}catch(e){
						
						alert('error when removing feed ' + e.description)
					}
				}
				try{
				removeFeedtween.start();
				}catch(e){
					
					alert('error when motion tween removes feed ' + e.description)
					alert('last element ' + lastElement + ', parent node ' + lastElement.parentNode)
				}
			}
		}
		   
	}
}
communityModule.initPulseArray = function()
{
	var childs = document.getElementById('pulse').getElementsByTagName("div")
	var child = childs[0];
	
	
	
	while ( child )
	{
		if(child && child.className && child.className.indexOf("feedsBlock") > -1)
		{
			communityModule.pulseArray.push( child );
		}
		
		child = child.nextSibling;
	}
}
communityModule.concatArrays = function( arr1, arr2)
{
	
	for(var index=0; index< arr2.length; index++) {
			arr1.push(arr2[index])
	}
	
	return arr1;
}
communityModule.addFeedToArray = function( feed, arr)
{
	//alert(" in function. feed: " + feed);
	var tempArr = new Array();
	//tempArr.push(feed);
	tempArr[0] = feed;
	for(var index=0; index< arr.length; index++) {
			tempArr[index+1] = arr[index];
	}
	
	return tempArr;
}

function printArray( arr )
{
	
			var temp = ""
			for(var index=0; index<arr.length; index++) {
				temp+= "    " + index + communityModule.pulseArray[index];
			}
			return temp;
	
}
communityModule.findLastFeed = function()
{
	var element = document.getElementById('pulse').getElementsByTagName("div")[0]
	var lastFeed = element;
	
	while( element  )
	{
		if( element.tagName == "div" || element.tagName == "DIV")
		{
			lastFeed = element;
		}
		element = element.nextSibling;
	}
	return lastFeed;
}

