window.focus();

function disableEnterKey(e)
{
     var key;
     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox
     if(key == 13)
          return false;
     else
          return true;
}

function readMore(moreLink,moreId) 
{
 	if (document.getElementById(moreLink).innerHTML == 'Read more') 
	{
	  document.getElementById(moreLink).innerHTML = 'Read less';
	  document.getElementById(moreId).style.display = 'block';
	} 
	else 
	{
	  document.getElementById(moreLink).innerHTML = 'Read more';
	  document.getElementById(moreId).style.display = 'none';
  }
}

function trimTextArea(areaId,maxlength,charRemain) 
{  
	areaLen = document.getElementById(areaId).value.length;
	
	areaLeft = maxlength - areaLen;
	
	document.getElementById(charRemain).innerHTML = areaLen + ' typed, ' + areaLeft + ' character(s) remaining.';
	
	if (document.getElementById(areaId).value.length > maxlength) 
	{
		document.getElementById(areaId).value = document.getElementById(areaId).value.substring(0,maxlength);  	
	}
}

function doLogin(loginEmail,loginPassword)
{
	$.post("http://future.storyfollower.com/actions/login_submit.php", { loginSubmit: '1', loginEmail: loginEmail, loginPassword: loginPassword },
		 function(data){
				//alert(data);
			 if (data == 'success')
			 {
				$.facebox.close(); 
			 }
			 else 
			 {
				document.getElementById('loginError').innerHTML = data;
			 }
		 });
}							

function loadNav(navPage)
{
	//alert(navPage);
	
	$.get(navPage, { noWay: navPage },
		 function(data){
				$('#navTop').html(data);
		 });
}

function faceboxClose()
{
	$.facebox.close(); 
}

function doFollow(SLId)
{
	//alert(SLId);
	$.post("/actions/follow.php", { SLId: SLId },
		 function(data){
				//alert(data);
			 if (data == 'notloggedin')
			 {
					$.facebox( $('<iframe id="loginIframe" src="https://www.storyfollower.com/login/popup.php" width="275px" height="275px" scrolling="auto" border="0" frameborder="0"></iframe>') )
			 }
			 else if (data == "success")
			 {
				document.getElementById('follow'+SLId).innerHTML = "Following";
			 }
		 });
}

function doViewFollow(SLId)
{
	//alert(SLId);
	$.post("/actions/follow.php", { SLId: SLId },
		 function(data){
				//alert(data);
			 if (data == 'notloggedin')
			 {
				var doLogin = confirm("Please log-in then try again.");
				if (doLogin)
				{
					location.href = 'https://www.storyfollower.com/login/';
				}
			 }
			 else if (data == "success")
			 {
				document.getElementById('follow'+SLId).innerHTML = "Following";
			 }
		 });
}

function doVote(articleId)
{
	$.post("/actions/vote.php", { articleId: articleId },
		 function(data){
				document.getElementById('vote'+articleId).innerHTML = data;
		 });
}

function doFlagArticle(articleId)
{
	$.post("/actions/flagArticle.php", { articleId: articleId },
		 function(data){
				//alert(data);
				
			 if (data == 'notloggedin')
			 {
					$.facebox( $('<iframe id="loginIframe" src="https://www.storyfollower.com/login/popup.php" width="275px" height="275px" scrolling="auto" border="0" frameborder="0"></iframe>') )
			 }
			 else if (data == "success")
			 {
				document.getElementById('flagArticle'+articleId).style.display = "none";
			 }
		 });
}

function doFlagComment(commentId)
{
	$.post("/actions/flagComment.php", { commentId: commentId },
		 function(data){
				//alert(data);
			 if (data == 'notloggedin')
			 {
					$.facebox( $('<iframe id="loginIframe" src="https://www.storyfollower.com/login/popup.php" width="275px" height="275px" scrolling="auto" border="0" frameborder="0"></iframe>') )
			 }
			 else if (data == "success")
			 {
				document.getElementById('flagComment'+commentId).style.display = "none";
			 }
		 });
}

function getComments(articleId)
{
	$.get("/actions/getComments.php", { articleId: articleId },
		 function(data){
				$('#commentsList'+articleId).html(data);
		 });
	
	$('#commentsAll'+articleId).show();
	

	$.get("/actions/commentsCount.php", { articleId: articleId },
		 function(data){
				$('#commentCount'+articleId).html(data);
		 });
  
}

function clearComments(commentTextId)
{
	if (document.getElementById(commentTextId).value == "Type your comment here...")
	{
		document.getElementById(commentTextId).value = "";
	}
}

function doComment(articleId, articleComment)
{
	if (document.getElementById('commentText'+articleId).value != "Type your comment here..." && document.getElementById('commentText'+articleId).value != "")
	{
	$.post("/actions/comments.php", { articleId: articleId, articleComment: articleComment },
		 function(data){
				//alert(data);
			 if (data == 'notloggedin')
			 {
					$.facebox( $('<iframe id="loginIframe" src="https://www.storyfollower.com/login/popup.php" width="275px" height="275px" scrolling="auto" border="0" frameborder="0"></iframe>') )
			 }
			 else if (data == "success")
			 {
				$('#commentText'+articleId).val('Type your comment here...');

				getComments(articleId);
				
			 }
		 });
	}
}


//$("#city").result(function(event, data, formatted) {
//	//auto submit happens here...
//	document.getElementById('nHoodForm').submit();
//});
