var json;
var xmlFile ="/feed/rss2/telcordianews.xml";

//when the dom is loaded generate the page from the xml
$(document).ready(function(){
	generateContent(xmlFile);
});

		

//TODO: refactor this function. place it in the utilities.js file -- similar function is used in the pres_xmlloader.s file	
//load the xml file, convert to json, create thecontent for the page.
function generateContent(xmlFile){
	$.ajax({
		  type: "GET",
		  url: xmlFile, 
		  dataType: "xml", 
		  complete: function(data) {
				json = $.xmlToJSON(data.responseXML); 
				createPressReleaseContent(json);
			}
		});
	}
	
//create each the html items required to display each press release item: date and title
// append the date to the date layer and the title to the title layer: for layout
// append date and title Layers to the main Content layer
// add a clear all layer
function createPressReleaseContent(json){

	//get the content layer and empty current content
	var content  = $('div#releases');
	content.empty();
	
	//navigate the JSON structure to the items node
	items = json.channel[0].item;	
		
	//loop through all the items  to create each press release
	 for(i=0;i<items.length;i++) {	
		//create the date layer
		var dateLayer  = jQuery("<div></div>");
		dateLayer.addClass("date");
		//create the title layer
		var titleLayer  = jQuery("<div></div>");
		titleLayer.addClass("title");	
		
		
		// strip parameters off of link
		var link = items[i].link[0].Text ;
		link = link.substr(0, link.length - 16);
		// strip off http://www.telcordia.com so links work in different environments
		link = link.substr(24,link.length);
		//Create publish Date  add link and append to the date layer
		publishdate = generateDate(items[i].pubDate[0].Text);		
		var publishDate = jQuery('<a href="' + link + '">' + publishdate + '</a>');
		publishDate.appendTo(dateLayer);	
		
		//append date layer to the content layer	
		/***********************************/	
		dateLayer.appendTo(content);
		/***********************************/
		
		// Create Title add link and append to the title layer
		var title = jQuery('<h3><a href="' + link + '">'  + items[i].title[0].Text + '</a></h3>');						
		title.appendTo(titleLayer);		
				
		//Create description and append to the title
		//var title = jQuery('<span>' + items[i].description[0].Text + '<span>');						
		//title.appendTo(title);	
		
		//append title layer to the content layer
		/***********************************/
		titleLayer.appendTo(content);
		/***********************************/
		
		//Create the clear div and append to content
		var clearDiv = jQuery("<div></div>");
		clearDiv.addClass("clear");
		
		//append clearDiv layer to the content layer	
		/************************************/
		clearDiv.appendTo(content);
		/***********************************/
	
	}


}


// converts date string from xml file into a friendly date
function generateDate(publishdate){
	day = publishdate.substr(5,2);
	day = jQuery.trim(day);
	if (day.length==1){
		month = publishdate.substr(7,3);
		year =publishdate.substr(11,4);
	}
	else{
		month = publishdate.substr(8,3);
		year =publishdate.substr(12,4);						
	}
	month = monthArray[month];//month array is in the utilities.js file
	return month + ' ' + day + ', ' +year;
						
}




