$(function(){
	if(!$.cookie("age")) {
		loadPopup();
	}

	// Rechtsklick
	$(document).bind("contextmenu",function(e){
	    return false;
	});
	    
	// Klickrahmen
	$("a").focus(function(){
		$(this).blur();
	});
	
	// Dropdowns
	$(".fake_select").click(function(){
		var el = $(this).attr("rel");
		$("#" + el).slideToggle("fast");
		if(el == "service_cloud") {
			$("#city_cloud").slideUp("fast");
		} else {
			$("#service_cloud").slideUp("fast");			
		}
	});
	
	// VIP Marquee
	$("#wrap_marquee").marquee('marquee');
 
	// Left menu mouseover / mouseout
	$("#categories a").mouseover(function() {
		$(this).css('border-bottom', '1px solid #fff');
		$(this).next().css('border-top', '0');
		$(this).css('margin-right', '0');
		$(this).next().css('margin-right', '0');
	});
	$("#categories a").mouseout(function() {
		$(this).css('border-bottom', '0');
		$(this).next().css('border-top', '1px solid #fff');
		$(this).css('margin-right', '6px');
		$(this).next().css('margin-right', '6px');
	});
});

$.fn.extend({
	marquee: function(id) {
		var $el = $("#" + id);
		var $firstChild = $el.children(":first");
		var $secondChild = $el.children(":nth-child(2)");
		var $step = parseFloat($firstChild.css('width')) * -1 - 12;
		
		$el.mouseover(function() {
			$stop = true;
		});

		$el.mouseout(function() {
			$stop = false;
		});
				
		var $left = parseFloat($el.css('left'));
//		$("#test").html("Hi: " + $el.css('left'));
		
		if($left == 0) {
			$firstChild.clone().appendTo($el);
		}
		
		if($left && $left < 0 && $left % $step == 0) {
			$secondChild.clone().appendTo($el);
			$firstChild.remove();
			$left = 0;
		}

		if($stop != true) {
			--$left;
			$el.css('left', $left.toString() + 'px');
		}
		
		window.setTimeout(function() {$("#wrap_marquee").marquee(id);}, 40);
	}	
});

function setPopupCookie() {
	var $cookieOptions = { path: "/" };
	var $cookieName = "age";
	$.cookie($cookieName, "over", $cookieOptions);
	disablePopup();
}

function removeFromWatchlist($name) {
	var $newCookieValue = null;
	var $cookieOptions = { path: "/", expires: 90 };
	var $cookieName = "l";
	var $cookieValue = $.cookie($cookieName);
	if($cookieValue != null) {
		if($cookieValue == $name) {
			$cookieValue = null;
		} else if($.inArray($name, $cookieValue.split(",")) != -1) {
			$.each($cookieValue.split(","), function(){
				if(this != $name) {
					if($newCookieValue != null) {
						$newCookieValue = $newCookieValue + "," + this;
					} else {
						$newCookieValue = this;
					}
				}
			});
		}
		$.cookie($cookieName, $newCookieValue, $cookieOptions);
		document.location.reload();
	}
}

function loadPopup() {  
	if($popupStatus==0) {  
		$("#backgroundPopup").css({  
			"opacity": "0.7" 
		});
		$("#backgroundPopup").fadeIn("slow");  
		$("#popupContact").fadeIn("slow");  
		$popupStatus = 1;  
		centerPopup();
	}
}

function disablePopup() {  
	if($popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#popupContact").fadeOut("slow");  
		$popupStatus = 0;  
	}
}

function centerPopup() {
	if($.browser.msie && parseInt(jQuery.browser.version) < 7) {
		var windowWidth = $("body").innerWidth();
		var windowHeight = $("body").innerHeight();
	} else {
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
	}
	var popupHeight = $("#popupContact").height();  
	var popupWidth = $("#popupContact").width();  
	$("#popupContact").css({  
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2  
	});  
	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
}

$(window).load(function(){
	placeNew();
});

function placeNew() {
	// Neu-Overlay
	$(".contact .img:has(.overlayNew)").each(function(e) {
		$(".overlayNew", $(this)).css("left", $("img", $(this)).position().left + "px");
//		$("img", $(this)).css("border-color", "#d15d50");
		$(".overlayNew", $(this)).show("slow");
	});
}

var $stop = false;
var $popupStatus = 0;  
