﻿// **** Page builds for "shows.aspx" *****
function buildSeason(y){
	var seasonPath = "resources/xml/thevault.xml";
	
	var theXpath = "/thevault/content[@year='" + y + "']/newsletter/@month";
	var builtUL = "<ul id='episodeList'>";
	if (window.ActiveXObject){ // ***code for IE***
	xml = loadXMLDoc(seasonPath);
		var nodes = xml.selectNodes(theXpath);
		for (i=0;i<=nodes.length-1;i++){
			builtUL = builtUL + "<li><div id='number'>" + nodes[i].childNodes[0].nodeValue + "</div> <a href=\"javascript:buildNewsletter(\'" + y + "\',\'" + nodes[i].childNodes[0].nodeValue + "\');\">" + pullNewsletterTitleMS(y,nodes[i].childNodes[0].nodeValue) + "</a></span></li>";
		}
	} else if (document.implementation && document.implementation.createDocument){ //*** code for  Mozilla ***
		xhttp=new XMLHttpRequest();
	    xhttp.open("GET",seasonPath,false);
        xhttp.send("");
        xml=xhttp.responseXML;
		var nodes = xml.evaluate(theXpath , xml, null, XPathResult.ANY_TYPE, null);
		while(result=nodes.iterateNext()){
			builtUL = builtUL + "<li><div id='number'>" + result.childNodes[0].nodeValue + "</div> <a href=\"javascript:buildNewsletter(\'" + y + "\',\'" + result.childNodes[0].nodeValue + "\');\">" + pullNewsletterTitleMOZ(y,result.childNodes[0].nodeValue) + "</a></span></li>";
		}
	}
	builtUL = builtUL + "</ul>";
	$("#generatedNewsletterList").html(builtUL);
	$("#generatedSeasonList").html(builtUL);
	$("#episodeList li a:first-child").children(0).addClass("highlightLIA");
	$("#episodeList li a:first-child").children(0).addClass("highlightLIA");
	$("#episodeList li a:first-child").click();
	$("#episodeList li:first-child").children(0).parent().addClass("highlightLI");
	buildNewsletter(y,$(builtUL).find("div#number:first").text()); //Load the first episode of the season when user switched season.
}
function buildNewsletter(y,m){
	var seasonPath = "resources/xml/thevault.xml";
	
	var setPath = "/thevault/content[@year='" + y + "']/newsletter[@month='" + m + "']/@file";
	var theXpath = setPath;
	if (window.ActiveXObject){ // ***code for IE***
	    xml = loadXMLDoc(seasonPath);
		var node = xml.selectSingleNode(theXpath);
		ajaxpath = newsletterpath + node.text;
		document.getElementById("bodyIFrame").src = ajaxpath ;
	} else if (document.implementation && document.implementation.createDocument){ //*** code for  Mozilla ***
	    xhttp=new XMLHttpRequest();
	    xhttp.open("GET",seasonPath,false);
        xhttp.send("");
        xml=xhttp.responseXML;
		var node = xml.evaluate(theXpath , xml, null, XPathResult.STRING_TYPE, null);
		ajaxpath = newsletterpath + node.stringValue;
		$.get(ajaxpath,function(data){
    		document.getElementById("bodyIFrame").src = ajaxpath ;
		});
	}
	setEvents();
}


// **** Support Functions to the Builds ****
function pullNewsletterTitleMS(y,m){ //*** code for  IE ***
	var title = xml.selectSingleNode("/thevault/content[@year='" + y + "']/newsletter[@month='" + m + "']/@title");
	return title.text;
}
function pullNewsletterTitleMOZ(y,m){ //*** code for  Mozilla ***
	var title = xml.evaluate("/thevault/content[@year='" + y + "']/newsletter[@month='" + m + "']/@title", xml, null, XPathResult.STRING_TYPE, null);
	return title.stringValue;
}

function pullNewsletterTitlex(y,m){ //*** code for  IE ***
	var title = xml.selectSingleNode("/thevault/content[@year='" + y + "']/newsletter[@month='" + m + "']/@title");
	return title.text;
}
function pullNewsletterTitlxeMOZ(y,m){ //*** code for  Mozilla ***
	var title = xml.evaluate("/thevault/content[@year='" + y + "']/newsletter[@month='" + m + "']/@title", xml, null, XPathResult.STRING_TYPE, null);
	return title.stringValue;
}