// JavaScript Document
// Daniel Raquel

//BLOG SYSTEM
//BLOG OBJECT
function Blog(){
	this.bid = 0;
	this.title = null;
	this.about = null;
	this.added = 0;
	this.updated = 0;
	this.entries = new DLList(); // List of BlogEntry
	this.threads = new DLList(); //List of BlogThread
	this.current = null; // Entry list position node
	this.page = 1; //Current Blog Output Page.
	
	//CLASS SET/GET FUNCTIONS
	this.getBID = getBID;
	function getBID(){
		return this.bid;	
	}
	this.setBID = setBID;
	function setBID(id){
		this.bid = id;	
	}
	this.getTitle = getTitle;
	function getTitle(){
		return this.title;	
	}
	this.setTitle = setTitle;
	function setTitle(t){
		this.title = t;
	}
	this.getAbout = getAbout;
	function getAbout(){
		return this.about;	
	}
	this.setAbout = setAbout;
	function setAbout(a){
		this.about = a;	
	}
	this.getAdded = getAdded;
	function getAdded(){
		return this.added;	
	}
	this.setAdded = setAdded;
	function setAdded(a){
		this.added = a;	
	}
	this.getUpdated = getUpdated;
	function getUpdated(){
		return this.updated;	
	}
	this.setUpdated = setUpdated;
	function setUpdated(u){
		this.updated = u;	
	}
	//END CLASS SET/GET FUNCTIONS
	//MENU FUNCTIONS
	this.setBlogMenu = setBlogMenu;
	function setBlogMenu(){
		var eid = "blogMenu";
		if(eid == null){
			return false;	
		}
		/*if(document.getElementById(eid+"EPM")){
			document.getElementById(eid+"EPM").innerHTML = "<table><form name='jumpW'><tr><td align='right'><p>Name:</p></td><td align='left'><select name='menu' onChange='location=document.jumpW.menu.options[document.jumpW.menu.selectedIndex].value;' value='GO'>";
		}*/
		if(document.getElementById(eid+"Threads")){
			document.getElementById(eid+"Threads").innerHTML = "<h3>Threads</h3>\n<a href=\"#\" onClick=\"b.resetBlog()\" >All</a><br />\n";
			curt = this.threads.getFirst();
			while(curt != null){
				document.getElementById(eid+"Threads").innerHTML +="<a href=\"#\" onClick=\"b.gotoThread("+curt.data.getTID()+");\">"+curt.data.getTitle()+"</a><br />\n";
				curt = curt.next;
			}
		}
		if(document.getElementById(eid+"Archive")){
			document.getElementById(eid+"Archive").innerHTML = "<h3>Archive</h3>\n";
		}
	}
	//END MENU FUNCTIONS
	//PAGE FUNCTIONS
	this.setBlogPage = setBlogPage;
	function setBlogPage(){
		var eid = "blog";
		var empty = 0;
		var end = false;
		
		if(eid == null){
			return false;	
		}
		if(document.getElementById(eid+"Title")){
			document.getElementById(eid+"Title").innerHTML = this.title;
		}
		if(document.getElementById(eid+"Thread")){
			document.getElementById(eid+"Thread").innerHTML = "";
		}
		if(document.getElementById(eid+"PrevNext")){
			document.getElementById(eid+"PrevNext").innerHTML = "<a href=\"#\" onclick=\"b.prevBlogPage()\">Prev</a> :: <a href=\"#\" onclick=\"b.nextBlogPage()\">Next</a>";
		}
		if(this.page <= 1){
			this.current = this.entries.getFirst();
		}else{
			this.current = this.entries.getFirst();
			var n = (_epp * (this.page - 1));
			for(i=0;i<n;i++){
				this.current = this.current.next;	
			}
		}
		for(var i = 1; i <= _epp; i++){
			if(!end){
				this.fillEntry(eid,i,false);
			}
			if(this.current.next == null){
				if(end){empty++;document.getElementById(eid+"Entry"+i).style.visibility = "hidden";}
				end = true;
			}else{
				this.current = this.current.next;	
			}
		}
	}
	this.nextBlogPage = nextBlogPage;
	function nextBlogPage(){
		if(_epp*this.page < this.entries.size()){
			if(isset("_cpa")){
				cblogCloseAll();
				instant = this;
				setTimeout("instant.page++;instant.setBlogPage();cblogReset()",750);
			}else{
				this.page++;
				this.setBlogPage();
			}
		}
	}
	this.prevBlogPage = prevBlogPage;
	function prevBlogPage(){
		if(this.page != 1){
			if(isset("_cpa")){
				cblogCloseAll();
				instant = this;
				setTimeout("instant.page--;instant.setBlogPage();cblogReset()",750);
			}else{
				this.page--;
				this.setBlogPage();
			}
		}
	}
	//THREAD FUNCTIONS
	this.setThreadPage = setThreadPage;
	function setThreadPage(tid){
		var eid = "blog";
		var empty = 0;
		var filled = 0;
		var end = false;
		
		if(eid == null){
			return false;	
		}
		if(isset("_cpa")){
				cblogReset();
		}
		if(document.getElementById(eid+"Title")){
			document.getElementById(eid+"Title").innerHTML = this.title;
		}
		if(document.getElementById(eid+"Thread")){
			var curt = this.threads.getFirst();
			while(curt != null){
				if(curt.data.getTID() == tid){
					break;
				}else{
					curt = curt.next;	
				}
			}
			document.getElementById(eid+"Thread").innerHTML = "<br /><b>Thread:</b> "+curt.data.getTitle() + " <a style=\"font-size:10px;\" href=\"#\" onClick=\"b.resetBlog()\" >(clear)</a>";
		}
		if(document.getElementById(eid+"PrevNext")){
			document.getElementById(eid+"PrevNext").innerHTML = "<a href=\"#\" onclick=\"b.prevThreadPage("+tid+")\">Prev</a> :: <a href=\"#\" onclick=\"b.nextThreadPage("+tid+")\">Next</a>";
		}
		if(this.page <= 1){
			this.current = this.entries.getFirst();
			while(this.current.data.getTID() != tid){
				this.current = this.current.next;
			}
		}else{
			this.current = this.entries.getFirst();
			var n = (_epp * (this.page - 1));
			c = 0;
			while (c < n && this.current != null){
				if(this.current.data.getTID() == tid){
					c++;
				}
				this.current = this.current.next;	
			}
		}
		for(var i = 1; i <= _epp; i++){
			if(!end){
				this.fillEntry(eid,i,true);
				filled++;
			}
			if(this.current.next == null){
				if(end){empty++;document.getElementById(eid+"Entry"+i).style.visibility = "hidden";}
				end = true;
			}else{
				this.current = this.current.next;
				while(this.current.data.getTID() != tid){
					this.current = this.current.next;
					if(this.current.next == null && this.current.data.getTID() != tid){
						end = true;
						break;
					}	
				}
			}
		}
	}
	this.nextThreadPage = nextThreadPage;
	function nextThreadPage(tid){
		var curr = this.entries.getFirst();
		if(_epp*this.page < this.entries.size()){
			if(_cpa){
				cblogCloseAll();
				instant = this;
				setTimeout("instant.page++;instant.setThreadPage("+tid+");cblogReset()",1000);
			}else{
				this.page++;
				this.setThreadPage(tid);
			}
		}
	}
	this.prevThreadPage = prevThreadPage;
	function prevThreadPage(tid){
		if(this.page != 1){
			if(_cpa){
				cblogCloseAll();
				instant = this;
				setTimeout("instant.page--;instant.setThreadPage("+tid+");cblogReset()",1000);
			}else{
				this.page--;
				this.setThreadPage(tid);
			}
		}
	}
	//END THREAD FUNCTIONS
	//TRANSITION FUNCTIONS
	this.resetBlog = resetBlog;
	function resetBlog(){
		if(isset("_cpa")){
				cblogCloseAll();
				instant = this;
				setTimeout("instant.page = 1;instant.setBlogPage();cblogReset()",750);
		}else{
			this.page = 1;
			this.setBlogPage();
		}
	}
	this.gotoThread = gotoThread;
	function gotoThread(tid){
		if(isset("_cpa")){
			this.page = 1;
			cblogCloseAll();
			instant = this;
			setTimeout("instant.setThreadPage("+tid+");_cpa[1].open();",750);
		}else{
			this.page = 1;
			this.setThreadPage(tid);
		}
	}
	//END TRANSITION FUNCTIONS
	//AJAX GET FUNCTION
	this.getBlog = getBlog;
	function getBlog(bid){
		var xmlhttp, url;
		xmlhttp = ajaxReq();
		url = "post/getBlog.php";
		xmlhttp.open("POST",url,false);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		if(_sid != null && bid != null ){
			ins = "s="+_sid+"&b="+bid;
		}else{
			return false;	
		}
		xmlhttp.send(ins);
		xmlDoc = loadXML(xmlhttp.responseText);
		this.readXML(xmlDoc);
		cblogSetArray();
	}
	//END AJAX GET FUNCTION
	this.fillEntry = fillEntry;
	function fillEntry(eid,i,te){
		document.getElementById(eid+"Entry"+i).style.visibility = "visible";
	
		if(document.getElementById(eid+"Entry"+i+"Title")){
			document.getElementById(eid+"Entry"+i+"Title").innerHTML = this.current.data.getTitle();
		}
		if(document.getElementById(eid+"Entry"+i+"Manage")){
			document.getElementById(eid+"Entry"+i+"Manage").innerHTML = "Edit Entry :: Remove Entry";
		}
		if(document.getElementById(eid+"Entry"+i+"Thread") && !te){
			var tt = null;
			var curr = this.threads.getFirst();
			while(curr != null){
				if(this.current.data.getTID() == curr.data.getTID()){
					tt = "<b>Thread: </b><a href=\"#\" onClick=\"b.gotoThread("+curr.data.getTID()+");\">"+curr.data.getTitle()+"</a>";
				}
				curr = curr.next;
			}
			if(tt != null){
				document.getElementById(eid+"Entry"+i+"Thread").innerHTML = tt;
			}
		}else{
			if(document.getElementById(eid+"Entry"+i+"Thread")){
				document.getElementById(eid+"Entry"+i+"Thread").innerHTML = "";
			}
		}
		if(document.getElementById(eid+"Entry"+i+"HTML")){
			document.getElementById(eid+"Entry"+i+"HTML").innerHTML = "&nbsp;&nbsp; "+this.current.data.getEntry();
		}
		if(document.getElementById(eid+"Entry"+i+"Sig") || this.current.data.getSig() == null){
			document.getElementById(eid+"Entry"+i+"Sig").innerHTML = "-"+this.current.data.getSig();
		}
		if(document.getElementById(eid+"Entry"+i+"Added")){
			/*var ds = parseInt(this.current.data.getAdded());
			var dms = ds * 1000;
			var d = new Date(dms);
			if(d.getSeconds() < 10){ var s = "0"+d.getSeconds();}else{var s = d.getSeconds();}
			var ds =  d.getMonth()+"."+d.getDay()+"."+d.getFullYear()+" "+d.getHours()+":"+d.getMinutes()+":"+s;*/
			document.getElementById(eid+"Entry"+i+"Added").innerHTML = "<b>Added:</b> "+this.current.data.getAdded();
		}
		if(document.getElementById(eid+"Entry"+i+"Updated")&& this.current.data.getUpdated() != 0){
			document.getElementById(eid+"Entry"+i+"Updated").innerHTML = "<b>Updated:</b> "+this.current.data.getUpdated()+" ";
		}
		if(this.current.data.getUpdated() == 0 && document.getElementById(eid+"Entry"+i+"Updated").innerHTML != ""){
				document.getElementById(eid+"Entry"+i+"Updated").innerHTML = null;
		}
	}
	//END PAGE FUNCTIONS
	this.getBlogHTML = getBlogHTML;
	function getBlogHTML(){
		
	}
	this.getBlogColapseHTML = getBlogColapseHTML;
	function getBlogColapseHTML(){
		
	}
	//XML FUNCTIONS
	this.readXML = readXML;
	function readXML(doc){
		d = doc.documentElement;
		a = d.attributes;
		this.bid = a.getNamedItem("bid").nodeValue;
		this.title = d.getElementsByTagName("title")[0].childNodes[0].nodeValue;
		this.about = d.getElementsByTagName("about")[0].childNodes[0].nodeValue;
		this.added = d.getElementsByTagName("added")[0].childNodes[0].nodeValue;
		this.updaded = d.getElementsByTagName("updated")[0].childNodes[0].nodeValue;
		var t,e;
		bti = d.getElementsByTagName("BlogThread");
		for(i = 0; i < bti.length; i++){
			t = new BlogThread();
			t.setTID(bti[i].attributes.getNamedItem("tid").nodeValue);
			t.setTitle(bti[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
			t.setAbout(bti[i].getElementsByTagName("about")[0].childNodes[0].nodeValue);
			this.threads.add(t);
		}
		bei = d.getElementsByTagName("BlogEntry");
		for(i = 0; i < bei.length; i++){
			e = new BlogEntry();
			e.setEID(bei[i].attributes.getNamedItem("eid").nodeValue);
			e.setTID(bei[i].attributes.getNamedItem("tid").nodeValue);
			e.setTitle(bei[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
			e.setEntry(bei[i].getElementsByTagName("entry")[0].childNodes[0].nodeValue);
			e.setAdded(bei[i].getElementsByTagName("added")[0].childNodes[0].nodeValue);
			e.setUpdated(bei[i].getElementsByTagName("updated")[0].childNodes[0].nodeValue);
			e.setSig(bei[i].getElementsByTagName("sig")[0].childNodes[0].nodeValue);
			this.entries.add(e);
		}
	}
	//END XML FUNCTIONS
	//TEST OUTPUT FUNCTION
	this.toHTMLString = toHTMLString;
	function toHTMLString(){
		s ="<h2>--"+this.title+"--</h2>\n";
		s +="<p>BID: "+this.bid+"<br />\n";
		s +="About: "+this.about+"</p>\n";
		s +="<h3>--Threads--</h3>";
		var curr = this.threads.getFirst();
		while(curr != null){
			s += curr.data.toHTMLString()+"\n";
			curr = curr.next;
		}
		s +="<h3>--Entries--</h3>";
		curr = this.entries.getFirst();
		while(curr != null){
			s += curr.data.toHTMLString()+"\n";
			curr = curr.next;
		}
		return s;
	}
}
//END BLOG OBJECT
//BLOG THREAD OBJECT
function BlogThread(){
	this.tid = 0;
	this.bid = 0;
	this.title = null;
	this.about = null;
	
	//CLASS SET/GET FUNCTIONS
	this.getTID = getTID;
	function getTID(){
		return this.tid;	
	}
	this.setTID = setTID;
	function setTID(id){
		this.tid = id;	
	}
	this.getBID = getBID;
	function getBID(){
		return this.bid;	
	}
	this.setBID = setBID;
	function setBID(id){
		this.bid = id;	
	}
	this.getTitle = getTitle;
	function getTitle(){
		return this.title;	
	}
	this.setTitle = setTitle;
	function setTitle(t){
		this.title = t;
	}
	this.getAbout = getAbout;
	function getAbout(){
		return this.about;	
	}
	this.setAbout = setAbout;
	function setAbout(a){
		this.about = a;	
	}
	//END CLASS SET/GET FUNCTIONS
	//Class Functions
	this.readXML = readXML;
	function readXML(doc){
		
	}
	this.toHTMLString = toHTMLString;
	function toHTMLString(){
		var s;
		s = "<h3>"+this.title+"</h3>";
		s += "<p>TID:"+this.tid+"<br />\n";
		s += "About:"+this.about+"</p>\n";
		return s;
	}
}
//END BLOG THREAD OBJECT
//BLOG ENTRY OBJECT
function BlogEntry(){
	this.eid = 0;
	this.bid = 0;
	this.tid = 0;
	this.title = null;
	this.entry = null;
	this.added = 0;
	this.updated = 0;
	this.sig = null;
	
	//CLASS SET/GET FUNCTIONS
	this.getEID = getEID;
	function getEID(){
		return this.eid;	
	}
	this.setEID = setEID;
	function setEID(id){
		this.eid = id;	
	}
	this.getBID = getBID;
	function getBID(){
		return this.bid;	
	}
	this.setBID = setBID;
	function setBID(id){
		this.bid = id;	
	}
	this.getTID = getTID;
	function getTID(){
		return this.tid;	
	}
	this.setTID = setTID;
	function setTID(id){
		this.tid = id;	
	}
	this.getTitle = getTitle;
	function getTitle(){
		return this.title;	
	}
	this.setTitle = setTitle;
	function setTitle(t){
		this.title = t;
	}
	this.getEntry = getEntry;
	function getEntry(){
		return this.entry;	
	}
	this.setEntry = setEntry;
	function setEntry(e){
		this.entry = e;
	}
	this.getAdded = getAdded;
	function getAdded(){
		return this.added;	
	}
	this.setAdded = setAdded;
	function setAdded(a){
		this.added = a;	
	}
	this.getUpdated = getUpdated;
	function getUpdated(){
		return this.updated;	
	}
	this.setUpdated = setUpdated;
	function setUpdated(u){
		this.updated = u;	
	}
	this.getSig = getSig;
	function getSig(){
		return this.sig;	
	}
	this.setSig = setSig;
	function setSig(s){
		this.sig = s;
	}
	//END CLASS SET/GET FUNCTIONS
	//Class Functions
	this.readXML = readXML;
	function readXML(doc){
	
	}
	this.toHTMLString = toHTMLString;
	function toHTMLString(){
		var s;
		s = "<h3>"+this.title+"</h3>";
		s += "<p>EID:"+this.eid+"<br />\n";
		s += "TID:"+this.tid+"<br />\n";
		s += "Added:"+this.added+"<br />\n";
		s += "Updated:"+this.updated+"<br />\n";
		s += "Entry:<br />\n"+this.entry+"<br />\n";
		s += "Sig:"+this.sig+"</p>";
		return s;
	}
}
//END BLOG ENTRY OBJECT
//SPRY Collapsible Panel Array FUNCTIONS
function cblogReset(){
	if(!_cpa[1].isOpen()){
		_cpa[1].open();
	}
	for(i = 2; i <= _epp; i++){
		if(document.getElementById("blogEntry"+i).style.visibility == "visible" && _cpa[i].isOpen()){
			_cpa[i].close();
		}
	}
}
function cblogCloseAll(){
	for(i = 1; i <= _epp; i++){
		if(document.getElementById("blogEntry"+i).style.visibility == "visible" && _cpa[i].isOpen()){
			_cpa[i].close();
		}
	}
}
function cblogOpenAll(){
	for(i = 1; i <= _epp; i++){
		if(document.getElementById("blogEntry"+i).style.visibility == "visible" && !_cpa[i].isOpen()){
			_cpa[i].open();
		}
	}
}

function cblogSetArray(){
	_cpa = new Array();
	for(i = 1; i <= _epp; i++){
		_cpa[i] = new Spry.Widget.CollapsiblePanel("blogEntry"+i);
		if(i > 1){
			_cpa[i].close();
		}
	}	
}
//END BLOG SYSTEM
