/**
  Copyright : Daniel Bulli  (www.nuff-respe.com)
  dbulli : 11/29/04 - Updated to cycle through images randomnly WITHOUT repeat
  **/
  
/** site nav **/
/** http://www.htmldog.com/articles/suckerfish/dropdowns/ **/
sfHover = function() 
{
	var sfEls = document.getElementById("site_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"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover)

/** orignally from **/
/** http://clagnut.com/sandbox/imagefades/ **/
/** http://www.brazendoxy.com/c72/fader/wonky_looper.html  **/

/** version does not pass variabels around, but uses globally set values **/

var doingFade = true;   //whether or not we are fading
var timeout;            //use as a handle on the timeout loop  
var globalOpacity = 0;  //global opacity 

var imageNames = "s_head_00.gif s_head_01.jpg s_head_02.jpg s_head_03.jpg s_head_04.jpg s_head_05.jpg s_head_06.jpg s_head_07.jpg s_head_08.jpg s_head_09.jpg s_head_10.jpg s_head_11.jpg s_head_12.jpg s_head_13.jpg s_head_14.jpg s_head_15.jpg s_head_16.jpg s_head_17.jpg s_head_18.jpg s_head_19.jpg s_head_20.jpg";
var imageNamesArray = null;

//create array from strings
resetImageArray();

//current image that is being faded
var indexValue = Math.floor(Math.random()*imageNamesArray.length-1)+1;

//since this is for main header use global
//basically too lazy to pass ids around
//and this is more efficient
var containerID = 'site_head';
var targetID    = 'site_photo';
var clickedID   = 'imgControl';
var container   = null;
var target      = null;
var clicked     = null;

/** a derivation (or mangling) of a script from Scott Andrew (www.scottandrew.com) **/
function addEvent(obj, evType, fn)
{ 
    if (obj.addEventListener)
    { 
        obj.addEventListener(evType, fn, true); 
        return true; 
    } 
    else if (obj.attachEvent)
    { 
        var r = obj.attachEvent("on"+evType, fn); 
        return r; 
    } 
    else 
    { 
        return false; 
    } 
}

/** start first photo after 4 seconds **/
function startPhoto()
{
    if (!document.getElementById) return;
    
    //set references to container && target ** a href    
    container = document.getElementById(containerID);
    target = document.getElementById(targetID);
    clicked = document.getElementById(clickedID);
        
    setalpha(0) ;
    timeout = window.setTimeout("nextPhoto()", 1000);
}


/** stop fade mid way **/
function stopPhoto()
{
    if (!document.getElementById) return;
    
    if(doingFade)
    {
        //stopfade
        clicked.innerHTML = "start fade";
        clearTimeout(timeout);
    }
    else
    {
        //start fade from where we left off
        clicked.innerHTML = "stop fade";
        timeout = window.setTimeout("reveal("+globalOpacity+")", 100);
    }
    //invert variable
    doingFade = !doingFade;
}



/** load next photo and start fade **/
function nextPhoto() 
{
	if (!document.getElementById) return; //protect older browsers
	clearTimeout(timeout); // only one transition at a time, please
    
    //flip image and container
    switchContainer();    
    
    //load next image   
    target.src = "/past/v1/i/head/" + imageNamesArray[indexValue];
    
    //delete this element from array
    deleteIndexFromArray(indexValue);
    
    //no more so let's start again
    if(imageNamesArray.length == 0)
    {
        resetImageArray();
    }
    
    //get new index
    indexValue = Math.floor(Math.random()*imageNamesArray.length);


    //give image 5 seconds before attempting to switch
	timeout = window.setTimeout("reveal('0')", 5000);
}


//** function does actual reveal **/
function reveal(opacity) 
{
  if (!document.getElementById) return;
  
  if (document.getElementById && opacity <= 100 ) 
  {
    setalpha(opacity); 
    
    //debug
    //clicked.innerHTML = opacity;
            
    opacity += 5;
    
    //store globally fo restart
    globalOpacity = opacity;
    
    //fade next step
    timeout = window.setTimeout("reveal("+opacity+")", 100);
  }
  else
  {
    //we are done .. load next photo in 5 seconds
    timeout = window.setTimeout("nextPhoto()", 500);
    
    switchContainer();
  }
}

function switchContainer()
{
    if (!document.getElementById) return;
    
    //make container background current image
	container.style.backgroundImage = 'url(' + target.src + ')';    
    
    //make image 0
    setalpha(0) ;
}

function resetImageArray()
{
    imageNamesArray = null;
    imageNamesArray = imageNames.split(" ");
}

//delete from array
function deleteIndexFromArray(indexval) 
{
    //assume valid index for laziness
    for (var i= indexval; i<imageNamesArray.length; i++) 
    {
        imageNamesArray[i] = imageNamesArray[i + 1];
    }
    imageNamesArray.length=imageNamesArray.length-1;
}

//** function does actual reveal **/
function setalpha(opacity) 
{
  if (document.getElementById ) 
  {    
    //fade next step based onbrowser compatibility
    if (target.style.MozOpacity!=null) {
       target.style.MozOpacity = (opacity/100) - 0.001; //patrick h. lauke (http://www.splintered.co.uk/) workaround for Mozilla 'flash' bug - I _never_ would have caught that
    } else if (target.style.opacity!=null) {
       target.style.opacity = opacity/100;
    } else if (target.style.filter!=null) {
       target.style.filter = "alpha(opacity=" + opacity + ")";
	} else if (target.style.KhtmlOpacity!=null) {
       target.style.KhtmlOpacity = opacity/100;
	}
  }
}    
    
// idea from : http://www.hicksdesign.co.uk/journal/    
// clear text in the search news box.
function clearSearchBox()
{
    if (document.getElementById('q').value == 'search nuff-respec')
    {
        document.getElementById('q').value = '';
    }
}    
