var sw_picArray;
var sw_currentPic;
var sw_loadedPic;
var sw_currentLeft = 0;

//Initializes banner by taking picture array, creating an inner div to id=sw_banner, and initializing css
function f_banner_initialize(pictures) {
	sw_picArray = pictures;
	sw_currentPic = 0;
	$("#sw_banner").append("<div id='sw_banner_inner'></div>").css({overflow: "hidden", zIndex: "20"});
	$("#sw_banner_inner").css({position: "absolute", top: 0, left: 0, zIndex: "10"});
	f_createPic(0);
	f_createPic(1);
	f_createPic(2);
	f_center();
	f_fadeIn(0);
	setTimeout("f_nextPic()",5000);	
}

//Creates picture div for a particular index. Needs div to create it in.
function f_createPic(index) 
{
	var temp = $("#sw_banner").height();
	var left;
	if ($("#tPic" + index).length == 0) {
		var website = sw_picArray[index].filename.slice(0, -4);
		$("#sw_banner_inner").append("<div id='tPic" + index + "'><a href='" + sw_picArray[index].link + "' alt='" + sw_picArray[index].link + "'><img src='http://bizspark.startupweekend.com/global sponsors/" + sw_picArray[index].filename + "' title='" + sw_picArray[index].name + "' /></a></div>");
	}
	//Resize logos if larger than 70% height of the div
	if (sw_picArray[index].height > 0.7 * temp) {
		$("#tPic" + index).css({position: "absolute", top: 0.15 * temp, left: sw_currentLeft});
		$("#tPic" + index + " img").css({position: "absolute", opacity: 0.1, height: 0.7 * temp, width: sw_picArray[index].width * ((0.7 * temp) / sw_picArray[index].height)});
		sw_currentLeft += sw_picArray[index].width * ((0.7 * temp) / sw_picArray[index].height) + 30; //30 pixel spacing btwn logos
	} else {
		$("#tPic" + index).css({position: "absolute", top: (temp - sw_picArray[index].height) / 2, left: sw_currentLeft});
		$("#tPic" + index + " img").css({position: "absolute", opacity: 0.1, width: sw_picArray[index].width});
		sw_currentLeft += sw_picArray[index].width + 30; //30 pixel spacing btwn logos
	}
	
	sw_loadedPic = index;
}

function f_destroyPic(index)
{
	var temp = $("#sw_banner").height();
	$("#tPic" + index).remove();
	if (index > sw_currentPic)
		sw_currentLeft -= sw_picArray[index].height * (temp / sw_picArray[index].filename);
}

function f_center() 
{
	var picPosition = $("#tPic" + sw_currentPic).position();
	var picWidth =  $("#tPic" + sw_currentPic + " img").width();
	var stripPositon = $("#sw_banner_inner").position();
	var left = (0.5 * $("#sw_banner").width()) - (picPosition.left + 0.5 * picWidth);
	$("#sw_banner_inner").animate({left: left}, 1000);
}

function f_fadeIn(index) 
{
	$("#tPic" + index + " img").fadeTo(1000, 0.7);
}

function f_fadeOut(index) 
{
	$("#tPic" + index + " img").fadeTo(1000, 0.1);
}

function f_animate()
{
	f_center();
	f_fadeIn(sw_currentPic);
	if (sw_currentPic == 0)
		f_fadeOut(sw_picArray.length - 1);
	else	
		f_fadeOut(sw_currentPic - 1);
}

function f_nextPic() 
{
	sw_currentPic++;
	if (sw_currentPic == sw_picArray.length)
		sw_currentPic = 0;
	f_animate();
	if (sw_currentPic + 2 < sw_picArray.length)
		f_createPic(sw_currentPic + 2);
	else if (sw_currentPic + 2 == sw_picArray.length)
		f_createPic(0);
	else if (sw_currentPic + 2 > sw_picArray.length)
		f_createPic(1);
	setTimeout("f_nextPic()",5000);	
}

	