var weaveData = [];
var startPics = [];
var startPicsIndex = 0;
var selectedMenuIndex = "";
var submenu = null;
var selectedMenu = null;
var selectedSubMenu = null;
var currentMenu = null;
var busyGallery = null;

var subMenuWidth = 159;
var backBoxWidth = 146;
var picLeftBorderWidth = 2;
var currentContainerWidth = 3000;
var maxSubmenuHeight = 300;
var windowWidth = 0;
var textContainerWidth = 502;
var contentHeight = 616;	// Höhe des gesamten Contents inklusive Scrollbar.

var startImages = [];
var startGalleryId = -1;
var startMenuId = -1;

function fillSubmenuEntries( entries ){
	resetSubmenuScroll();

	$( "#submenu_inner" ).css( "margin-top", "0px" );
	$( ".sm_up,.sm_dw" ).css( "visibility", "hidden" );
	var isTemp = (arguments[1]) ? true : false;
	var container = $( "#submenu_inner" );
	var c = container[0];

	var ctr = 0;
	var template = null;

	if( c ){
		for( var n = c.firstChild; n!=null; n=n.nextSibling ){
			if( n.className == "smen_entry" ){
				if( !template ){ template = n; }
				if( ctr < entries.length ){
					var link = n.firstChild;
					link.innerHTML = entries[ctr].g_name;
					link.data = entries[ctr];
					link.id = "sm_gal_" + entries[ctr].g_id;
					link.href = "index.php?g=" + entries[ctr].g_id;;
					$( n ).show();
				}else{
					$( n ).hide();
				}
				ctr++;
			}
		}
	}else{
		container.children().each(function(){
			if( this.className == "smen_entry" ){
				if( !template ){ template = this; }
				if( ctr < entries.length ){
					var link = this.firstChild;
					link.innerHTML = entries[ctr].g_name;
					link.data = entries[ctr];
					link.id = "sm_gal_" + entries[ctr].g_id;
					link.href = "index.php?g=" + entries[ctr].g_id;;
					$( this ).show();
				}else{
					$( this ).hide();
				}
				ctr++;
			}
		});
	}

	while( ctr < entries.length ){
		var o = template.cloneNode( true );
		var link = o.firstChild;
		link.href = "index.php?g=" + entries[ctr].g_id;;
		link.className = "link_smen_c";
		link.innerHTML = entries[ctr].g_name;
//alert( link.id );
		if( c ){
			c.appendChild( o );
		}else{
			container.append( o );
		}

		link.id = "sm_gal_" + entries[ctr].g_id;
		link.data = entries[ctr];

//		$( link ).click(function(e){ showGallery( this ); return false; });
		$( link ).focus(function(e){ this.blur(); });

		ctr++;
	}

	container.show();

	function updateScroller(){
		smContainerHeight = container.innerHeight();
		if( smContainerHeight > maxSubmenuHeight ){ $( ".sm_dw" ).css( "visibility", "visible" ); }
	}
	setTimeout( updateScroller, 150 );	// Dauert ein Weilchen, bis die Daten gesetzt wurden.
}


