var scenes = new Array();
var scene_width  = 950;
var scene_flash  = 0;

var scene_dest   = 0;
var scene_offset = 0;
var scene_shown  = Array();
var scene_img;
var scene_background;
var scene_imgView;
var scene_largeWmax = 800;
var scene_largeHmax = 600;
var scene_imgPre    = null;

function initScene(offset) {
	var ruler = 0;
	var cScenes = document.getElementById('scenes');
	var cActive = document.getElementById('active');
	var cBar1 = document.getElementById('bar1');
	var cBar2 = document.getElementById('bar2');
	if(!cScenes||!cBar1||!cBar2) return;
	if(!offset) offset = 0;

	if(!scene_imgView) {
		scene_img = document.getElementById('scene_image');
		
		scene_background = document.getElementById('background');
		scene_background.style.visibility = 'hidden';
		
		scene_imgView = document.getElementById('scene_image_view');
		

		if( scene_img && scene_imgView && scene_background )
		{
			scene_imgView.onmousedown = function () { 
			scene_img.style.visibility = 'hidden'; 
			scene_background.style.visibility = 'hidden';
			};
			
			scene_background.onmousedown = function () { 
			scene_img.style.visibility = 'hidden'; 
			scene_background.style.visibility = 'hidden';
			};
			
		}
	}
	

	for(var num in scenes)
	{
		scene_shown[num] = num == 0;

		scene = document.createElement("div");
		scene.className = "scene" + (num == 0 ? " first" : "");
		scene.id = scenes[num]["id"];

		scene_ul = document.createElement("ul");
		scene_ul.className = "img";
		for(var inum in scenes[num]["pics"])
		{
			scene_li = document.createElement("li");
			scene_a = document.createElement("a");
			if( scenes[num]["pics"][inum]["l"] )
			{
				scene_a.href = scenes[num]["pics"][inum]["l"];
			}
			else if( scenes[num]["pics"][inum]["h"] )
			{

				scene.innerHTML = scenes[num]["pics"][inum]["h"];
			}
			else
			{
				scene_a.href = "javascript:popupScene("+num+",'"+inum+"')";
			}
			scenes[num]["pics"][inum]["img"] = document.createElement("img");
			scene_a.appendChild(scenes[num]["pics"][inum]["img"]);
			scene_li.appendChild(scene_a);
			scene_ul.appendChild(scene_li);
		}
		scene.appendChild(scene_ul);

		scene_h1 = document.createElement("h1");
		scene_h1.innerHTML = scenes[num]["name"];
		scene.appendChild(scene_h1);
		
		if( num > 0 )
		{
			scene_p = document.createElement("p");
			scene_a = document.createElement("a");
			scene_a.href = "gal.php?off="+num+"&gal="+urlencode(scene.id)+"&ttl="+urlencode(scenes[num]["bar"]);
			scene_a.innerHTML = "Galerie anzeigen";
			scene_p.appendChild(scene_a);
			scene.appendChild(scene_p);
		}

		scene_ul = document.createElement("ul");
		scene_ul.className = "ruler";
		for(var i=0; i<19; i++)
		{
			scene_li = document.createElement("li");
			scene_li.innerHTML = ruler;
			scene_ul.appendChild(scene_li);
			ruler++;
		}
		scene.appendChild(scene_ul);

		cScenes.appendChild(scene);

		scene_li = document.createElement("li");
		scene_a = document.createElement("a");
		scene_a.href = "javascript:showScene("+num+")";
		scene_a.className = "";
		scene_a.innerHTML = scenes[num]["bar"];
		scene_li.appendChild(scene_a);
		scenes[num]["ruler"] = scene_a;

		if( cActive && num == 0 )
		{
			cActive.href = "javascript:showScene(0)";
		}
		else if( num <= scenes.length / 2 )
		{
			if( cBar1.childNodes.length == 0 ) scene_li.className = "first";
			cBar1.appendChild(scene_li);
		}
		else
		{
			if( cBar2.childNodes.length == 0 ) scene_li.className = "first";
			cBar2.appendChild(scene_li);
		}
	}

	// load the images
	for(var num in scenes)
	{
		for(var inum in scenes[num]["pics"])
		{
			scenes[num]["pics"][inum]["img"].src = "image.php?type=home&file="+urlencode(scenes[num]["pics"][inum]["f"]);
		}
	}
	
	if(offset) {
		scene_offset = scene_width * offset + 2;
		scene_dest = scene_width * offset;
		scrollScene()
	}
	
	window.setTimeout('flashScene()', 1000);
}

