var slideimgSeconds = 10;
var slideshowTimer = null;

function clearInput(node){
	node.value = "";
}

var ssCache = new Array();
ssCache[0] = false; 
ssCache[1] = false; 
ssCache[2] = false; 
ssCache[3] = false;
var ssImage = new Array();

var currentImage = 0;
// Shows the next image in the slideshow
function slideshow(){
	var id = 1;
	if(currSlideimg != null)
		id = (parseInt(currSlideimg.id.substr(8, 1)) % 4) + 1;
	
	currentImage = id - 1;
	// Select the next image
	slideimgSelect(dojo.byId("slideimg" + id));
}
		
dojo.addOnLoad(function(){
	if(dojo.byId("slideimg1") != null){
		// Start the timer
		slideshowTimer = setInterval("slideshow()", slideimgSeconds * 1000);
		
		// Select the first image
		slideimgSelect(dojo.byId("slideimg1"));
	}

	if(dojo.byId("imgSponsor") != null){
		pullImageFromPHP();
	}
});

var currSlideimg = null;

function slideimgSelect(node){
	if(currSlideimg == null){
		// Pretend the image already being displayed is something
		// other than the first one, so we can select the first image
		// when the page first loads
		currSlideimg = dojo.byId("slideimg2");
	}

	if(node != currSlideimg){
		dojo.fadeOut({
			node: "slideshow",
			onEnd: function(){
				
				currentImage = (parseInt(node.id.substr(8, 1)) % 4);
				
				if(!ssCache[currentImage]){
					ssImage[currentImage] = new Image();
					ssImage[currentImage].src = "image/" + node.id + ".jpg";
					ssCache[currentImage] = true;
					
					ssImage[currentImage].onload = dojo.hitch(this, function(){
						dojo.style("slideshow", "backgroundImage", "url('image/" + node.id + ".jpg')");                        
						dojo.fadeIn({node: "slideshow"}).play();
					});
				}else{
					dojo.style("slideshow", "backgroundImage", "url('" + ssImage[currentImage].src + "')");
					dojo.fadeIn({node: "slideshow"}).play();							
				}

				}
		}).play();
		
		dojo.style(currSlideimg, "border", "1px solid #cccccc");
		dojo.style(node, "border", "1px solid #800000");
		
		// Reset the timer
		clearInterval(slideshowTimer);
		slideshowTimer = setInterval("slideshow()", slideimgSeconds * 1000);
		
		currSlideimg = node;
	}
}

var sponsorimages = new Array();
var hasCache = new Array();
var imageCache = new Array();
var sponsorhrefs = new Array();
var siIndex = 0;
var siSize = 0;

var allCached = false;

function pullImageFromPHP(){
	// This is an array of 'parameters' to be passed into the function
	// just so that it's easier to read
	var xhrParameters = {
		url: "imagepull.php", 	// URL to go to
		handleAs: "json",		// Treat what is given by the URL as text
		load: function(imageSource){	// What happens when the 'data' is received from the URL
			
			sponsorimages = imageSource.images;
			siSize = imageSource.size;
			
			dojo.forEach(imageSource.images, function(item, idx){
				
				sponsorimages[idx] = item.src;
				sponsorhrefs[idx] = item.href;
				
				// no cache yet
				hasCache[idx] = false;
			});

			nextSponsorImage();
			setInterval("nextSponsorImage()", 4000);
		}
	};
	
	// Call the function with the parameters
	// This is basically saying, 'go to some link, and give me back
	// the results'
	dojo.xhrGet(xhrParameters);
}
		
function nextSponsorImage(){
	if(!hasCache[siIndex]){
		imageCache[siIndex] = new Image();
		imageCache[siIndex].src = sponsorimages[siIndex];
		hasCache[siIndex] = true;
	}
	
	dojo.byId("imgSponsor").src = imageCache[siIndex].src;
	dojo.byId("aSponsor").href = sponsorhrefs[siIndex];
	
	siIndex++;
	if(siIndex >= sponsorimages.length)
		siIndex = 0;
}		

//expand and collapse effect
function more(node){
	dojo.style(node, "display", "none");
	
	var openId = node.id + "text";
	var anim1 = dojo.fadeIn({node: openId});
	var anim2 = dojo.fx.wipeIn({node: openId});
	
	dojo.fx.combine([anim1, anim2]).play();
}
	
function less(node){
	var openId = node.id.substr(0, 5) + "moretext";
	var lblMore = node.id.substr(0, 5) + "more";
	dojo.style(lblMore, "display", "inline");
	
	var anim1 = dojo.fadeOut({node: openId});
	var anim2 = dojo.fx.wipeOut({node: openId});
	
	dojo.fx.combine([anim1, anim2]).play();
}