function fillFotostrecke( entries ){
	var container = $( "#fotostrecke" );

	var containerWidth = subMenuWidth + backBoxWidth;

	var ctr = 0;
	var template = null;
	var c = container[0];
	var images = [];
	if( c ){
		for( var n = c.firstChild; n!=null; n=n.nextSibling ){
			if( n.className == "galpic" ){
				if( !template ){ template = n; }
				if( ctr < entries.length ){
					n.style.visibility = "hidden";
					n.style.display = "none";
					n.src = entries[ctr].path;
					n.width = entries[ctr].width;
					containerWidth += entries[ctr].width + picLeftBorderWidth;
					n.height = entries[ctr].height;
					n.data = entries[ctr];
					images.push( n );
					//$( this ).show();
				}else{
					$( n ).hide();
				}
				ctr++;
			}
		}
	}else{
		container.children().each(function(){
			if( this.className == "galpic" ){
				if( !template ){ template = this; }
				if( ctr < entries.length ){
					this.style.visibility = "hidden";
					this.style.display = "none";
					this.src = entries[ctr].path;
					this.width = entries[ctr].width;
					containerWidth += entries[ctr].width + picLeftBorderWidth;
					this.height = entries[ctr].height;
					this.data = entries[ctr];
					images.push( this );
					//$( this ).show();
				}else{
					$( this ).hide();
				}
				ctr++;
			}
		});
	}
	while( ctr < entries.length ){
		//var e = document.createElement( "img" );
		var e = template.cloneNode( true );
		e.style.visibility = "hidden";
		e.style.display = "none";
		//e.className = "galpic";
		e.src = entries[ctr].path;
		e.width = entries[ctr].width;
		containerWidth += e.width + picLeftBorderWidth;
		e.height = entries[ctr].height;
		e.data = entries[ctr];

		container.append( e );
		images.push( e );
		ctr++;
	}

	resetFotoScroll();
	$( "#container" ).css( "width", containerWidth + "px" );
	for( var i=0; i<images.length; i++ ){ images[i].style.display = "block"; }
	currentContainerWidth = containerWidth;
	updateFotoScroll();

	container.show();
	setTimeout( function(){busyGallery = null;}, 300 );
}

function showImage( img ){
	if( img.src == "" ){ return; }
	img.style.visibility = "visible";
	img.style.display = "block";
}


