var xml_template = "http://www.thewesternmuslim.com/index.php/multimedia/comments/";

var delay_when_cached = 500;



var cacheObjectComments = new commentCache();

var cachedComments_store, entry_id_store, comment_offset_store, comment_limit_store;



//Clear the Cache

function requestCommentRefresh(entry_id, offset, limit) {

	cacheObjectComments = null;

	cacheObjectComments = new commentCache();

	requestCommentList(entry_id, offset, limit);

	}



//Fetch the Comments

function requestCommentList(entry_id, offset, limit){



	//Check the Cache

	var cachedComments = cacheObjectComments.search(entry_id, offset, limit);



	if (cachedComments){

	

		//Hide Content, Display Loading

		var comment_loading = document.getElementById("comment_loading");

		 comment_loading.style.display="block";

		var comment_content = document.getElementById("comment_content");

		 comment_content.style.display="none";





		//Store current display variables so the timeout function can access them when called

		cachedComments_store=cachedComments;

		entry_id_store=entry_id;

		comment_offset_store=offset;

		comment_limit_store=limit;





		//Timeout to display content

		setTimeout("writeStoredObject()", delay_when_cached);



		}

	else {



		//Hide Content, Display Loading

		var comment_loading = document.getElementById("comment_loading");

		var comment_content = document.getElementById("comment_content");

		comment_content.style.display="none";

		comment_loading.style.display="block";



	

		//Lookup the XMl file



		var req = new ajaxCommentRequest(xml_template,entry_id, offset, limit);

		req.loadXMLDoc();



		}

	}



function writeStoredObject(){

	writeCurrentObject(cachedComments_store,entry_id_store, comment_offset_store, comment_limit_store);

	}



function commentCache(){



	//Hold each cached page in the array

	this.cacheObjectCommentss = new Array();



	this.add = function addToCache(obj, entry_id, offset, limit){



		this.cacheObjectCommentss[this.cacheObjectCommentss.length] = new cachedCommentList(obj, entry_id, offset, limit);



		}



	//Find a cached page

	this.search = function searchCache(entry_id, offset, limit){



		for (var i=0;i<this.cacheObjectCommentss.length; i++){



			if (this.cacheObjectCommentss[i].entry_id==entry_id && this.cacheObjectCommentss[i].offset==offset && this.cacheObjectCommentss[i].limit==limit){



				return this.cacheObjectCommentss[i].obj;



				}



			}

		return null;

		}



	}



function cachedCommentList(obj, entry_id, offset, limit){



	//Hold the reference to the object (and also the offset, limit and entry id for searching the cache)

	this.obj=obj;

	this.offset =offset;

	this.limit=limit;

	this.entry_id=entry_id;



	}



function writeCurrentObject(obj, entry_id, offset, limit){



	var comment_loading = document.getElementById("comment_loading");

	 comment_loading.style.display="none";

	var comment_content = document.getElementById("comment_content");

	 comment_content.style.display="block";

	 



	var comment_count = obj.Comments.length;

	comment_content.innerHTML="";





	if (comment_count>0){



		//Clear the old HTML

		

		var total_count = obj.Comments[0].count;



		for (var i=0; i<comment_count; i++){













		//**********************************************************************

		//EDIT BELOW FOR ADDITIONAL STYLING



			var comment_paragraph = document.createElement("p");

			 comment_paragraph.setAttribute("class", "comment_p");

			 comment_paragraph.setAttribute("className", "comment_p");

			 comment_paragraph.appendChild(document.createTextNode(obj.Comments[i].comment_body));

			 



			var comment_detail = document.createElement("div");

			 comment_detail.setAttribute("class", "comment_detail");

			 comment_detail.setAttribute("className", "comment_detail");

			 comment_detail.appendChild(document.createTextNode("Posted by " + obj.Comments[i].author_name + " on " + obj.Comments[i].comment_date));





			comment_content.appendChild(comment_paragraph);

			comment_content.appendChild(comment_detail);



		//EDIT ABOVE FOR ADDITIONAL STYLING

		//**********************************************************************









			}



		var paging = document.createElement("DIV");

		 paging.setAttribute("id", "comment_paging");

		 paging.setAttribute("id", "comment_paging");

		

		var ul = document.createElement("UL");



		var li_previous =document.createElement("LI");

		 li_previous.setAttribute("class", "comment_previous");

		 li_previous.setAttribute("className", "comment_previous");



		if (offset>0){



			var previous = document.createElement("A");

			 previous.appendChild(document.createTextNode("Previous"));

			 previous.href="javascript:requestCommentList("+entry_id+", "+(offset-limit)+", "+limit+")";

			

			li_previous.appendChild(previous);



			}

		else {li_previous.appendChild(document.createTextNode("Previous"));}



		ul.appendChild(li_previous);



		var li_count = document.createElement("LI");

		 li_count.setAttribute("class", "comment_pagingcount");

		 li_count.setAttribute("className", "comment_pagingcount");



		var nPages = parseInt(total_count/limit);



		if (nPages < (total_count/limit)){nPages=nPages+1;}



		var page_count_txt = "(Comment "+((offset/limit)+1)+" of "+nPages+")";

		li_count.appendChild(document.createTextNode(page_count_txt));



		var li_next =document.createElement("LI");

		 li_next.setAttribute("class", "comment_next");

		 li_next.setAttribute("className", "comment_next");



		if (((offset/limit)+1)<nPages){



			var next = document.createElement("A");

			 next.appendChild(document.createTextNode("Next"));

			 next.href="javascript:requestCommentList("+entry_id+", "+(offset+limit)+", "+limit+")";

			li_next.appendChild(next);



			}

		else {li_next.appendChild(document.createTextNode("Next"));}



		ul.appendChild(li_count);

		ul.appendChild(li_next);

		paging.appendChild(ul);



		comment_content.appendChild(paging);

		}



	else {



		var comment_paragraph = document.createElement("p");

		 comment_paragraph.setAttribute("class", "comment_p");

		 comment_paragraph.setAttribute("className", "comment_p");

		 comment_paragraph.appendChild(document.createTextNode("This entry has no comments. Login to submit a comment."));

			 

		comment_content.appendChild(comment_paragraph);



		}

	}





