// image functions javascript include v1.1
// (c) 2004 P.de Wit, Leek 
// Please feel free to use and improove this code as long as you keep it free and public.

/* How to..
  1) Include this file using a script source= statement
  2) Initialise the folowing script statements in your page: 

   <script language="javascript">
	  var imageLocation='images/';  // point to where your images are
	  var imageList=new Array();
	  jsImg[00]="sample1.jpg";   // can be any image name
	  jsImgList[01]="sample2.jpg";
	  jsImgList[..]="...";		// add as many as you like
	</script>		 
    <img src="images/sample1.jpg" name="theImage" id="theImage" alt="random image">

  3) Call jsImage function
               
       Random: omit Index parameter
       Timed: when ms parameter > 0, the next image will be picked after specified miliseconds

       EXAPLE:
       <script language="javascript">jsImage(document.theImage,50,50, 5000, 0)</script>  

       You can use this with multiple image lists and images on one page	 

*/

// -- Constants, can be changed to alter configuration
    var imageLocation='images/';
    var initialized=false;
    
// -- Code starts here (no changes required from here) --

    function jsImage(objImage, w, h, ms, Index) {
	    if (!jsImg) return;
    
        if (initialized==false) {
            document.write('<DIV id="jsImgDiv" style="visibility:hidden"><IMG id="jsImg" src=""></DIV>');
            initialized=true;
        }
        var range = jsImg.length-1

        // Index omitted? pick random image..
        if (!Index) {
		    if (Math.random) Index= Math.round(Math.random() * (range-1)); 
		    else { 
			    var now = new Date(); 
			    Index= (now.getTime() / 1000) % range; 
    		}
        }
        var imgSrc=imageLocation+jsImg[Index];

        // Resize?
	    if (w) {
            DomObj('jsImg').src=imgSrc;
            if (w>0) self.setTimeout("resizeWithAspect('"+objImage+"',"+w+","+h+")",180);
	    }
        else {
            w=0;
            h=0;
            DomObj(objImage).src=imgSrc;
        }

        // Timed?
        if (ms) {
            if (ms > 0) {
                if (ms < 500) ms=500;
                Index++;
                if (Index > range) Index=0;        
                self.setTimeout("jsImage('"+objImage+"',"+w+","+h+","+ms+","+Index+")",ms);
            }
        }
     }

    function DomObj(id) {
        if (document.getElementById) { // DOM3 = IE5/6, NS6 
            theObj=document.getElementById(id);
        }
        else {
            theObj=eval("document."+objImage);
        }
        return theObj;
    }
	
	function resizeWithAspect(objImage,w,h) {
        obj=DomObj(objImage);
        obj.src=DomObj('jsImg').src;

		var imgW=0+obj.width;
		var imgH=0+obj.height;
		
		if (imgH>imgW) {
			var ratio=h/imgH;
			imgH=h;
			imgW=Math.round(imgW*ratio);
			var dir="Y";
			}
		else {
			var ratio=w/imgW;
			imgW=w;
			imgH=Math.round(imgH*ratio);
			var dir="X";
			}
		 if (imgW > 0 && imgW < 1000) obj.width=imgW;
		 if (imgH > 0 && imgH < 1000) obj.heigth=imgH;
	}