$(function(){
	$( "#container" ).css( "width", "auto" );


	for( var i=0; i<weaveData.length; i++ ){
		var d = weaveData[i];
		var o = document.getElementById( d.target );
		if( o ){ o.data = d; }
	}
	submenu = document.getElementById( "submenu" );
	selectedMenu = (selectedMenuIndex != "") ? document.getElementById( selectedMenuIndex ) : null;

	$( ".link_mmen_c,.link_mmen_h" ).click(function(e){
		if( selectedMenu ){ selectedMenu.className = "link_mmen_c"; }
		this.className = "link_mmen_h";
		selectedMenu = this;
		currentMenu = this;
		showContent( 'mainmenu', this );
	});

//	$( ".link_smen_h,.link_smen_c" ).click(function(e){ showGallery( this ); });
	$( "a" ).focus(function(e){ this.blur(); });


	new Image().src = "images/mpf_h.gif";
	new Image().src = "images/bpf_h.gif";
	new Image().src = "images/pfup_c.png";
	new Image().src = "images/pfdw_c.png";
	new Image().src = "images/pfup_h.png";
	new Image().src = "images/pfdw_h.png";
	new Image().src = "images/mbgd.png";

	var o = $( ".sm_up" )[0];
	if( o ){
		o.direction = "up";
		o.imgHot = "./images/pfup_h.png";
		o.imgCold = "./images/pfup_c.png";
	}else{
		throw "submenu up scroll not found";
	}
	var o = $( ".sm_dw" )[0];
	if( o ){
		o.direction = "down";
		o.imgHot = "./images/pfdw_h.png";
		o.imgCold = "./images/pfdw_c.png";
	}else{
		throw "submenu down scroll not found";
	}

	var h = function(){
		var menuWidth = 0;
		$( "#menu" ).children().each(function(){
			menuWidth += $( this ).outerWidth( true ) + 5;
		});
		//$( "#menu" ).css( "width", menuWidth + "px" );
	};
	setTimeout( h, 50 );	// Sonst ist der DOM wieder nicht bereits und passt nix.

	$( ".sm_up,.sm_dw" ).mousedown(function(e){
		if( this.style.visibility != "visible" ){ return; }
		this.style.backgroundImage = "url(" + this.imgHot + ")";
		smScrollDirection = this.direction;
		startSubmenuScroll();
	});
	$( ".sm_up,.sm_dw" ).mouseup(function(e){
		if( this.style.visibility != "visible" ){ return; }
		stopSubmenuScroll();
	});
	$( ".sm_up,.sm_dw" ).mouseover(function(e){
		if( this.style.visibility != "visible" ){ return; }
		this.style.backgroundImage = "url(" + this.imgHot + ")";
	});
	$( ".sm_up,.sm_dw" ).mouseout(function(e){
		if( this.style.visibility != "visible" ){ return; }
		this.style.backgroundImage = "url(" + this.imgCold + ")";
	});


	//currentContainerWidth = $( "#container" ).outerWidth();
	currentContainerWidth = subMenuWidth + backBoxWidth;
	if( startPics.length > 0 ){ currentContainerWidth += startPics[0].width + picLeftBorderWidth; }
	$( "#container" ).css( "width", currentContainerWidth + "px" );

	windowWidth = $( window ).width();
	$( window ).resize( function(e){
		windowWidth = $( window ).width();
		var h = $( window ).height() - contentHeight;
		if( h < 0 ){ h = 0; }
		$( "#stonko" ).css( { width: windowWidth + "px", height : h + "px" } );
		updateFotoScroll();
	});

	fScrollMinX = $( ".scl" ).outerWidth();
	fScrollMaxX = $( ".scr" ).offset().left - $( "#slider" ).outerWidth();
	fScrollRange = fScrollMaxX - fScrollMinX;

	$( "#slider" ).css( "left", fScrollMinX + "px" );


	$( "#slider" ).draggable({
		axis 			: "x",
		containment	: [fScrollMinX, 0, fScrollMaxX, 1500],
		scroll		: false,
		cursor		: "pointer",
		drag			: function( e, ui ){
			var x = ui.position.left - fScrollMinX;
			//var x = ui.item.position().left;
			if( x < 0 ){
				x = 0;
			}else if( x > fScrollRange ){
				x = fScrollRange;
			}
			fScrollPosPercentage = x / fScrollRange;

			//ui.helper[0].style.cursor = "pointer";
			//var s = "position : " + Math.round( fScrollPosPercentage * 100 ) + " %";
			//var s = "";
			//var s = "cursor: " + ui.helper[0].style.cursor;
			//for( var p in ui.helper[0] ){ s += p + " : " + ui.helper[0][p] + "<br />"; }
			//$( "#fotostrecke" ).hide();
			//$( "#text_content" ).html( s );
			//$( "#text_content" ).show();
//alert( ui.helper.length );
//for( var i=0; i<ui.helper.length; i++ ){ }

			var scroll = Math.round( fScrollWidth * fScrollPosPercentage );
			if( scroll < 0 ){
				// Sann sitzen sowohl der Container als auch die Scrollbar ganz rechts.
				scroll = 0;
				var pos = fScrollMinX;
				$( "#slider" ).css( "left", pos + "px" );
			}
			$( "#container" ).css( "margin-left", "-" + scroll + "px" );
		},
		start			: function( e, ui ){
			//$( document.body ).css( "cursor", "pointer" );
			//$( ".scroll_cont" ).css( "cursor", "pointer" );
		},
		stop			: function( e, ui ){
			//$( document.body ).css( "cursor", "auto" );
			//$( ".scroll_cont" ).css( "cursor", "auto" );
		}
	});

	var h = $( window ).height() - contentHeight;
	if( h < 0 ){ h = 0; }
	$( "#stonko" ).css( { width: windowWidth + "px", height : h + "px" } );

	$( "#slider" ).disableSelection();

	if( startGalleryId > 0 && startImages.length > 0 && startMenuId > 0 ){
		var m = document.getElementById( "menu_entry_" + startMenuId );
		if( m ){
			selectedMenu = m;
			currentMenu = m;
			m.className = "link_mmen_h";
			fillSubmenuEntries( m.data.entries );
			$( "#submenu" ).show();
			var g = document.getElementById( "sm_gal_" + startGalleryId );
			showGallery( g, startImages );
		}
	}

});


