var objBanners = new Array(10);

function banner_rotator(num, running) {
    var obj = objBanners[num];
    if (obj.banner_timer) {
        window.clearInterval(obj.banner_timer);
    }    
    if (obj.fader_timer) {
        window.clearInterval(obj.fader_timer);
    }
    if (!running) return false;	

	if (obj.index > obj.list.length)
		obj.index = 0;
	else 
		obj.index += 1;

    document.getElementById(obj.xframe).src = obj.xsource + obj.index;
    obj.fader_timer = window.setInterval("opacity_fadeout('" + obj.id + "')", 9000);
    obj.banner_timer = window.setInterval("banner_rotator(" + num + ", true)", 10000);
}

function banner_stop(num) {
    var obj = objBanners[num];
	obj.runningstate = false;
    if (obj.banner_timer) {
        window.clearInterval(obj.banner_timer);
    }    
    if (obj.fader_timer) {
        window.clearInterval(obj.fader_timer);
    }
	set_runningstate(num);
}

function banner_continue(num) {
    var obj = objBanners[num];
    if (!obj.runningstate)
	obj.runningstate = true;
	else return false;
    banner_rotator(num, true);	
	set_runningstate(num);
}

function set_runningstate(num) {
	var obj = objBanners[num];
	if (!obj.runningstate) {
		document.getElementById('runningstate'+num).style.backgroundColor = 'red';
		document.getElementById('runningstate'+num).innerHTML = 'Tryk på start for at fortsætte visning';
	} else {
		document.getElementById('runningstate'+num).style.backgroundColor = 'seagreen';
		document.getElementById('runningstate'+num).innerHTML = 'Tryk på stop for at stoppe visning';
	}
}

function banner_list(num, list) {
    var obj = objBanners[num];
    obj.list = list;
}

function banner_next(num) {
    var obj = objBanners[num];
    if (obj.banner_timer) {
        window.clearInterval(obj.banner_timer);
    }    
    if (obj.fader_timer) {
        window.clearInterval(obj.fader_timer);
    }
    obj.index += 1;
    if (obj.index > obj.list.length) {
        obj.index = 0;
    } 
    document.getElementById(obj.xframe).src=obj.xsource+obj.index;
	obj.runningstate = false;
	set_runningstate(num);
}

function banner_prev(num) {
    var obj = objBanners[num];
    if (obj.banner_timer) {
        window.clearInterval(obj.banner_timer);
    }    
    if (obj.fader_timer) {
        window.clearInterval(obj.fader_timer);
    }
    obj.index -= 1;
    if (obj.index < 0) {
        obj.index = obj.list.length;
    } 
	obj.runningstate = false;
	set_runningstate(num);
    document.getElementById(obj.xframe).src=obj.xsource+obj.index;
}

function opacity_fadein (id) {
    opacity(id, 0, 100, 1000);    
}

function opacity_fadeout (id) {
    opacity(id, 100, 0, 1000);    
}

function clsBanner(div_id, page_id) {
    this.id = div_id;
    this.fid = page_id;
    this.list = new Array(0);
    this.index = 0;
    this.runningstate = true;
    this.fader_timer = null;
    this.banner_timer = null;
    this.xframe = 'xframe';
    this.xsource = '/banner_src2.asp?did='+div_id+'&fid='+page_id+'&index=';
    this.rotator = banner_rotator;
    this.fadeout = opacity_fadeout;
    this.fadein = opacity_fadein;
}


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;
	if (document.getElementById(id)) {
		//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); 
	object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);
	object.style.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if (document.getElementById(id)) {
		if(document.getElementById(id).style.opacity == 0) {
			opacity(id, 0, 100, millisec);
		} else {
			opacity(id, 100, 0, millisec);
		}
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	if (document.getElementById(divid) && document.getElementById(imageid)) {

		var speed = Math.round(millisec / 100);
		var timer = 0;
	
		//set the current image as background
		document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

		//make image transparent
		changeOpac(0, imageid);
	
		//make new image
		document.getElementById(imageid).src = imagefile;

		//fade in image
		for(i = 0; i <= 100; i++) {
			setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
			timer++;
		}
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}

