/*/////////////////////////////////////////////
SAS.js
Utility Script
for Sandhills Astronomical Society
by
Hypervelo Internet Media
a division of
Vaughn Labs, Inc.

Copyright 2008, All Rights Reserved

/////////////////////////////////////////////*/

// meeting on line 209
var sas = {};
sas.root = "/";

sas.resparser = {
		version:"DBG-0801"
};
sas.chrono = {
		today: new Date(),
		JD: function() {
				d = this.today;
				cf = 1000 * 3600 * 24; //ms per day
				ref = new Date(Date.UTC(1970,0,1,0,0,0));
				d.setHours(0); d.setMinutes(0); d.setSeconds(0);
				days = Math.floor((d-ref)/cf);
				return 2440587.5 + days;
		}
};

sas.feeds = {
	nextfeed: 0,
	channel: [],
	loadFeed:function(f) {
			sas.ace.fetch("getfeed.aspx?feed="+f);
			//test result here
			x = sas.ace.responseXML;
			n = x.getElementsByTagName("channel");
			alert("size of channel element: "+n.length);
			if(n) {
//				alert("loadFeed: nextfeed="+sas.feeds.nextfeed+"\n feed="+f);
				i = sas.feeds.nextfeed++;
				nt = n[0].getElementsByTagName("title"); 
				nl = n[0].getElementsByTagName("link");
				nd = n[0].getElementsByTagName("description");
				alert("number of title children of channel: "+nt.length+"\n link children: "+nl.length+"\n desc children: "+nd.length);
				s = ""; q="";
				jmax = 7; if(nt.length<jmax) jmax = nt.length;
				sas.feeds.channel[i] = {newsitem:[]};
				sas.feeds.channel[i].newsitem[0] = {}; alert("jmax="+jmax);
				for(j=0;j<jmax;j++) {alert("j="+j);
					s += nt[j].firstChild.nodeValue+" :"+ nl[j].firstChild.nodeValue + "\n"+nd[j].firstChild.nodeValue+"\n\n";
					sas.feeds.channel[i].newsitem[j] = {title:nt[j].firstChild.nodeValue,desc:nd[j].firstChild.nodeValue,href:nl[j].firstChild.nodeValue};
					ni = sas.feeds.channel[i].newsitem[j];
					q += ni.title + " : " + ni.href + "\n" + ni.desc + "\n\n";
				};
				alert("items read:\n"+s);
				alert("items stored:\n"+q);
				return i;
			};
			return -1;
	},
	loadFeed2:function(f) {
			sas.ace.fetch("getfeed.aspx?feed="+f);
			x = sas.ace.responseXML;
			ch = x.getElementsByTagName("channel"); 
			if(ch) {
				n = ch[0];
				i = sas.feeds.nextfeed++;
				//loop through children of the channel element
				//for each child, if it has title, link, desc, or pubDate children, then store values.
				// ---need to load all and sort by date to select the most recent items.
				sas.feeds.channel[i] = {newsitem:[]};
				ni=sas.ace.getChildrenByTagName(n,"item");
				jmax = 6; if(ni.length<jmax) jmax = ni.length;q="";//alert("jmax="+jmax);
				for(j=0;j<jmax;j++) {
					nt = sas.ace.getChildrenByTagName(ni[j],"title");
					nh = sas.ace.getChildrenByTagName(ni[j],"link");
					nd = sas.ace.getChildrenByTagName(ni[j],"description");
					np = sas.ace.getChildrenByTagName(ni[j],"pubDate");
//alert("length nt,nh,nd,np="+nt.length+","+nh.length+","+nd.length+","+np.length);
//alert("nodevalue of child of nt.0="+nt[0].firstChild.nodeValue);
					sas.feeds.channel[i].newsitem[j] = {};
					sas.feeds.channel[i].newsitem[j].title = (nt[0].hasChildNodes())?nt[0].firstChild.nodeValue : "";
					sas.feeds.channel[i].newsitem[j].href  = (nh[0].hasChildNodes())?nh[0].firstChild.nodeValue : "";
					sas.feeds.channel[i].newsitem[j].desc  = (nd[0].hasChildNodes())?nd[0].firstChild.nodeValue : "";
					sas.feeds.channel[i].newsitem[j].pdate = (np[0].hasChildNodes())?np[0].firstChild.nodeValue : "";
					nitem = sas.feeds.channel[i].newsitem[j];
					q += nitem.title + " : " + nitem.href + "\n" + nitem.desc+ "\n" + nitem.pdate + "\n\n";
				}
//				alert("items stored:\n"+q);
				return i;
			}else{
				return -1;
			}
	}
};

