// JavaScript Document

jQuery(window).bind("unload", function() {
	//$(".QOverlay").remove();
});

function str_replace(search, replace, subject) {
return subject.split(search).join(replace);
}

var context_is_on = false;
var context_is_on_x = null;
var context_is_on_y = null;
var context = null;

$(document).ready(function() {
						   
	$('a[href*=#]').click(function() {
 if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'')
 && location.hostname == this.hostname) {
   var $target = $(this.hash);
   $target = $target.length && $target
   || $('[name=' + this.hash.slice(1) +']');
   if ($target.length) {
  var targetOffset = $target.offset().top;
  $('#content')
  .animate({scrollTop: targetOffset}, 1000);
    return false;
   }
 }
  });

	
	$(".keks_button").each(function() {
		$(this).mouseover(function() {
			var lnk = $(this).attr("src");
			lnk = str_replace('.png', '_over.png', lnk);
			$(this).attr("src",lnk);
		});
		$(this).mouseout(function() {
			var lnk = $(this).attr("src");
	  		lnk = str_replace('_over.png', '.png', lnk);
			$(this).attr("src",lnk);
		});
	});
	
	$(".nav_button_overlay").mouseover(function(){
      var lnk = $(this).attr("href");
	  lnk = lnk.split("=");
	  $(".overlay_start_"+lnk[1]).fadeIn(100);
    });
	$(".nav_button_overlay").mouseout(function(){
      var lnk = $(this).attr("href");
	  lnk = lnk.split("=");
	  $(".overlay_start_"+lnk[1]).fadeOut(100);
    });
	
	$(document).bind("contextmenu",function(e){
		if(!context_is_on) {
			context = $("<div></div>").appendTo($("body"));
			$(context).addClass("context");
			$(context).hide();
			$(context).toggle("fast");
			$(context).css({ top: e.pageY-10, left: e.pageX-10});
			context_is_on_x = e.pageX;
			context_is_on_y = e.pageY;
			context_is_on = true;
		}
		return false;
    });
	
	$(document).bind("mousemove",function(e) {
			if(context_is_on && ((e.pageX > context_is_on_x+30 || e.pageX < context_is_on_x-30) || (e.pageY > context_is_on_y+30 || e.pageY < context_is_on_y-30))) {
					$(context).toggle("fast", function() {
						context_is_on = false;
						$(context).remove();								   
					});
			}
	});

	
	$(".navigation_holder_button").focus(function() {
		$(this).blur();
	});
	
	$(".navigation_button").click(function() {
		if($(".QOverlay").length==0) {
			var overlay = $("<div></div>").appendTo($("body"));
			$(overlay).addClass("QOverlay");
			$(overlay).css({opacity:0});
		}
		var lnk = $(this).attr("href");
		var lnkid = lnk;
	    lnkid = lnkid.split("=");
		if(lnkid[1]!="keks") {
			$(".QOverlay").css({backgroundColor: '#000000'});
			$(".QOverlay").fadeIn(200, function() {
				document.location.href = lnk;				
			});
		}
		else {
			$(".QOverlay").css({backgroundColor: '#000000', display: 'block', opacity:0});
			$(".QOverlay").animate({opacity: 0.4},400);
			$(".keks").toggle(400, function() {
				$(".QOverlay").bind("click",function(e) {	
					leave_keks();								  
				});
			});		
		}
		return false;
	});
	
	function leave_keks() {
		$(".QOverlay").unbind("click");
		$(".QOverlay").animate({opacity: 0},400);
		$(".keks").toggle(400, function() {
			$(".QOverlay").css({backgroundColor: '#000000', display: 'none', opacity:0});							
		});
	}
	
	var leave_deakt = false;
	$("#overlay_deaktivierung").click(function() {
		leave_deakt = true;		
	});
	$(".lifestyle_geschichten_container TD").each(function() {
		$(this).click(function() {
			leave_deakt = true;	
		});
	});
	$("#content_container DIV").each(function() {
		if($(".lifestyle_geschichten_container").length==0) {
			$(this).click(function() {
				leave_deakt = true;	
			});
		}
	});
	
	$(".lifestyle_geschichten_container").click(function() {
		exit_site();
	});
	
	$("#outer_container").click(function() {
		if($(".lifestyle_geschichten_container").length==0) {
			exit_site();
		}
	});
	
	
	
	function exit_site() {
		if($(".QOverlay").length==0) {
			var overlay = $("<div></div>").appendTo($("body"));
			$(overlay).addClass("QOverlay");
			$(overlay).css({opacity:0});
		}
		if(!leave_deakt) {
			var lnk = "index.php";
			$(".QOverlay").css({backgroundColor: '#000000'});
			$(".QOverlay").fadeIn(200, function() {
				document.location.href = lnk;				
			});				
		} else {
			leave_deakt = false;
		}
	}
	
	$(".fwbklink").click(function() {
		/*switch_to_site($(this).attr("href"));
		return false;*/
	});
	
	function switch_to_site(lnk) {
			if($(".QOverlay").length==0) {
				var overlay = $("<div></div>").appendTo($("body"));
				$(overlay).addClass("QOverlay");
				$(overlay).css({opacity:0});
			}
			$(".QOverlay").css({backgroundColor: '#000000'});
			$(".QOverlay").fadeIn(200, function() {
				document.location.href = lnk;				
			});				
		
	}
	
	var tempCounter = 0;
	var windowResizeTimeout;
	
	window.onresize = function()
	{
	window.clearTimeout(windowResizeTimeout);
	windowResizeTimeout = window.setTimeout(DelayedResize, 100);
	};
	
	var navigation_news_cache_size = parseInt($('.navigation_start_news').css('width'));
	var scrollOff = 0;
	
	function DelayedResize()
	{
		
		$(".lifestyle_geschichten_container").css({ height: $(window).height() });
		//alert($(window).height());
		$(".QOverlay").css({ height: $(window).height(),width: $(window).width() });
		var position = $("#content_container").position();
		var checkw = (parseInt($('.navigation_start_news').css('left'))+parseInt($('.navigation_start_news').width()))+1;
		var checkd = Math.round($(document).width() - position.left);
		//document.title = ($(window).width()-$(document).width())/2;
		//document.title = $(window).width() + ' - ' + $(document).width() + ' - ' ;
		$('.navigation_start_news').css({width: navigation_news_cache_size});
		$('.overlay_start_news').css({width: navigation_news_cache_size});
		
		if(($(window).width()-$(document).width())<0 ) {
			
			scrollOff = ($(document).width()-1300);
			$('.navigation_start_news').css({width: navigation_news_cache_size-scrollOff});
			$('.overlay_start_news').css({width: navigation_news_cache_size-scrollOff});
			//document.title = navigation_news_cache_size-scrollOff;
		} else {
			scrollOff = 0;
			$('.navigation_start_news').css({width: navigation_news_cache_size});
			$('.overlay_start_news').css({width: navigation_news_cache_size});
			
		}
		
	}
	
});
