var continuous = true;
var pauseOnMouseover = false;
var paused = false;

var testMode = false;
var testText = '<a href="http://www.fireflycom.net" target="_blank">Lorem ipsum dolor sit amet\nconsectetuer adipiscing elit\nQuisque et risus.</a>';

function createTicker(objId,sourceFile,direction,speed,captionText,captionPos) {
	var w, h, x, y;
	var obj = document.getElementById(objId);
	var parentObj = obj.parentNode;

	obj.style.visibility = 'hidden';
	parentObj.style.visibility = 'hidden';

	// set text properties
	switch (direction) {
		case 'horizontal':
			obj.style.whitespace = 'nowrap';
			break;
		case 'vertical':
			obj.style.whitespace = 'normal';
			break;

	}

	if (pauseOnMouseover) {
		obj.onmouseover = pause;
		obj.onmouseout = play;
	}

	// for a horizontal ticker, set container height to text height
	switch (direction) {
		case 'horizontal':
			h = getHeight(obj);
			parentObj.style.height = h + 'px';
			break;
		case 'vertical':
			//w = getWidth(obj);
			//parentObj.style.width = w + 'px';
			break;
	}
	
	// set caption properties
	if (captionText != '') {
		var captionObj = document.createElement('div');
		captionObj.id = parentObj.id + '-caption';
		captionObj.className = 'caption';
		parentObj.parentNode.appendChild(captionObj);
		captionObj.innerHTML = captionText;
		y = getTop(parentObj);
		x = getLeft(parentObj);
		
		switch (captionPos) {

			case 'top':
				h = getHeight(captionObj);
				w = getWidth(parentObj) - getBorderWidth(parentObj);
				parentObj.style.top = (y+h) + 'px';
				captionObj.style.top = y + 'px';
				captionObj.style.left = x + 'px';
				captionObj.style.width = w + 'px';
				break;

			case 'bottom':
				h = getHeight(parentObj);
				captionObj.style.top = (y+h) + 'px';
				captionObj.style.left = x + 'px';
				break;

			case 'left':
				w = getWidth(captionObj);
				parentObj.style.left = (x+w+2) + 'px';
				captionObj.style.top = y + 'px';
				captionObj.style.left = x + 'px';
				break;

			case 'right':
				w = getWidth(parentObj);
				captionObj.style.top = y + 'px';
				captionObj.style.left = (x+w+2) + 'px';
				break;
		}
		
		captionObj.style.visibility = 'visible';
			
	}

	getText(sourceFile,objId,direction,speed);

}	


function setText(objId,s) {
	var obj = document.getElementById(objId);
	obj.innerHTML = s;
}


function restart(objId,direction,speed) {
	var pos;
	var obj = document.getElementById(objId);
	var parentObj = obj.parentNode;
	parentObj.style.visibility = 'visible';
	
	switch (direction) {

		case 'horizontal':
			pos = getWidth(parentObj);
			obj.style.left = pos + 'px';
			break;

		case 'vertical':
			pos = getHeight(parentObj);
			obj.style.top = pos + 'px';
			break;
	}

	obj.style.visibility = 'visible';

	setTimeout('move(\''+objId+'\',' + pos + ',\'' + direction + '\',' + speed + ');', speed);

}


function move(objId,pos,direction,speed) {
	var obj = document.getElementById(objId);
	if (!paused) pos = pos - 1;

	switch (direction) {

		case 'horizontal':
			var w = getWidth(obj);

			if (pos > -w) {
				obj.style.left = pos + 'px';
				setTimeout('move(\''+objId+'\',' + pos + ',\'' + direction + '\',' + speed + ');', speed);
			} else {
				obj.style.left = -w + 'px';
				if (continuous) setTimeout('restart(\''+objId+'\',\'' + direction + '\',' + speed + ');', speed);
			}
			break;

		case 'vertical':
			var h = getHeight(obj);

			if (pos > -h) {
				obj.style.top = pos + 'px';
				setTimeout('move(\''+objId+'\',' + pos + ',\'' + direction + '\',' + speed + ');', speed);
			} else {
				obj.style.top = -h + 'px';
				if (continuous) setTimeout('restart(\''+objId+'\',\'' + direction + '\',' + speed + ');', speed);
			}
			break;
		
	}

}


function pause() {
	paused = true;
}


function play() {
	paused = false;
}


function getWidth(obj) {
	var w = obj.offsetWidth;
	return w;
}

function getHeight(obj) {
	var h = obj.offsetHeight;
	return h;
}

function getTop(obj) {
	var y = obj.offsetTop;
	return y;
}

function getLeft(obj) {
	var x = obj.offsetLeft;
	return x;
}

function getBorderWidth(obj) {
	var w = obj.style.borderWidth;
	return w;
}

//
// AJAX stuff
//

var http = createRequestObject();

function createRequestObject() {
    var ro;
	if (document.getElementById) ro = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	return ro;
}


function getText(filename,objId,direction,speed) {
    if (testMode) {
		var s = formatText(testText,direction);
		setText(objId,s);
		restart(objId,direction,speed);
    } else {
		http.open('get', filename);
		http.onreadystatechange = function() {
			if (http.readyState == 4) {
				if (http.status == 200) {
					var s = formatText(http.responseText,direction);
					setText(objId,s);
					restart(objId,direction,speed);
					http=null;
				} else {
					//alert('Error: HTTP request for file '+filename+' failed with error '+http.status+'.');
				}
			}
		};
		http.send(null);
	}
}


function formatText(sIn,direction) {
	var sOut = '';
	if(sIn.indexOf('\n' != -1)) {
		var a = new Array();
		a = sIn.split('\n');
		for (var i=0;i<a.length;i++) {
			if (a[i].length>2) {
				if (direction=='horizontal') sOut += a[i] + '&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;&nbsp;';
				if (direction=='vertical') sOut += a[i] + '<br><br>';
			}
		}
    } else {
    	sOut = text;
    }
    return sOut;
}

