// JavaScript Document

var current;
var currentPage;
var numPages;

var interval;
var isPlaying = false;

var attachments = new Array();

var array;


window.onload = function() {
	currentPage = 0;
	array = getImageArray();
	numPages = Math.ceil(array.length/6);
	openImage(0);
}

function writeOn() {
	if(commentShow) {
		$(document.getElementById("write-comment")).hide("slow");
	} else {
		$(document.getElementById("write-comment")).show("slow");
	}
	commentShow = !commentShow;
}

function imageClicked(id) {
	openImage(id);
}

function openImage(id) {
	if(current!=id) {
		if(id > array.length-1) id = 0;
		var src = array[id][0];
		var width = array[id][1];
		var height = array[id][2]; 
		var container = document.getElementById("display");
		var div = document.createElement("div");
		div.setAttribute("id", "photo-"+id);
		div.setAttribute("class", "photo-container");
		
		div.innerHTML = "<div class='pic-title'>"+array[id][3] +"</div>";
		div.innerHTML += "<img src='"+src+"' height='331px' />";
		
		if(current != null) {
			var remove = document.getElementById("photo-"+current);
			$(remove).fadeOut('fast', function() { container.removeChild(remove) });
		}
		
		container.appendChild(div);
		
		$(div).fadeIn();
		
		$(document.getElementById("image-"+current)).animate({opacity:".5"}, 0);
		current = id;
		$(document.getElementById("image-"+current)).animate({opacity:"1"}, 0);
		
		gotoPage(Math.floor(current/6));
	}
}

function next() {
	var to = current;
	openImage(to+1);
}

function previous() {
	openImage(current-1);
}

window.onkeypress = function(event) {
	switch(event.keyCode) {
		case 39:
			next();
			break;
		case 37:
			previous();
			break;
	}
}

function nextPage() {
	if(currentPage < numPages-1){
		var container = document.getElementById("thumb-container");
		currentPage++;
		$(container).animate({left:-(currentPage*498)});
	}
}

function previousPage() {
	if(currentPage > 0) {
		var container = document.getElementById("thumb-container");
		currentPage--;
		$(container).animate({left:-(currentPage*498)});
	}
}

function gotoPage(to) {
	if(to < numPages-1) {
		var container = document.getElementById("thumb-container");
		currentPage = to;
		$(container).animate({left:-(currentPage*498)});
	}
}

function togglePlay() {
	if(isPlaying) {
		clearInterval(interval);
		isPlaying = false;
		var button = document.getElementById("play-pause");
		button.setAttribute("class", "nav play");
	} else {
		interval=setInterval(function() { next(); }, 5000);
		isPlaying = true;
		var button = document.getElementById("play-pause");
		button.setAttribute("class", "nav pause");
	}
}

