var fadeInLenth = 500;
var fadeOutLenth = 500;
var displayLength = 2000;
var fgcolor = "#666666";
var bgcolor = "#ffffff";
var steps = 10;

var titles = new Array();
var links = new Array();

// Press Releases

titles[0] = "Latest Releases: Alfresco and Optaros Announce Platinum-Level Partnership ";
links[0] = "http://www.alfresco.com/media/releases/2007/05/optarosplatinum/";

titles[1] = "Latest Releases: Alfresco Recognized As Laureate By The Computerworld Honors Program";
links[1] = "http://www.alfresco.com/media/releases/2007/05/computerworld/";

titles[2] = "Latest Releases: Alfresco Becomes Founding Member of Red Hat Exchange";
links[2] = "http://www.alfresco.com/media/releases/2007/05/rhx/";

// Media Coverage

titles[3] = "Latest Coverage: The rise of Alfresco: ECM that people will really use";
links[3] = "http://www.alfresco.com/media/coverage/2007/04/linuxworld-rise-of-ecm/";

titles[4] = "Latest Coverage: Adobe Open-Source Move Sets Showdown with Microsoft";
links[4] = "http://www.alfresco.com/media/coverage/2007/04/eweek-flex-open-source/";

titles[5] = "Latest Coverage: Ricoh and Alfresco Team Up";
links[5] = "http://www.alfresco.com/media/coverage/2007/04/econtent-ricoh/";

var fTimer = null;
var timer = null;
var itemCount = 0;
var running = false;
var fadeLevel = 0;
var stage = 0;

function initialiseTicker() {
	itemCount = 0;
	get_xml_file();
	running = false;
}

function incrementCounter() {
	itemCount ++;
	if (itemCount > titles.length) {
		itemCount = 1;
	}
}

function decrementCounter() {
	itemCount --;
	if (itemCount < 1) {
		itemCount = titles.length - 1;
	}
}

function runTickerOld() {
	stage = 1;
	fadeOut();
	incrementCounter();
	setTitle(titles[itemCount]);
	fadeIn();
	if (running == true) {
		// Set the timer
		timer = self.setTimeout("runTicker()", displayLength);
	}
}

function runTicker() {
	stage = 2;
	//timer = self.setTimeout("work()", displayLength);
	work();
}

function work() {
	//alert("running = " + running + ", stage = " + stage);
	if (running == true) {
		//stage = 1 Fading out
		//stage = 2 Swapping
		//stage = 3; Fading in
		//stage = 4; Waiting
	if (stage == 1) { // fade out
		if (running == true && fadeLevel <= steps) {
			setTickerColor(htmlColor(calculateColor(fgcolor, bgcolor, fadeLevel)))
			fadeLevel ++;
			fTimer = self.setTimeout("work()", Math.round(fadeOutLenth/steps));
		}
		if (fadeLevel > steps) {
			stage = 2;
		}
	} else if (stage == 2) { // swap
		incrementCounter();
		setTitle(titles[itemCount - 1]);
		setLink(links[itemCount -1]);
		if (running == true) {
			// Set the timer
			timer = self.setTimeout("work()", 1);
		}
		stage = 3;
	} else if (stage == 3) { // Fade in
		if (running == true && fadeLevel >= 0) {
			setTickerColor(htmlColor(calculateColor(fgcolor, bgcolor, fadeLevel)))
			fadeLevel --;
			fTimer = self.setTimeout("work()", Math.round(fadeInLenth/steps));
		}
		if (fadeLevel < 0) {
			stage = 4;
		}
	} else if (stage == 4) { // Waiting
		if (running == true) {
			// Set the timer
			timer = self.setTimeout("work()", displayLength);
		}
		stage = 1; // next stage
	}
	}
}