function showScene(scene_next) {
	scene_dest = scene_width * scene_next;
	scrollScene();
	scene_flash = 0;
}

function scrollScene() {
	var num = 0;
	var iOffset = 0;
	var scene;

	if(scene_dest < scene_offset )
	{
		scene_offset -= Math.floor(( scene_offset - scene_dest ) / 5 + 1);
	}
	else if( scene_dest > scene_offset )
	{
		scene_offset += Math.floor(( scene_dest - scene_offset ) / 5 + 1);
	}
	else
	{
		return;
	}

	for(var num in scenes)
	{
		scene = document.getElementById(scenes[num]["id"]).style;
		iOffset = scene_width * num;

		if( iOffset > scene_offset - scene_width && iOffset < scene_offset + scene_width )
		{
			scene.left = ( iOffset - scene_offset )+'px';
			if( !scene_shown[num] )
			{
				scene_shown[num] = 1;
				scene.display = 'block';
			}
		}
		else if( scene_shown[num] )
		{
			scene_shown[num] = 0;
			scene.display='none';
		}
	}
	
	window.setTimeout('scrollScene()', 50);
}

function popupScene(num, inum, retry) {
	var s = scenes[num]["pics"][inum];
	
	if( !retry )
	{
		scene_imgPre = document.createElement("img");
		scene_imgPre.src = "image.php?type=small&file="+urlencode(s["f"]);
		retry = 0;
	}
	else if( retry > 50 )
	{
		scene_imgPre = null;
		alert("Bild "+num+"/"+inum+" konnte nicht geladen werden.");
		return;
	}
	if( !isImageOk(scene_imgPre))
	{
		retry++;
		window.setTimeout("popupScene('"+num+"', '"+inum+"', "+retry+")", 100 );
		return;
	}

	n=scenes[num]["id"]+inum;
	w = scene_imgPre.width;
	h = scene_imgPre.height;

	if( w < scene_largeWmax && h < scene_largeHmax )
	{
		if( w / h > scene_largeWmax / scene_largeHmax )
		{
			h = scene_largeWmax * h / w;
			w = scene_largeWmax;
		}
		else
		{
			w = scene_largeHmax * w / h;
			h = scene_largeHmax;
		}
	}
	
	scene_imgView.innerHTML = '<img src="image.php?type=large&file='+urlencode(s['f'])+'" width="'+w+' height="'+h+' alt="'+scenes[num]["name"]+'">';
	scene_imgView.style.left = - Math.floor((w + 20) / 2)+'px';
	scene_imgView.style.top = - Math.floor((h + 20) / 2)+'px';
	scene_imgView.style.width = (w + 20)+'px';
	scene_imgView.style.height = (h + 20)+'px';
	scene_img.style.visibility = 'visible';
	scene_background.style.visibility = 'visible';

}

function flashScene(next) {
	var s
	var n = 0;
	var t = 0;
	
	if( !scene_flash ) return;
	if( !next ) next = 0;
	for(s in scenes)
	{
		if(t) {
			n = s;
			t = 0;
		}
		if(s == next) {
			t = 1;
			scenes[s]["ruler"].className = "flash";
			window.setTimeout('scenes['+s+']["ruler"].className = ""', 100);
		}
	}

	window.setTimeout('flashScene('+n+')', n ? 30 : 2000);
}

function urlencode( str ) {
    // URL-encodes string  
    // 
    // version: 904.1412
    // discuss at: http://phpjs.org/functions/urlencode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brettz9.blogspot.com)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var histogram = {}, tmp_arr = [];
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u00DC'] = '%DC';
    histogram['\u00FC'] = '%FC';
    histogram['\u00C4'] = '%D4';
    histogram['\u00E4'] = '%E4';
    histogram['\u00D6'] = '%D6';
    histogram['\u00F6'] = '%F6';
    histogram['\u00DF'] = '%DF';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function isImageOk(img) {
    if (!img.complete) {
        return false;
    }

    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    return true;
}
