/**
\	Track google analytics playa!
*/
function googleAnalytics()
{
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

	try {
		var pageTracker = _gat._getTracker("UA-12628310-1");
		pageTracker._trackPageview();
	} catch(err) { }
}


/**
\	Retrieve the xml http object based on the browser we are currently viewing in
\	@return the xml http request object
*/
function getXmlHttp()
{
	if (window.XMLHttpRequest)
	{	// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else
	{	// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}

	return null;
}

/**
\	Checks for a file to exist on the server side
*/
function checkForFile(file)
{	
	// request the file to exist or not
	xmlhttp = getXmlHttp();
	xmlhttp.open("HEAD",file, false);
	xmlhttp.send();
	
	// will return 404 if it doesnt exist
	return xmlhttp.status!=404;
}

/**
\	Retrieves the item for the given table id
*/
function getItem(id)
{
    var itm = false;
    if(document.getElementById)
        itm = document.getElementById(id);
    else if(document.all)
        itm = document.all[id];
    else if(document.layers)
        itm = document.layers[id];

    return itm;
}

/**
\	Will toggle the collapse of a table id
*/
function toggleItem(id)
{
    itm = getItem(id);

    if(!itm)
        return false;

    if(itm.style.display == 'none')
        itm.style.display = '';
    else
        itm.style.display = 'none';

    return false;
}

/**
\	Will build the updates pulled from the updates.xml file. To update a message add add
\	<item> tag in the the updates.xml
*/
function buildUpdates()
{
	
	document.write( "<font class='tweeter_large'><b>Updates</b></font>");

	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/updates.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	var x=xmlDoc.getElementsByTagName("item");	
	for ( i = 0; i < x.length; i++ )
	{	 
		var tdClass = (i % 2 != 3 ) ? "normal" : "alternate";
		
		// build our title with the date
		document.write("<table class='update'><tr><td class='alternate'><font class='tweeter'><i>" );
		document.write(x[i].getElementsByTagName("date")[0].childNodes[0].nodeValue);
		document.write("</i></font></td></tr>");
		
		// build update table with ability to hide and show
		document.write( "<tbody id=\"" + i + "\"><tr><td class='content'><font class='tweeter'>");
		document.write(x[i].getElementsByTagName("update")[0].childNodes[0].nodeValue);
		document.write("</font></td></tr></tbody>" );
		
		// add our show hide link
		document.write( "<tr><td class='content' align='right'><font class='tweeter_small'><a href=\"#\" onclick=\"toggleItem('" + i  + "')\">show/hide</a></font></td></tr>")
		
		// finish up our table
		document.write( "</table>");
	}
}

/**
\	Will build the download link area and dynamically pull the version from the download.xml
\	which needs to be updated with the latest version when posting new versions.
\
\	@param fromRoot TRUE if download locations are from the root, FALSE otherwise
*/
function buildDownload( fromRoot )
{	
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/download.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	var x=xmlDoc.getElementsByTagName("download");
	document.write( "<font class='tweeter_large'><b>Download</b></font>");
	document.write("<table class='download'><tr>");
	document.write("<td class='content'><font class='tweeter'><center>");
	
	
	// default not from root
	dmg = "../tweeter/Tweeter.dmg";
	image = "../images/img_drive.png";
	release_notes = "../release_notes/"
	
	// if we are from the root, we need to change our location
	if( fromRoot )
	{
		dmg = "./tweeter/Tweeter.dmg";
		image = "./images/img_drive.png"
		release_notes = "./release_notes/";
	}
	
	document.write("<a href='" + dmg + "' onClick='pageTracker._trackPageview('" + dmg + "');'>");
	document.write("<img src='" + image + "' /></a><br/>Version ");
	
	// pull the version here
	var x=xmlDoc.getElementsByTagName("download");
	document.write(x[0].getElementsByTagName("version")[0].childNodes[0].nodeValue);
	
	document.write("<br/><a href='" + release_notes + "'>Release notes</a><br/>");
	document.write("<br/><form action='https://www.paypal.com/cgi-bin/webscr' method='post'>");
	document.write("<input type='hidden' name='cmd' value='_s-xclick'>");
	document.write("<input type='hidden' name='hosted_button_id' value='8VX8QXUMPZV92'>");
	document.write("<input type='image' src='https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'>");
	document.write("<img alt='' border='0' src='https://www.paypal.com/en_US/i/scr/pixel.gif' width='1' height='1'>");
	document.write("</form></center></font></td></tr></table>");

}

/**
\	Build our navigation panel from navigation.xml.
\
\	@param current the ID of the current panel to build the proper reference points to index.html
*/
function buildNavigation(current)
{	
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/navigation.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	var x=xmlDoc.getElementsByTagName("navigation_item");	
	for ( i = 0; i < x.length; i++ )
	{
		// id of the navigation link
		id = x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue;
		
		// build our link
		link = "";
		if( id == current )
		{
			link = "./"; //index.html
		}
		else
		{
			if( id != "0" )
			{
				link = "../" + x[i].getElementsByTagName("location")[0].childNodes[0].nodeValue + "/"; //index.html
			}
			else
			{
				link = "../"; // index.html
			}
		}
		
		// setup our reference location
		ref = ".."
		if( current == "0" )
		{
			ref = ".";
		}
		
		// create our image locations
		image = ref + "/images/nav/" + x[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
		hover = ref + "/images/nav/" + x[i].getElementsByTagName("hover")[0].childNodes[0].nodeValue;
			
		// create the link
		document.write("<a class='nav' href='" + link + "'>" );
		document.write("<img src='" + image + "'");
		document.write("onmouseover='this.src=\"" + hover + "\"'" ); 
		document.write("onmouseout='this.src=\"" + image + "\"' />");
		document.write("</a><br/>");
	}
}

/**
\	Builds the following data dynamically from the follow.xml
*/
function buildFollow()
{	
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/follow.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	document.write( "<font class=\"tweeter\" >");
	document.write( "<b>Follow us</b><br/>");
	
	// pull the message
	var message=xmlDoc.getElementsByTagName("follow");
	document.write(message[0].getElementsByTagName("message")[0].childNodes[0].nodeValue);
	document.write("<br/>");
	
	// build our links here
	var item = xmlDoc.getElementsByTagName("item");
	for( i = 0; i < item.length; i++ )
	{
		// build each link
		document.write( "<br/><a href=\"" + item[i].getElementsByTagName("link")[0].childNodes[0].nodeValue 
						+ "\"><b>" + item[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + "</b></a>");
		document.write( "<br/>");
	
	}
	
	document.write( "</font>" );
	
	// finish up the table
	//document.write("</font></td></tr></table>");
}

function buildFeatures()
{
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/features.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;	
	
	document.write( "<font class='tweeter_large'><b>Features</b></font><font class=\"tweeter\"><ul>");
	
	var item = xmlDoc.getElementsByTagName("feature");
	for( i = 0; i < item.length; i++ )
	{
		document.write( "<li>" + item[i].childNodes[0].nodeValue + "</li>");
	}
	
	document.write( "</ul></font>" );
}

function buildAbout()
{	
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/information.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	//document.write("<font class=\"tweeter\">");
	
	// dump the about data	
	var about = xmlDoc.getElementsByTagName( "about" );
	
	document.write( "<font class='tweeter_large'>");
	document.write( "<b>" + about[0].getElementsByTagName("title")[0].childNodes[0].nodeValue  + "</b></font><font class=\"tweeter\"><br/>" );
	document.write( about[0].getElementsByTagName("data")[0].childNodes[0].nodeValue  );

	// separate
	document.write( "<br/><br/>" );
	
	// dump developer data
	var dev = xmlDoc.getElementsByTagName( "developer" );
	document.write( "</font><font class='tweeter_large'>");
	document.write( "<b>" + dev[0].getElementsByTagName("title")[0].childNodes[0].nodeValue  + "</b></font><font class=\"tweeter\"><br/>" );
	document.write( dev[0].getElementsByTagName("data")[0].childNodes[0].nodeValue  );
				
	document.write( "</font>" );
	
}

/**
\	Builds the header, plain and simple
*/
function buildHeader(fromRoot)
{
	image = "../images/tweeter_header.png";
	if( fromRoot )
	{
		image = "./images/tweeter_header.png";
	}
	document.write( "<img src=\"" + image + "\" />" );
}

/**
\	builds the footer, plan and simple
*/
function buildFooter()
{
	// start our font stuff
	document.write( "<center><br/><font class=\"tweeter\">" );
	
	// request our link follow data
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/follow.xml", false);
	xmlhttp.send();
	xmlDoc=xmlhttp.responseXML;
	
	// build our links here
	var item = xmlDoc.getElementsByTagName("item");
	for( i = 0; i < item.length; i++ )
	{
		// build each link
		document.write( "<a href=\"" + item[i].getElementsByTagName("link")[0].childNodes[0].nodeValue 
						+ "\"><b>" + item[i].getElementsByTagName("title")[0].childNodes[0].nodeValue + "</b></a>");
		document.write( " | ");
	
	}
	
	// add contact link
	document.write( "<a href=\"mailto:tweeterformac@gmail.com\" > Contact</a>" );
	
	
	// show copyright
	document.write( "<br/>Copyright © 2010-2011 Mike Owens" );
	document.write( "</font></center>" );
}

function buildScreenshots()
{	
	document.write( "<font class='tweeter_large'><b>Screenshots</b></font>");
	
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/screenshots.xml", false);
	xmlhttp.send();	
	xmlDoc=xmlhttp.responseXML;
	
	// create our table and header
	document.write( "<table class=\"download\">" );
	
	// iterate over each of our groups of screen shots
	var x=xmlDoc.getElementsByTagName("group");	
	for ( i = 0; i < x.length; i++ )
	{
		document.write( "<tr><td class=\"content\"><font class=\"tweeter\">" );
		// create our title for this set of screenshots
		document.write( "<b>" + x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue + "</b><br/>" );
		
		// create our description for this set of screenshots
		document.write( x[i].getElementsByTagName("desc")[0].childNodes[0].nodeValue );
		
		document.write( "</font></td></tr>" );
		
		document.write( "<tbody id=\"" + i + "\"><tr><td class=\"content\"><font class=\"tweeter\">" );
		
		// pull the shorts tag
		var shots = x[i].getElementsByTagName( "shot" );
		for( j = 0; j < shots.length; j++ )
		{
			// create our image screenshot
			document.write( "<a href=\"" + shots[j].getElementsByTagName("image")[0].childNodes[0].nodeValue + "\" class=\"highslide\" onClick=\"return hs.expand(this)\">" );
			document.write( "<img src=\"" + shots[j].getElementsByTagName("image")[0].childNodes[0].nodeValue );
			document.write( "\" alt=\"Highslide JS\" title=\"Click to enlarge\" height=\"100\" width=\"100\" /></a>" );
			
			// create our caption used for our screenshot
			document.write( "<div class=\"highslide-caption\">" + shots[j].getElementsByTagName("desc")[0].childNodes[0].nodeValue );
			document.write( "</div>&nbsp;" );
			
		}
		
		document.write("</font></td></tr></tbody>" );

		// add our show hide link
		document.write( "<tr><td class='content' align='right'><font class='tweeter_small'><a href=\"#\" onclick=\"toggleItem('" + i  + "')\">show/hide</a></font></td></tr>")
	}
	
	// close up our table
	document.write( "<tr><td class=\"content\"><font class=\"tweeter\"><center>Click on an image to enlarge screenshot with description.</center></font></td><tr/></table>" );
}

/**
\	Builds the FAQ section of the site
*/
function buildFaq()
{
	// create our table and header
	document.write( "<font class='tweeter_large'><b>FAQ</b></font>");
	document.write( "<table class=\"update\"><td class=\"content\">" );

	// query our xml data
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/faq.xml", false);
	xmlhttp.send();	
	xmlDoc=xmlhttp.responseXML;
	
	// iterate over each of our faq
	var x=xmlDoc.getElementsByTagName("faq");	
	for ( i = 0; i < x.length; i++ )
	{
		document.write( "<font class=\"question\">Q.</font><font class=\"question_text\"> " );
		document.write(  x[i].getElementsByTagName("question")[0].childNodes[0].nodeValue );
		document.write( "</font><br/><font class=\"answer\">A.</font><font class=\"answer_text\"> " );
		document.write( x[i].getElementsByTagName("answer")[0].childNodes[0].nodeValue );
		document.write( "</font><br/><br/>" );
	}
	document.write( "</td><tr/></table>" );
}

function buildDonators()
{
	// query our xml data
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/donators.xml", false);
	xmlhttp.send();	
	xmlDoc=xmlhttp.responseXML;
	
	var message=xmlDoc.getElementsByTagName("donators");
	var lastUpdated = message[0].getElementsByTagName("updated")[0].childNodes[0].nodeValue;
	
	// builds the header with the updated date
	document.write( "<font class='tweeter_large'><b>Donators</b></font>");
	document.write( "<table class=\"update\" width=\"100%\"><tr><td class=\"content\">");
	document.write( "<font class=\"tweeter\">" );
	
	document.write( "A special thanks goes out to everyone one who donates to Tweeter. Your donations " );
	document.write( "keep Tweeter development alive. Thank you all!" );
	document.write( "</font></td></tr><tr>");
	document.write( "<td class='content' align='right'><i><font class='tweeter_small'>" );
	document.write( "Updated on " + lastUpdated );
	document.write( "</font></i></td></tr>" );
	document.write( "</td></tr></table><table class=\"update\" width=\"100%\"><font class=\"tweeter\">" );
	
	// iterate over each of our faq
	var x=xmlDoc.getElementsByTagName("group");	
	for ( i = 0; i < x.length; i++ )
	{
	
		<!-- iterator over the names to display --> 
		var y = x[i].getElementsByTagName("name");
		for( j = 0; j < y.length; j++ )
		{	
			// display the names in a table with alternating colored lines.
			var tdClass = (j % 2 != 0 ) ? "normal" : "alternate";	
			document.write( "<tr><td class=\"" + tdClass + "\">" + y[j].childNodes[0].nodeValue + "</td></tr>" );
		}
	}
	
	// close up the table
	document.write( "</font></table>" );
}

/**
\	build the release notes
*/
function buildReleaseNotes( buildAll )
{
	document.write( "<font class='tweeter_large'><b>Release Notes</b></font>");
	
	// query our xml data
	xmlhttp = getXmlHttp();
	xmlhttp.open("GET","../data/releasenotes.xml", false);
	xmlhttp.send();	
	xmlDoc=xmlhttp.responseXML;
	
	// start our tabling
	document.write( "<table class=\"update\" width=\"100%\">");
	
	// iterater over each release tag
	var release = xmlDoc.getElementsByTagName("release");	
	for( i = 0; i < ((buildAll) ? release.length : 1 ); i++ )
	{
		var tdClass = (i % 2 == 0 ) ? "normal" : "alternate"; 
		
		document.write( "<tr><td class=\"" + tdClass + "\">" );
		
		document.write( "<font class=\"tweeter\">");
		document.write( "<font class='tweeter_large'><b>" + release[i].getElementsByTagName("version")[0].childNodes[0].nodeValue
						+ "</b></font> <i>" + release[i].getElementsByTagName("date")[0].childNodes[0].nodeValue + "</i><br/><br/>" );
		
		
		// now add the fixed values
		var messages = release[i].getElementsByTagName("fixed")[0].getElementsByTagName("message");
		for( k = 0; k < messages.length; k++ )
		{
			document.write( "<b>[FIXED]</b> ");
			document.write( messages[k].childNodes[0].nodeValue + "<br/>" );
		}
		
		// now add the fixed values
		var messages = release[i].getElementsByTagName("added")[0].getElementsByTagName("message");
		for( k = 0; k < messages.length; k++ )
		{
			document.write( "<b>[ADDED]</b> ");
			document.write( messages[k].childNodes[0].nodeValue + "<br/>" );
		}
		
		// now add the fixed values
		var messages = release[i].getElementsByTagName("removed")[0].getElementsByTagName("message");
		for( k = 0; k < messages.length; k++ )
		{
			document.write( "<b>[REMOVED]</b> ");
			document.write( messages[k].childNodes[0].nodeValue + "<br/>" );
		}
		
		document.write( "</font></td></tr>");					
	}
	
	// close up the table
	document.write( "</table>" );
}
