/**
  author: daniel bulli
  wgbh interactive
  **/

/** puts rollover on img **/  
function initHrefs() 
{
    //time to run
    //var d = new Date();
    //var curr_msec = d.getTime();

    initRolloversHead();
    initPopUps();
    window.focus(); //always focus
    //var d2 = new Date();	    
    //var curr_msec2 = d2.getTime();
    //how long?
    //alert(curr_msec2-curr_msec);
}

/** puts pop ups on hrefs that need **/  
function initPopUps() 
{
    //if we don't support getElementById then exit
	if (!document.getElementById("bdy_wrap")) return;

	
	//grab all hrefs from body
	if(!document.getElementById("bdy_wrap"))  return;

    var aHref = document.getElementById("bdy_wrap").getElementsByTagName("a");

    
    //loop over Hrefs
    for (var i = 0; i < aHref.length; i++) 
    {	
        if (aHref[i].className.match('pop_horiz_img')) 
        {
            //horizontal image pop up
            aHref[i].setAttribute('title', "View larger image in new window");
            
            //on mouseover grab hsrc
            aHref[i].onclick = function() 
            {
                window.open(this.href, 'horiz', 'width=421,height=316,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;
            }	
        }
        else if (aHref[i].className.match('pop_vert_img')) 
        {
            //vertical image pop up
            
            aHref[i].setAttribute('title', "View larger image in new window");
            
            //on mouseover grab hsrc
            aHref[i].onclick = function() 
            {
                window.open(this.href, 'vert', 'width=337,height=400,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;
            }	
        }	
        else if (aHref[i].className.match('pop_video')) 
        {
            //video pop up
            aHref[i].setAttribute('title', "View video in new window");
            
            //on mouseover grab hsrc
            aHref[i].onclick = function() 
            {
                window.open(this.href, 'video', 'width=545,height=400,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;
            }	
        }	        
        else if (aHref[i].className.match('tamayo')) 
        {
            //video pop up
            aHref[i].setAttribute('title', "View video in new window");
            
            //on mouseover grab hsrc
            aHref[i].onclick = function() 
            {
                window.open(this.href, 'video', 'width=600,height=500,toolbar=0,location=0,status=0,menubar=0,scrollbars=0,resizable=1'); return false;
            }	
        }	        
    }

}

/** puts rollover on img **/  
function initRolloversHead() 
{
    //if we don't support getElementById then exit
	if (!document.getElementById) return
	
	//yes it even does preload
	var aPreLoad = new Array();
	var sTempSrc;
	
	//grab all hrefs from nav
	var aHref = document.getElementById("hdr_nav").getElementsByTagName("a");

    
    //loop over Hrefs
	for (var i = 0; i < aHref.length; i++) 
	{	
	    //we use class name to find href	
		if (aHref[i].className.match('hrefover')) 
		{
		    var hrefID = aHref[i].id;
		    //grab all images inside this href
		    var intImages = aHref[i].getElementsByTagName("img");
		    
		    
	        //loop over internal images to preload
            for (var j = 0; j < intImages.length; j++) 
            {	
                //match the ones with rollover states
                //you could skip this step is all othe are
                if (intImages[j].className.match('imgover')) 
                {
                    //grab src
                    var src = intImages[j].getAttribute('src');
                    
                    //grab extension (neato works for gif and jpg nd png if you so feel)
                    var ftype = src.substring(src.lastIndexOf('.'), src.length);
                    
                    //create a new attribute for rollover (for fyi = '_on')			
                    var hsrc = src.replace(ftype, '_on'+ftype);
                    intImages[j].setAttribute('hsrc', hsrc);
                    intImages[j].setAttribute('osrc', src);

                    //PRELOAD !!
                    aPreLoad[j] = new Image();
                    aPreLoad[j].src = hsrc;                
                }
		
		    }

			
			//on mouseover grab hsrc
			aHref[i].onmouseover = function() 
			{
				mouseOverFunc(this.id);
			}	
			
			//on mouse out strip out "on"
			aHref[i].onmouseout = function() 
			{
				mouseOutFunc(this.id);
			}
		}
	}
}
 
/** note this puts roll over ref **/
function initRolloversImg() 
{
    //if we don't support getElementById then exit
	if (!document.getElementById) return
	
	//yes it even does preload
	var aPreLoad = new Array();
	var sTempSrc;
	
	//grab all nav images 
	var aImages = document.getElementById("hdr_nav").getElementsByTagName("img");

    //nb if you want all images on page  you do this instead (i only wanted nav)
    //var aImages = document.getElementsByTagName("img");

    
    //loop over images
	for (var i = 0; i < aImages.length; i++) 
	{	
	    //we use class name to find image	
		if (aImages[i].className.match('img_o')) 
		{
		    //grab src
			var src = aImages[i].getAttribute('src');
			
			//grab extension (neato works for gif and jpg nd png if you so feel)
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			
			//create a new attribute for rollover (for fyi = '_on')			
			var hsrc = src.replace(ftype, '_on'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			
			//PRELOAD !!
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			//on mouseover grab hsrc
			aImages[i].onmouseover = function() 
			{
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}	
			
			//on mouse out strip out "on"
			aImages[i].onmouseout = function() 
			{
			    //alert(sTempSrc);
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_on'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function mouseOverFunc(hrefID)
{    
    //if we don't support getElementById then exit
	if (!document.getElementById) return
	
	//grab images
	var intImages = document.getElementById(hrefID).getElementsByTagName("img");

    //loop over internal images to preload
    for (var j = 0; j < intImages.length; j++) 
    {	
        //match the ones with rollover states
        //you could skip this step is all othe are
        if (intImages[j].className.match('imgover')) 
        {
            //alert(hrefID);
            intImages[j].setAttribute('src', intImages[j].getAttribute('hsrc'));
        }
    }
}

function mouseOutFunc(hrefID)
{    
    //if we don't support getElementById then exit
	if (!document.getElementById) return
	
	//grab images
	var intImages = document.getElementById(hrefID).getElementsByTagName("img");

    //loop over internal images to preload
    for (var j = 0; j < intImages.length; j++) 
    {	
        //match the ones with rollover states
        //you could skip this step is all othe are
        if (intImages[j].className.match('imgover')) 
        {
            //alert(hrefID);
            intImages[j].setAttribute('src', intImages[j].getAttribute('osrc'));
        }
    }
}

window.onload = initHrefs;

