var latestNewsSlider = new LatestNewsSlider(), latestNewsIsOpened = false;
window.attachEvent("onload", function() {
	latestNewsSlider.Initialize();
	latestNewsSlider.Start();
});

function LatestNewsSlider() {
	var containerHeight = 0, isInitialized = false, stopSlide = false, target1 = 0, target2 = 0, timer;
	this.Container = null;
	this.Delay = 50;
	this.IsRunning = false;
	this.IsWaiting = true;
	this.ItemHeight = 0;
	this.Step = 1;
	this.WaitDelay = 5000;
	this.Initialize = function() {
		if(isInitialized) return;
		this.Container = document.getElementById("LatestArticleContainer");
		this.ItemHeight = this.Container.childNodes.item(0).scrollHeight;
		with(document.createElement("div")) {
			style.position = "absolute";			
			style.top = "0px";
			innerHTML = this.Container.innerHTML;
			this.Container.innerHTML = outerHTML + outerHTML;
		}
		containerHeight = target2 = parseInt(this.Container.childNodes.item(0).scrollHeight);
		this.Container.childNodes.item(1).style.top = target2 + "px";
		isInitialized = true;
	}
	this.MoveNext = function() {
		if(!this.IsWaiting) return;
		target1 -= this.ItemHeight;
		target2 -= this.ItemHeight;
		if(target1 < -containerHeight) {target1 += containerHeight * 2; this.Container.childNodes.item(0).style.top = (target1 + this.ItemHeight) + "px";}
		if(target2 < -containerHeight) {target2 += containerHeight * 2; this.Container.childNodes.item(1).style.top = (target2 + this.ItemHeight) + "px";}
		this.Slide();
	}
	this.Reset = function() {
		stopSlide = true;
		this.Stop();
		this.Container.childNodes.item(0).style.top = (target1 = 0) + "px";
		this.Container.childNodes.item(1).style.top = (target2 = -containerHeight) + "px";
	}
	this.Slide = function() {
		if(stopSlide) {this.IsWaiting = true; stopSlide = false; return;}
		this.IsWaiting = false;
		var top1 = Math.max(parseInt(this.Container.childNodes.item(0).style.top) - this.Step, target1);
		var top2 = Math.max(parseInt(this.Container.childNodes.item(1).style.top) - this.Step, target2);
		this.Container.childNodes.item(0).style.top = top1 + "px";
		this.Container.childNodes.item(1).style.top = top2 + "px";
		if(top1 == target1) this.IsWaiting = true;
		else {var t = this; window.setTimeout(function() {t.Slide();}, this.Delay);}
	}
	this.Start = function() {
		if(!isInitialized || this.IsRunning) return;
		var t = this;
		this.IsRunning = true;
		timer = window.setInterval(function() {t.MoveNext();}, this.WaitDelay);
	}
	this.Stop = function() {
		if(!this.IsRunning) return;
		window.clearInterval(timer);
		this.IsRunning = false;
	}
}

function ToggleLatestNews() {
	var img = event.srcElement;
	if(latestNewsIsOpened) {
		with(latestNewsSlider.Container.style) {
			backgroundImage = "none";
			height = latestNewsSlider.ItemHeight + "px";
			position = "relative";
			top = "0px";
		}
		latestNewsSlider.Start();
		img.src = "/images/buttons/collapse/down.gif";
		latestNewsIsOpened = false;
	}
	else {
		latestNewsSlider.Reset();
		with(latestNewsSlider.Container.style) {
			backgroundImage = "url(/images/background/latestnews.png)";
			height = parseInt(latestNewsSlider.Container.childNodes.item(0).scrollHeight) + "px";
			position = "absolute";
			top = "0px";
		}
		img.src = "/images/buttons/collapse/right.png";
		latestNewsIsOpened = true;
	}
}
