/*
* Copyright(c) Arun Viswanathan
* 
* Released under GPLv3.
*
*/

/*
* Type 1 = Personal Blog
* Type 2 = Jugaad Blog
*
* Please note that these scripts assume existence of /content/personalblog.xml and /content/jugaadblog.xml. These xmls are fetched via cronjobs on the server. This indirection is because of bailiwick issues i.e. javascript cannot load XMLs from other bailiwicks.
*/

/* Load the blogs by default when the script is invoked*/
loadAll();


function loadAll() {
	parseXML(1,15);
	parseXML(2,5);
}

function createTable(type, numblogs)
{
   var x = xmlDoc.getElementsByTagName('item');
   var channel = xmlDoc.getElementsByTagName('channel');
	var mainLinkNode = channel[0].getElementsByTagName('link');
	var mainLink = mainLinkNode[0].firstChild.nodeValue;

	var categoryArray = new Array(numblogs);

   var newEl = document.createElement('TABLE');
   newEl.setAttribute('cellPadding',2);
   newEl.setAttribute('border',0);
   var tmp = document.createElement('TBODY');
   newEl.appendChild(tmp);

   /* Create a row for tags*/	
   var tagrow = document.createElement('TR');
	var tagcol = document.createElement('TD');
	var tagstyle = document.createElement('HR');
	tagcol.appendChild(tagstyle);
	var tagstyle = document.createTextNode('Active Tags: ');
	tagcol.appendChild(tagstyle);
	
   /*Only latest numblogs blogs*/
   for(i = 0; i < numblogs; i++)
   {
			 var title = '';
			 var link = '';			
			 var category = '';
			 var pubDate = ''; 
	
			  /*Find the nodes  'link' and 'title'*/
			  for(j = 0; j < x[i].childNodes.length; j++) {
					
					if(x[i].childNodes[j].nodeName == "category") {
						category = x[i].childNodes[j].firstChild.nodeValue;
					}  
					if(x[i].childNodes[j].nodeName == "pubDate") {
						pubDate = x[i].childNodes[j].firstChild.nodeValue;
					}  
					if(x[i].childNodes[j].nodeName == "title") {
					   /* text inside a tag is the nodeValue of the first childNode of that tag*/
						title = x[i].childNodes[j].firstChild.nodeValue;
					}  
					if(x[i].childNodes[j].nodeName == "link") {
					   /* text inside a tag is the nodeValue of the first childNode of that tag*/
						link = x[i].childNodes[j].firstChild.nodeValue;
					}  
			  }
			
			  /* Add a row TR with one data element TD*/  
			  var row = document.createElement('TR');
			  var container = document.createElement('TD');


			  /* Add a link to the data*/
			  var a = document.createElement('A');
			  a.setAttribute('href', link);			
			  a.setAttribute('target', "_blank");			
			  
			  /*Add data as the child of A*/
			  var theData = document.createTextNode(title);
			  a.appendChild(theData);
			  /*Add A as child of TD*/
  		     container.appendChild(a);

			  if(category != '') {	
				  
              /* Add category to the tagrow if we have not already added */  
				  if(categoryArray.indexOf(category) < 0) { 
					  categoryArray.push(category);   
					  var acat = document.createElement('A');
 					  var acatlink = mainLink + 'search/label/' + category;
					  acat.setAttribute('href', acatlink);			
					  acat.setAttribute('target', "_blank");			
				  
					  var italics = document.createElement('EM');
					  var cat = category + ' '; 
					  var catnode = document.createTextNode(cat);
					  italics.appendChild(catnode); 
					  acat.appendChild(italics);
					  tagcol.appendChild(acat);
				  }	
				  
				  var italics = document.createElement('EM');
				  var cat = ' (' + category +')' 
				  var catnode = document.createTextNode(cat);
				  italics.appendChild(catnode); 
   			  container.appendChild(italics);
			  }
			  
			  /* Add a pubDate*/
 		     var italics = document.createElement('EM');
 		     var font = document.createElement('FONT');
		 	  font.setAttribute('size',1);
			 	
			  var datetext = ' ' + pubDate + ' '; 
			  var dnode = document.createTextNode(datetext);
			  font.appendChild(dnode);
			  italics.appendChild(font); 
			  container.appendChild(italics);
			  
			  /*Add TD as child of TR*/
			  row.appendChild(container);
			  
			  /*Add TR as child of TBODY*/
			  tmp.appendChild(row);
	}
	tagrow.appendChild(tagcol);	  
   tmp.appendChild(tagrow);

	if(type == 1) {
		document.getElementById('list').appendChild(newEl);
		//var link = document.getElementById('loadblog');
		//link.innerHTML = "Hide Blogs";
		//link.setAttribute('href', "javascript:unload(1)");
	} else if(type == 2) {
		document.getElementById('list2').appendChild(newEl);
		//var link = document.getElementById('loadblog2');
		//link.innerHTML = "Hide Blogs";
		//link.setAttribute('href', "javascript:unload(2)");
	} 

}

// This function will never be invoked after the above commenting
function unload(type)
{
	if(type == 1) {
		var list = document.getElementById('list');
		list.innerHTML = '';
		var link = document.getElementById('loadblog');
		link.innerHTML = "Load latest blogs";
		link.setAttribute('href', "javascript:parseXML(1, 10)");
	} else if (type == 2) {
		var list = document.getElementById('list2');
		list.innerHTML = '';
		var link = document.getElementById('loadblog2');
		link.innerHTML = "Load latest blogs";
		link.setAttribute('href', "javascript:parseXML(2, 5)");
	}
} 

// Credits : This code is adopted from http://www.w3schools.com/Xml/xml_parser.asp
function parseXML(type, numblogs)
{
  var file;
  if(type == 1) {
	  file = "/content/personalblog.xml";
  }else  if(type == 2) {
	  file = "/content/jugaadblog.xml";
  }
  
  try //Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  	 xmlDoc.async = false;
	 xmlDoc.load(file);
  }
  catch(e)
  {
    try //Firefox, Mozilla, Opera, etc.
    {
      xmlDoc=document.implementation.createDocument("","",null);
  		xmlDoc.async = false;
	   xmlDoc.load(file);
    }
    catch(e)
    { 
		try //Google Chrome
  		{	
   		var xmlhttp = new window.XMLHttpRequest();
  			xmlhttp.open("GET",file,false);
   		xmlhttp.send(null);
   		xmlDoc = xmlhttp.responseXML.documentElement;
  		}	
  		catch(e)
  		{
      	alert(e.message);
      	return;
  		}
    }
  }

  createTable(type, numblogs);
} 


