var site = null;

function WrapperControl(tw, th, w, h, startel)
{
	this.chain = new Array();
	this.chain[0] = startel;
	this.loading = 0;
	
	this.lastel = null;
	
	for(var y = 0; y < th; y++)
	{
		for(var x = 0; x < tw; x++)
		{
			var elid = 'e_'+(y*tw+x+1);
			
			if(elid != startel)
				this.chain[this.chain.length] = elid;
			
			$("#e_wrapper").append('<div id="'+elid+'" class="box" style="left:'+(x*w)+'px; top:'+(y*h)+'px; z-index:10;"><div style="position:absolute; left:320px; top:265px;"><img border="0" src="images/loading_large.gif"></div></div>');
			$("#" + elid).css("opacity", "0.2");
		}
	}
	
	this.loadNext = function()
	{
		var elid = this.chain[this.loading];
		
		$("#" + elid).load(elid+'.html', function()
		{
			$(this).onImagesLoad({ 
	        callbackIfNoImagesExist: site.setLoaded,
	        selectorCallback: site.setLoaded
	    });
		});
	}
	
	this.setLoaded = function(el)
	{
		var divid = el.attr("id");
		var menid = "l" + divid.split("_")[1];
		
		$("#" + menid + " .loading").remove();
		$("#" + menid).unbind('click');
		/*
		$("#" + menid).click(function() {
			site.activate(divid);
		});
		*/
		
		if(site.loading == 0)
		{
			if(!location.hash)
				location.hash = "#Anfang";
			window.setInterval('checkHash();', 100);
			//site.activate(startel);
		}
		
		if(site.loading < site.chain.length-1)
		{
			site.loading++;
			site.loadNext();
		}
	}
	
	this.activate = function(el_id)
	{
		var menid = "l" + el_id.split("_")[1];
		$("#" + menid + " img").attr("src", $("#" + menid + " img").attr("src").replace(/_off/, "_on"));
		
		$("#" + menid + " img").css("filter", $("#" + menid + " img").css("filter").replace(/_off/, "_on"));
		
		if(this.lastel)
		{
			menid = "l" + this.lastel.attr("id").split("_")[1];
			$("#" + menid + " img").attr("src", $("#" + menid + " img").attr("src").replace(/_on/, "_off"));
			
			$("#" + menid + " img").css("filter", $("#" + menid + " img").css("filter").replace(/_on/, "_off"));
		}
		
		if(this.lastel && el_id == this.lastel.attr("id"))
			return;
		var el = $("#" + el_id);
		
		el.stop();
		el.animate({
			opacity: 1
		}, 500);
		
		if(this.lastel)
		{
			this.lastel.stop();
			this.lastel.animate({
				opacity: 0.2
			}, 500);
		}
		
		$("#e_wrapper").stop();
		$("#e_wrapper").animate({ 
        left: "-" + el.css("left"),
        top: "-" + el.css("top")
    	}, 500, "easeOutQuad");
    
    this.lastel = el;
	}
	
	this.loadNext();
}

var logobig = new Image;
logobig.src = "images/pannobile_gross.png";

$(document).ready(function(){
	var startel = null;
	
	if(location.hash)
	{
		var el = $("#mainnav li a[href='" + location.hash + "']");
		if(el.length != 0)
			startel = "e_" + el.attr("id").split("l")[1];
	}
	
	if(!startel)
		startel = "e_5";
	
	site = new WrapperControl(3, 4, 1000, 690, startel);
	
	$("#mainnav li a").append('&nbsp;<img class="loading" border="0" src="images/tiny_loading.gif">');
	$("#mainnav li a").click(function(ev){ev.preventDefault();});
	
	$("#mainnav").draggable();
		
	$("#mainnav").mouseenter(function(){
		$("#mnc").show();
	}).mouseleave(function(){
    $("#mnc").hide();
  });
  

	$.ifixpng.pixel = 'images/nix.gif';
	$("img[src$='.png'], .pngfix").ifixpng();
});

var oldhash = "";
function checkHash()
{
	var newhash = location.hash;
	if(oldhash != newhash)
	{
		oldhash = newhash;
		site.activate("e_" + $("#mainnav li a[href='" + newhash + "']").attr("id").split("l")[1]);
	}
}

function make_logobig()
{
	$("#logosmall").mouseover(function() {
		$("#logobig").show();
		$(this).hide();
		$("#logobig").stop();
		$("#logobig").animate({
			width: 413
		}, 300, "easeOutQuad");
	});
	$("#anfangimg").mouseover(function() {
		if($("#logobig").css("display") == "block" || $("#logobig").css("display") == "inline")
		{
			$("#logobig").stop();
			$("#logobig").animate({
				width: 207
			}, 300, "easeOutQuad", function() {$("#logobig").hide();$("#logosmall").show();});
		}
	});
}



function update_scrollpane(){
	$('#bb_story').jScrollPane(
		{
			showArrows: false,
			scrollbarWidth: 26
		}
	);
}

