/* ON LOAD */
$(document).ready(function() {
		
// On the startpage, add an image for transition effect
	if ($("body#start").length > 0) {
		// Hide folders
		$("#navi-level1 li").hide();

		$("#page").after('<img src="fileadmin/templates/images/bg_no_animation_start.gif" alt="" id="start-transition" />');
		$("#start-transition").fadeOut(4000, function () {
		});
		
		// Show folders shortly before finishing the transition
		setTimeout(function(){$("#navi-level1 li").show();}, 3500);
	}
	
// Make all folders on desktop draggable
	$("#navi-level1 li").draggable({ cursor: 'move', containment: '#page' });

// Make all windows draggable
	$(".window").draggable({ cursor: 'move', cancel: '.window-inner, .close-window', stack: { group: '.window', min: 1 } });

// Give all windows focus on click
	$(".window").click( function () {
		$(this).css( 'zIndex',  getHighestValues());
	});

// All internal links must open their target in a window
	setInternalLinks();
	
// Set external links
	$("a.neues_fenster").attr("target", "_blank");

});

function setInternalLinks() {
	$(".window-inner a[href]").not(".window-inner a[href^=mailto:], .window-inner a[href^=http://], .window-inner a[href^=www], .window-inner a[href$=.pdf]").each(function() {
		var href = $(this).attr('href').split('.');
		$(this).click( function () {				
			showWindow(href[0]);
			return false;
		});
	});
	// IE returns full path as href for AJAX-loaded content, so we need a different approach here
	//$(".window-inner a[href^=http://127.0.0.1/markenbauer/websucht.info/]").not(".window-inner a[href$=.pdf]").each(function() {
	$(".window-inner a[href^=http://www.websucht.info/]").not(".window-inner a[href$=.pdf]").each(function() {
		var href = $(this).attr('href').split('/').pop();
		var filename = href.split('.');
		$(this).click( function () {				
			showWindow(filename[0]);
			return false;
		});
	});
}

/* OPEN WINDOWS */
var windowCode = '<div class="window content" id="###WINDOW-ID###"><div class="h1-container"><h1>###WINDOW-TITLE###</h1></div><a href="#" onclick="javascript:closeWindow(\'###WINDOW-ID###\');return false;" class="close-window">&nbsp;</a><div class="inner-container"><div class="window-inner">###WINDOW-CONTENT###</div></div></div>';

function showWindow(pageid) {
	
	var windowOutputCode = '';
	
	// Append window, if it does not exist yet
	if (!$("#"+pageid).length) {
		
		// Give the window an ID
		windowOutputCode = windowCode.replace(/###WINDOW-ID###/g, pageid);
		
		// Append it to the page
		$("#content").append(windowOutputCode);
		
		// Hide it for a short while
		$("#"+pageid).hide();
		
		// Fill in the contents
		$("#"+pageid+" div.h1-container").load(pageid+".html #"+pageid+" h1", '', function () {
			$("#"+pageid+" div.inner-container").load(pageid+".html #"+pageid+" div.window-inner", '', function () {
				// Give focus right now
				$("#"+pageid).css( 'zIndex', getHighestValues() );
				
				// Set initial position
				var newPosition = getPosition(pageid);
				$("#"+pageid).css( 'left', newPosition[0] );
				$("#"+pageid).css( 'top', newPosition[1] );
				
				// Give it focus on click
				$("#"+pageid).click( function () {
					$(this).css( 'zIndex', getHighestValues() );
				});
				
				// Make it draggable
				$("#"+pageid).draggable({ cursor: 'move', cancel: '.window-inner, .close-window', stack: { group: '.window', min: 1 } });
				
				// Show it
				$("#"+pageid).show();
				
				// Activate scripts
				if ($("a.javascript").length > 0) {
					var videoScript = $("a.javascript").html();
					$("a.javascript").replaceWith('<script type="text/javascript">' + videoScript + '</script>');
				}
				
				// Set the external links in that window
				$("#"+pageid+" a.neues_fenster").attr("target", "_blank");
				
				// All internal links must open their target in a window
				setInternalLinks();
			});
		});
	}
	// But if the window does already exist, give focus to it
	else {
		// Set a small timeout, so this z-index gets calculated as the final one
		setTimeout(function(){$('#'+pageid).css( 'zIndex', getHighestValues() );}, 100);
	}
}

function doShowWindow(windowid) {
	$("#"+windowid).show();
}

function closeWindow(windowid) {
	//curx = curx-13;
	//cury = cury-26;
	$("#"+windowid).remove();
}

function getHighestValues() {
	var zmax = 0;

	$(".window").each(function() {
		var curz =  $(this).css('zIndex');
		zmax = parseInt(curz > zmax ? curz : zmax)+1;
	});
	
	return parseInt(zmax)+1;
}

var curx = 0;
var cury = 10;
function getPosition(pageid) {
	// Wenn die Y-Position + die Höhe noch im Anzeigebereich sind, erstelle die Kaskade
	if ((cury+26)+$("#"+pageid).height() < document.documentElement.clientHeight) {
		curx = curx+13;
		cury = cury+26;
	}
	else {
		curx = 0;
		cury = 10;
	}
	
	return new Array(curx, cury);
}