var diashow = new Diashow();

$(document).ready(function() {			
	$("div#nav ul li").click(function(){
		$(".selectedItem").toggleClass("selectedItem");
		var href = $(this.firstChild).attr('href');
		$('#content').load(href);
		$(this).toggleClass("selectedItem");
		return false;
	});
	diashow.go();
});

function Diashow() {
	this.i = 0;
	this.iSlogan = 0;
	this.j = 13;
	this.jMax = 13;
	this.imageArray = new Array();
	this.imgEntryArray = new Array();
};

Diashow.prototype.nextImage = function(){
	// var slogan = $("div#slogan");
	// $(slogan[0].children[this.i]).fadeOut("slow");

	if ($(this.imgEntryArray[this.i].img1).css("display") != "none") { 
		// case 1: img above is visible
		//
		
		// Is this the first image in the row?
		//
		if (this.i == 0) {
			// feed in the next image
			//
			this.imgEntryArray[this.i].img2.src = this.imageArray[this.j].src;
			this.j=(this.j==0?this.jMax:--this.j)			
		} else {
			// pick the visible image from the left
			//
			this.imgEntryArray[this.i].img2.src = this.imgEntryArray[this.i-1].img1.src;		
		}
		$(this.imgEntryArray[this.i].img1).fadeOut(2000);
	} else {
		// case 2: img below is visible
		//

		// Is this the first image in the row?
		//
		if (this.i == 0) {
			// feed in the next image
			//
			this.imgEntryArray[this.i].img1.src = this.imageArray[this.j].src;
			this.j=(this.j==0?this.jMax:--this.j)			
		} else {
			// pick the visible image from the left
			//
			this.imgEntryArray[this.i].img1.src = this.imgEntryArray[this.i-1].img2.src;		
		}
		$(this.imgEntryArray[this.i].img1).fadeIn(2000);
	}
	
	this.i = (this.i==3?0:++this.i);
	// $(slogan[0].children[this.i]).fadeIn("slow");
	setTimeout("diashow.nextImage()", 2000);
};

Diashow.prototype.nextSlogan = function(){
	var slogan = $("div#slogan");
	$(slogan[0].children[this.iSlogan]).fadeOut("slow");
	this.iSlogan = (this.iSlogan==3?0:++this.iSlogan);
	$(slogan[0].children[this.iSlogan]).fadeIn("slow");
	setTimeout("diashow.nextSlogan()", 10000);
};

Diashow.prototype.initImageArray = function() {
	for (var i = 1; i < 15; ++i) {
		var img = new Image();
		img.src = "img/head/" + i + ".jpg";
		this.imageArray.push(img);
	}
	var imgEntry = $("div#img_entry");
	for (var i = 0; i < 4; ++i) {
		var img1 = imgEntry[i].children[0];
		var img2 = imgEntry[i].children[1];
		this.imgEntryArray.push({img1:img1, img2:img2});
	}
}

Diashow.prototype.go = function(){
	diashow.initImageArray();
	setTimeout("diashow.nextImage()", 2000);
	setTimeout("diashow.nextSlogan()", 10000);
}
