///////////////////////////
// quoted api (namespace)

function Quoted() 
{


Quoted.hideAllTips = function()
{
	var arrSpans = document.getElementsByTagName("span");
	for(var i = 0; i < arrSpans.length; ++i)
	{
		var eltSpan = arrSpans[i];
		if (eltSpan.className == "quotedToolTipVisible")
			eltSpan.className = "quotedToolTip";
	}
	return true;
}

Quoted.setPosition = function(element)
{
    var offsetTrail = element;
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    var visibleTop = 0;
    var visibleBottom = 0;
    var spans = element.getElementsByTagName("span");
    var i;
    if (window.innerHeight)
	{
		visibleTop = window.pageYOffset;
		visibleBottom = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		visibleTop = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		visibleTop = document.body.scrollTop
		visibleBottom = document.body.offsetHeight;
	}
    for (i = 0; i < spans.length; i++)
	{
		var subChannel = document.getElementById("subMenu");
		if (spans[i].className == "quotedToolTipBox" && spans[i].style)
		{
			// For some reason IE is giving the wrong value for offsetTop, that's why the adjustments (100 and 150) are there.  No idea.
			if ((((offsetTop - visibleTop) + spans[i].offsetHeight) > visibleBottom)
				&& ((offsetTop - spans[i].offsetHeight - 100) > visibleTop)
				&& ((offsetTop - spans[i].offsetHeight - 150) > (subChannel.offsetTop + subChannel.offsetHeight)))
			{
				spans[i].style.top = "-" + (spans[i].offsetHeight + 3) + "px";
			}
			else
			{
				spans[i].style.top = "19px";
			}
			// The 375 is to accomodate for the ad and such
			if ((offsetLeft + spans[i].offsetWidth) > (document.body.offsetWidth - 375) && (offsetLeft - spans[i].offsetWidth) > 0)
			{
				spans[i].style.left = ((element.offsetWidth - spans[i].offsetWidth) - 4) + "px";
			}
			else
			{
				spans[i].style.left = "0";
			}
		}
	}
}

Quoted.setup = function()
{

	var arrSpans = document.getElementsByTagName("span");
	for(var i = 0; i < arrSpans.length; ++i)
	{
		var eltSpan = arrSpans[i];
		var toolTip = eltSpan.getAttribute("quotedToolTip");
		if (eltSpan.className == "quotedToolTip")
		{
			var symbol = eltSpan.getAttribute("quoted");
			eltSpan.removeAttribute("quoted");
			eltSpan.onmouseover = function() 
			{
				this.className = "quotedToolTipVisible";
				Quoted.setPosition(this);
				Quoted.getChart(this);
				return true;
			};
			eltSpan.onmouseout = function() 
			{
				this.className = "quotedToolTip";
				return true;
			};	
			var spanSymbol = document.createElement("span");
			spanSymbol.className = "qted symbol";
			var link = document.createElement("a");
			link.href = "http://www.marketwatch.com/tools/quotes/detail.asp?view=detail&symb=" + symbol;
			link.onmouseover = function()
			{
				Quoted.showSponsorAd(this);
				return true;
			}
			link.onmouseout = function()
			{
				Quoted.doPixelTracking(this);
				return true;
			};
			link.appendChild(document.createTextNode(symbol));
			spanSymbol.appendChild(link);
			eltSpan.appendChild(spanSymbol);
		}
	}
}

Quoted.doPixelTracking = function(element)
{
	var quotedToolTip = element.parentNode.parentNode;
	if (quotedToolTip.className == "quotedToolTipVisible")
	{
		var images = quotedToolTip.getElementsByTagName("img");
		var now = new Date();
		var seed = now.getTime();
		var random = Math.random(seed);
		for (var i = 0; i < images.length; i++)
		{
			var image = images[i];
			if (image.className == "pixelTracking")
			{
				image.src = "/tpx/storyquotetip.tpx?random=" + random;
			}
		}
	}
}

Quoted.changeAd = function(adUrl, element)
{
	var request = Quoted.getRequest();
		
	try
	{
		request.open("GET", adUrl, true);		
		request.onreadystatechange = function() {
			if (request.readyState == 4) 
			{
				var responseAd = request.responseText;

				if(responseAd != null && responseAd.length > 0)
				{
					element.innerHTML = responseAd;
				}		
			}
		}

		request.send(null);	
	}
	catch(sendException) 
	{ 		
	}
}

Quoted.getRequest = function()
{
	var xmlHttp = false;
	
	try 
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (msxml2Exception) 
	{
		try 
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch (microsoftException) 
		{
			xmlHttp = false;
		}
	}
	
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
	{
		xmlHttp = new XMLHttpRequest();
	}
	
	return xmlHttp; 
}

Quoted.showSponsorAd = function(element)
{
	var quotedToolTip = element.parentNode.parentNode;
	if (quotedToolTip.className == "quotedToolTip")
	{
		var divs = quotedToolTip.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++)
		{
			if (divs[i].className == "qtPieSponsor")
			{
				Quoted.changeAd("http://www.marketwatch.com/RealMedia/ads/adstream_sx.cgi/cbs.marketwatch.com/mutualfundsrollover.asp@x15", divs[i]);
			}
		}
	}
}

Quoted.getChart = function(element)
{
	var images = element.getElementsByTagName("img");
	var i;
	for (i = 0; i < images.length; i++)
	{
		if (images[i].className == "quoteTipChart")
		{
			if (images[i].getAttribute("chartAddress") != "")
			{
				images[i].src = images[i].getAttribute("chartAddress");
				images[i].setAttribute("chartAddress", "");
			}
		}
	}
}

Quoted.onSetupDelay=function()
{
	var manager = QuotedManager.current();
	var quotedElements = manager.getQuotedElements();
	for(var i = 0; i != quotedElements.length; ++i)
	{
		quotedElements[i].Display(Quoted._value);
	}
	if (Quoted._value > -2)
	{
		Quoted._value = Quoted._value - .01;
		window.setTimeout('Quoted.onSetupDelay()', 500);
	}
}


Quoted.onPickupDelay=function()
{
	var manager = QuotedManager.current();
	manager.Pickup();
}

}; 

Quoted();
