function Scroller(container) {
	this.use_container = container;
	this.scrollAmount = 2;
	this.scrollDelay = 110;
	this.startAt = 0;
	this.runId = 0;
	this.running = false;
	this.paused = false;
	this.startDelay = 5000;
	this.endDelay = 5000;
	
	this.setDimensions = function (dir, myDim, parDim) {
		this.dir = (dir ? dir.toLowerCase() : "");
		this.dirsign = 0;
		this.mydir = "";
		
		if (this.dir == "up" || this.dir == "down") {
			this.mydir = "v";
			this.dirsign = this.dir == "up" ? -1 : 1;			
		}
		else if (this.dir == "left" || this.dir == "right") {
			this.mydir = "h";
			this.dirsign = this.dir == "left" ? -1 : 1;
		}
		else return false;
		
		this.endAt = myDim;
		this.visibleP = parDim;
		
		this._move();
	}
	this.start = function () {
		if (!this.running) this.stop();
		this.move();
	}
	this.stop = function () {
		if (this.runId != 0) clearTimeout(this.runId);
		this.runId = 0; 
		this.running = false; 
		this.paused = false;
		this.startAt = 0;
		this._move();
	}
	this.pause = function () {
		if (this.runId != 0) clearTimeout(this.runId);
		this.runId = 0; 
		this.paused = true;
	}
	this.resume = function () {
		var MyThis = this, lambda;
		
		if (this.runId != 0) clearTimeout(this.runId);
		this.runId = 0;
		this.paused = false;
				
		lambda = function () { MyThis.move(true); };
		this.runId = window.setTimeout(lambda, this.scrollDelay);
	}
	this._move = function () {
		var useStart = this.startAt, computed, ended = false;
		
		if (this.startAt >= (this.endAt - this.visibleP)) {
			useStart = (this.endAt - this.visibleP);
			ended = true;
		}
		
		computed =  useStart < 0 ? 0 : ( this.dirsign == -1 ? useStart * -1 : ((this.endAt - this.visibleP)- useStart) );
		
		if (this.mydir == "v") {
			this.use_container.style.top = computed + "px";
		}
		else if (this.mydir == "h") {
			this.use_container.style.left = computed + "px";
		}
		else return false;
		
		if (useStart < 0) {
			useStart = 0;
			ended = true;
		}
		
		this.startAt = useStart;
		return ended;
	}
	this.move = function (nocheck) {
		var MyThis = this, lambda;
		
		clearTimeout(this.runId);
		this.runId = 0;
		
		if (!this.running && !nocheck) {
			if (this.startDelay != 0) {
				lambda = function () { MyThis.move(true); };
				this.runId = window.setTimeout(lambda, this.startDelay);
				return;
			}
		}
		
		this.startAt += this.scrollAmount;
		
		if (this._move()) {
			this.running = false;
			this.paused = false;
			
			if (this.onfinish) {
				if (this.endDelay == 0) this.onfinish();
				else {
					lambda = function () { MyThis.onfinish(); };
					this.runId = window.setTimeout(lambda, this.endDelay);
				}
			}
		}
		else {
			this.running = true;
			this.paused = false;
			
			lambda = function () { MyThis.move(true); };
			this.runId = window.setTimeout(lambda, this.scrollDelay);
		}
	}	
}

function newsMarquee() {
	//
}

