var index;
var MinNumEvents=0;
var NumEvents=0;
var images=new Array();
var eventIDs=new Array();
var timeout;
var permantEvents=new Array();

function start(current)
{
	getEvents(current);
	makebuttons();
	index=-1;
	Change();
}

function getEvents(current)
{
	//add all current events to the array;
	var c =0;
	while(current[c]!=null)
	{
		eventIDs[c]=current[c][0];
		images[c]='event_logo/'+current[c][1];
		c++;
	}
	
	//this is the number of rows in the db
	//if there is less than four current events then we fill space with society events(MIN 4 BANNERS AT ALL TIMES)
	/*if (c < MinNumEvents)
	{ 
		var count=0;	  
		for(var x =c; x<NumEvents;x++)
		{
			images[x]=permantEvents[count];
			count++;
			if(count>=permantEvents.length){break;}		
		}
	}*/
	NumEvents=images.length;	
}


function makebuttons()
{
	$('#magicbox').append($("<div id='mbcontrols'></div>"));
	var h ="<img class='arrowbutton' src='images/magicbox/arrowleft.gif' onclick='left()' />";
	h=h+"<img class='arrowbutton' src='images/magicbox/arrowright.gif' onclick='right()' />";
	$('#mbcontrols').html(h);
	$('#mbcontrols').append("<div id='mblink'></div>");
	$('#mblink').html('<a class="result-buy" href="?page=all&action=seatselection&id="'+eventIDs[index]+'"><span id="mblink-text">Buy Tickets</span><a>');
}

function Change()
{
	//animate fadeout
	$('#blocker').animate(
		{opacity: 0.0},
		'slow', function() {switchimg(); }
			);//animate fadein
}

function switchimg()
{
	index++;
	
	if ( index >= images.length){
		index = 0;
	}
	
	$('#mblink').html('<a id="mbbuy" href="?page=all&action=seatselection&id='+eventIDs[index]+'"><span id="mblink-text">Buy Tickets</span><a>');
	var s = '?page=pages&action=EVENT&id='+eventIDs[index];
	document.getElementById("imglink").setAttribute("href",s);
	if(index>=images.length){index=0;}//reset the index
	$("#magicboximage").removeAttr("src").attr('src',images[index]);
	$('#blocker').animate(
	{opacity: 1.0},
	'slow', function(){
			clearTimeout(timeout);//clear any existing timeouts
			timeout=setTimeout("Change()",5000);
		});//animate fadeout
}

function left()
{	
	clearTimeout(timeout);
	for (var x = 0; x < 2; x++) {
		index--;
		if (index < 0) {
			index = images.length-1;
		}
	}
	Change();
}

function right(){
	clearTimeout(timeout);
	Change();
}




