var base_url = '';
var target   = '#main';
var lightbox = "[rel='lightbox']";
var anchorInterval = '';
var currentAnchor = '';
var currentAnchorTime = new Date().getTime();
var checkCurrentAnchorTime = currentAnchorTime;
var pics = new Array();
pics[0] = new Object();
pics[0]['/'] = 0;
pics[0]['/about'] = 1;
pics[0]['/agenda'] = 2;
pics[0]['/contact'] = 3;
pics[0]['/gallery'] = 4;
pics[0]['/charts'] = 5;
pics[0]['/links'] = 6;
pics[0]['/gbook'] = 7;
pics[0]['/impressum'] = 8;



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);	
}
function searchForIndexOfElement(array, element)
{
	for(var i=0; i<array.length; i++)	
	{
		if(array[i] == element)	
		{
			return i;
		}
	}
}
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 prepare_ajax(sel)
{
	var selector = sel+' a:not('+lightbox+'),'+sel+' area';
	var selector_form = sel+' form';
	
	$(selector).click(function()
	{
		var href = $(this).attr('href');
		if( (href == "")  || ((href.indexOf('http://') == -1) && (href.indexOf('https://') == -1)) )
		{
			$('#main').css('opacity', '0');
			window.scrollTo(0,0);
			window.location.href = '#'+href.replace(base_url, '');
			currentAnchorTime = new Date().getTime();
			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);
			$('#main').css('opacity', '0');
			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" />');
				$(this).append('<input type="hidden" name="ajax" value="1" />');
				
			}	
			$(target).load(href, $(this).serializeArray(), function()
			{
			
				document.title = '...::: Dj Freaky-Stylez :::...';
				$('#main').animate({opacity: '1'}, 200);
				prepare_ajax(target);
				image_ausrichtung();
				gbook_event('#gbook_name', 'Name');
				gbook_event('#gbook_text', 'Text');
				gbook_show_hide();
				setPreviewSettings("div#blogpage img[width][!height],ol#blogpage img[!width][height],ol#blogpage img[width][height]");
				$(lightbox).lightBox();
				anchorInterval = setInterval('checkAnchor()', 500);	
			
			});
			return false;
		}
	});	
}
function checkAnchor()
{
	if( (currentAnchor != document.location.hash) || (currentAnchorTime != checkCurrentAnchorTime) )
	{
		checkCurrentAnchorTime = currentAnchorTime;
		$('#main').css('opacity', '0');
		currentAnchor = document.location.hash;
		if(!currentAnchor)
		{
			href = "/";  
		}
		else  
		{  
			href = currentAnchor.substring(1);
		}
		if(typeof(pics[0][href]) != "undefined")
		{
			$('#toplable').css('background-position', '0 -'+65*pics[0][href]+'px');
		}
		window.scrollTo(0,0);
		$(target).load(href, {'ajax' : ''}, function()
		{
			document.title = '...::: Dj Freaky-Stylez :::...';
			$('#imageView').hide();
			$('#main').animate({opacity: '1'}, 200);
			prepare_ajax(target);
			image_ausrichtung();
			gbook_event('#gbook_name', 'Name');
			gbook_event('#gbook_text', 'Text');
			gbook_show_hide();
			setPreviewSettings("div#blogpage img[width][!height],ol#blogpage img[!width][height],ol#blogpage img[width][height]");
			$(lightbox).lightBox();
			setPreviewSettings(".livePreviewContent img");
		});
	}
}
function image_ausrichtung()
{
	$(".blogText img").each(function(i)
		{
		if($(this).css('float') == 'left')
		{
			$(this).css('padding-left', '0px')
		}
		if($(this).css('float') == 'right')
		{
			$(this).css('padding-right', '0px')
		}
	})
	$(".blogText p").each(function(i)
	{
		var text = $(this).html().substr(0,4);
		if(text == '<img')
		{
			$(this).find('img:eq(0)').css('padding-top', '0px')
		}
	})
}
function gbook_event(id, value)
{
	$(id).focus(function()
	{
		if($(this).val() == value)
		{
			$($(this).val(''));
		}
	});
	$(id).blur(function()
	{
		if($(this).val() == '')
		{
			$($(this).val(value));
		}
	});
}
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 gbook_show_hide()
{
	if( $('#gbookInput').hasClass('visibleByError') == false )
	{
		$('#gbookTitle').html('<a href="javascript:toogle_gbook_input()">'+$('#gbookTitle').text()+'</a>');
		$('#gbookInput').hide();
		$('#gbookTitle').css('background-image', 'none');
	}
}
$(document).ready(function()
{
	prepare_ajax('body');
	image_ausrichtung();
	gbook_event('#gbook_name', 'Name');
	gbook_event('#gbook_text', 'Text');
	gbook_show_hide();
	anchorInterval = setInterval('checkAnchor()', 500);
	$("a").focus(function()
	{
		$(this).blur();
	});
	$('#landingPage').click(function()
	{
		$(this).css('display', 'none');
		$('#allWrapper').css('visibility', 'visible');
		document.body.style.overflow = 'auto';
	})
});
$(window).load(function()
{
	setPreviewSettings("#galleryPreview img");
	setPreviewSettings("div#blogpage img[width][!height],ol#blogpage img[!width][height],ol#blogpage img[width][height]");
});
$(function() {
	 $('a[rel*=lightbox]').lightBox();
});

