﻿if (typeof MBD === "undefined")
{
	var MBD = {};
}

MBD.NewsTickerItem = function(text, link)
{
	this.text = text;
	this.link = link || null;
};

MBD.NewsTicker = function()
{
	var m_iDelay = 4500;
	
	var m_elContainer = null;
	var m_iCurItem = 0;
	var m_arItems = [];
	
	var m_elPrevious = null;
	var m_elCurrent = null;
	var m_ivalTransition = null;
	var m_twAnimIn = null;
	var m_twAnimOut = null;
	
	var m_fnStartTicker = function()
	{
		m_elContainer.innerHTML = "";
		
		m_elPrevious = document.createElement("div");
		m_elPrevious.id = "news-ticker-current";
		m_elPrevious.innerHTML = m_arItems[0].text;
		
		m_elCurrent = document.createElement("div");
		m_elCurrent.id = "news-ticker-next";
		m_elCurrent.innerHTML = m_arItems[0].text;
		
		m_elContainer.appendChild(m_elPrevious);
		m_elContainer.appendChild(m_elCurrent);
		
		setTimeout("MBD.NewsTicker.nextItem()", m_iDelay);
	}
	
	return {
		
		init : function()
		{
			var elUSP;
			var arEls;
			var el;
			var i;
			
			m_elContainer = document.getElementById("news");
			
			if (!m_elContainer) return;
			
			// add the current heading
			m_arItems.push(new MBD.NewsTickerItem(document.getElementById("headMain").innerHTML));
			
			// add the links
			arEls = m_elContainer.getElementsByTagName("ul")[0].getElementsByTagName("a");
			
			for (i = 0; i < arEls.length; i++)
			{
				el = arEls[i];
				
				m_arItems.push(new MBD.NewsTickerItem(el.firstChild.nodeValue, el.href));
			}
			
			// add the usps
			elUSP = document.getElementById("usp");
			arEls = elUSP.getElementsByTagName("li");
			
			for (i = 0; i < arEls.length; i++)
			{
				m_arItems.push(new MBD.NewsTickerItem(arEls[i].firstChild.nodeValue));
			}
			
			elUSP.parentNode.removeChild(elUSP);
			
			if (m_arItems.length > 1)
			{
				m_fnStartTicker();
			}
			
		},
		
		nextItem : function()
		{
			m_iCurItem++;
			
			if (m_iCurItem > m_arItems.length - 1)
			{
				m_iCurItem = 0;
			}
			
			// animate current item out
			m_elPrevious.innerHTML = m_elCurrent.innerHTML;
			
			if (m_twAnimOut)
			{
				m_twAnimOut.fforward();
				m_twAnimOut.stop();
			}
			
			m_twAnimOut = new Tween(
				m_elPrevious.style,
				"marginTop",
				Tween.regularEaseOut,
				0,
				40,
				0.5,
				"px"
			);
			
			m_twAnimOut.start();
			
			// animate next item in
			if (m_arItems[m_iCurItem].link)
			{
				m_elCurrent.innerHTML = "<a href=\"" + m_arItems[m_iCurItem].link + "\">" + m_arItems[m_iCurItem].text + "</a>";
			}
			else
			{
				m_elCurrent.innerHTML = m_arItems[m_iCurItem].text;
			}
			
			if (m_twAnimIn)
			{
				m_twAnimIn.fforward();
				m_twAnimIn.stop();
			}
			
			m_twAnimIn = new Tween(
				m_elCurrent.style,
				"marginLeft",
				Tween.regularEaseOut,
				300,
				0,
				0.5,
				"px"
			);
			
			m_twAnimIn.start();
			
			setTimeout("MBD.NewsTicker.nextItem()", m_iDelay);
		}
		
	}
}();