 // <![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;  
    

    
    //next image index to be faded
    var indexValue = Math.floor(Math.random()*imageNames.length-1)+1;
    
    //variables for imgContainer/controller/target
    var imgContainerID = 'imgContainer';
    var targetID    = 'target';
    var controlID   = 'control';
    var imgContainer   = null;
    var target      = null;
    var control     = null;
    
    
    function startPhoto()
    {
        if (!document.getElementById) return;
        
        //set references to imgContainer && target ** a href    
        imgContainer = document.getElementById(imgContainerID);
        target = document.getElementById(targetID);
        control = document.getElementById(controlID);
            
        setalpha(0) ;
        timeout = window.setTimeout("nextPhoto()", 4000);
    }
    
    function nextPhoto() 
    {

    	if (!document.getElementById) return; 
        
        // only one transition at a time, please
    	clearTimeout(timeout); 
        
        //flip image and imgContainer
        switchimgContainer();    
        
        //load next image   
        target.src = imageNames[indexValue];

        //get new index differnet from current
        var newvalue = indexValue;
        while(newvalue == indexValue)
        {
            newvalue = Math.floor(Math.random()*imageNames.length);
        }
        indexValue = newvalue;
		objImage = new Image();
		// set what happens once the image has loaded 
		objImage.onLoad=imagesLoaded();
		// preload the image file
		objImage.src=newvalue;
    }

	function imagesLoaded(){
		timeout = window.setTimeout("reveal('0')", 500);
		}
    
    function switchimgContainer()
    {
        if (!document.getElementById) return;
        
        //make imgContainer background current image
    	imgContainer.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');
        document.getElementById('numero').style.left = (opacity) + 'px';
        opacity += 1;
        
        //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()", 10000);
        
        switchimgContainer();
      }
    }
    
    
    function stopPhoto()
    {
        if (!document.getElementById) return;
        
        if(doingFade)
        {
            //stopfade
           // control.innerHTML = "start";
		   document.getElementById('control').style.backgroundPosition = '1px 0';
            clearTimeout(timeout);
        }
        else
        {
            //start fade from where we left off
           // control.innerHTML = "stop";
		    document.getElementById('control').style.backgroundPosition = '-13px 0';
            timeout = window.setTimeout("reveal("+globalOpacity+")", 100);
        }
        //invert variable
        doingFade = !doingFade;
    }
    
    /** 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; 
        } 
    }  
    
    
    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;
    	}
      }
    }    
           
    // ]]>