function commentList(){



	this.Comments = new Array();



	this.add = function addComment(id, entry_id, author_name, author_id, comment_body, comment_date, count){



		this.Comments[this.Comments.length] = new Comment(id, entry_id, author_name, author_id, comment_body, comment_date, count);



		}



	}



function Comment(id, entry_id, author_name, author_id, comment_body, comment_date, count){



	this.id=id;

	this.entry_id=entry_id;

	this.author_name=author_name;

	this.author_id=author_id;

	this.comment_date=comment_date;

	this.comment_body=comment_body;

	this.count=count;



	}



function sendCommentList(commentListObject, entry_id, offset, limit){



	cacheObjectComments.add(commentListObject, entry_id, offset, limit);

	writeCurrentObject(commentListObject, entry_id, offset, limit);



	}



function ajaxCommentRequest(url, entry_id, offset, limit)

{

  var req;

  var method = "GET";

  var nocache = true;

  var entry_id=entry_id;

  var offset=offset;

  var limit=limit;

  var params = "entry_id="+entry_id+"&offset="+offset+"&limit="+limit;

  



  // define publi_count methods



  this.loadXMLDoc = function()

  {



    if(window.XMLHttpRequest) {



      try {

        req = new XMLHttpRequest();

      } catch(e) {

        req = false;

 

      }

    } else if(window.ActiveXObject) {

      try {

        req = new ActiveXObject("Msxml2.XMLHTTP");

      } catch(e) {

        try {

          req = new ActiveXObject("Microsoft.XMLHTTP");

        } catch(e) {

          req = false;

        }

      }

    }

    if(req) {



      req.onreadystatechange = processReqChange;



	

      if(method == "POST") {

        req.open("POST", url, true);

        req.setRequestHeader('Content-Type', 'appli_countation/x-www-form-urlencoded');

        req.send(this.params);

      } else {



        req.open(method, url + '?' + params, true);

        req.send(null);

      }

      return true;

    }

    return false;

  }



  this.setMethod = function(newmethod) { method = newmethod.toUpperCase(); }

  this.nocache = function() { nocache = true; }



  // define private methods



  var getNodeValue = function(parent, tagName)

  {

    var node = parent.getElementsByTagName(tagName)[0];

    return (node && node.firstChild) ? node.firstChild.nodeValue : '';

  }

  var editHtmlElements = function() 

  {



  }

  var processReqChange = function runProcess() 

  {



    if(req.readyState == 4 && req.status == 200) {

      if(!req.responseXML) return;



      	var response  = req.responseXML;



		var root = response.getElementsByTagName('stats');

		var count = root[0].getElementsByTagName("total_count")[0].firstChild.nodeValue;

     		root = response.getElementsByTagName('comment');

     	

		var commentListObject = new commentList();

	

     		for (var i=0; i<root.length;i++){

    

      		var id = root[i].getElementsByTagName("id")[0].firstChild.nodeValue;

			var author_name=root[i].getElementsByTagName("author_name")[0].firstChild.nodeValue;

			var author_id=root[i].getElementsByTagName("author_id")[0].firstChild.nodeValue;

			var comment_body =root[i].getElementsByTagName("comment_body")[0].firstChild.nodeValue;

			var comment_date =root[i].getElementsByTagName("comment_date")[0].firstChild.nodeValue;

		

			commentListObject.add(id, entry_id, author_name, author_id, comment_body, comment_date, count);

      	

    			}

		

		sendCommentList(commentListObject,entry_id, offset, limit);

		}

	}

}

