var all_desired_scope = "";
var fbStatus = "other";  
var allowFBReload = true;

var notificationsVisible=false;
var hideNotificationsCount = 0;

var inviteModalPanelOpenCount = 0;
var shortMessageCount = 0;

var notificationsBelow = "";

var _reloadNotificationsPanel=false;

function pdfblogout(){
	
}

function hideFollowIcon(userID){
	var userClass='.followUser' + userID;
	jQuery(userClass).css("display", "none");
	
	increaseFollowing();
	
	return true;
}

function increaseFollowing(){
	var followingLink = document.getElementById("followingLink");
	followingLink.innerHTML = parseInt(followingLink.innerHTML) + 1;	
}

function decreaseFollowing(){
	var followingLink = document.getElementById("followingLink");
	followingLink.innerHTML = parseInt(followingLink.innerHTML) - 1;	
}

function markFollowing(userID, label){
	var userClass='.followUser' + userID;
	//$$(userClass).each(function (elmt){elmt.setStyle({display: 'none'})});
	$$(userClass).each(function (elmt){elmt.update(label)});
	return true;
}

function getJSFElementById(id){
	formGetter = document.getElementById("formGetter");
	formID = formGetter.form.id;
	var jsfElement = document.getElementById(formID + ":" + id);
	return jsfElement;
}

function getJSFElementByIdAndFormGetterId(id, formGetterId){
	formGetter = document.getElementById(formGetterId);
	formID = formGetter.form.id;
	var jsfElementId = formID + ":" + id;
	var jsfElement = document.getElementById(jsfElementId);
	return jsfElement;
}

function getJSFFormByElementById(fortmGetterID){
	var formGetter = document.getElementById(fortmGetterID);
	formID = formGetter.form.id;
	form = document.getElementById(formID);
	return form;
}

function followCategory(categoryID){
	var followPanel = document.getElementById("followPanel" + categoryID);
	var unfollowPanel = document.getElementById("unfollowPanel" + categoryID);
	var categoryName = document.getElementById("categoryName" + categoryID);
	followPanel.className = "settings-followcategorypanel-invisible";
	unfollowPanel.className = "settings-followcategorypanel-visible";
	categoryName.className="settings-category-name-followed";
}

function unfollowCategory(categoryID){
	var followPanel = document.getElementById("followPanel" + categoryID);
	var unfollowPanel = document.getElementById("unfollowPanel" + categoryID);
	var categoryName = document.getElementById("categoryName" + categoryID);
	followPanel.className = "settings-followcategorypanel-visible";
	unfollowPanel.className = "settings-followcategorypanel-invisible";
	categoryName.className="settings-category-name-not-followed";
}

function getUserPointVoteTooltip(positiveUserPointVotes, negativeUserPointVotes, positivePointVotesSize, negativePointVotesSize){
	var rating = positivePointVotesSize - negativePointVotesSize;
	if (positivePointVotesSize + negativePointVotesSize == 1){
		var POINT_votesWording = POINT_vote;
	} else {
		var POINT_votesWording = POINT_votes;
	} 
		
	
	var tooltip = POINT_pointRatingIs + " <strong>" + rating + " " + POINT_outOf + " " 
			+ (positivePointVotesSize + negativePointVotesSize) + "</strong> " + POINT_votesWording + "<br/>";
	
	if (positiveUserPointVotes.length > 0 || negativeUserPointVotes.length > 0){
		if (positivePointVotesSize == 1){
			var POINT_positive = POINT_positiveSingular;
			var POINT_voteWording = POINT_vote;
		} else {
			var POINT_positive = POINT_positivePlural;
			var POINT_voteWording = POINT_votes;
		}
		
		tooltip += "<strong>" + positivePointVotesSize + " " + POINT_positive + "</strong> " + POINT_voteWording;
		
		
		if (positiveUserPointVotes.length > 0){
			tooltip += " " + POINT_by +  " ";
			for (var i=0; i < positiveUserPointVotes.length; i++){
				tooltip += "@" + positiveUserPointVotes[i];
				if (i < positiveUserPointVotes.length - 1){
					if (i == positiveUserPointVotes.length - 2 && positivePointVotesSize <= positiveUserPointVotes.length){
						tooltip += " " + POINT_and + " ";
					} else {
						tooltip += ", ";
					}
				}
			}
			if (positivePointVotesSize > positiveUserPointVotes.length){
				var difference = positivePointVotesSize - positiveUserPointVotes.length;
				tooltip += " " + POINT_and + " " + (difference);
				if (difference == 1){
					tooltip += " " + POINT_other;
				} else {
					tooltip += " " + POINT_others;
				}
			}
		}
		tooltip += "<br/>";
		
		if (negativePointVotesSize == 1){
			var POINT_negative = POINT_negativeSingular;
			var POINT_voteWording = POINT_vote;
		} else {
			var POINT_negative = POINT_negativePlural;
			var POINT_voteWording = POINT_votes;
		}
		
		tooltip += "<strong>" + negativePointVotesSize + " " + POINT_negative + " </strong> " + POINT_voteWording;
		if (negativeUserPointVotes.length > 0){
			tooltip += " " + POINT_by +  " ";
			for (var i=0; i < negativeUserPointVotes.length; i++){
				tooltip += "@" + negativeUserPointVotes[i];
				if (i < negativeUserPointVotes.length - 1){
					if (i == negativeUserPointVotes.length - 2 && negativePointVotesSize <= negativeUserPointVotes.length){
						tooltip += " " + POINT_and + " ";
					} else {
						tooltip += ", ";
					}
				}
			}
			if (negativePointVotesSize > negativeUserPointVotes.length){
				var difference = negativePointVotesSize - negativeUserPointVotes.length;
				tooltip += " " + POINT_and + " " + (difference);
				if (difference == 1){
					tooltip += " " + POINT_other;
				} else {
					tooltip += " " + POINT_others;
				}
			}
		}
	}
	
	
	return tooltip;
}

//cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=.pointdebate.net";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function followCurrentUser(){
	var followCurrentUser = document.getElementById("mainForm:followCurrentUser");
	var unfollowCurrentUser = document.getElementById("mainForm:unfollowCurrentUser");
	followCurrentUser.style.display = "none";
	unfollowCurrentUser.style.display = "block";
	
	increaseFollowing();
}

function unfollowCurrentUser(){
	var followCurrentUser = document.getElementById("mainForm:followCurrentUser");
	var unfollowCurrentUser = document.getElementById("mainForm:unfollowCurrentUser");
	unfollowCurrentUser.style.display = "none";
	followCurrentUser.style.display = "block";
	
	decreaseFollowing();
}

function followDebate(){
	var followDebate = document.getElementById("mainForm:followDebate");
	var unfollowDebate = document.getElementById("mainForm:unfollowDebate");
	followDebate.style.display = "none";
	unfollowDebate.style.display = "block";
	
}

function unfollowDebate(){
	var followDebate = document.getElementById("mainForm:followDebate");
	var unfollowDebate = document.getElementById("mainForm:unfollowDebate");
	unfollowDebate.style.display = "none";
	followDebate.style.display = "block";
}

function deletePoint(orderInDebate){
	var pointDiv = document.getElementById("p" + orderInDebate);
	pointDiv.style.display = "none";
}

function showNotificationPanel(below){
	if (notificationsVisible == true && _reloadNotificationsPanel == false){
		hideNotificationPanel();
	} else {
	
		//jQuery("#unread-notifications-count-span").text("");
		jQuery("#unread-notifications-count-span").html("&nbsp;");
		jQuery("#unread-notifications-count").removeClass("atleastone-notification").addClass("zero-notifications");
		
		if (notificationsBelow == null || notificationsBelow == ""){
			var notificationsURL = "/siteNotifications.jsf";
		} else {
			var notificationsURL = "/siteNotifications.jsf?currentBelow=" + notificationsBelow;
		}
		var notificationPanel = jQuery("#notification-panel");
		notificationPanel.addClass("notification-panel-visible");
		notificationPanel.load(notificationsURL);
		notificationsVisible=true;
		hideNotificationsCount = 2;
		_reloadNotificationsPanel=false;
	}
	//set notifications to 0
	
	
}

function reloadNotificationsPanel(below){
//	var notificationsURL = "/siteNotifications.jsf?currentBelow=" + below;
//	var notificationPanel = jQuery("#notification-panel");
//	alert(notificationPanel);
//	alert(notificationsURL);
//	notificationPanel.hide().load(notificationsURL).show();
//	alert("after reload");
	notificationsBelow = below;
	_reloadNotificationsPanel = true;
	jQuery("#notification-panel").text("Loading...");
	
}

function hideNotificationPanel(){
	if (notificationsVisible == true){
		hideNotificationsCount = hideNotificationsCount - 1;
		if (hideNotificationsCount == 0){
			var notificationPanel = jQuery("#notification-panel");
			notificationPanel.removeClass("notification-panel-visible");
			notificationsVisible=false;
		}
	}
}

function logoutFBUser(){
	var localStatus; 
		
    FB.getLoginStatus(function(response) {
  		localStatus = response.status;
  		//alert(localStatus);
  		if (localStatus=="undefined"){
  			FB.getLoginStatus(function(response) {
  		  		localStatus = response.status;
  		  		//alert("second time:" + localStatus); //ask two times because fb doesn't get it correct the first time
  			});
  		}
	});
    
    
    
    if (localStatus=="connected"){
    	FB.logout(function(response) {mojarra.jsfcljs(document.getElementById('logInOutForm'),{'logInOutForm:sitelogout':'logInOutForm:sitelogout'},'');});
    } else {
    	mojarra.jsfcljs(document.getElementById('logInOutForm'),{'logInOutForm:sitelogout':'logInOutForm:sitelogout'},'');
    }
}




