sfHover = function() {
	var sfEls = document.getElementById("global-nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

// IE-only method of attaching an event to window loading.
if (window.attachEvent) window.attachEvent("onload", sfHover);

document.observe('dom:loaded', function(){
  if($$('h1').length > 0){
    fix_page_title();
  }
  if($('full-background-toggle')){
    observe_full_background_toggle();
  }
  if($$('a.video-to-play').length > 0){
    set_up_video_player();
  }

  if($('rotateFeature')){

  /* 
    // setTimeout("fadeOut(" + random + ")", 10000);
    var totalFeatures = $('rotateFeature').childElements().size();

    // slideshow takes delay, total number of slides and base element name.

    var slideshow = new Slideshow(6, totalFeatures, "rotate");
    slideshow.start();
    */

      var firstElement = $$('#rotateFeatureContent .slide')[0];
      var htmlCopy = firstElement.cloneNode(true);
      Element.insert('rotateFeatureContent', htmlCopy);

      new Carousel('rotateFeature', $$('#rotateFeatureContent .slide'), $$('a.carousel-control'), {auto: true, circular: true, frequency: 5});
  }

  test_window_height();

});

function test_window_height(){

    if( window.innerHeight && window.scrollMaxY ){
        //Firefox
        pageWidth = window.innerWidth + window.scrollMaxX;
        pageHeight = window.innerHeight + window.scrollMaxY;
    }
    else if( document.body.scrollHeight > document.body.offsetHeight ) {
        pageWidth = document.body.scrollWidth;
        pageHeight = document.body.scrollHeight;
    }
    else { 
        pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
        pageHeight = document.body.offsetHeight + document.body.offsetTop; 
    }

    if(pageHeight > 1000){
        $$('h2').each(function(hEl){
                // Can I say again that I hate internet explorer?
                // if IE wasn't braindead, I could set this color to "inherit" 
                // and this'd be much more reliable as we change themes.
                hEl.setStyle({color:'#AAAAAA'});
                });
    }
}

function observe_full_background_toggle(){
    $('full-background-toggle').observe('click', function (){
           Effect.toggle('full-background-content', 'blind');
            });
}

function fix_page_title(){
   var titleContent = $$('h1')[0].innerHTML;
   if(titleContent.match(/\s+/)){
   //there's a space. Continue.
    var words = titleContent.split(' ');
    var lastWord = words[words.length - 1];
    var newWords = words;
    newWords[words.length - 1] = undefined;
    var replacedContent = newWords.join(' ');
    $$('h1')[0].innerHTML = replacedContent + '<span>' + lastWord + '</span>';
   }
}

function set_up_video_player(){
    $$('a.video-to-play').each(function (video){
        flowplayer(video.id, "/images/flowplayer-3.1.0.swf",
            {clip: { 
            // these two configuration variables does the trick 
            autoPlay: false,  
            autoBuffering: true // <- do not place a comma here   
        } 
    });
    });
}

function init_container(videoId,vidurl,keyframe,vidtitle,height,width){
    if(Prototype.Browser.IE){
        var videoContainer = $('video-container-' + videoId).remove();
        Element.insert('content', '<div id="video-player-' + videoId +'">Bar.</div>');
        show_video(videoId,vidurl,keyframe,vidtitle,450,600);
        $('video-player-' + videoId).scrollTo();
    } else {
        Modalbox.show($('video-container-' + videoId), {title: vidtitle, width: width + 80, beforeLoad:show_video(videoId,vidurl,keyframe,vidtitle,height,width)});
    }
}

function show_video(videoId,vidurl,keyframe,title,height,width){
    var s1 = new SWFObject("http://cyber.law.harvard.edu/sites/cyber.law.harvard.edu/themes/blueprint/mediaplayer.swf","single",width,height + 20,"7");
    s1.addParam("allowfullscreen","true");
    s1.addVariable("file",vidurl);
    s1.addVariable("image",keyframe);
    s1.addVariable("width",width);
    s1.addVariable("height",height + 20);
    s1.write("video-player-" + videoId);
}

