Sphere = {}
Sphere.Tracker = new function() {
    this.track = function ( ele, site, area ) {
        if( !ele.href ) return;
        var trackimg = new Image();
        trackimg.src = 'http://www.sphere.com/widgets/sphereit/tracker?site='+site+'&area='+area+'&href='+ele.href;
    }
}
Sphere.Widget = new function() {

    var SKELETON_MARKUP = '<iframe id="sphereiframeblock"></iframe><div class="widgetWrapper"><div class="roundTop"></div><div id="widget"><div class="headerBar"><div class="spherelink" id="spherelink">powered by<a href="http://www.sphere.com"><div id="SphereLogo"></div></a></div><h1>Related Blogs and Articles</h1><div onclick="Sphere.Widget.close()" class="closeBtn" title="Close"></div></div><div id="sphere_widget_loading" class="loadingArea"><div><img src="http://www.sphere.com/images/sphereit/LoadingGraphic.gif" alt="loading" />We\'re getting you the good stuff...</div></div><div id="sphere_widget_content" class="contentArea"></div></div><div id="roundBot"></div></div>';
    
    var CONTENT_URL = '/sphere/widgets/sphereit/content?\
siteid=marketwatch';
    if(document.characterSet) CONTENT_URL += ('&cset='+escape(document.characterSet));
    CONTENT_URL += '&url=';
    var widget; var widgetContent; var widgetLoading;
    var content;
    
    
    appendStylesheet();
    
    
    this.search = function( source ) {
        if( !content || (source && content.source!=source ) ) {
             requestContent( source );
             if( widget ){ widget.parentNode.removeChild(widget); content=null; widget=null; }
        }
        if( !widget ) draw();
        show();
        
        return false;
    }
    
    this.close = function() {
        if( widget ) widget.style.display ='none';
    }

    this.processContent = function (incomingContent) {
        content = incomingContent;
		//alert("processContent:"+content);
        if( !widgetContent ) return;
        widgetContent.innerHTML = content.content;
        widgetContent.style.height = widgetLoading.clientHeight +'px';
        widgetLoading.style.display = 'none';
        widgetContent.style.display = 'block';
         
            widgetContent.style.height = widgetContent.scrollHeight+'px';
            if( iecheck() ) { 
              ieahack(widget.getElementsByTagName("a"));
              document.getElementById("sphereiframeblock").style.height = (Number(widget.scrollHeight)-24)+'px';
              document.getElementById("sphereiframeblock").style.display = 'block';
            } 
            center();
                
    }

			function requestContent( local ) {
			if( window.XMLHttpRequest) {
			        	try {
			    			XHR = new XMLHttpRequest();
			            } catch( e ) { }
			        } else if( window.ActiveXObject ) {
			           	try {
			            	XHR = new ActiveXObject( "Msxml2.XMLHTTP" );
			          	} catch( e ) {
			            	try {
			              		XHR = new ActiveXObject( "Microsoft.XMLHTTP" );
			            	} catch( e ) { }
			    		}
			        }
			    	if( XHR ) {
			    		XHR.onreadystatechange = function() {
			    		    if( XHR.readyState == 4 ) {
			                    if( XHR.status == 200 ) {
									eval(XHR.responseText);
			                    }
			                    XHR = null;
			                }   
			    		};
			    		XHR.open( "GET", CONTENT_URL + escape(local || location.href), true );
			    		XHR.send( "" );
			    	}
		}
	    
    function zoomToFull() {
        //alert("zoomToFull");
        Animator.add( {
            process: function() {
                if( widgetContent.clientHeight >= widgetContent.scrollHeight-20) {
                    widgetContent.style.height = widgetContent.scrollHeight+'px';
                    if( iecheck() ) { 
                      ieahack(widget.getElementsByTagName("a"));
                      document.getElementById("sphereiframeblock").style.height = (Number(widget.scrollHeight)-24)+'px';
                      document.getElementById("sphereiframeblock").style.display = 'block';
                    } 
                    center();
                    return false;
                }
                widgetContent.style.height = (widgetContent.clientHeight + 20) +'px';
                center();
                return true;
            }
        });
    }
    
    function ieahack( col ) {
        for( var i=0; i<col.length; i++) {
            col[i].style.position = "relative";
        }
    }
    function center() {
        if( !widget || iecheck() ) return;
        windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
        windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
        widget.style.left = (windowWidth/2 - widget.scrollWidth/2) +'px';
        widget.style.top = (windowHeight/2 - widget.scrollHeight/2) +'px';

    }

    function show() {
        if( widget ) widget.style.display ='block';
        if( widget ) widget.style.visibility ='visible';
    }
        
    function draw() {
		
        if( SKELETON_MARKUP ) {
            widget = document.createElement( "div" );
            widget.id = "sphere_widget";
                widget.style.position = (iecheck())?'absolute':'fixed';   
            widget.innerHTML = SKELETON_MARKUP;
            document.body.appendChild( widget );
            alphaBackgrounds();
            center();
            window.onresize = center;
        }
        //alert("end draw");
        widgetContent = document.getElementById('sphere_widget_content');
        widgetLoading = document.getElementById('sphere_widget_loading');

		if( content ) Sphere.Widget.processContent( content );
    }
    
    function appendStylesheet() {
        stylesheet = document.createElement("link");
        stylesheet.rel = "stylesheet";
        stylesheet.type = "text/css";
        stylesheet.href = "http://www.sphere.com/widgets/sphereit/css?siteid=marketwatch";
        stylesheet.media = "all";
        document.lastChild.firstChild.appendChild(stylesheet);
    }
    
/*    function zIndexFlashFix() {
        var col = document.getElementsByTagName("embed");
        for( var i=0; i<col.length; i++) {
            var embed = col[i];
            alert(embed.src);
            embed.style.zIndex = "5";
            embed.parentNode.style.zIndex = "5";
            embed.parentNode.parentNode.style.zIndex = "5";
            //embed.setAttribute("wmode","opaque");

            //<PARAM value="#ffffff" name="bgcolor"/>
        }
    }*/
} 

Animator = new function() {
    var interval;
    var effects = [];
    
    this.add = function( effect ) {
        effects.push( effect );
        if( !interval ) start();
    }
    
    function remove() {
        effect = effects.pop();
        if( effect.after_process ) effect.after_process();
        if( effects.length==0 ) stop();
    }
            
    function start() {
        interval = setInterval(heartbeat, 1);
        //alert("Start Animator "+typeof(interval));
    }
    function stop() {
        //alert("stop animator");
        clearInterval( interval );
        interval = null;
    }
    
    function heartbeat() {
        if( effects.length==0 ) { stop(); return; }
        if( effects[0].process) {
            if( effects[0].process() ) effects[0].counter++;
            else remove();
        }
    }
}
function trace( val ) {
    var output = document.getElementById("output");
    if(output) output.innerHTML += (val + "<br/>");
}

function iecheck() {
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    	var iever = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7 );
    }
    return iever;
}

function alphaBackgrounds() {
    //alert("alphaBackgrounds");
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7 );
    	for (i=0; i<document.all.length; i++){
    		var bg = document.all[i].currentStyle.backgroundImage;
    		if (itsAllGood && bg){
    			if (bg.match(/[_Bot|_Mid|_Top|_Btn|_alpha_]\.png/i) != null){
    				var mypng = bg.substring(5,bg.length-2);
    				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
    				document.all[i].style.backgroundImage = "url('/widgets/sphereit/x.gif')";
    			}
    		}
    	}
    }   
}


