var fadeSpeed = 50;
var stopSpeed = 5000;
var thumbsrc = new Array("","images/fade1/", "images/fade2/", "images/fade3/");
var totalCnt = new Array(0,4,5,2);
var isstaff = false;
var notfading = true;

function initImage(theId) {
  outerId = 'fade'+theId+'_outer';
  innerId = 'fade'+theId+'_inner';
  //changeimg(outerId, innerId, theId, 0);
  imageId = innerId;
  image = document.getElementById(imageId);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  changeimg(outerId, innerId, theId, 0,1);
}

function setOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function fadeIn(outerId,innerId,opacity,isIn, theid, igoredefault) {
/*alert(outerId);
alert(innerId);
alert(opacity);
alert(isIn);*/

//alert(isstaff);
  if (isstaff && !notfading){
	if (theid!=staffno){
		gofade = true;
	}else if (igoredefault){
		gofade = true;
	}else{
		gofade = false;
	}
  }else{
  	gofade = true;
  }

  if (gofade && !notfading){
	  nextfade = fadeSpeed;
	  if (document.getElementById) {
	    obj = document.getElementById(innerId);
		if (isIn==1){
			if (opacity <= 100) {
				setOpacity(obj, opacity);
				opacity += 10;
				isIn = 1;
				window.setTimeout("fadeIn('"+outerId+"','"+innerId+"',"+opacity+","+isIn+", "+theid+", "+igoredefault+")", nextfade);
			}else{
				opacity -= 10;
				isIn = 0;
				notfading = true;
				//nextfade = stopSpeed;
			}
		}else{
			if (opacity >= 0) {
				setOpacity(obj, opacity);
				opacity -= 10;
				isIn = 0;
				window.setTimeout("fadeIn('"+outerId+"','"+innerId+"',"+opacity+","+isIn+","+theid+", "+igoredefault+")", nextfade);
			}else{
				opacity += 10;
				isIn = 1;
				notfading =true;
				//nextfade = stopSpeed;
			}
		}
		//window.setTimeout("fadeIn('"+outerId+"','"+innerId+"',"+opacity+","+isIn+")", nextfade);
	  }
	}
}

function changeimg(objId, objId1, theid, isIn){
	obj = document.getElementById(objId);
	obj1 = document.getElementById(objId1);
	if (isIn){
		newinner = obj1.src;
		skipid = newinner.substr(newinner.lastIndexOf('/')+1,1);
		newouter = thumbsrc[theid]+ranNumber(theid, skipid)+".jpg";
		
	}else{
		newouter = obj.style.backgroundImage;
		newouter = newouter.replace(')', '');
		newouter = newouter.substring(4);
		skipid = newouter.substr(newouter.lastIndexOf('/')+1,1);
		newinner = thumbsrc[theid]+ranNumber(theid, skipid)+".jpg";
		
	}
	
	obj.style.background="url("+newouter+")";
	obj1.src=newinner;
	
}

function ranNumber(theid, skipid){
	ran_number = -1;
	do{
		ran_unrounded=Math.random()*totalCnt[theid];
		ran_number=Math.floor(ran_unrounded);
		ran_number++;
	}while(ran_number == skipid);
	return ran_number;	
}

function goloop(){
	/*changeimg('fade1_outer', 'fade1_inner', 1, 0);
	changeimg('fade2_outer', 'fade2_inner', 2, 0);
//	changeimg('fade3_outer', 'fade3_inner', 3, 0);
	changeimg('fade1_outer', 'fade1_inner', 1, 1);
	changeimg('fade2_outer', 'fade2_inner', 2, 1);
	//changeimg('fade3_outer', 'fade3_inner', 3, 1);
	fadeIn('fade1_outer', 'fade1_inner',1,100,0);
	window.setTimeout("fadeIn('fade2_outer', 'fade2_inner',2,100,0);", 5000);
	//window.setTimeout("fadeIn('fade3_outer', 'fade3_inner',3,100,0);", 10000);*/
}

function fadeChangingImg(thebg, thetop, newimgloc){	
	
	thebgobj = document.getElementById(thebg);
	thetopobj = document.getElementById(thetop);
	
	//if(thetopobj.src.indexOf(newimgloc)<0 && newimgloc.indexOf("spacer.jpg")<0 && notfading){
	if(newimgloc.indexOf("spacer.gif")<0){
	
		//thebgobj.style.background="url("+thetopobj.src+")";
		//setOpacity(thetopobj, 0);
		
		thetopobj.src=newimgloc;
		//notfading = false;
		//fadeIn(thebg, thetop ,0,1,0,0);
	}
	//else alert("no action to be prefomed!");
}
