/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Written by Doug Arcuri for JPC Consulting

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	var xmlHttp;
	var ATOMHttp;

	var titles = new Array();
	var links = new Array();
	var images = new Array();
	var dates = new Array();

	//DOM object array
	var stubs = new Array();
	var stubnum = 0;

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Create a standard AJAX object

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function createXMLHttpRequest() {
		
		if ( window.ActiveXObject ) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if ( window.XMLHttpRequest ) {
			xmlHttp = new XMLHttpRequest();
		}
	}
	
	function createATOMHttpRequest() {
		
		if ( window.ActiveXObject ) {
			ATOMHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if ( window.XMLHttpRequest ) {
			ATOMHttp = new XMLHttpRequest();
		}
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Start the AJAX request

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function startXMLHttpRequest(article, type) {

		createXMLHttpRequest();
		
		if ( type == "ATOM" ) {
		 xmlHttp.onreadystatechange = callbackXMLHttpATOMRequest;
		} else {
		 xmlHttp.onreadystatechange = callbackXMLHttpRSSRequest;
		}
		
		xmlHttp.open("GET", "xmlfeed.php?id=" + article + "&type=" + type, true);
		xmlHttp.send(null);
	}
	
	function startATOMHttpRequest() {

		createATOMHttpRequest();
		ATOMHttp.onreadystatechange = callbackXMLHttpATOMRequest;
		
		ATOMHttp.open("GET", "xmlfeed.php?id=5&type=ATOM", true);
		ATOMHttp.send(null);
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	AJAX callback function

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function callbackXMLHttpRSSRequest() {

		if ( xmlHttp.readyState == 4 ) {
			if ( xmlHttp.status == 200 ) {
				createTicker();
			}
		}
	}
	
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	AJAX callback function for ATOM

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function callbackXMLHttpATOMRequest() {
	
		if ( ATOMHttp.readyState == 4 ) {
			if ( ATOMHttp.status == 200 ) { 
			
				//remove loading graphic.
				var oNodeToRemove = document.getElementById("loading");
   			        oNodeToRemove.parentNode.removeChild(oNodeToRemove);
			
				var items = ATOMHttp.responseXML.getElementsByTagName("entry");
				var btitles = new Array();
				var bcontent = new Array();
				var blinks = new Array();
				var bdates= new Array();
				
				for ( var i = 0; i < 3 /*items.length */; i++ )
				{
					btitles[i] = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
					bcontent[i] = items[i].getElementsByTagName("content")[0].childNodes[0].nodeValue.replace(/(<([^>]+)>)/ig,"").substr(0,40) + "...";
					blinks[i] = items[i].getElementsByTagName("link")[0].getAttribute("href"); 
					//if ( items[i].getElementsByTagName("published")[0] ) {
					//	bdates[i] = items[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
					//}
					
					var dl = document.getElementById("blogarticles");
					
					
					var dd = document.createElement("dd");
					var link = document.createElement("a");
					var linktext = document.createTextNode(bcontent[i]);
					var sumtext = document.createTextNode(btitles[i]);
					var dt = document.createElement("dt");
		
					link.setAttribute("href", blinks[i]);
					link.setAttribute("title", btitles[i]);
					link.setAttribute("target", "_blank");
					
					dt.appendChild(sumtext);
					link.appendChild(linktext);
					dd.appendChild(link);
					dl.appendChild(dt);
					dl.appendChild(dd);
					
				
					
					//if ( dates[i] ) {
					//	var datetext = document.createTextNode(convertPubDate(dates[i]));
					//	para.appendChild(datetext);
					//}
				}
				
			}
		}
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Sets inward animation of stub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function moveStubIn() {
		setStubPosition("700","6");
		animateStub("0", "6", "20");
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Sets outward animation of stub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function moveStubOut() {
		setStubPosition("0","6");
		animateStub("0", "50", "20");
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Returns the current stub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function getStub() {
		return stubs[stubnum];
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Sets inital position of stub

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function setStubPosition(left, top) {
		var stub = getStub();
		stub.style.top = top + "px";
		stub.style.left = left + "px";
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Animates the current stub with setTimeout

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function animateStub(final_x,final_y,interval) {
		
		var ticker = document.getElementById("newsticker");
  		var elem = getStub();

  		if (elem.movement) {
   		 clearTimeout(elem.movement);
  		}

  		var xpos = parseInt(elem.style.left, 10);
  		var ypos = parseInt(elem.style.top, 10);

		//alert("ypos: " + ypos + " style: " + elem.style.top);

  		if (xpos == final_x && ypos == final_y) {			
			if (  final_y >= 40 ) {

				if ( ticker && ticker.hasChildNodes ) {
					//ticker.removeChild(elem);
					stubnum++;
					
					if ( stubnum >= stubs.length ) {
						stubnum = 0;
					}

					moveStubIn();
				}
				
			} else {
				setTimeout("moveStubOut()", 4000);
			}
    			return true;
  		}
  		if (xpos < final_x) {
    			var dist = Math.ceil((final_x - xpos)/10);
    			xpos = xpos + dist;
 		}
  		if (xpos > final_x) {
    			var dist = Math.ceil((xpos - final_x)/10);
    			xpos = xpos - dist;
  		}
  		if (ypos < final_y) {
    			var dist = Math.ceil((final_y - ypos)/10);
    			ypos = ypos + dist;
  		}
  		if (ypos > final_y) {
    			var dist = Math.ceil((ypos - final_y)/10);
    			ypos = ypos - dist;
  		}
  		elem.style.left = xpos + "px";
		elem.style.top = ypos + "px";
  		var repeat = "animateStub("+final_x+","+final_y+","+interval+")";
  		elem.movement = setTimeout(repeat,interval);
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Creates presentational date out of <pubDate>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function convertPubDate(string) {
		var temp = new Array();
		temp = string.split(' ');
		return ": " + temp[2] + " " + temp[1] + ", " + temp[3] + ": ";
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	1. Creates each headline stub for animation.
	2. Create an array called 'stubs' containing stubs.
	3. Appends these stubs to the newsticker container.

	Each stub looks like this:
	<p>
		<img src="[image]" alt="(Headline Picture)" />
		[link[Source]] [Date]: 
		<a href="[link]" title="[title]">[title]</a>
	</p>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function createStubs() {

		//limit amount of newest stubs to 50
		var amt = titles.length;

		if ( amt > 50 ) {
			amt = 50;
		}
 
		for ( var i = 0; i < amt; i++ ) {
			var para = document.createElement("p");
			var link = document.createElement("a");
			var linktext = document.createTextNode(titles[i]);

			if ( images[i] ) {
				var img = document.createElement("img");
				img.setAttribute("src", images[i]);
				img.setAttribute("alt", "(Headline Picture)");
				para.appendChild(img);
			}

			link.setAttribute("href", links[i]);
			link.setAttribute("title", titles[i]);
			link.setAttribute("target", "_blank");

			link.appendChild(linktext);


			var source = retrieveSource();

			para.appendChild(source);

			if ( dates[i] ) {
				var datetext = document.createTextNode(convertPubDate(dates[i]));
				para.appendChild(datetext);
			}

			para.appendChild(link);
		
			stubs[i] = para;
		}	
	
		var ticker = document.getElementById("newsticker");
		for ( var i = 0; i < stubs.length; i++ ) {
			ticker.appendChild(stubs[i]);
		}
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Creates link element of XML RSS channel source

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function retrieveSource() {
		var channel = xmlHttp.responseXML.getElementsByTagName("channel");
		var texttitle = "Unknown";
		var textlink = "http://www.jaglabs.com/";
		var link = document.createElement("a");
		
		for ( var i = 0; i < channel.length; i++ ) {
			texttitle = channel[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
			textlink = channel[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
		}
	
		link.setAttribute("href", textlink);
		var texno = document.createTextNode(texttitle);
		link.appendChild(texno);

		return link;
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Grabs info from XML RSS and loads into global vars

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function retrieveInfo() {

		var items = xmlHttp.responseXML.getElementsByTagName("item");

		for ( var i = 0; i < items.length; i++ )
		{
			titles[i] = items[i].getElementsByTagName("title")[0].childNodes[0].nodeValue.substr(0,48) + "...";
			links[i] = items[i].getElementsByTagName("link")[0].childNodes[0].nodeValue;
			if ( items[i].getElementsByTagName("image")[0] ) {
				images[i] = items[i].getElementsByTagName("image")[0].childNodes[0].nodeValue;
			}
			if ( items[i].getElementsByTagName("pubDate")[0] ) {
				dates[i] = items[i].getElementsByTagName("pubDate")[0].childNodes[0].nodeValue;
			}
		}
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Creates newsticker div container in a specified 
	location

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function prepareContainer() {
		var inside = document.getElementById("content");

		if ( inside ) {
			var ref = document.getElementById("headlines");
			if ( ref ) {
				var newsticker = document.createElement("div");
				//newsticker.appendChild(document.createTextNode("World News: "));
				newsticker.setAttribute("id", "newsticker");
				return inside.insertBefore(newsticker, ref);
			}
		}

		return false;
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Creates ticker. Fired when XML RSS is fully loaded

	1. create visual container of newsticker.
	2. grab titles, links, dates and images from XML RSS
	3. create stubs and append to container (hidden)
	4. begin animation sequence

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function createTicker() {

		if ( prepareContainer() ) {
			retrieveInfo();
			createStubs();
			moveStubIn();
		}
	}

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

	Load XML RSS file to obtain news articles
	http://www.quirksmode.org/dom/importxml.html
	
	This method has been replaced with AJAX.
	RSS feeds are now obtained from foreign webservers.
	Javascript does not have the ability to grab
	xml files outside this domain. Therefore, AJAX.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

	function loadXML () {
		if (document.implementation && document.implementation.createDocument)
		{
			xmlDoc = document.implementation.createDocument("", "", null);
			xmlDoc.onload = createTicker;
		}
		else if (window.ActiveXObject)
		{
			xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.onreadystatechange = function () {
				if (xmlDoc.readyState == 4){ 
					createTicker();
				}
			}
 		}
		else
		{
			return false;
		}	

		xmlDoc.load("/xml/5150-22-0.xml");
	}

	window.onload = function () {
		if ( !document.getElementById ) { return false; }
		if ( !document.getElementsByTagName ) { return false; }
		if ( !document.appendChild ) { return false; }

		//loadXML () for news articles;
		startXMLHttpRequest(3, "RSS");
		startATOMHttpRequest(5, "ATOM");

	}