// ie6/7, moz, safari
// Matt

var sponsor_div_id = "sponsors_widget";

var sponsorIndex = 0;
var lastSponsorIndex = 0;
function construct_sponsors_widget(headingPosition, caption) {
	if(typeof sponsors === "undefined" || sponsors.length == 0) {
		return;
	}
	
	var sponsors_widget = document.getElementById(sponsor_div_id);
	
	var maxHeight=0;
	var maxWidth=0;
	var widgetHeight=0;
	var widgetWidth=0;
	for (var x=0;x<sponsors.length;x++) {
		if (sponsors[x].logo.width > maxWidth) {
			maxWidth=sponsors[x].logo.width;
		}
		if (sponsors[x].logo.height > maxHeight) {
			maxHeight=sponsors[x].logo.height;
		}
	}
	
	widgetHeight = maxHeight;
	widgetWidth = maxWidth;
	/*
	 font-size:120%;letter-spacing:-.04em;font-weight:normal;color:#1C3D4C;font-family:Verdana, Geneva, Arial, Helvetica, sans-serif;
	 */
	if (headingPosition.length > 0) {
		var sponsors_widget_heading = document.createElement("div");
		sponsors_widget_heading.innerHTML = caption;
		if (headingPosition=="text-top" || headingPosition==0) {
			widgetHeight += 23;
			sponsors_widget_heading.style.cssText = "font-size:20px;font-weight:normal;color:#333;font-family:Arial, Helvetica, sans-serif;white-space:nowrap;margin-bottom:5px;text-align:center;width:200px;";
		}
		else if (headingPosition=="text-left") {
			widgetWidth += 185;
			sponsors_widget_heading.style.cssText = "font-size:20px;font-weight:normal;color:#333;font-family:Arial, Helvetica, sans-serif;white-space:nowrap;margin-right:10px;float:left;clear:none;";
		}
		sponsors_widget.appendChild(sponsors_widget_heading);
	}
	
	sponsors_widget.style.cssText = "width:"+widgetWidth+"px;height:"+widgetHeight+"px;";
	
	var spanchor = document.createElement("a");
	spanchor.id = 'sponsor_link';
	
	var spimage = document.createElement("img");
	spimage.id = 'sponsor_image';
	
	spanchor.appendChild(spimage);
	sponsors_widget.appendChild(spanchor);
	
	if (sponsors.length == 1) {
		switch_sponsor();
	}
	else {
		rotate();
	}
}

function rotate() {
	switch_sponsor();
	setTimeout('changeOpac(100,"sponsor_link")', 1);
	setTimeout('opacity("sponsor_link", 100, 0, 500)', (sponsors[lastSponsorIndex].delay));
	setTimeout('rotate()', (sponsors[lastSponsorIndex].delay + 600));
}

function switch_sponsor() {
	var spimage = document.getElementById('sponsor_image');
	var spanchor = document.getElementById('sponsor_link');
	
	spanchor.href = sponsors[sponsorIndex].url;
	spanchor.target = sponsors[sponsorIndex].name;
	spanchor.title = sponsors[sponsorIndex].communityName + " is sponsored by " + sponsors[sponsorIndex].name + ". Click here to visit " + sponsors[sponsorIndex].name + "'s website";
	spanchor.style.cssText = "display:block;width:"+sponsors[sponsorIndex].logo.width+"px;height:"+sponsors[sponsorIndex].logo.height+"px;";
	spanchor.tabIndex = 999;
	
	spimage.src = sponsors[sponsorIndex].logo.src;
	spimage.alt = sponsors[sponsorIndex].communityName + " is sponsored by " + sponsors[sponsorIndex].name + ". Click here to visit " + sponsors[sponsorIndex].name + "'s website";
	spimage.style.cssText = "display:block;width:"+sponsors[sponsorIndex].logo.width+"px;height:"+sponsors[sponsorIndex].logo.height+"px;";
	spimage.tabIndex = 999;
	
	lastSponsorIndex = sponsorIndex;
	sponsorIndex = ((sponsorIndex<sponsors.length-1)?(sponsorIndex+1):0);
}

// http://www.brainerror.net/scripts_js_blendtrans.php
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 + ")";
}
