$(document).ready(function() {
	initSite();
});

var useAjax = true;

function initSite()
{
	
	// calculate content width on load an resize
	calculateContentWidth();
	$(window).resize( function() {
	   calculateContentWidth();
	});
	
	/* init links */
	
	// IE --> AJAX test
	if($.browser.msie)
	{
		var ajaxTest = new ActiveXObject("Msxml2.XMLHTTP") 
		if(typeof ajaxTest != 'object' )
		{
			useAjax = false;
		}
	}
	
	// ajax ok!
	if(useAjax)
	{
	
		//deactive layer links
		$("a.layer_link").click(function(){
			
			var url = this.href + "";
			loadLayerContent(url);
			return false;		
		});
	
		// bookmarked ? 
		var uri = window.location + ''; //making a string
		uri = uri.replace("%23","#"); // replacing encoding
		if(uri.indexOf("##") != -1)
		{
			uri = uri.split("#");
			// avaliable?
			loadLayerContent(uri[2]);
		}
	}

}

function calculateContentWidth()
{
	var newWidthEm = 0;
	if(emToPx($("div#agenda-content-zone").css("max-width")) > $("div#agenda-content-zone").width())
	{
		if($("div.movingarea-position").width() != null)
		{ // padding left
			newWidthEm =  ($("div#agenda-content-zone").width() - 160 - getAbsolutePosition($("div.content")[0]).x) / 10;
		}
		else
		{ // no padding left
			newWidthEm =  ($("div#agenda-content-zone").width() - emToPx($("div.content").css("padding-left")) - 150) / 10;
		}
		
		$("div.content").css("width",newWidthEm+"em");
	}
	else
	{	
	
		newWidthEm =  (emToPx($("div#agenda-content-zone").css("max-width")) - 160 - getAbsolutePosition($("div.content")[0]).x) / 10;
		
		$("div.content").css("width",newWidthEm+"em");
	}
}


function loadLayerContent(pUrl)
{
	$.ajax({
		    type: "GET",
			url: pUrl,
			async: false, // blocks showing the layer
			success: function(html)
			{
				// "imagetoolbar" craches IE 6
				html = html.replace("imagetoolbar", "");
				
				// parsing xhtml				
				var layerContent = $(html).children("div#agenda-layer").html();
				// IE workauround
				if(layerContent == null)
				{
					layerContent = $(html).find("div#agenda-layer").html();
				}
				// still not found? searching manually (IE 5?)
				if(layerContent == null)
				{
					layerContent = $(html).children("div#agenda-content-zone").find("div").filter("div#agenda-layer").html();
				}

				
				// insert the new content
				$("div#agenda-layer").html(layerContent);
				
				// show layer
				openLayer(pUrl);
  			}
		  });
}

function openLayer(pUrl)
{
	
	// deactive close link
	$("a#agenda-layer-close").click(function()
	{
		
		// disable bookmark-anchor
		var uri = window.location + ''; //making a string
		uri = uri.split("#");
		window.location = uri[0] + '#';
	
		// show scrollbars
		//$("body").css("overflow", "auto");	
	
		$("div#agenda-layer").fadeOut(300);
		
		// set z-index for background
		$("div#agenda-content-zone").css("z-index", 0);	
		
		return false;		
	});


	//  making bookmarkable
	var uri = window.location + '';//making a string
	uri = uri.split("#");
	window.location = uri[0] + "##" + pUrl;

	// init validator
	setValidators();
	
	// init submit
	initLayerSubmit();
	
	// show layer	
	$("div#agenda-layer").show().fadeIn(300);
	
	// reinit width 
	calculateContentWidth();
	// reinit layer height 
	calculateLayerHeight();
	
	// set z-index for forground
	$("div#agenda-content-zone").css("z-index", 8);	
}

function calculateLayerHeight()
{
	var contentSizeEm = (getAbsolutePosition($("div#imprint-movingarea")[0]).y + $("div.footer").height()) / 10;
	$("div#agenda-layer").css("height", contentSizeEm + "em");
}

function initLayerSubmit()
{
	$("div#agenda-layer form").submit(function(){
		
		// scroll to top
		window.scrollTo(0,0); 	
	
		if(validateOnSubmit(this.id))
		{
			// ajax
			$.ajax({
			  type: this.method,
			  url: this.action,
			  data: $("div#agenda-layer form :input").serialize(),
			  success: function(html){
			  	
					// parsing xhtml				
					var layerContent = $(html).children("div#agenda-layer").html();
					// IE workauround
					if(layerContent == null)
					{
						layerContent = $(html).find("div#agenda-layer").html();
					}
					// still not found? searching manually (IE 5?)
					if(layerContent == null)
					{
						layerContent = $(html).children("div#agenda-content-zone").find("div").filter("div#agenda-layer").html();
					}
					
					// insert the new content
					$("div#agenda-layer").html(layerContent);
					
					// reinit width 
					calculateContentWidth();
					// init validator
					setValidators();
					
					// init submit
					initLayerSubmit();
					
			  }
			});
			
			
			return false;
		}
		else
		{
			return false;	
		}
		
	});
}