sas.ui = {
	hideCallout:function() {
		e = document.getElementById("callout");
		if(e) {
			e.visibity = 'hidden';
			e.innerHTML = "";
			e.style.top = 0;
			e.style.left = 0;
		}
	},
	showCallout:function(x,y,content) {
		e = document.getElementById("callout");
		if(e) {
			e.style.visibity = 'hidden';
			e.innerHTML = content;
			e.style.top = y; e.style.left = x;
			e.style.position = 'absolute';
			e.style.visiblity = 'visible';
		}
	},
	onCallout:function(c) {
		xadj = document.body.scrollLeft;
		yadj = document.body.scrollTop;
		sas.ui.showCallout(xadj+window.event.x+10,yadj+window.event.y-10,c);
//		e = window.event.toElement;
//		f = document.getElementById("feed_1");s = "feed_1 coord: "+f.style.left+", "+f.style.top;alert(s);
//		sas.ui.showCallout(e.style.left+10,e.style.top+10,c);
	},
	setDatebar:function(id) {
		e = document.getElementById(id);
		if(e) {
			e.innerHTML = sas.chrono.today.toLocaleDateString() + "  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  JD " + sas.chrono.JD().toString()
		}
	},
	initFeed:function(id,t) {
		e = document.getElementById(id);
		if(e) {
			hdstr = "<span class='blockheading'>" + t + "</span><br>";
			bdstr = "<span class='blockcontent' style='color:#ff0000;'>Loading ...</span>";
			e.innerHTML = hdstr + bdstr;
		}

	},
	setMenu:function(id,loc) {
		e = document.getElementById(id);
		if(e) {
		sbegin = "<div class=";		
		slink = "><a class=";
		shref = " href=";
		stab = ">&nbsp;&nbsp;&nbsp;&nbsp;";
		sclose = "</a></div>";
		divclassON  = "mainmenuON";  divclassOFF = "mainmenuOFF";
		linkclassON  = "mainmenulinkON";  linkclassOFF = "mainmenulink";
		s = "";
		for(i=0;i<sas.mainmenu.count;i++){
			if(loc == sas.mainmenu.labels[i]){
				divclass = divclassON;
				linkclass = linkclassON;
				thelink = "#";
				thelabel = sas.mainmenu.labels[i];
			}else{
				divclass = divclassOFF;
				linkclass = linkclassOFF;
				thelink = sas.mainmenu.links[i];
				thelabel = sas.mainmenu.labels[i];
			}
		s += sbegin + divclass+ slink+ linkclass + shref + thelink + stab + thelabel + sclose;
		}
		e.innerHTML = s;	
		}	
	},
	setFeed:function(id,f,t) {
		e = document.getElementById(id);
		if(e) {
			hdstr = "<span class='blockheading'>" + t + "</span><br>";
			bdstr2 = "<span class='blockcontent' >";

			n = sas.feeds.loadFeed2(f);
//			alert("setFeed: channel "+n+" returned.\n size="+sas.feeds.channel[n].newsitem.length);
			if(n>=0) {
				listr = "";jmax = sas.feeds.channel[n].newsitem.length;
				for(j=0;j<jmax;j++) {
					ni = sas.feeds.channel[n].newsitem[j];
//					alert("j="+j+":\n"+ni.pdate+"\n"+ni.title+"\n"+ni.href+"\n"+ni.desc+"\n\n");
					itemdt = ni.pdate; qRE=/"/g;  
					itemdesc = ni.desc.replace(qRE,"&quot;");  //alert("RE:desc "+itemdesc);
					itemdt = ni.pdate; qRE=/'/g;  
					itemdesc = ni.desc.replace(qRE,"&acute;");  //alert("RE:desc "+itemdesc);
					
					qt = "\"";
					cstr = "<b><u>" + itemdt + "</u></b><br>" + itemdesc;
					
//					onMOver = ""; onMOut = "";
//					liname = "li_"+n+"_"+j;			
					onMOver = "onMouseover=" + qt + "javascript:sas.ui.onCallout('" + cstr + "')" + qt;
					onMOut = " onMouseout=" + qt + "javascript:sas.ui.hideCallout()" + qt;
					listr += "<li><a href='"+ ni.href +"' class='blocklink' " + onMOver + onMOut + ">";
					listr += ni.title  + "</a></li>";
				}
				e.innerHTML = hdstr + bdstr2 + listr + "</span>";
			} else {
				e.innerHTML = hdstr + bdstr2 + " No Content </span>" ;
			}
		}
	},
	setNextMeeting:function(id){
		e = document.getElementById(id);
		s = "<span class=blockheading>Next Meeting</span><br>";
		s += "Wed January 25, 2012 7:30 pm<br>Topic: <b>Farsight: Optics for Astronomy</b><br>Location:  FSU Planetarium<br>";
		s += "<br><br>Open to All Interested Persons<br>";
		e.innerHTML = s;
	},
	setOfficers:function(id){
		e = document.getElementById(id);
		s = "<span class=blockheading>Officers</span><br>";
		s += "<span class=blockcontent>President<span class=simple style='font-size:110%;'>Tony Vaughn</span><br>";
		s += "Vice President<span class=simple style='font-size:110%;'>Donald Brinkley</span><br>";
		s += "Treasurer<span class=simple style='font-size:110%;'>Vacant</span><br>";
		s += "Secretary<span class=simple style='font-size:110%;'>Dr. Jonathan Breitzer</span></span><br>";
		e.innerHTML = s;
	},
	setAdFoot:function(f){
		document.writeln("<tr><td><table cellSpacing=0 cellPadding=0 width=800 border=0><tbody>");
		document.writeln("<tr><td class='adcellhi'></td></tr>");
		document.writeln("<tr><td class='adcell'><div class='simple'></div></td></tr>");
		document.writeln("<tr><td class='adcelllow'></td></tr>");
		document.writeln("</tbody></table>");
		document.writeln("</td></tr>");
		document.writeln("<tr><td><img src='/images/spacer.gif' height='30'></td></tr>");
		document.writeln("");
	},
	placePageHeader:function(t,p){ //t=type (0..2), p=place
		document.writeln("<table class=alltable cellSpacing=0 cellPadding=0 width=800 border=0><tbody>");
		document.writeln("<tr><td id='sas_signinline' class='simplyred' align='right'></td></tr>");
		document.writeln("<tr><td class=logocell vAlign=top align=right width=800><a href='http://www.sandhillsastronomicalsociety.org'><img height=70 alt='Sandhills Astronomical Society' src='/images/banner.jpg' width=800 border=0></a> </td></tr>");
		document.writeln("<tr><td class=topcell1 vAlign=top align=left><span id='datebar' class='datebar'></span></td></tr>");
		document.writeln();
		document.writeln();
		document.writeln();
	},
	placeEndPage:function(){
		document.writeln("</tbody></table>");
	},
	placeBeginContent:function(){
		document.writeln("<tr><td class=contentcell vAlign=top align=left>");
	},
	placeEndContent:function(){
		document.writeln("</td></tr>");
	},
	placeFooter:function(){
		document.writeln("<tr><td class=footercell vAlign=top align=middle><br/><br/>");
		document.writeln("Copyright &copy; 2010, Sandhills Astronomical Society.<br>");
		document.writeln("Site Developed by <a class='himlink' href='http://www.hypervelo.com'>Hypervelo Internet Media</a><br> </td></tr>");
	},
	readCookie:function(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	setSignInLine:function(){
		if (!document.getElementById('sas_signinline'))
			return false;

		var username = sas.ui.readCookie('sas_username');
		var userrole = sas.ui.readCookie('sas_role');

		if(username && username != "anonymous"){
			if (userrole =='admin' || userrole =='master')
				username = '<span style="color: yellow;">' + username + '</span>';
			if (userrole == 'subscriber') {
				userlink = 'users/profile.aspx">Profile</a> | ';
			}else{
				userlink = 'members/console.aspx">Console</a> | ';
			}
			document.getElementById('sas_signinline').innerHTML = '<span style="font-size:11px;">'+username+'</span> - <a href="'+sas.root+userlink;
			document.getElementById('sas_signinline').innerHTML+= '<a href="'+sas.root+'login/signout.aspx">Logout</a>';
		}else{
//			document.getElementById('sas_signinline').innerHTML = '<a href="'+sas.root+'users/register.aspx">Register</a> | ';
			document.getElementById('sas_signinline').innerHTML = '<a href="'+sas.root+'standby.html">Register</a> | ';
			document.getElementById('sas_signinline').innerHTML+= "<a href=\""+sas.root+"login/login.aspx\">Login</a>";
		}
	}
};

sas.ace = {
	domain:"http://sas.hypervelo.net",
	xmlbase:"/xml/",
	responseText:"default",
	responseFull:"default",
	responseXML:"",
	
	fetch:function(p) {
		if (window.XMLHttpRequest)
		{	var oReq = new XMLHttpRequest;
			oReq.open("GET", this.domain+this.xmlbase+p,false);
			oReq.send(null);
		}
		else
		{	var oReq = new ActiveXObject("MSXML2.XMLHTTP.3.0");
			//if (oReq)
				//{	alert("Using ActiveX Version..."); }
			oReq.open("GET", this.domain+this.xmlbase+p,false);

			oReq.send();
		} 
		if( window.DOMParser)
		{
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(oReq.responseText, "text/xml");
		} else {
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  			xmlDoc.async="false";
  			xmlDoc.loadXML(oReq.responseText);
		}

		this.responseXML  =  xmlDoc;
 		this.responseText = oReq.responseText;
 		this.responseFull = oReq.getAllResponseHeaders()+oReq.responseText;
 	},
	insert:function(e) {},
	serialize:function(n){
		var v;
		if(typeof XMLSerializer != "undefined"){
			return (new XMLSerializer()).serializeToString(n);
		}
		else if(typeof n.xml == "string"){
			return n.xml;
		}
	},
	getChildrenByTagName:function(n,t) {
		var j, k, r = new Array(); j=0;
		if(n.hasChildNodes()) {
			for(k=0; k<n.childNodes.length; k++) {
				if(n.childNodes[k].nodeName == t) {
					r[j]=n.childNodes[k]; j++;
				}
			}
		}
		return r;
	},
	getChildrenByTag:function(n,t) {
		var a, j, r = new Array(); j=0;
		a = n.firstChild;
		while(a) { 
			if(a.nodeName==t) {r[j]=a; j++;}
			a=a.nextSibling;
		}
		return r;
	}
};