function getHTTPObject(){
	var request = new XMLHttpRequest();
	if(window.XMLHttpRequest){
    	request = new XMLHttpRequest();   
	}else if (window.ActiveXObject){
    	request=new ActiveXObject("Microsoft.XMLHTTP");
    	if (! request){
        	request=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    }
	return request;
}

function get_xml_file() {

    var httpreq = getHTTPObject();

    //Precondition: must have a URL
    url = 'http://www.novaworks.it/readFeed.php';
    //if (url == "") url = "http://library.theserverside.com/data/bpXchange?b=ka_bp_systemsint&d=31&f=rss&u=rss"

    httpreq.open("GET", url, true);

    httpreq.onreadystatechange = function (  ) {
        if (httpreq.readyState == 4) {
            last_xml_response = httpreq.responseXML;
            format_rss_data (last_xml_response);
        }
    }
	httpreq.setRequestHeader("Content-Type", "text/xml")
    httpreq.send (null);
}

function format_rss_data (response) {

    var doc = response.documentElement;
    var items = doc.getElementsByTagName('item');
    
	if(items.length > 1)
	{
		titles = new Array();
		links = new Array();
		
	    	for (var i=0; i < items.length; i++) {
	
	        	var title = items[i].getElementsByTagName('title')[0];
	        	var link = items[i].getElementsByTagName('link')[0];
	
	        	titles[i] = title.firstChild.data;
	        	links[i]= link.firstChild.data;	        
	
	        	/*var cbDetails = document.getElementById("cbDetails");
	        	if (cbDetails.checked) {
	           		var desc = items[i].getElementsByTagName('description')[0];
	            	html += "<font size='-1'>"
	                    	+ desc.firstChild.data
	                    	+ "</font><p>";
	        	}*/
	    	}
    }
}

function startTicker() {
	initialiseTicker();
	running = true;
	runTicker();
}

function stopTicker() {
	initialiseTicker();
	if (timer) {
		self.clearTimeout(timer);
	}
}

function pauseTicker() {
	running = false;
	if (timer) {
		self.clearTimeout(timer);
	}
	if (fTimer) {
		self.clearTimeout(fTimer);
	}
	// Restore color
	fadeLevel = 0;
	setTickerColor(htmlColor(calculateColor(fgcolor, bgcolor, fadeLevel)))
}

function unpauseTicker() {
	running = true;
	stage = 4;
	work();
}

function clearTimer() {
}

function fadeIn() {
	if (running == true && fadeLevel >= 0) {
		setTickerColor(htmlColor(calculateColor(fgcolor, bgcolor, fadeLevel)))
		fadeLevel --;
		//fTimer = self.setTimeout("fadeIn()", Math.round(fadeInLenth/steps));
	}
}

function fadeOut() {
	if (running == true && fadeLevel <= steps) {
		setTickerColor(htmlColor(calculateColor(fgcolor, bgcolor, fadeLevel)))
		fadeLevel ++;
		//fTimer = self.setTimeout("fadeOut()", Math.round(fadeOutLenth/steps));
	}
}

function setTickerColor(colour) {
	//alert("Setting color to " + colour);
	tickerContent = document.getElementById('ticker-link');
	if (tickerContent) {
		tickerContent.style.color = colour;
	}
}

function clearTitle() {
	document.getElementById('ticker-content').innerHTML = "";
	return true;
}

function calculateColor(start, end, step) {
	var startColors = makeColorArray(start);
	var endColors = makeColorArray(end);
	
	var colors = null;
	
	if (startColors && endColors) {
		var diff = new Array(endColors[0]-startColors[0], endColors[1]-startColors[1], endColors[2]-startColors[2]);
		colors = new Array(Math.round(startColors[0] + ((diff[0]*step)/steps)), Math.round(startColors[1] + ((diff[1]*step)/steps)), Math.round(startColors[2] + ((diff[2]*step)/steps)));
	}
	
	return colors;
}

function htmlColor(colors) {
	return "rgb(" + colors[0] + ", " + colors[1] + ", " + colors[2] + ")";
}

function makeColorArray(colorStr) {

	var cArray = null;
	
	if (colorStr.length == 7) {
		cArray = new Array(parseInt("0x"+colorStr.substr(1, 2)), parseInt("0x"+colorStr.substr(3, 2)), parseInt("0x"+colorStr.substr(5, 2)));
	} else if (colorStr.length == 18) {
		cArray = new Array(parseInt(colorStr.substr(4, 3)), parseInt(colorStr.substr(9, 3)), parseInt(colorStr.substr(14, 3)));
	} else if (colorStr.length == 15) {
		cArray = new Array(parseInt(colorStr.substr(4, 2)), parseInt(colorStr.substr(8, 2)), parseInt(colorStr.substr(12, 2)));
	} else if (colorStr.length == 12) {
		cArray = new Array(parseInt(colorStr.substr(4, 1)), parseInt(colorStr.substr(7, 1)), parseInt(colorStr.substr(10, 1)));
	}
	
	return cArray;
}

function setTitle(title) {
	document.getElementById('ticker-link').innerHTML = title;
	return true;
}

function setLink(link) {
	document.getElementById('ticker-link').href = link;
	return true;
}
