var base_url = 'http://freedt.dyndns.org/';
var target   = '#content';
var lightbox = "[rel='lightbox']";
var anchorInterval = '';
var currentAnchor = '';
var currentAnchorTime = new Date().getTime();
var checkCurrentAnchorTime = currentAnchorTime;

function prepare_ajax(sel)
{
	var selector = sel+' a:not('+lightbox+'),'+sel+' area';
	var selector_form = sel+' form';
	
	$(selector).click(function()
	{
		//clearInterval(anchorInterval);
		var href = $(this).attr('href');
		if( (href == "") || (href.indexOf(base_url) == 0) || ((href.indexOf('http://') == -1) && (href.indexOf('https://') == -1)) )
		{
			$('body').animate({opacity: '0.5'}, 200);
			window.scrollTo(0,0);
			window.location.href = '#'+href.replace(base_url, '');
			currentAnchorTime = new Date().getTime();
			//currentAnchor = '#'+href.replace(base_url, '');
			// $(target).load(href+' '+target+' > *', function()
			// {
				// $('body').animate({opacity: '1'}, 200);
				// prepare_ajax(target);
				// $(lightbox).lightBox();
				// anchorInterval = setInterval('checkAnchor()', 400);				
			// });
			return false;
		}
	});
	
	$(selector_form).submit(function()
	{
		var href = $(this).attr('action');
		if( (href == "") || (href.indexOf(base_url) == 0) || ((href.indexOf('http://') == -1) && (href.indexOf('https://') == -1)) )
		{
			clearInterval(anchorInterval);
			$('body').animate({opacity: '0.5'}, 200);
			window.scrollTo(0,0);	
			if(href == "")
			{
				href = currentAnchor.substring(1);
				if(href.substr(-1) != '/')
				{
					href = href+'/';
				}
			}
			else
			{
				window.location.href = '#'+href.replace(base_url, '');
				currentAnchor = '#'+href.replace(base_url, '');
				currentAnchorTime = new Date().getTime();
				checkCurrentAnchorTime = currentAnchorTime;
				$(this).append('<input type="hidden" name="submit" value="1" />');
			}		
			$(target).load(href+' '+target+' > *', $(this).serializeArray(), function()
			{
				$('body').animate({opacity: '1'}, 200);
				prepare_ajax(target);
				$(lightbox).lightBox();
				image_ausrichtung();
				anchorInterval = setInterval('checkAnchor()', 500);				
			});
			return false;
		}
	});
	
}

$(document).ready(function()
{
	anchorInterval = setInterval('checkAnchor()', 500);
	prepare_ajax('body');
	$(lightbox).lightBox();
	image_ausrichtung();
	gbook_event('#gbook_name', 'Name');
	gbook_event('#gbook_text', 'Text');
	gbook_show_hide();
	setPreviewSettings(".livePreviewContent img");
	$('#sidebar object').each(function()
	{
		$(this).css('display', 'none');
		$(this).append('<param name="wmode" value="transparent" />');
		$(this).find('embed').attr('wmode', 'transparent');
		$(this).css('display', 'block');
	})
})

function image_ausrichtung()
{
	$(".livePreviewContent img").each(function(i)
		{
		if($(this).css('float') == 'left')
		{
			$(this).css('padding-left', '0px')
		}
		if($(this).css('float') == 'right')
		{
			$(this).css('padding-right', '0px')
		}
	})
	$(".livePreviewContent p").each(function(i)
	{
		var text = $(this).html();
		text = text.substr(0,text.indexOf('<img'));
		text = text.replace(/<[a-z]*>/gm, '');
		if(text == '')
		{
			$(this).find('img:eq(0)').css('padding-top', '2px')
		}
	})
}

function gbook_event(id, value)
{
	$(id).focus(function()
	{
		if($(this).val() == value)
		{
			$($(this).val(''));
		}
	});
	$(id).blur(function()
	{
		if($(this).val() == '')
		{
			$($(this).val(value));
		}
	});
}	
function gbook_show_hide()
{
	$('#gbookTitle').html('<a href="javascript:toogle_gbook_input()">'+$('#gbookTitle').text()+'</a>');
	$('#gbookInput').hide();
	$('#gbookTitle').css('background-image', 'none');
}
function checkAnchor()
{
	if( (currentAnchor != document.location.hash) || (currentAnchorTime != checkCurrentAnchorTime) )
	{
		checkCurrentAnchorTime = currentAnchorTime;
		$('body').animate({opacity: '0.5'}, 200);
		currentAnchor = document.location.hash;  
		if(!currentAnchor)
		{
			href = "/";  
		}
		else  
		{  
			href = currentAnchor.substring(1);
		}
		window.scrollTo(0,0);
		$(target).load(href+' '+target, function()
		{
			$('body').animate({opacity: '1'}, 200);
			prepare_ajax(target);
			$(lightbox).lightBox();
			image_ausrichtung();
			gbook_event('#gbook_name', 'Name');
			gbook_event('#gbook_text', 'Text');
			gbook_show_hide();
			setPreviewSettings(".livePreviewContent img");
		});
	}
}
function toogle_gbook_input()
{
	var input = $('#gbookInput');
	if(input.css('display') == 'block')
	{
		input.slideUp();
		$('#gbookTitle').css('background-image', 'none');
	}
	else
	{
		input.slideDown();
		$('#gbookTitle').css('background-image', 'url(/images/trennung.png)');
	}
}

function setPreviewSettings(selector)
{	
	$(selector).each(function()
	{
		//var i = $("div[id^=eventCatcher_]").length;
		var image = $(this);
		var imagePosition = $(this).position();
		$("#blanko").clone().insertAfter("#blanko");//
		var newDiv = $("#blanko:last");
		var id = new Date().getTime();
		newDiv.attr("id", "eventCatcher_"+id);
		image.attr("id", "eventCatcherImage_"+id);
		newDiv.insertAfter(image);
		
		newDiv.css("position", "absolute");
		newDiv.css("top", imagePosition.top + 3);
		newDiv.css("left", imagePosition.left);
		newDiv.css("width", image.width());
		newDiv.css("height", image.height());
		newDiv.css("z-index", "80");
	
		newDiv.mouseenter(function()
		{
			
			var id = $(this).attr("id").split("_");
			var n = id[1];
			var image = $("#eventCatcherImage_"+n);
			$("#imageView img").attr("src", image.attr("src"));
			$("#imageView span").text(image.attr("alt"));
			$("#imageView").css("display", "block");
		});
		
		newDiv.mouseleave(function()
		{
			$("#imageView").css("display", "none");
		});

		newDiv.mousemove(function(e)
		{
			movePreview(e);
		});
	});
}
function movePreview(e)
{
		var top = e.pageY - ($("#imageView").outerHeight() / 2);
		if (top < 0) top = 0;
		var left = e.pageX + 20;
		$("#imageView").css("top", top);
		$("#imageView").css("left", left);	
}