$(document).ready(function() {
	var current = 0;

	//show the current menu with deeper color
	$('#header li a').each(function() {
		if($(this).attr('href').slice(-8) == location.pathname.slice(-8)) {
			$(this).addClass('current');
			current = 1;
		}
	});

	//set the "exhibition" menu deep colored in artists' page
	if(!current) {
		$('#header li a:eq(2)').addClass('current');
	}

	//part only for exhibition page
	if($('#room').length > 0) {
		//configuration array
		var artists = [], artist;
		//loop throught the rooms to fill the artists array
		for	(var i = 1; i <= 5; i++) {
			$('.room_' + i + '').each(function(index) {
				artist = $(this).next().text().split('-');
				artists.push({name:artist[0],href:artist[1],nation:artist[2],room:i});
			});
		}

		//"mapping" array's key is the room number minus 1 and
		//the corresponding value is the index in the array above for the given room
		var mapping = [0,4,8,11,14];

		//function for updating the contents according to the current artist
		function update_screen(c_artist) {
			var i;
			//change "room x" style
			var c_room = artists[c_artist].room;
			for (i = 1; i <= 5; i++) {
				if(c_room == i) {
					$('#room_'+i).addClass('current');
				}else {
					$('#room_'+i).removeClass('current');
				}
			}

			//change background image
			var pos_y = -453 * (c_room-1);
			$('#room').css('background', 'url(./image/rooms.png) no-repeat scroll 0px '+pos_y+'px');

			//change the artist's name and nationality (also the link)
			$('#name').attr('href', './artist_'+artists[c_artist].href+'.php').text(artists[c_artist].name);
			$('#nation span').text(artists[c_artist].nation);

			//change to the picture of the current artist
			for (i = 1; i <= artists.length; i++) {
				if(c_artist+1 == i) {
					$('#artist_'+i+"_pic").css('display', 'block').imagecube({speed:3000});
				}else {
					$('#artist_'+i+"_pic").css('display', 'none').imagecube('destroy');
				}	
			}
		}

		var c_artist = 0;
		update_screen(c_artist);

		//click events on backward and forward buttons
		$('#backward').click(function(event) {
			c_artist--;
			if(c_artist < 0) {
				c_artist = artists.length-1;
			}
			update_screen(c_artist);
			event.preventDefault();
		});
		$('#forward').click(function(event) {
			c_artist++;
			if(c_artist == artists.length) {
				c_artist = 0;
			}
			update_screen(c_artist);
			event.preventDefault();
		});

		//click events on "room x" links
		for(var i=1; i<=5; i++) {
			$('#room_'+i).click(function(event) {
				c_artist = mapping[this.id.slice(-1)-1];
				update_screen(c_artist);
				event.preventDefault();
			});
		}
	}

	//start lightbox
	$('#artwork a').lightBox();
});