newsMarquee.prototype = {
	ajax: null, runId: 0, Height: 0, marquee: null, news: null, oItem: null, clicked: false, currentsel: null,
	errorLoadDelay: 10000, _varname: null, _varpos: null, source_url: null, intro_container_id: null,
	containers: {display: null, preview: null, intro: null, url: null}, query: null,

	init: function (source_url, url_query, display_container, preview_container, intro_container, url_container) {
		$Lib.createGlobalVarName(this); 
		var MyThis = this, oInnerItem;		
		
		this.runId = 0;
		this.setNewsSource(source_url, url_query);
		
		this.ajax = new MyAjaxDoc(false);
		this.ajax.addDataHandler('_handleData', this);
		this.ajax.setErrorHandler('_handleError', this);
		this.ajax.useP2J(true); //Set to JS Parser.
		
		this.containers.display = document.getElementById(display_container);
		this.containers.preview = document.getElementById(preview_container);
		this.intro_container_id = intro_container;
		this.containers.url = document.getElementById(url_container);

		this.marquee = new Scroller(this.containers.display);
		this.marquee.onfinish = function () { MyThis._finished(); };

		this.oItem = document.createElement("DIV");
		oInnerItem = document.createElement("DIV");

		this.oItem.style.width = "175px"; this.oItem.style.height = "20px";
		this.oItem.style.width = "175px"; this.oItem.style.borderBottom = "1px solid #DDDDDD";

		this.oItem.onmouseover = 
		function () { 
			if (MyThis.marquee.running) MyThis.marquee.pause(); 
			this.style.backgroundColor = "#DDEEDD"; 
		};
  		this.oItem.onmouseout = 
		function () { 
			if (MyThis.marquee.running && MyThis.marquee.paused && !MyThis.clicked) MyThis.marquee.resume(); 
			if (MyThis.currentsel != this) this.style.backgroundColor = "transparent"; 
		};

		try { this.oItem.style.background = "content/images/website/line.gif"; } catch (e) { }

		oInnerItem.style.overflow = "hidden"; oInnerItem.style.height = "14px"; oInnerItem.style.width = "175px";
		oInnerItem.style.cursor = "default";
		try { oInnerItem.style.cursor = "hand"; } catch (e) { }
  		try { oInnerItem.style.background = "content/images/website/line.gif"; } catch (e) { }

		this.oItem.appendChild(oInnerItem);

	},
	getNews: function (delay) {
		var MyThis = this, lambda;
		if (this.runId != 0) clearTimeout(this.runId);
		this.runId = 0;
		
		if (delay) {
			lambda = function () { MyThis.getNews(false); };
			this.runId = window.setTimeout(lambda, this.errorLoadDelay);
		}
		else {
			this.ajax.load(this.source_url, "get", this.queryString(), null);
		}
	},
	setNewsSource: function (source_url, url_query) {
		this.source_url = source_url;		
		this.query = $Lib.objConcat(url_query);
	},
	queryString: function () {
		this.query["_rand_key"] = $Lib.genUID(20, 8, 9);
		return $Lib.buildQuery(this.query);
	},
	//Some Handlers
	_handleData: function (docobj, JSOBJ, ajax) {
		if (typeof JSOBJ.news != 'undefined' && typeof JSOBJ.news_count != 'undefined' && JSOBJ.news_count > 0) {
			this.Height = (20 * JSOBJ.news_count) + (JSOBJ.news_count * 1);
			this.news = JSOBJ.news;
			this._addNews();
			this.marquee.setDimensions("up", this.Height, 160);
			this.marquee.stop();
			this.marquee.start();
		}
		else this.getNews(true);
	},
	_addNews: function () {
		var i, oItem, oInnerItem, MyThis = this;
		this.containers.display.innerHTML = "";

		for (i in this.news) {
			oItem = this.oItem.cloneNode(true);
			oItem.onmouseover = this.oItem.onmouseover;
  			oItem.onmouseout = this.oItem.onmouseout;
            oItem.onclick = new Function("", this._varname + ".click_news(" + i + ", this);");
            oItem.title = this.news[i].TITLE;

			oInnerItem = oItem.firstChild;
			oInnerItem.innerHTML = this.news[i].TITLE;
			//alert(this.news[i].TITLE);
			this.containers.display.appendChild(oItem);
		}
	},
	_handleError: function (docobj, Msg, ajax) {		
		if ($Lib.DEBUG) alert("News Error: \n" + Msg);
		this.getNews(true);
	},
	_finished: function () {
		this.getNews();
		this.marquee.start();
	},
	hide_news: function () {
		try {
			this.clicked = false;
			
			try { this.currentsel.style.backgroundColor = "transparent"; } catch (e) { }
			try { this.containers.intro.parentNode.removeChild(this.containers.intro); } catch (e) { }
			this.currentsel = null;
			this.containers.intro = null;
			
			this.containers.preview.style.display = "none";
			this.marquee.resume();
		}
		catch (e) { }
	},
	click_news: function (nodeid, currentsel) {
		var coll, i;
		
		this.clicked = true;
		
		if (this.currentsel && this.currentsel != currentsel) {
			try { this.currentsel.style.backgroundColor = "transparent"; } catch (e) { }
		}
		try { this.containers.intro.parentNode.removeChild(this.containers.intro); } catch (e) { }
		
		this.currentsel = currentsel;
		
		this.marquee.pause();
		var news = this.news[nodeid];
		
		this.containers.intro = document.createElement("DIV");
		this.containers.intro.id = this.intro_container_id;
		
		this.containers.intro.innerHTML =
		"<strong>" + news.TITLE + "</strong><br />" + news.DESCRIPTION +
		"<br />Pub Date: " + news.PUB_DATE + "<br /><hr />" +
		"Category: " + news.CATEGORY + "<br />" +
		(news.SOURCE_URL ? 
		'Provider: <a href="' + news.SOURCE_URL + '" target="_blank">' + news.SOURCE + '</a>' :
		'Provider: ' + news.SOURCE);
		
		coll = this.containers.intro.getElementsByTagName("A");
		for (i = 0; i < coll.length; i++) coll[i].target = "_blank";
		
		this.containers.intro = this.containers.preview.insertBefore(this.containers.intro, this.containers.preview.firstChild);
		
		this.containers.url.href = news.LINK;
		this.containers.preview.style.display = "block";
	}
};
/*addEvent(d, "finish", fin);
var x = new Array();
for (i in d) x.push('this.' + i + ' = ' + (typeof d[i] == 'string' ? "'" + d[i] + "'" : d[i]));
y.value = x.join("\n");*/
