/* Flags */
ratingsEnabled = true;
commentsEnabled = true;
mostCommentedEnabled = true;
personaEnabled = true;
updateArticleEnabled = true;

var serverUrl = "http://sitelife.todaysparent.com/ver1.0/Direct/Process";

/** Rating Widget Redirection **/

/* GetRatings(contentId, url)
 * Gets the ratings widget and drops it on the page wrapped in a DIV tag
 * contentId: a Unique identifier
 * url: the optional url of the current page. */
function getRatings(contentId, url) 
{
	if (ratingsEnabled) 
	{
		gSiteLife.Rating("ExternalResource", contentId, url); 
	} 
	else 
	{
		return false;
	}
}


/** Comments Widget Redirection **/

/* GetComments(contentId, commentsPerPage)
 * this gets the full comments widget including the display and the form and drops on the page in a DIV tag
 * contentId: a Unique identifier 
 * commentsPerPage: number of comments to display per page*/
function getComments(contentId, commentsPerPage) 
{
	if (commentsEnabled) 
	{
		if (commentsPerPage == null) 
		{
			commentsPerPage = 10;
		}
		gSiteLife.Comments("ExternalResource", contentId, commentsPerPage, "TimeStampDescending"); 
	} 
	else 
	{
		return false;
	}
}
/* GetCommentsDisplay(contentId, commentsPerPage)
 * this gets the comments display and drops on the page in a DIV tag
 * contentId: a Unique identifier
  * commentsPerPage: number of comments to display per page*/
function getCommentsDisplay(contentId, commentsPerPage) 
{
	if (commentsEnabled) 
	{
		if (commentsPerPage == null) 
		{
			commentsPerPage = 10;
		}
		gSiteLife.CommentsOutput("ExternalResource", contentId, false, commentsPerPage, "TimeStampDescending"); 
	} 
	else 
	{
		return false;
	}
}
/* GetCommentsForm(contentId, url)
 * this gets the comments form and drops on the page in a DIV tag
 * contentId: a Unique identifier
 * url: the optional url of the current page. */
function getCommentsForm(contentId, url) 
{
	if (commentsEnabled) 
	{
		gSiteLife.CommentsInput("ExternalResource", contentId, url); 
	} 
	else 
	{
		return false;
	}
}
/* GetMostCommented(num)
 * this gets the 'num' must commented article then  drops it in on the page in a DIV tag
  * num: number of top commented articles to fetch*/
function getMostCommented(num) 
{
	if (mostCommentedEnabled) 
	{
		if (num == null) 
		{
			num = 5;
		}
		gSiteLife.SummaryArticlesMostCommented(num);
	} 
	else 
	{
		return false;
	}
}

/** Comment Summary **/

/* GetCommentSummary(contentId)  - UnImplemented
 * this gets the number of comments available for this article and drops on the page
 * contentId: a Unique identifier */
function getCommentSummary(contentId)
{
	if (commentsEnabled) 
	{
		document.write("this is test od summary");
	} 
	else 
	{
		return false;
	}
}

/** Persona **/

/* PersonaHeader(userUd)
 * This gets the persona headder for the specified User.
 * userId: the unique user identifier */
function getPersonaHeader(userId)
{
	if (personaEnabled)
	{
		gSiteLife.PersonaHeader(userId);
	}
	else
	{
		return false;
	}
}  
/* PersonaProfile(userUd)
 * This gets the persona profile for the specified User.
 * userId: the unique user identifier */
function getPersonaProfile(userId)
{
	if (personaEnabled)
	{
		gSiteLife.Persona(userId);
	}
	else
	{
		return false;
	}
}  


/*  updateArticle()
 *  This will update the dek of the Article 
This Function updates the title of all the legacy articles
* title: The browser Title/ Title from Artcile DCR
* contentID: Content ID, this is passed for article.jsp
* uri: This the Article URI
*/

