$(document).ready(function() 
{
	// Hauptseite laden
	LoadMain();
	
	// Administration
	Administration();
	
	// Links generieren
	CreateLinks();
	
	// Header �ndern
	ChangeHeader();
	
	// DropShadow
	CreateDropShadow();
	
	// Inhalt vorladen
	Preloader();
});

function Administration()
{
	var anfrage = GetQueryParamter("anfrage");
	if(anfrage != "")
	{
		$('#inhalt').load('pages/zimmer/zimmer_rueckmeldung.php');
	}
}

function GetQueryParamter(name)
{
	name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
	var regexS = "[\\?&]" + name + "=([^&#]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}

$(window).resize(function() 
{
	$("#kopf").removeShadow(); 
	$("#kopf").dropShadow(); 
});

var ContentCache = new Array();

// Links generieren
function CreateLinks()
{
	$('a').each(function(index) 
	{
		var linkItem = $(this);
		if($(this).parent().get(0).tagName == 'LI')
			linkItem = $(this).parent();
		
		linkItem.attr('linkSite', $(this).attr('href'));	
		linkItem.click(function() 
		{	
			var html = $(this).html();
			var htmlElement = $(this);
			$(this).html('<img src="style/themes/loader.gif" ' + (($.browser.msie) ? 'Width=80' : 'Width=85') + ' />');
			$('#inhalt').load(linkItem.attr("linkSite") + "?random=" + Math.random(), function(data)
			{
				htmlElement.html(html);
			});
		});
	
		$(this).removeAttr('href');		
		linkItem.css("cursor", "pointer");
	});
	
	$('#navigation').children().each(function(index)
	{
		$(this).mouseover(function()
		{
			this.style.backgroundColor = 'yellow';
		});
		$(this).mouseout(function()
		{
			this.style.backgroundColor = '';
		});
	});
}

// Header �ndern
function ChangeHeader()
{	
	$('#kopf').css("background-image", "url(image/header/header.jpg)");
	window.setInterval("ChangeHeaderWorker()", 7500);
}

var headerElement = 1;
function ChangeHeaderWorker()
{
	if(headerElement == 1)
	{
		$('#kopf').css("background-image", "url(image/header/header.jpg)");
		headerElement = 2;
	}
	else if (headerElement == 2)
	{
		$('#kopf').css("background-image", "url(image/header/berge.jpg)");
		headerElement = 3;
	}
	else if (headerElement == 3)
	{
		$('#kopf').css("background-image", "url(image/header/kirche.jpg)");
		headerElement = 4;
	}
	else
	{
		$('#kopf').css("background-image", "url(image/header/katze.jpg)");
		headerElement = 1;
	}
}

// Bilder mit Untertitel
function ImageCaptionize()
{
	$('img').each(function(index)
	{
		if($(this).attr('class') == 'ImageAnnotation')
		{
			var divID = $(this).attr('src').replace('.jpg', '').replace('/', '') + '_div';
			$(this).parent().append('<div id="' + divID +'">&nbsp;</div>');	
		
			$('#' + divID).css('opacity', '0.7');
			$('#' + divID).css('background-color', 'black');
			$('#' + divID).css('width', $(this).width() + 1);
			$('#' + divID).css('position', 'absolute');
			$('#' + divID).css('z-index', '5');
			$('#' + divID).css('left', $(this).position().left);
	
			var spanID = $(this).attr('src').replace('.jpg', '').replace('/', '') + '_span';
			$(this).parent().append('<span id="' + spanID +'">' + $(this).attr('title') + '</span>');	
		
			$('#' + spanID).css('width', $(this).width() - 10);
			$('#' + spanID).css('position', 'absolute');
			$('#' + spanID).css('z-index', '10');
			$('#' + spanID).css('color', 'white');
			$('#' + spanID).css('font-family', 'Tahoma');
			$('#' + spanID).css('left', $(this).position().left + 5);
			$('#' + spanID).css('top', $(this).position().top + $(this).height() - $('#' + spanID).height() - 5);	
		
			$('#' + divID).css('top', $(this).position().top + $(this).height() - $('#' + spanID).height() - 10);
			$('#' + divID).css('height', $('#' + spanID).height() + 12);
		}
	});
}

function SideBarInitialize()
{
	$('#seite').append('<div id="sideBar">Dies ist ein Sidebar Test</div>');
	$('#sideBar').css('position', 'absolute');
	$('#sideBar').css('top', $('#mitte').position().top);
	$('#sideBar').css('left', ($('body').width() - $('#mitte').width()) / 2 + $('#mitte').width() - 150);
	$('#sideBar').css('background-color', 'black');
	$('#sideBar').css('width', '150px');
	$('#sideBar').css('height', '376px');
	$('#sideBar').css('opacity', '0.5');
	//$('#sideBar').css('top', $('#seite').position().top + $('#seite').height() / 2);
}

function LoadMain()
{
	$('#inhalt').load('main.htm', function() 
	{ 
		//CreateLinks();
		//ImageCaptionize() 
	});
}

function CreateDropShadow()
{
	$("#kopf").dropShadow();
	$("#navigation li").dropShadow();
}

(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
		var images = arguments[0];
	    var args_len = images.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = images[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)

function PreloadImages()
{	
	$.get('pages/worker/image_preload.php', function(data) 
	{
		jQuery.preLoadImages(data.split('|'));		
	});
}

function PreloadHTML()
{
	$("#navigation a").each(function(index)
	{
		$.get($(this).attr('linkSite'), function(data)
		{
			ContentCache[$(this).attr('linkSite')] = data;
		});
	});
}

function Preloader()
{
	// Bilder vorladen
	PreloadImages();
	
	// Seiten vorladen
	PreloadHTML();
}


