//--- external dependencies ---
// script HTML: element("orange.gif"), javascript array names[ticker] => title
// parent HTML: element("popupmenu")
// parent functions: drawChart(ticker,0), subtitle("title"), showM(eventX, eventY, msg), noM()
//                   drawThumbnails(symbols,title)

var maxItems = 20
var	curr = 0
var last = 0

//--- cookie ---

function getName(s)	{ return s.substr(0,s.indexOf("=")) }
function getStr(s)	{ return s.substr(s.indexOf("=")+1) }
function setCookie(sName, sValue) { document.cookie = sName + "=" + escape(sValue) + (sValue.length ? "; expires=Sat, 04-Sep-2010 23:59:59 GMT" : "; expires=Thu, 01-Jan-70 00:00:01 GMT") }
function getCookie(sName)
{
	var aCookie = document.cookie.split("; ")
	for (var i=0; i < aCookie.length; i++)
		if (sName == getName(aCookie[i])) 
			return unescape(getStr(aCookie[i]))

	return ""
}

//--- list management ---

function index(name)
{
	var str
	var firstEmpty = 3
	name = name.toLowerCase()+","
	for (var idx=0; (str = getCookie("l"+idx) && str.length) || idx < 3; idx++)
		if (str.indexOf(name) == 0)
			return idx
		else if (idx >= 3)
			firstEmpty = (str.length == 0)? idx : idx+1

	return firstEmpty
}

function add(list,symbol)
{
	var name = list+","
	symbol = symbol.toUpperCase()

	var cookie = name+symbol
	var idx = index(list)

	{
		var str = getCookie("l"+idx)
		if (str.length)
		{
			var items = str.substr(name.length)+","
			var symbols = items.split(",")

			for (var i = 0; i < symbols.length && i < maxItems; i++)
				if (symbols[i] != symbol)
					cookie += "," + symbols[i]

		}
		setCookie("l"+idx, cookie)
		lookup(symbol)
	}
}

var menu
var theTop = 0
var old = theTop
var ie = document.all
var ns6=document.getElementById&&!document.all
var opera=navigator.userAgent.indexOf("Opera")!=-1

function getEl(id)	{ return ie ? document.all[id] : document.getElementById(id) }

//--- popup menu within tickers.html --- 

function getX(el)
{
	var x = 0
	while (el != null)
	{
		x += el.offsetLeft
		el = el.offsetParent
	}
	return x
}

function getY(el)
{
	var y = 0
	while (el != null)
	{
		y += el.offsetTop
		el = el.offsetParent
	}
	return y
}

function showmenu(e,msg)
{
	var thisWin = parent.getEl("tickers")

	eventX = getX(thisWin) + (ie? event.clientX : ns6? e.clientX : e.x) + 8
	eventY = getY(thisWin) + (ie? event.clientY : ns6? e.clientY : e.y)

	// adjust for any scrolling in parent window
	offsetY = (ie? parent.document.body.scrollTop : ns6? parent.pageYOffset : 0)
	eventY -= offsetY

	parent.showM(eventX, eventY, msg)
}

//--- marker/return ORANGE.GIF --- 

function init(name)
{
	menu = getEl(name)
	setTimeout('moveExitSign()',200)
}

function moveExitSign()
{
	if (window.innerHeight)
		  pos = window.pageYOffset
	else if (document.documentElement && document.documentElement.scrollTop)
		pos = document.documentElement.scrollTop
	else if (document.body)
		  pos = document.body.scrollTop

	if (pos < theTop) pos = theTop
	if (pos == old)
		menu.style.top = pos
	old = pos
	setTimeout('moveExitSign()',200)
}

//--- link/symbol management ---

function go4(ticker, special)
{
	// unhilite the current symbol, them move highlight to the newly active
	if (last != 0) document.links[last].className = ""
	last = curr
	document.links[curr].className = "curr"

	// call parent to draw chart, compose URLs
	if (special != 1)
		parent.drawChart(ticker,0)

	// is this perhaps futures or forex page? use the symbol to the left of the link for text
	var prev = document.links[curr].previousSibling
	if (prev.nodeType == 3)
	{
		var title = prev.nodeValue
		if ((stopAt = title.lastIndexOf('(')) != -1)
			parent.subtitle (title.substring (0, stopAt))

		// lookup name in dictionary or call back to server to suggest a name for this ticker symbol
		else
			lookup(ticker)
	}
}

function go2(idx)
{
	curr = idx
	go4(document.links[idx].innerHTML,0)
}

function skip(n)
{
	if (curr >= document.links.length)
		curr = 1
	else if (curr <= 0)
		curr = document.links.length - 1
	ti = document.links[curr].href
	if (ti.indexOf("go2") == -1)
		curr += n
	go2(curr)
}

function next() { curr++; skip(1)  }
function prev() { curr--; skip(-1) }

//--- thumbnail gallery ---

function thumbnails(title,first,last)
{
	var symbols = ""
	for (i = first; i <= last; i++)
		symbols += document.links[i].innerHTML + (i == last ? "" : " ")

	parent.drawThumbs(symbols, title)
}

//--- XMLHttp asyncronous request ---

var xmlReq
var lastTicker

function lookup(ticker)
{
	// has this symbol been defined in the names Array?  If so, use that description text
	if (typeof names != "undefined" && names != null && names[ticker] != undefined)
		parent.subtitle (names[ticker])

	// call back to server to suggest a name for this ticker symbol
	else
	{
		if (xmlReq == null) 
			xmlReq = new xmlHttp()

		lastTicker = ticker
		try {
			if (xmlReq)
				xmlReq.connect("GET", "look4", "name&q=" + ticker, xmlName)
		} catch (e)
		{
			xmlReq = null
			parent.subtitle(ticker)
		}
	}
}

function xmlName(get)
{
	var title = get.responseText
	if (get.status == 200 && title.length)
		parent.subtitle(title)
	else
		parent.subtitle(lastTicker)
}

function xmlHttp()
{
	var xmlhttp
	var bComplete = false
	try { xmlhttp = new XMLHttpRequest() }
	catch (e) { try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP") }
	catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP") }
	catch (e) { xmlhttp = false }}}
	if (!xmlhttp) return null

	this.connect = function(sMethod, sURL, sVars, fnDone, bAsynch)
	{
		if (!xmlhttp) return false
		bComplete = false
		sMethod = sMethod.toUpperCase()

		if (bAsynch == null) bAsynch = true //treat asynch as an optional argument

		try {
			if (sMethod == "GET")
			{
				if (sVars.length) sURL += "?"+sVars
				xmlhttp.open(sMethod, sURL, (bAsynch == true))
				sVars = ""
			}
			else
			{
				xmlhttp.open(sMethod, sURL, (bAsynch == true))
				xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1")
				xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
			}

			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == 4 && !bComplete)
				{
					bComplete = true
					fnDone(xmlhttp)
				}
			}
			xmlhttp.send(sVars)

			// Firefox <= 2.0.0 doesn't fire onreadystatechange for synchronous requests.
			// See http://lukav.com/wordpress/2007/04/12/firefox-firebug-and-synchronos-calls-problem/
			var isGecko = (document.addEventListener) ? true : false
			try {
				if (!bAsynch && isGecko && xmlhttp.onreadystatechange == null) {
					bComplete = true
					fnDone(xmlhttp)
					return true
				}
			}
			catch (e) { return false }
		}
		catch(z) { return false }
		return true
	}
	return this
}