var req;
//function to load atom.xml feed
function loadAtomXML(url) { 
if (url == "http://columbiacollege.blogspot.com/" || url == "http://spotlight.ccis.edu/") {
		req = false;
		// branch for native XMLHttpRequest object
		if(window.XMLHttpRequest && !(window.ActiveXObject)) {
			try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		// branch for IE/Windows ActiveX version
		} 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;
			 //opens atom.xml.  the format=xml part is necessary to prevent the xml file from being "burned" by feedburner
			req.open("GET", "feeds/posts/default?format=xml&max-results=20", true);
			req.send("");
		}	
}//end if
}//end loadAtomXML


//function to transform the atom.xml loaded by loadAtomXML() and display it on home page
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
			//the XSL stylesheet used for the transform
			txt = '<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:atom="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/1999/xhtml" version="1.0">\
			  <xsl:output method="xml"/>\
			  <xsl:param name="query">Featured</xsl:param>\
			  <xsl:template match="atom:feed">\
				<span>\
				  <xsl:for-each select="atom:entry[atom:category[@term=\'Featured\']]">\
					<xsl:sort order="descending" select="atom:published"/>\
					<xsl:call-template name="featuredstories"/>\
				  </xsl:for-each>\
				  <xsl:apply-templates select="atom:entry">\
					<xsl:sort order="descending" select="atom:published"/>\
				  </xsl:apply-templates>\
				</span>\
			  </xsl:template>\
			\
			  <xsl:template match="atom:entry[atom:category[@term=\'Featured\']]">\
			  </xsl:template>\
			\
			  <xsl:template match="atom:entry">\
				<div class="shortpost">\
				  <a href="{atom:link[@rel=&quot;alternate&quot;]/@href}"><img src="http://www.ccis.edu/spotlight/{substring-before(atom:published,\'-\')}/{substring(atom:published,6,2)}/{substring-after(atom:id,\'post-\')}_t.jpg" alt="{atom:title}" class="thumbnail"/></a>\
				<h3>\
				  <xsl:apply-templates select="atom:title"/>\
				</h3>\
				<span class="blurb"><span class="date"><xsl:value-of select="substring(atom:published,1,10)"/>: </span> <xsl:value-of select="substring(atom:summary,1,150)"/>...</span>\
				</div>\
			  </xsl:template>\
			\
			  <xsl:template name="featuredstories">\
				<xsl:choose>\
				  <xsl:when test="position()=1">\
				  	<div class="featuredpost">\
					 <a href="{atom:link[@rel=&quot;alternate&quot;]/@href}"><img src="http://www.ccis.edu/spotlight/{substring-before(atom:published,\'-\')}/{substring(atom:published,6,2)}/{substring-after(atom:id,\'post-\')}.jpg" alt="{atom:title}" /></a>\
					<h1>\
					  <xsl:apply-templates select="atom:title"/>\
					</h1>\
					<span class="blurb"><span class="date"><xsl:value-of select="substring(atom:published,1,10)"/>: </span> <xsl:value-of select="atom:summary"/>...</span>\
					</div>\
				  </xsl:when>\
				  <xsl:otherwise>\
					<div class="shortpost">\
					<a href="{atom:link[@rel=&quot;alternate&quot;]/@href}"><img src="http://www.ccis.edu/spotlight/{substring-before(atom:published,\'-\')}/{substring(atom:published,6,2)}/{substring-after(atom:id,\'post-\')}_t.jpg" alt="{atom:title}" class="thumbnail"/></a>\
					<h3>\
					  <xsl:apply-templates select="atom:title"/>\
					</h3>\
					<span class="blurb"><span class="date"><xsl:value-of select="substring(atom:published,1,10)"/>: </span> <xsl:value-of select="substring(atom:summary,1,150)"/>...</span>\
					</div>\
				  </xsl:otherwise>\
				</xsl:choose>\
			  </xsl:template>\
			\
			  <xsl:template match="atom:entry/atom:title">\
				<xsl:apply-templates select="../atom:link"/>\
			  </xsl:template>\
			  <xsl:template match="atom:entry/atom:link[@rel=&quot;alternate&quot;]">\
				<a href="{@href}"><xsl:value-of select="../atom:title"/></a>\
			  </xsl:template>\
			</xsl:stylesheet>';
			
			// code for IE
			if (window.ActiveXObject)
				 {
				//load inline XSLT	  
				var xsl=new ActiveXObject("Microsoft.XMLDOM");
				var xml=new ActiveXObject("Microsoft.XMLDOM");
				xsl.async="false";
				xsl.loadXML(txt);
				xml.async="false";
				xml.loadXML(req.responseText);
				transformeddoc=xml.transformNode(xsl);
				document.getElementById("homepagecopy").innerHTML=transformeddoc;
				}
			// code for Mozilla, Firefox, Opera, etc.
			else if (document.implementation 
			&& document.implementation.createDocument)
				{
				//load inline XSLT	  	  
				var parser=new DOMParser();
				var xsl=parser.parseFromString(txt,"text/xml");
				xsltProcessor=new XSLTProcessor();
				xsltProcessor.importStylesheet(xsl);
				transformeddoc = xsltProcessor.transformToFragment(req.responseXML,document);
				document.getElementById("homepagecopy").appendChild(transformeddoc);
				}
			else {
				alert("Your browser doesn't support XML parsing.");	
				}
		} else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    }
}

//given a post ID and post URL, write an <img> tag for the post's thumbnail
//the img src has the format /spotlight/YYYY/MM/postidnumber_t.jpg
function printThumbnail(postid,posturl){
	idstartindex = postid.indexOf("post-");
	postid = postid.substring(idstartindex); //the post id minus extraneous characters
	datestartindex = posturl.lastIndexOf("/") - 7; // 7 characters for YYYY/MM
	year = posturl.substr(datestartindex,4);
	month = posturl.substr(datestartindex+5,2);
	document.write('<img src="http://www.ccis.edu/spotlight/'+year+'/'+month+'/'+postid+'_t.jpg" alt="" class="thumbnail" />'); //absolute URL required to accommodate blogger's friendly URLs for labels (e.g. www.ccis.edu/search/label/Sports)
}

//function to print out caption below an image
//the function is called using the image onload event, and it uses the image's ALT text as the caption, inserting the text in a <p> element after the image, within the same div as the image.
function caption(x){
	var p = document.createElement("p");
	x.parentNode.appendChild(p);
	p.innerHTML = x.alt;	
}