/*
backbox
submenu
submenu_inner
scrollbar
slider

fotostrecke
text_content
*/
function showContent( ident/*, obj */ ){
	/*$( ".nav_close_links,.nav_close_rechts,.pfeil_links,.pfeil_rechts" ).hide();
	$( "#submenu,#content_pic,#content_text,#content_gallery" ).hide();
	$( "#" + ident ).show();*/
	$( "#backbox" ).show();
	$( "#text_image_container" ).hide();
	$( document.body ).css( "background-image", "url(images/mbg.png)" );
	if( ident == "startseite" ){
		$( "#fotostrecke,#backbox_link,#submenu,#submenu_inner,#scrollbar,#slider,#text_content" ).hide();
		if( currentMenu ){
			currentMenu.className = "link_mmen_c";
			currentMenu = null;
		}
		if( selectedSubMenu ){
			selectedSubMenu.className = "link_smen_c";
			selectedSubMenu = null;
		}
		if( startPics.length > 0 ){
			startPicsIndex++;
			if( startPicsIndex >= startPics.length ){ startPicsIndex = 0; }
			var img = startPics[startPicsIndex];
			fillFotostrecke( [img] );
			$( "#fotostrecke" ).show();
		}
	}else if( ident == "mainmenu" ){
		var obj = arguments[1];
		if( !obj || !obj.data ){ throw "no main menu specified or main menu has no data"; }

		if( selectedMenu ){ selectedMenu.className = "link_mmen_c"; }
		currentMenu.className = "link_mmen_h";
		selectedMenu = currentMenu;
		if( obj.data.type == "text" ){
			selectedSubMenu = null;	// Text hat kein Submenü.
			currentContainerWidth = subMenuWidth + textContainerWidth + backBoxWidth;
			$( "#container" ).css( "width", currentContainerWidth + "px" );
			resetFotoScroll();
			updateFotoScroll();
			//var contentWidth = windowWidth - subMenuWidth - 2;	// 2 Pixel linker Rand.
			//$( "#text_content_innen" ).css( "width", contentWidth + "px" );
			//$( "#container" ).css( "width", windowWidth + "px" );
			$( "#fotostrecke,#submenu,#submenu_inner,#backbox_link,#backbox" ).hide();
			$( "#text_head" ).html( obj.data.title );
			$( "#text_body" ).html( Acu.nl2br(obj.data.text) );
			$( "#text_body" ).css( "line-height", obj.data.lineHeight + "px" );
			$( document.body ).css( "background-image", "url(images/mbgd.png)" );
			//$( "#text_image_container" ).show();
			if( obj.data.image != "" ){
				var img = $( "#text_image" )[0];
				if( img ){
					if( !img.initialized ){
						$( img ).load(function(e){ this.parentNode.style.display='block'; });
						img.initialized = true;
					}

					var dim = new String( obj.data.imageDimension ).split( "x" );
					img.width = dim[0];
					img.height = dim[1];
					img.src = "images/article/" + obj.data.image;
				}
			}
			if( CryptedMail && CryptedMail.transform ){ CryptedMail.transform(); }
			$( "#text_content,#scrollbar_dummy" ).show();
		}else{
			location.href = "index.php?g=" + obj.data.entries[0].g_id;

			/*$( "#text_content" ).hide();
			fillSubmenuEntries( obj.data.entries );
			var o = document.getElementById( "sm_gal_" + obj.data.entries[0].g_id );
			showGallery( o );
			$( "#submenu" ).show();*/
		}
	}
}

function showGallery( obj, images ){
	if( !obj || !obj.data ){ throw "no gallery specified or gallery has no data"; }
	var data = obj.data;

	if( selectedSubMenu ){ selectedSubMenu.className = "link_smen_c"; }
	$( ".smen_entry > a" ).removeClass( "link_smen_h" ).addClass( "link_smen_c" );
	obj.className = "link_smen_h";
	selectedSubMenu = obj;

	$( "#fotostrecke" ).hide();

	fillFotostrecke( images );
	$( "#fotostrecke" ).show();

	/*$.ajax({
		type: "POST",
		url: "stdctrl.php",
		dataType: "json",
		data: { c : "fetch_gallery", gallery : obj.data.g_id },
		async: true,
		error: function( request ){
			alert( "error sending request for gallery data to server\n" + request.responseText );
		},
		success: function( reply ){
			if( reply.error ){
				alert( reply.errmsg );
			}else{
				fillFotostrecke( reply.images );
				$( "#fotostrecke" ).show();
			}
		}
	});*/
}



