/* -- BEGIN SPHERE JS -- */      

var sphere_account="aolsphere";

Sphere = {}
Sphere.Browser = new function() {
	var ua, s, i;

	this.isIE = false;
	this.isNS = false;
	this.version = null;

	ua = navigator.userAgent;

	s = "MSIE";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isIE = true;
	  this.version = parseFloat(ua.substr(i + s.length));
	  return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isNS = true;
	  this.version = parseFloat(ua.substr(i + s.length));
	  return;
	}

	// Treat any other "Gecko" browser as NS 6.1.

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
	  this.isNS = true;
	  this.version = 6.1;
	  return;
	}	
}
Sphere.Tracker = new function() {
    this.track = function ( ele, site, area ) {
        if( ! _proxy_jslib_handle(ele, 'href', '', 0, 0) ) return;
        
        //var imgurl = 'http://stats.sphere.com/widgets/sphereit/?action=click&siteid='+site+'&q='+area+'&url='+ele.href;
        //var trackimg = new Image();
        //trackimg.src = imgurl;
        //wait(150); 
        sphereom = new (SphereOmniture)();
	    sphereom.track(area, area);    

    }
}



Sphere.Widget = new function() {


	var SKELETON_MARKUP = '<iframe id="sphereiframeblock"></iframe> <div class="widgetWrapper"> <div class="roundTop"></div> <div id="widget"> <div id="sphere_widget_header" class="headerBar"><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/widgets/sphereit/images/LoadingGraphic.gif" alt="loading" />We\'re getting you the good stuff...</div> </div> <div id="sphere_widget_content" class="sphereMainContentArea"></div> <div class="footerBar"> <a target="_blank" href="http://www.sphere.com/tools?requestform=1" class="getThis">Get this Widget for Your Site &raquo;</a> &nbsp;&nbsp;&nbsp;<a class="footerBarSphereLogo" href="http://www.sphere.com" target="new"><img class="footerBarSphereLogo" src="http://www.sphere.com/widgets/sphereit/images/SphereAol.png"></a> &nbsp;&nbsp;&nbsp;</div> </div> <div id="roundBot"></div> </div>';


        var CONTENT_URL = 'http://www.sphere.com/widgets/sphereit/content?siteid=oreilly';
        if(document.characterSet) CONTENT_URL += ('&cset='+escape(document.characterSet));
        CONTENT_URL += '&CXNID=1000009.430098529NXC&url=';
        var widget; var widgetContent; var widgetLoading;
        var content;
		var dragObj = {};
        
        

                
               if( _proxy_jslib_handle(document, 'getElementsByTagName', '', 1, 0)('base').length>0)  { 
                if (window.addEventListener){
                  window.addEventListener('load', appendStylesheet, false); 
                } else if (window.attachEvent){
                  window.attachEvent('onload', appendStylesheet);
                }  
               } else {
                appendStylesheet();
            }
                
            
        if (Math.random() <= 0.1) {
           // var imgurl = 'http://stats.sphere.com/widgets/sphereit/?action=page&siteid=oreilly';
           // var trackimg = new Image();
           // trackimg.src = imgurl;
             try{
		  	sphereom = new (SphereOmniture)();
	        sphereom.track('popup_view', 'oreilly_popup_view');
	  
			}catch(anErr){
			}
        }
        
                
        
                
        
    	 _proxy_jslib_assign('', this, 'search', '=', ( function( source ) {
        if( ! _proxy_jslib_handle(null, 'content', content, 0, 0) || (source &&  _proxy_jslib_handle(null, 'content', content, 0, 0).source!=source ) ) {
            requestContent( source );
			dragObj.elNode = null;
            if( widget ){ widget.parentNode.removeChild(widget);  content= _proxy_jslib_assign_rval('', 'content', '=', (null), content); widget=null; }
        }
        if( !widget ) draw();
        show();
        /*
        var reqimgurl = 'http://stats.sphere.com/widgets/sphereit/?action=display&siteid=oreilly';
        var reqimg = new Image();
        reqimg.src = reqimgurl;
        
        if (reqimg.style) {
            // In Safari 2.0.x, reqimg has no style until it's appended, so skip this (fixed in Safari 3)
            reqimg.style.display = 'none'; 
            document.body.appendChild(reqimg);
        }
        */
        sphereom = new (SphereOmniture)();
		sphereom.track('popup_search', 'oreilly_popup_search');
	
    
        return false;
    }))
 
  _proxy_jslib_assign('', this, 'close', '=', ( function() {
        if( widget ) widget.style.display ='none';
    }))

 this.processContent = function (incomingContent) {
         content= _proxy_jslib_assign_rval('', 'content', '=', ( incomingContent), content);
        for(i=0;i<1000;i++){ test='444'};
        if( !widgetContent ) return;
         _proxy_jslib_assign('', widgetContent, 'innerHTML', '=', (  _proxy_jslib_handle( _proxy_jslib_handle(null, 'content', content, 0, 0), 'content', '', 0, 0)));
        widgetContent.style.height = widgetLoading.clientHeight +'px';
        widgetLoading.style.display = 'none';
        widgetContent.style.display = 'block';
                    zoomToFull();
                
    }

            function requestContent( local ) {
            //alert("requestContent");
            Sphere.Widget.script = document.createElement('script');
             _proxy_jslib_assign('', Sphere.Widget.script, 'src', '=', ( CONTENT_URL + escape(local ||  _proxy_jslib_handle( _proxy_jslib_handle(null, 'location', location, 0, 0), 'href', '', 0, 0))));
             _proxy_jslib_handle(null, 'setTimeout', setTimeout, 1, 0)(function() {   _proxy_jslib_handle(document, 'body', '', 0, 0).appendChild(Sphere.Widget.script);}, 1);
        }
        
    function zoomToFull() {
        //alert("zoomToFull");
        Animator.add( {
            process: function() {
                if( widgetContent.clientHeight >= widgetContent.scrollHeight-20) {
                    widgetContent.style.height = widgetContent.scrollHeight+'px';
                    if( iecheck() ) { 
                      ieahack( _proxy_jslib_handle(widget, 'getElementsByTagName', '', 1, 0)("a"));
                       _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("sphereiframeblock").style.height = (Number(widget.scrollHeight)-24)+'px';
                       _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("sphereiframeblock").style.display = 'block';
                      alphaBackgrounds();   
                    } 
                    center();
                    return false;
                }
                widgetContent.style.height = (widgetContent.clientHeight + 20) +'px';
                center();
                return true;
            }
        });
    }
    
    function ieahack( col ) {
        for( var i=0; i<col.length; i++) {
             _proxy_jslib_handle(col, (i), 0, 0).style.position = "relative";
        }
    }

    function center() {
        if( !widget || (iecheck((document.compatMode=='BackCompat')?true:false)) ) return;
		if( dragObj.elNode != undefined) return;
        windowHeight = window.innerHeight || document.documentElement.clientHeight ||  _proxy_jslib_handle(document, 'body', '', 0, 0).clientHeight;
        windowWidth = window.innerWidth || document.documentElement.clientWidth ||  _proxy_jslib_handle(document, 'body', '', 0, 0).clientWidth;       
        widget.style.left = (windowWidth/2 - widget.scrollWidth/2) +'px';
         _proxy_jslib_assign('', 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((document.compatMode=='BackCompat')?true:false))?'absolute':'fixed';     
             _proxy_jslib_assign('', widget, 'innerHTML', '=', ( SKELETON_MARKUP));
             _proxy_jslib_handle(document, 'body', '', 0, 0).appendChild( widget );
            alphaBackgrounds();
            center();
            window.onresize = center;
        }
        //alert("end draw");
        widgetContent =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('sphere_widget_content');
        widgetLoading =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('sphere_widget_loading');
		widgetHeader =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)('sphere_widget_header');
		
	    if (Sphere.Browser.isIE) {
			widgetHeader.attachEvent("onmousedown", dragStart);
		}
		if (Sphere.Browser.isNS) {
			widgetHeader.addEventListener("mousedown", dragStart, true);
		}

        if(  _proxy_jslib_handle(null, 'content', content, 0, 0) ) Sphere.Widget.processContent(  _proxy_jslib_handle(null, 'content', content, 0, 0) );
    }

	function dragStart(event, id) {
		dragObj = {};
		
	    dragObj.elNode = widget;

		if (Sphere.Browser.isIE) {
		  x = window.event.clientX + document.documentElement.scrollLeft
 +  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollLeft;
		  y = window.event.clientY + document.documentElement.scrollTop
 +  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollTop;
		}
	 	if (Sphere.Browser.isNS) {
		   x = event.clientX + window.scrollX;
		   y = event.clientY + window.scrollY;
		}


	    dragObj.cursorStartX = x;
	    dragObj.cursorStartY = y;
	    dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
	    dragObj.elStartTop = parseInt( _proxy_jslib_handle(dragObj.elNode.style, 'top', '', 0, 0),  10);

	    if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
	    if (isNaN(dragObj.elStartTop))  dragObj.elStartTop = 0;          

		if (Sphere.Browser.isIE) {
			document.attachEvent("onmousemove", dragGo);
			document.attachEvent("onmouseup",   dragStop);
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (Sphere.Browser.isNS) {
			document.addEventListener("mousemove", dragGo,   true);
			document.addEventListener("mouseup",   dragStop, true);
			event.preventDefault();
		}

	}
	function dragGo(event) {
		var x, y;
		if (Sphere.Browser.isIE) {
		  x = window.event.clientX + document.documentElement.scrollLeft
 +  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollLeft;
		  y = window.event.clientY + document.documentElement.scrollTop
 +  _proxy_jslib_handle(document, 'body', '', 0, 0).scrollTop;
		}
		if (Sphere.Browser.isNS) {
		  x = event.clientX + window.scrollX;
		  y = event.clientY + window.scrollY;
		}
	
	    dragObj.elNode.style.left =
	        (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
	     _proxy_jslib_assign('', dragObj.elNode.style, 'top', '=', (
	        (dragObj.elStartTop + y - dragObj.cursorStartY) + "px"));

		if (Sphere.Browser.isIE) {
		  window.event.cancelBubble = true;
		  window.event.returnValue = false;
		}
		if (Sphere.Browser.isNS)
		  event.preventDefault();
	}
	function dragStop(event) {
		if (Sphere.Browser.isIE) {
		  document.detachEvent("onmousemove", dragGo);
		  document.detachEvent("onmouseup",   dragStop);
		}
		if (Sphere.Browser.isNS) {
		  document.removeEventListener("mousemove", dragGo,   true);
		  document.removeEventListener("mouseup",   dragStop, true);
		}
	}

        
    function appendStylesheet() {
        var stylesheet = document.createElement("link");
        stylesheet.rel = "stylesheet";
        stylesheet.type = "text/css";
            _proxy_jslib_assign('', stylesheet, 'href', '=', ( "http://www.sphere.com/widgets/sphereit/css?siteid=oreilly"));        
        stylesheet.media = "all";
        document.lastChild.firstChild.appendChild(stylesheet);
    }
    

} 

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 =  _proxy_jslib_handle(null, 'setInterval', setInterval, 1, 0)(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 =  _proxy_jslib_handle(document, 'getElementById', '', 1, 0)("output");
    if(output)  _proxy_jslib_assign('', output, 'innerHTML', '+=', ( (val + "<br/>")));
}

function iecheck(inc_seven) {
    if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
        var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
        var iever = (inc_seven)?(rslt != null && Number(rslt[1]) >= 5.5):(rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7 );
    }
    return iever;
}
function wait(ms) { var now=new (Date)();var exitTime=now.getTime()+ms;while(true){now=new (Date)();if(now.getTime()>exitTime){return;}}}

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 =  _proxy_jslib_handle( _proxy_jslib_handle(document.all, (i), 0, 0).currentStyle, 'backgroundImage', '', 0, 0);
            if (itsAllGood && bg!='none'){
                if (bg.match(/(_Bot|_Mid|_Top|_Btn|_alpha_)\.png/i) != null){
                    var mypng = bg.substring(5,bg.length-2);
                     _proxy_jslib_handle(document.all, (i), 0, 0).style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
                     _proxy_jslib_assign('',  _proxy_jslib_handle(document.all, (i), 0, 0).style, 'backgroundImage', '=', ( "none"));
                }
            }
        }
    }   
}

// Omniture object
//********************************
function SphereOmniture() {
  
}

function _initSphereOm() {

    					
}

function _trackSphereOm(quad,context) {

    // comScore url attr
    // omniture tracking code 
	
	
	

}


SphereOmniture.prototype.init = _initSphereOm;
SphereOmniture.prototype.track = _trackSphereOm;


// added this method so that gadget would call search after data had loaded.
//-----------------------------------
function loadGadgetView(permlnk) {
	if(Sphere.Inline){
		//alert("found an inline");
		 _proxy_jslib_handle(Sphere.Inline, 'search', '', 1, 0)("sphereInline", permlnk);
	} else {
		//alert("no inline");
	}
}


/* -- END SPHERE JS -- */      
 ;
_proxy_jslib_flush_write_buffers() ;