///////////////////////////
// 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.parentNode.scrollTop;
		visibleBottom = document.body.parentNode.offsetHeight;
	}
    for (i = 0; i < spans.length; i++)
	{
		var subChannel = document.getElementById("subMenu");
		
		if (spans[i].className == "quotedToolTipBox" && spans[i].style)
		{
		
			if ((((offsetTop - visibleTop) + spans[i].offsetHeight) > visibleBottom) &&
					((offsetTop - spans[i].offsetHeight) > visibleTop))
				{
					spans[i].style.top = "-" + (spans[i].offsetHeight + 3) + "px";
				}
			else
				{
					spans[i].style.top = "19px";
				}			
			
			// The 900 is to accomodate for the right hand nav
			//if ((offsetLeft + spans[i].offsetWidth) > (document.body.offsetWidth - 900) && (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");

            if(window.addEventListener)
            {
            eltSpan.addEventListener('mouseover',function() 
            {this.className = "quotedToolTipVisible";
            Quoted.showSponsorAd(this, symbol, true);
            Quoted.setPosition(this);
            Quoted.getChart(this);return true},true)
            
            eltSpan.addEventListener('mouseout',function() 
            {this.className = "quotedToolTip";return true;},true);
            }
            else
            {
                eltSpan.onmouseenter = function() 
			    {
			        this.className = "quotedToolTipVisible";
			        Quoted.showSponsorAd(this, symbol, false);
				    Quoted.setPosition(this);
				    Quoted.getChart(this);
				    return true;
			    };
			    eltSpan.onmouseleave = function() 
			    {
				    this.className = "quotedToolTip";
				    Quoted.killshowSponsorAd(this);
				    return true;
			    };	
			}
			
			var spanSymbol = document.createElement("span");
			spanSymbol.className = "qted symbol";
			var link = document.createElement("a");
			link.href = "http://www.marketwatch.com/tools/quotes/quotes.asp?symb=" + symbol;
			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;
				image.width = "0";
				image.height = "0";
			}
		}
	}
};

Quoted.killAd = function(element)
{
	element.innerHTML = "";
};

Quoted.changeAd = function(adUrl, element,w,h)
{
    var adcontent = '<iframe src="' + adUrl + '" name="framename" width="'+ w + '" height="'+ h + '" frameborder="0" border="0"  marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true">';
	if(navigator.userAgent.indexOf("Gecko")== -1)
	{
	    adcontent = adcontent + '<script language="JavaScript" src="' + adUrl + '"type="text/javascript"><\/script>';
    }
	adcontent = adcontent + '</iframe>';
	//adcontent = adcontent + "debug info:" + "<BR>" + "adURL =" + adUrl + "<BR>";
	element.innerHTML = adcontent;
};

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, symbol, changename)
{
    //default iframe size for advertisment
    var w = 220;
    var h = 32;
   
	var spans = element.getElementsByTagName("span");
	for (var i = 0; i < spans.length; i++)
	{
	    if (spans[i].className == "mwlivequotes")
	    {
	  
	     if (spans[i].getAttribute("mwsymbol") != "")
		    {
		   
		      var mwsymbol = spans[i].getAttribute("mwsymbol")
	        }
	    }
	}
	
	var random =(Math.floor(Math.random()*10000000))
	
	{
		
		var divs = element.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++)
		{
		
			if (divs[i].className == "qtPieSponsor")
			{
			
			if(changename == true)
		    {
		    divs[i].className = "qtPieSponsordone"
		    }
		    
			Quoted.changeAd("http://ad.doubleclick.net/adi/marketwatch.com/livequotes;ord=" + random + ";sz=" + w + "x" + h + ";symb=" + mwsymbol, divs[i],w,h);
			}
		}
	}
};

Quoted.killshowSponsorAd = function(element)
{
	var spans = element.getElementsByTagName("span");
	{
		var divs = element.getElementsByTagName("div");
		for (var i = 0; i < divs.length; i++)
		{
		
			if (divs[i].className == "qtPieSponsor")
			{
				Quoted.killAd(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 - 0.01;
		window.setTimeout('Quoted.onSetupDelay()', 500);
	}
};


Quoted.onPickupDelay=function()
{
	var manager = QuotedManager.current();
	manager.Pickup();
};

} 

Quoted();