var smScrollPos = 0;
var smScrollTimer = null;
var smScrollTimerInterval = 50;
var smScrollSchrittweite = 3;
var smScrollDirection = "down";
var smContainerHeight = 0;
function scrollSubmenu(){
	if( smScrollDirection == "up" ){
		smScrollPos += smScrollSchrittweite;
	}else{
		smScrollPos -= smScrollSchrittweite;
	}

	if( smScrollPos >= 0 ){
		smScrollPos = 0;
		if( smScrollTimer ){
			clearInterval( smScrollTimer );
			smScrollTimer = null;
		}
		$( ".sm_up" ).css( "visibility", "hidden" );
		var o = $( ".sm_up" )[0];
		if( o ){ o.style.backgroundImage = "url(" + o.imgCold + ")"; }
	}else if( smScrollPos < 0 ){
		if( smContainerHeight + smScrollPos <= maxSubmenuHeight ){
			if( smScrollTimer ){
				clearInterval( smScrollTimer );
				smScrollTimer = null;
			}
			$( ".sm_dw" ).css( "visibility", "hidden" );
			var o = $( ".sm_dw" )[0];
			if( o ){ o.style.backgroundImage = "url(" + o.imgCold + ")"; }
		}else{
			$( ".sm_up" ).css( "visibility", "visible" );
			$( ".sm_dw" ).css( "visibility", "visible" );
		}
	}

	$( "#submenu_inner" ).css( "margin-top", smScrollPos + "px" );
}

function resetSubmenuScroll(){
	if( smScrollTimer ){
		clearInterval( smScrollTimer );
		smScrollTimer = null;
	}
	smScrollPos = 0;
	$( "#submenu_inner" ).css( "margin-top", "0px" );
	smScrollDirection = "down";
}

function startSubmenuScroll(){
	if( !smScrollTimer ){ smScrollTimer = setInterval( scrollSubmenu, smScrollTimerInterval ); }
}

function stopSubmenuScroll(){
	if( smScrollTimer ){
		clearInterval( smScrollTimer );
		smScrollTimer = null;
	}
}



var fScrollMinX = 0;
var fScrollMaxX = 0;
var fScrollRange = 0;
var fScrollPosPercentage = 0;
var fScrollWidth = 0;	// Bereich, der gescrollt werden muss, dh. Containerbreite minus Fensterbreite.
function updateFotoScroll(){
	if( (currentContainerWidth-backBoxWidth) > windowWidth ){
		$( "#scrollbar_dummy" ).hide();
		$( "#slider,#backbox_link,#scrollbar" ).show();
	}else{
		fScrollPosPercentage = 0;
		$( "#slider,#backbox_link,#scrollbar" ).hide();
		$( "#scrollbar_dummy" ).show();
	}

	fScrollMinX = $( ".scl" ).outerWidth();
	fScrollMaxX = $( ".scr" ).offset().left - $( "#slider" ).outerWidth();
	fScrollRange = fScrollMaxX - fScrollMinX;
	fScrollWidth = currentContainerWidth - windowWidth;

	var pos = fScrollMinX + Math.round( fScrollRange * fScrollPosPercentage );

	var scroll = Math.round( fScrollWidth * fScrollPosPercentage );
	if( scroll < 0 ){
		// Sann sitzen sowohl der Container als auch die Scrollbar ganz rechts.
		scroll = 0;
		pos = fScrollMinX;
	}

	$( "#slider" ).css( "left", pos + "px" );
	$( "#slider" ).draggable( 'option', 'containment', [fScrollMinX, 0, fScrollMaxX, 1500] );
	$( "#container" ).css( "margin-left", "-" + scroll + "px" );
}


function resetFotoScroll(){
	$( "#slider" ).css( "left", fScrollMinX + "px" );
	$( "#container" ).css( "margin-left", "0px" );
	fScrollPosPercentage = 0;
}


