var i = 1;
var slideshow;

init = function() {
	new Ajax.Request('slideshow.php', {
		method:'get',
		onSuccess: loadSlideshow
	});
}
loadSlideshow = function(transport) {
	var json = transport.responseText.evalJSON(true);
	
	slideshow = json.slideshow;

	if (slideshow.length > 1) {
		for(var i=0;i<slideshow.length;i++) {
			preloadImage(slideshow[i].image);
		}
		setTimeout('runSlideshow()',5000);
	}
}

runSlideshow = function() {
	new Effect.Fade('home-slideshow-image',{afterFinish:function() {
		$('home-slideshow-image').src = slideshow[i].image;
		$('home-slideshow-link').href = slideshow[i].link;
		new Effect.Appear('home-slideshow-image');
		i++;
		if (i >= slideshow.length) {
			i=0;
		}
		setTimeout('runSlideshow()',5000);
	}});	
}

var images = new Array();

preloadImage = function(src) {
	image = new Image();
	image.src = src;
	images.push(image);	
}

var intervalId;

slideshowMore = function(direction) {
	if (direction == 'left') {
		intervalId = setInterval('moveSlideshowLeft()',1);
	}
	else {
		intervalId = setInterval('moveSlideshowRight()',1);		
	}
}
moveSlideshowLeft = function() {
	if(i < ($('slideshow-wrapper').style.width.sub('px','') - $('master-plan-slideshow').style.width.sub('px',''))) {
		$('slideshow-wrapper').style.marginLeft = -i + "px";
		i+=3;
	}
	else {
		clearInterval(intervalId);
	}
}
moveSlideshowRight = function() {
	if(i > 0) {
		$('slideshow-wrapper').style.marginLeft = -i + "px";
		i-=3;
	}
	else {
		clearInterval(intervalId);
	}
}
slideshowStop = function() {
	clearInterval(intervalId);	
}

openLargeImage = function(imageURL,imageTitle,imageWidth,imageHeight) {
	imagePopup = window.open('','photoGalleryImage','width=' +imageWidth + ',height=' + imageHeight);
	imagePopup.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n');
	imagePopup.document.write('"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	imagePopup.document.write('\n<html>\n<head>');
	imagePopup.document.write('\n<title>' + imageTitle + '</title>\n</head>');
	imagePopup.document.write('\n<body style="margin:0;">\n');
	imagePopup.document.write('<img src="' + imageURL + '" width="' + imageWidth + '" height="' + imageHeight + '" alt="' + imageTitle + '" />\n</body>\n</html>');
}