function updateArticle(title,contentID,uri)
{
//	alert ("updateArticle() called");  // for debugging: when article is updated
	if(updateArticleEnabled)
	{						
		
		var OnPageTitle = title;
		var OnPageUrl = "http://www.todaysparent.com"+uri+"?content="+contentID+"&page=1";
		var psection =getSection(uri);
		if(psection == "tprecipe")
		{
			OnPageUrl = "http://recipes.todaysparent.com"+uri+"?recipeId="+contentID;
		}
	
		// get form elements and page info
		var UpdateArticle = contentID;

		var categories = new Array();

//		categories[0] = new Category(document.getElementById("Categories0").value);
//		categories[1] = new Category(document.getElementById("Categories1").value);
//		categories[2] = new Category(document.getElementById("Categories2").value);

		var articleKey = new ArticleKey(UpdateArticle);
		var section = new Section(psection);

		// create and send request
		var requestBatch = new RequestBatch();
		var updateAction = new UpdateArticleAction(articleKey, OnPageUrl, OnPageTitle, section, categories);
		requestBatch.AddToRequest(updateAction);
		requestBatch.BeginRequest(serverUrl, articleUpdated);


	}
	else {
		return false;
		}
	 
	
}



function articleUpdated(responseBatch) {
			
			console.dir(responseBatch);
			
			/*Uncomment this for debugging*/
//			if (responseBatch.Messages[0].Message == "ok")
//			{

//				alert("Article Updated !");
//			}
            
}

/*This function retuns vertical name from the URI Param */
function getSection(uri){

	
	var url = uri;
    var urlparts =url.split("/");
	return urlparts[1];

}



function getArticleInfo() {

			var articleID = document.getElementById("articleID").value;
			var articleKey = new ArticleKey(articleID);
			var requestBatch = new RequestBatch();
			requestBatch.AddToRequest(articleKey);
			requestBatch.BeginRequest(serverUrl, renderArticle);

		}

		function renderArticle(responseBatch) {

			console.dir(responseBatch);

			if (responseBatch.Responses.length == 0) {
				alert('Article not found');
			} else {


				// get article from response
				var article = responseBatch.Responses[0].Article;

				var articleInfoHTML = "<table>"

				// update page elements
				articleInfoHTML += "<tr><td>PageTitle: </td><td>" + article.PageTitle + "</td></tr>";
				articleInfoHTML += "<tr><td>PageUrl: </td><td>" + article.PageUrl + "</td></tr>";
				articleInfoHTML += "<tr><td>Categories: </td><td>" + article.Categories.length + "</td></tr>";

				for (x=0;x<article.Categories.length;x++)
				{
					var name = article.Categories[x].Name;
					articleInfoHTML += "<tr><td>&nbsp;</td><td>&nbsp;&nbsp;&nbsp;&nbsp;" + name + "</td></tr>";
				}

				//articleInfoHTML += "<tr><td>Section: </td><td>" + article.Section.Name + "</td></tr>";

				if (article.SiteOfOrigin != null) {
					articleInfoHTML += "<tr><td>Site of Origin: </td><td>" + article.SiteOfOrigin + "</td></tr>";
				}

				articleInfoHTML += "<tr><td>Number of Comments: </td><td>" + article.Comments.NumberOfComments + "</td></tr>";
				articleInfoHTML += "<tr><td>Number of Recommendations: </td><td>" + article.Recommendations.NumberOfRecommendations + "</td></tr>";
				articleInfoHTML += "<tr><td>Number of Ratings: </td><td>" + article.Ratings.NumberOfRatings + "</td></tr>";
				articleInfoHTML += "<tr><td>Number of Reviews: </td><td>" + article.Reviews.NumberOfReviews + "</td></tr>";

				for (var i = 0; i < responseBatch.Messages.length; i++) {
					var serverMessage = responseBatch.Messages[i];
					articleInfoHTML += "<tr><td>Server Message " + i + ":  " + serverMessage.MessageTime + " -- " + serverMessage.Message + "</td></tr>";
				}


				articleInfoHTML += "</table>";

				var getArticleInfoDiv = document.getElementById('getArticleInfoDiv');
				getArticleInfoDiv.innerHTML = articleInfoHTML;
			}
		}




// Start Temp cookie code //
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

function setCookie(c_name,c_host,expiredays)
{
	alert('setting cookie ...');
	var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape("testUser|testUserName|4546|test@macleans.com")+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString())+'host='+c_host;
	alert('completed setting cookie ...');
}

function checkCookie()
{
username=getCookie(prompt('Please enter the cookie name to search:',""));
if (username!=null && username!="")
  {
  alert('Welcome again '+username+'!');
  }
  else
  {
  alert('Cookie does not exist');
  username=prompt('Enter the cookie name to set:',"");
  domain=prompt('Enter your cookie domain to set:',"");
  if (username!=null && username!="")
    {
    setCookie(username,domain,365);
    }
  }
}
// End Temp cookie code //

