
function in_array (needle, haystack, argStrict) { 
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    } 
    return false;
}
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
	if(opacity==0){
		document.getElementById(id).src=rpsrc;
	}
} 
function rand (min, max) {    
    var argc = arguments.length;
    if (argc === 0) {
        min = 0;
        max = 2147483647;
    } else if (argc === 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getShownPics(){
	SHOWN_PICS=new Array();
	AVAIL_PICS=new Array();
	SP=document.getElementById('pics').getElementsByTagName("img");
	for(i=0; i<SP.length; i++){
		SHOWN_PICS.push(SP[i].src);
	}
	for(i=0; i<ALL_PICS.length; i++){
		if(!in_array(ALL_PICS[i],SHOWN_PICS)){
			AVAIL_PICS.push(ALL_PICS[i]);
		}
	}
}
function swapPic(){
	getShownPics();
	rpnr=rand(1,(SHOWN_PICS.length)-1);	
	rpsrc=AVAIL_PICS[rand(0,AVAIL_PICS.length-1)];
	document.getElementById('divpic_' + rpnr).style.backgroundImage='url(' + rpsrc + ')';
	opacity('picid_' + rpnr,100,0,1250);
}
window.onload=function(){
	setInterval('swapPic()',2000);
};