// <![CDATA[
    
    //boolean: whether or not we are fading
    var doingFade = true;   
    
    //use as a handle on the timeout loop  
    var timeout;   
    
    //variable to hold global opactiy          
    var globalOpacity = 0;  
    
    var imageNames = "bed.jpg green.jpg poster.jpg table.jpg ottomon.jpg".split(" ");
    
    //next image index to be faded
    var indexValue = Math.floor(Math.random()*imageNames.length-1)+1;
    
    //variables for container/controller/target
    var containerID = 'container';
    var targetID    = 'target';
    var controlID   = 'control';
    var container   = null;
    var target      = null;
    var control     = null;
    
    
    function startPhoto()
    {
        if (!document.getElementById) return;
        
        //set references to container && target ** a href    
        container = document.getElementById(containerID);
        target = document.getElementById(targetID);
        control = document.getElementById(controlID);
            
        setalpha(0) ;
        timeout = window.setTimeout("nextPhoto()", 3000);
    }
    
    function nextPhoto() 
    {
    	if (!document.getElementById) return; 
        
        // only one transition at a time, please
    	clearTimeout(timeout); 
        
        //flip image and container
        switchContainer();    
        
        //load next image   
        target.src = "img/" + imageNames[indexValue];
        
        //get new index differnet from current
        var newvalue = indexValue;
        while(newvalue == indexValue)
        {
            newvalue = Math.floor(Math.random()*imageNames.length);
        }
        indexValue = newvalue;
    
        //fade image in
    	timeout = window.setTimeout("reveal('0')", 500);
    }

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


    function reveal(opacity) 
    {
      if (!document.getElementById) return;
      
      if (document.getElementById && opacity <= 100 ) 
      {
        setalpha(opacity); 
        
        numero = document.getElementById('numero');
        numero.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()", 5000);
        
        switchContainer();
      }
    }
    
    
    function stopPhoto()
    {
        if (!document.getElementById) return;
        
        if(doingFade)
        {
            //stopfade
            control.innerHTML = "start fade";
            clearTimeout(timeout);
        }
        else
        {
            //start fade from where we left off
            control.innerHTML = "stop fade";
            timeout = window.setTimeout("reveal("+globalOpacity+")", 100);
        }
        //invert variable
        doingFade = !doingFade;
    }
    
    /** a derivation (or mangling) of a script **/
    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; 
        } 
    }  
    
    
    function setalpha(opacity) 
    {
      if (document.getElementById ) 
      {    
        //fade next step based onbrowser compatibility
        if (target.style.MozOpacity!=null) {
           target.style.MozOpacity = (opacity/100) - 0.001; 
        } 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;
    	}
      }
    }    