/* Helper */

function show_element(id) {
	document.getElementById(id).style.display = "block";
}

function hide_element(id) {
	document.getElementById(id).style.display = "none";
}

function element_is_visible(id) {
	document.getElementById(id).style.visibility = "visible";
}

function element_is_invisible(id) {
	document.getElementById(id).style.visibility = "hidden";
}

/* Site */

function init_gallery(category,anz) {

	hide_all_photo_information();
	
	if(category != "") {
		
		tt = false;
		new Effect.Opacity('tooltip',
			{ 	duration: 0, 
				transition: Effect.Transitions.linear, 
				from: 1, to: 0 });
		init_tt(15000);
		
		count = 1;
		anzahl = anz;
		categoryname = category;
		
		if(preload_images(category,anz)) {
			display_photo(1);
		} else {
			alert("Die Bilder konnten nicht geladen werden.");
		}
		
		document.getElementById("content").onmouseover = function() {
			element_is_visible("next");
		}
		
		document.getElementById("content").onmouseout = function() {
			element_is_invisible("next");
		}
		
		document.getElementById("content").onclick = function() {
			next();
		}
	}
}

function display_photo(id) {
	current_photo = create_photo_id(id);
	
	set_photo_id(current_photo);
	update_photo(current_photo);
	update_photoinfo(id);
}

function hide_all_photo_information() {
	photoinfo = document.getElementsByTagName("dl");
	
	for(i=0; i < photoinfo.length; i++) {
		photoinfo[i].style.display = "none";
	}
}

function set_photo_id(current_photo) {
	b = document.getElementsByTagName("body");
	b[0].id = current_photo;
	position_infobox(current_photo);
}

function update_photo(current_photo) {
	document.getElementById("content").style.backgroundImage = "URL(../../../fotos/"+current_photo+".jpg)";
}

function update_photoinfo(id) {
	update_counter_string();
	hide_all_photo_information();
	show_element(id);
}

function update_counter_string() {
	c = count + " l " + anzahl;
	document.getElementById("counter").innerHTML = c;
}

function position_infobox(id) {
	info_oben = new Array("industry-4","nature-3","nature-4","nature-5","nature-7","visuals-1","visuals-3","visuals-8");
	abstand = "320px";
	for(i=0; i < info_oben.length; i++ ) {
		if(id == info_oben[i]) {
			abstand = "70px";
		}
	}
	document.getElementById("infobox").style.marginTop = abstand;
}

function create_photo_id(i) {
	return categoryname + "-" + count;
}

function next() {
	if((count+1) <= anzahl) {
		count++;
		display_photo(count);
		if(!tt) {
			set_tt_timeout(100);
		}
	} else {
		document.location = "../";
	}
}

/* Bild-Preloader */

function preload_images(category,anz) {
	photos = new Array();
	for(i=1; i <= anz; i++) {
		photos[i] = new Image();
		photos[i].src = "fotos/" + category + "-" + i + ".jpg";
	}
	
	duration = 0;
	timeout = 100;
	lastimage = category + "-" + anz;
	
	while((duration < timeout) && (document.images[lastimage].width != 10)) {
		duration++;
	}
	
	if(duration == timeout) {
		return false;
	} else {
		return true;
	}
}


/* tt einblenden */

function init_tt(timeout) {
	window.setTimeout("show_tt()", timeout);
}

function set_tt_timeout(timeout) {
	tt = true;
	window.setTimeout("hide_tt()", timeout);
}

function show_tt() {
	if(!tt) {
	show_element("tooltip-box");
	new Effect.Opacity('tooltip',
		{ 	duration: 0.5, 
			transition: Effect.Transitions.linear, 
			from: 0, to: 1 });
	}
	
}

function hide_tt() {
	new Effect.Opacity('tooltip',
		{ 	duration: 0.5, 
			transition: Effect.Transitions.linear, 
			from: 1, to: 0 });
	window.setTimeout("hide_element('tooltip-box')",1000);
}
