// Can You Tell It JavaScript Document
// Please Don't Look At Me!

var uniqueID=0; // Used to create unique IDs for inserted elements
function readFeed(rssObj, rssModDate)
{
	if( rssObj == null )
	{
		return;
	}
	var n = 0;
	var ep = rssObj.getElementsByTagName("item");
	
	// Populate Mini Episode List on the right sidebar
	if(document.getElementById('mini_episodes')!=undefined)
	{
		if( ep.length != 0 )
		{
			n = ep.length - 1;
			do{
				buildEpisodeMini(ep[n--]);
			} while( (n > -1) && (n > ep.length - 4) );
		}
	}
	if( document.getElementById('full_episode') )
	{
		var epNum = ped_getVar("episode");
		if( epNum == "" )
		{
			// No specific episode, so show the last one
			buildEpisodeFull(ep[ep.length - 1]);
		}
		else
		{
			for(n=0;n<ep.length;n++)
			{
				if(ep[n].getElementsByTagName("title").item(0).firstChild.nodeValue.match(epNum)!=null)
				{
					buildEpisodeFull(ep[n]);
				}
			}
		}
	}
}

function buildEpisodeMini(ep)
{
	var testDOM = ep.getElementsByTagName("itunes:duration");
	if( testDOM.length == 0 )
	{
		// Our DOM can't search pseudo-tags
		var pseudoPrefix = "";
	}
	else
	{
		// Our DOM CAN search pseudo-tags
		var pseudoPrefix = "itunes:";
	}
	
	// Get Episode Info
	var title = ep.getElementsByTagName("title").item(0).firstChild.nodeValue;
	var desc = ep.getElementsByTagName("description").item(0).firstChild.nodeValue;
	var guid = ep.getElementsByTagName("guid").item(0).firstChild.nodeValue;
	var pubdate = new Date(ep.getElementsByTagName("pubDate").item(0).firstChild.nodeValue);
	var duration = ep.getElementsByTagName(pseudoPrefix+"duration").item(0).firstChild.nodeValue;
	var summary = ep.getElementsByTagName(pseudoPrefix+"summary").item(0).firstChild.nodeValue;
	
	// Build the HTML
	var papa = document.getElementById('mini_episodes');
	newEp = document.createElement('div');
	newEp.className = "episode";
	
	var head = document.createElement('h1');
	var titleParts = title.split(" ");
	var epNum = titleParts[titleParts.length - 1];
	head.innerHTML = "<a href=\"episode.html?episode=" + epNum + "\">" + title + "</a>";
	newEp.appendChild(head);
	
	var head2 = document.createElement('h2');
	head2.innerHTML = "<span>Running Time: " + duration + "</span>" + pubdate.toDateString();
	newEp.appendChild(head2);
	
	var para = document.createElement('p');
	para.innerHTML = summary;
	newEp.appendChild(para);
	
	var inspt = document.createElement('div');
	inspt.id="tag"+(uniqueID++);
	newEp.appendChild(inspt);
		
	papa.appendChild(newEp);
}
function buildEpisodeFull(ep)
{
	var testDOM = ep.getElementsByTagName("itunes:duration");
	if( testDOM.length == 0 )
	{
		// Our DOM can't search pseudo-tags
		var pseudoPrefix = "";
	}
	else
	{
		// Our DOM CAN search pseudo-tags
		var pseudoPrefix = "itunes:";
	}
	// Get Episode Info
	var title = ep.getElementsByTagName("title").item(0).firstChild.nodeValue;
	var desc = ep.getElementsByTagName("description").item(0).firstChild.nodeValue;
	var guid = ep.getElementsByTagName("guid").item(0).firstChild.nodeValue;
	var pubdate = new Date(ep.getElementsByTagName("pubDate").item(0).firstChild.nodeValue);
	var duration = ep.getElementsByTagName(pseudoPrefix+"duration").item(0).firstChild.nodeValue;
	var summary = ep.getElementsByTagName(pseudoPrefix+"summary").item(0).firstChild.nodeValue;
	var size = ep.getElementsByTagName("enclosure").item(0).getAttribute('length') / 1024 / 1024;
	
	if( document.title == "EP_HOLDER" )
	{
		document.title = title;
	}
	
	// Build the HTML
	var newEp = document.getElementById('full_episode');
	
	var titleh = document.createElement('h1');
	titleh.innerHTML = title;
	newEp.appendChild(titleh);
	
	var head = document.createElement('div');
	head.id = "ep-head";
	newEp.appendChild(head);
	
	var epnum = document.createElement('h2');
	var titleParts = title.split(" ");
	var epNum = titleParts[titleParts.length - 1];
	epnum.innerHTML = "Episode Number: <span>" + epNum + "</span>";
	head.appendChild(epnum);
	
	var epDate = document.createElement('h2');
	epDate.innerHTML = "Date: <span>" + pubdate.toDateString() + "</span>";
	head.appendChild(epDate);
	
	var epTime = document.createElement('h2');
	epTime.innerHTML = "Running Time: <span>" + duration + "</span>";
	head.appendChild(epTime);
	
	var epSize = document.createElement('h2');
	epSize.innerHTML = "Size: <span>" + size.toFixed(2) + "Mb</span>";
	head.appendChild(epSize);
	
	var epHosts = document.createElement('h2');
	epHosts.innerHTML = "Hosts: <span>Jessie Weems &amp; Troy Corbin</span>";
	head.appendChild(epHosts);
	
	var inspt = document.createElement('div');
	inspt.id="tag"+(uniqueID++);
	head.appendChild(inspt);
	
	var flashvars={	podURL: guid };
	
	swfobject.embedSWF("tellitplayer.swf", inspt.id, "182", "42", "9.0.0","",flashvars);
	
	var download = document.createElement('a');
	download.href = guid;
	download.innerHTML = "Download the podcast mp3.";
	head.appendChild(download);
	
	//var addThis = document.createElement('a');
	//addThis.href = "http://www.addthis.com/bookmark.php";
	//addThis.onClick = "addthis_url = location.href; addthis_title = document.title; return addthis_click(this);";
	//addThis.target = "_blank";
	//addThis.innerHTML = "<img src=\"http://s7.addthis.com/button1-addthis.gif\" width=\"125\" height=\"16\" border=\"0\" alt=\"Bookmark and Share\" />";
	//head.appendChild(addThis);
	
	//var addThis2 = document.createElement('script');
	//addThis2.setAttribute("type","text/javascript");
	//addThis2.innerHTML = "var addthis_pub = 'troycorbinz';";
	//head.appendChild(addThis2);
	
	//var addThis3 = document.createElement('script');
	//addThis3.type = "text/javascript";
	//addThis3.src = "http://s7.addthis.com/js/widget.php?v=10";
	//head.appendChild(addThis3);
	
	var epsum = document.createElement('h3');
	epsum.innerHTML = "Show Summary:";
	newEp.appendChild(epsum);
	
	var para = document.createElement('p');
	para.innerHTML = summary;
	newEp.appendChild(para);
	
	ped_getXML("shownotes.xml", getNotes);
}
// Reads Show Notes from shownotes.xml and populates ULs in the page
function getNotes(rssObj, rssModDate)
{
	var n = 0;
	var ep = rssObj.getElementsByTagName("episode");
	var epNum = ped_getVar("episode");
	var newEp = document.getElementById('full_episode');
	if( epNum == "" ){epNum = ep.length;}
	
	for(n=0;n<ep.length;n++)
	{
		if(ep[n].getElementsByTagName("number").item(0).firstChild.nodeValue.match(epNum)!=null)
		{
			var hosts = ep[n].getElementsByTagName("host");
			for(i=0;i<hosts.length;i++)
			{
				var hnotes = document.createElement('h3');
				hnotes.innerHTML = hosts[i].getAttribute('name') + "'s Show Notes:";
				newEp.appendChild(hnotes);
				
				var hlist = document.createElement('ul');
				var hitems = hosts[i].getElementsByTagName("note");
				for(x=0;x<hitems.length;x++)
				{
					var ulitem = document.createElement('li');
					var uldesc = hitems[x].getElementsByTagName("title").item(0).firstChild.nodeValue;
					var ulurl = hitems[x].getElementsByTagName("url").item(0).firstChild.nodeValue;
					ulitem.innerHTML="<a href=\"" + ulurl + "\">" + uldesc + "</a>";
					hlist.appendChild(ulitem);
				}
				newEp.appendChild(hlist);
			}
		}
	}
	
}

function go(e)
{
	var destination = e.options[e.selectedIndex].value;
	if (destination && destination != 0) location.href = destination;

} 
ped_getXML("canyoutellit.xml", readFeed);