$(function(){
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect'
});

$(document).ready(function() {
	$('.disableSelection').disableTextSelect();

	if ($.browser.msie && $.browser.version.substr(0,1) <= 6) {
		$("#background").hide();
	} else {
		$("#background").each(function() {
			var bgAspectRatio = 1;
			
			var resizeFn = function() {
				$("#background").hide();
				var height = Math.max($(window).height(), $("#footer").offset().top+$("#footer").height()+30);
				var width = Math.max($(window).width(), $("#container").offset().left+$("#container").width()+20);
				var aspectRatio = width/height;
				$("#background").show();
				
				$("#background").css("width", width);
				$("#background").css("height", height);
				$("#left").css("height", height);
			
				//console.log(height, width, $("#main").height(), $("#main").offset().top);
				//console.log(aspectRatio, bgAspectRatio);
				
				if (aspectRatio > bgAspectRatio) {
					//console.log("W", width);
					$("#background img").css("width", width);
					$("#background img").css("height", "auto");
					$("#background img").css("top", -((width/bgAspectRatio-height)/2));
					$("#background img").css("left", 0);
					//console.log(0, -((width/bgAspectRatio-height)/2), width/bgAspectRatio, height);
				} else {
					//console.log("H", height);
					$("#background img").css("height", height);
					$("#background img").css("width", "auto");
					$("#background img").css("top", 0);
					$("#background img").css("left", -((height*bgAspectRatio-width)/2));
				}
			};
			
			bgAspectRatio = 3000/1574;
			resizeFn();
			$(window).resize(resizeFn);
		});
	}
	
	// Navigation
	$(".mod_navigation ul.level_2").hide();
	$(".mod_navigation ul.level_2 li.active").parent().show();
	$(".mod_navigation ul.level_2 li.trail").parent().show();
	
	$(".mod_navigation ul.level_1 .sectionlabel").click(function() {
		var section = $(this).parent();
		$("ul.level_2:hidden", section).each(function () {
			$(".mod_navigation ul.level_2").slideUp();
			$(this).slideDown();
		});
	});
	
	// Domain-Check
	$("#domaincheck").each(function() {
		if ($("#domaincheck input.text").val() != "Domain-Check") {
			$("#domaincheck input.text.inactive").removeClass("inactive").css("color", "");
		}
		
		$("#domaincheck input.text").focus(function() {
			$("#domaincheck input.text.inactive").css("color", "");
			$("#domaincheck input.text.inactive").each(function() {
				$(this).val("");
				$(this).removeClass("inactive");
			});
		}).blur(function() {
			if ($(this).val()=="") {
				$(this).val("Domain-Check");
				$(this).addClass("inactive");
			}
		});
		
		if (!$.browser.msie || $.browser.version.substr(0,1) > 6) {
			var domaincheck = $('<div id="domaincheck_overlay"><a href="#" class="close">&nbsp;x&nbsp;</a><div class="inside"></div></div>');
			domaincheck.hide();
			$(this).append(domaincheck);
			
			$("a.close", domaincheck).click(function() {
				domaincheck.fadeOut();
				return false;
			});
			
			$(this).bind("submit", function() {
				if ($("#domaincheck input.text.inactive").length == 0) {
					$(".inside", domaincheck).empty().append('<h1>Domain-Check</h1><div class="ajax"></div>');
					$.get("domain-check.html", { domain: $("#domaincheck input.text").val() }, function(data) {
						$(".inside .ajax", domaincheck).append($("#main > .inside", data).clone());
					});
					domaincheck.fadeIn();
					if (typeof(pageTracker) != "undefined") pageTracker._trackPageview("/domain-check.html");
				} else {
					$("#domaincheck input.text.inactive").css("color", "red").val("Bitte Domain-Namen angeben");
				}
				return false;
			});
		}
		
	});
	
	// Referenzen
	var initReferenzen = function() {
		var pagination = $("#referenzen .pagination");
		pagination.find("a").click(function() {
			var elem = $(this);
			
			var oldReferenzen = $("#referenzen");
			oldReferenzen.css("position", "relative");
			oldReferenzen.css("z-index", "1000");
			oldReferenzen.find(".pagination").fadeOut("fast");
			if ($.support.opacity) oldReferenzen.fadeTo("slow", 0.01);
			
			$.get(elem.attr("href"), function(data) {
				if (typeof(pageTracker) != "undefined") pageTracker._trackPageview(elem.attr("href"));
				
				var newReferenzen = $("#referenzen", data).clone();
				if (oldReferenzen) oldReferenzen.css("margin-bottom", -oldReferenzen.height());
				if ($.support.opacity) newReferenzen.fadeTo(0, 0.01);
				newReferenzen.insertAfter(oldReferenzen);
				
				if ($.support.opacity) {
					newReferenzen.fadeTo("slow", 1, function() {
						oldReferenzen.remove();
						initReferenzen();
						if (typeof(this.style) != "undefined" &&
								typeof(this.style.removeAttribute) != "undefined") {
							this.style.removeAttribute("filter");
						}
					});
				} else {
					oldReferenzen.remove();
					initReferenzen();
				}
			});
		
			
			return false;
		});
	}
	$("#referenzen").each(initReferenzen);
	
});
