var delayBetweenImages = 5000;

var supportSearchCleared = 0;
var associationSearchCleared = 0;

var nbImages = 1;
var lastLoadedImageId = 1;
var currentImageId = 1;
var nextImage;
var previousImageId;
$(document).ready(function() {
	$("#supportSearch").focus(function(){
		if (supportSearchCleared==0) {
			$("#supportSearch").val("");
			supportSearchCleared=1;
		}
	});
	$("#associationSearch").focus(function(){
		if (associationSearchCleared==0) {
			$("#associationSearch").val("");
			associationSearchCleared=1;
		}
	});
	$("#supportSearchDiv form").submit(function(){
		if (supportSearchCleared==0) {
			$("#supportSearch").val("");
		}
		return true;
	});
	$("#associationSearchDiv form").submit(function(){
		if (associationSearchCleared==0) {
			$("#associationSearch").val("");
		}
		return true;
	});
	
	if ($("#imageSrc2").length>0) {
		preloadImage();
		setTimeout(changeImage,delayBetweenImages);
	}
});

var homeButtonImgs = new Array(
	"/images/layout/ok.gif",
	"/images/layout/ok_hover.gif",
	"/images/layout/support_now.gif",
	"/images/layout/support_now_hover.gif",
	"/images/layout/support_later.gif",
	"/images/layout/support_later_hover.gif"
);
$(document).ready(function() {

	for(var i=0; i<homeButtonImgs.length; i++){
		homeButtonImgs[i] = $("<img>").attr("src", homeButtonImgs[i]);
	}

	$("#supportNowBtn").hover(function() {
		$(this).children("img").attr("src","/images/layout/support_now_hover.gif");
	},function() {
		$(this).children("img").attr("src","/images/layout/support_now.gif");
	});
	
	$("#supportLaterBtn").hover(function() {
		$(this).children("img").attr("src","/images/layout/support_later_hover.gif");
	},function() {
		$(this).children("img").attr("src","/images/layout/support_later.gif");
	});
	
	$("#searchDiv a.submit").hover(function() {
		$(this).children("img").attr("src","/images/layout/ok_hover.gif");
	},function() {
		$(this).children("img").attr("src","/images/layout/ok.gif");
	});

});

function changeImage() {
	previousImageId = currentImageId;
	if (currentImageId==nbImages) {
		currentImageId=1;
	} else {
		currentImageId++;
	}
	if (currentImageId>lastLoadedImageId) {
		nextImage.load(function() {
			$("#image"+previousImageId).css("z-index",2);
			$(this).css("z-index",1);
			$(this).show();		
			$("#image"+previousImageId).fadeOut(400);
			setTimeout(changeImage,delayBetweenImages);
		});
	} else {
		$("#image"+previousImageId).css("z-index",2);
		$("#image"+currentImageId).css("z-index",1);
		$("#image"+currentImageId).show();
		$("#image"+previousImageId).fadeOut(400);
		setTimeout(changeImage,delayBetweenImages);
	}
}

function preloadImage() {
	if ($("#imageSrc"+(lastLoadedImageId+1)).length>0) {
		nbImages++;
		nextImage = $("<img>");
		nextImage.load(function() {
			$("#bannerDiv").append(this);
			lastLoadedImageId++;
			preloadImage();
		});
		nextImage.attr("id", "image"+(lastLoadedImageId+1));
		nextImage.hide();
		nextImage.attr("src", $("#imageSrc"+(lastLoadedImageId+1)).val());
	}
}