function openInviteModalDialog(debateID, jsfPage, title){
	var url = "/" + jsfPage + "?debateID=" + debateID + "&inc=" + inviteModalPanelOpenCount;
	var inviteDebateDivID = "inviteDiv" + inviteModalPanelOpenCount;
	
	jQuery("body").append("<div id='" + inviteDebateDivID +"'>Loading...</div>");
	var inviteDebateDiv = jQuery("#" + inviteDebateDivID);

	inviteDebateDiv.load(url).dialog({
		  modal:true,
		  minHeight: 350,
		  minWidth: 800,
		  width: 800,
		  title: title
		});
	
	inviteModalPanelOpenCount++;
}

function showShortMessage(message){
	var inviteDebateOKID = "inviteDebateOK" + shortMessageCount;
	jQuery("body").append("<div id='" + inviteDebateOKID +"'>" + message + "</div>");
	jQuery("#" + inviteDebateOKID).dialog({
		hieght: 60,
		width: 300,
		buttons: { "Ok": function() { $(this).dialog("close"); } }
	});
	setTimeout(function(){jQuery("#" + inviteDebateOKID).dialog("close");}, 1500);
	shortMessageCount++;
}

function sendInvitations(){
	jQuery("#inviteDiv"+ (inviteModalPanelOpenCount - 1)).dialog("close");
	showShortMessage('Invites sent successfully!');
	
}

function userSuggestionChange(checkbox){
	var remainingInvitations = document.getElementById(invitationFormID + ":remainingInvitations");
	var remainingInvitationsValue=parseInt(remainingInvitations.innerHTML);
	
	if (checkbox.checked){
		if(remainingInvitationsValue > 0){
			remainingInvitations.innerHTML = parseInt(remainingInvitations.innerHTML) - 1;
		} else {
			checkbox.checked=false;
		}
	} else {
		remainingInvitations.innerHTML = parseInt(remainingInvitations.innerHTML) + 1;
	}

	if (parseInt(remainingInvitations.innerHTML) <= 0){
		remainingInvitations.className="no-remaining";
	} else {
		remainingInvitations.className="";
	}
	
	
}

function checkFowNewPoints(debateID, lastPoint){
	jQuery.get("/newpoints?debateID=" + debateID + "&lastPoint=" + lastPoint, 
			function(data){
				var newpointscount = parseInt(data);
				if (newpointscount > 0){
						jQuery("#newpointscount").html(newpointscount);
						if(newpointscount == 1){
							jQuery(".newSingular").removeClass("invisible").addClass("inline");
							jQuery(".newPlural").removeClass("inline").addClass("invisible");
						} else {
							jQuery(".newSingular").removeClass("inline").addClass("invisible");
							jQuery(".newPlural").removeClass("invisible").addClass("inline");
						}
						jQuery("#newPointsNotification").removeClass("invisible").addClass("block");
						onNewElementsTimer(newpointscount);
				}
	});
}

function checkFowNewActivities(lastKnownActivityID){
	jQuery.get("/newactivities?lastActivity=" + lastKnownActivityID, 
			function(data){
				var newactivitiescount = parseInt(data);
				if (newactivitiescount > 0){
						jQuery("#newactivitiescount").html(newactivitiescount);
						if(newactivitiescount == 1){
							jQuery(".newSingular").removeClass("invisible").addClass("inline");
							jQuery(".newPlural").removeClass("inline").addClass("invisible");
						} else {
							jQuery(".newSingular").removeClass("inline").addClass("invisible");
							jQuery(".newPlural").removeClass("invisible").addClass("inline");
						}
						jQuery("#newActivitiesNotification").removeClass("invisible").addClass("block");
						onNewElementsTimer(newactivitiescount);
				}
	});
}


function reRenderDebateThumbDivPanel(){
	var refreshButton = document.getElementById('mainForm:debate_thumb_div_panel');
	mojarra.ab(refreshButton,null,'action','@form','mainForm:debate_thumb_div_panel');
	return false;
}

function reRenderAvatarDiv(){
	var curAvatarDiv = document.getElementById("mainForm:curAvatarDiv");
	mojarra.ab(curAvatarDiv,null,'action','@form','mainForm:curAvatarDiv');
	return false;
}

function removeComponent(componentID){
	jQuery("#"+componentID).remove();
}

function markReported(pointID){
	
}


function onNewElementsTimer(newElementsCount){
	//alert("onNewElementsTimer=" + newPointsCount);
	if (newElementsCount > 0){
		//var oldTitle = document.title;
		//var newTitle = "(" + newPointsCount + ") " + originalDebateTitle + pageTitleSuffix;
		var newTitle = "(" + newElementsCount + ") " + originalDocumentTitle;
		document.title = newTitle;
	}
}

