$(document).ready(function() {
	var popOut = "#popout";
	var bdBox = "#bdbox"; 
	var bdWidth = $(bdBox).width() + $("#cap").width();
	var screenWidth = document.body.offsetWidth - 20 || document.body.clientWidth - 20 || window.innerWidth - 37 || self.innerWidth - 37;
	var contentImgWidth = $(".banderole_content_img").css('width').substring(0, ($(".banderole_content_img").css('width').length-2));
	var innerContentLeft = Math.round((((screenWidth+20)*0.5) - (contentImgWidth/2)));
	var closeRight = screenWidth-(parseInt(innerContentLeft) + parseInt(contentImgWidth)) + 24;
	var actState = 0;
	var tease;
	var teaserImgHeight;
	var teaserImgTop;
	
	
	$("#close").css('right', closeRight);
	$(".banderole_content_img").css('margin-left', innerContentLeft);
	$(bdBox).css('margin-right', -(screenWidth+40));
	$(bdBox).css('width', (screenWidth+20));
	
	//Funktion um den Banner zu resizen beim verändern der Browsergröße
	
	$(window).resize(function() {
		
  		screenWidth = document.body.offsetWidth - 20 || document.body.clientWidth - 20 || window.innerWidth - 37 || self.innerWidth - 37;
		innerContentLeft = Math.round((((screenWidth+20)*0.5) - (contentImgWidth/2)));
		closeRight = screenWidth-(parseInt(innerContentLeft) + parseInt(contentImgWidth)) + 24;
		
		$("#close").css('right', closeRight);
		$(".banderole_content_img").css('margin-left', innerContentLeft);
		$(bdBox).css('margin-right', -(screenWidth+40));
		$(bdBox).css('width', (screenWidth+20));
		if(actState == 1)
		{
			$(bdBox).css('margin-right', -20);
		}
	});
	
	//Funktion um eine zufällige Zahl aus einem bestimmten Bereich zu erzeugen
	
	function getRandom(min, max) {
	if(min > max) {
		return -1;
	}
	 
	if(min == max) {
		return min;
	}
	 
	var r;
	 
	do {
		r = Math.random();
	}
	while(r == 1.0);
	 
	return min + parseInt(r * (max-min+1));
	}
	
	//Funktionen zum öffnen und schließen
	
	function openBdfirstTime()
	{
		display();
		vanish_teaser();
		$(popOut).width(bdWidth+"px");
		$(bdBox).animate({marginRight: "-20"},1200);
		actState = 1;
	}
	
	function openBd() {
		vanish_teaser();
		$(popOut).width(bdWidth+"px");
		$(bdBox).animate({marginRight: "-20"},1200);
		actState = 1;
	}
	
	function closeBd() {
		$(bdBox).animate({marginRight: "-"+(screenWidth+40)+"px"},1200,"linear",
		function(){ $(popOut).width($(".cap").width() + "px"); }
		);
		actState = 0;
	}
	
	//Teaser Funktionen
	
	function openTease() {
		$(popOut).width(bdWidth+"px");
		$(bdBox).animate({marginRight: "-"+(screenWidth/100*95)},900);
	}
	
	function closeTease() {
		$(bdBox).animate({marginRight: "-"+(screenWidth+40)+"px"},900,"linear",
		function(){ $(popOut).width($(".cap").width() + "px"); }
		);
	}
	
	function open_close_Tease()
	{
		openTease();
		closeTease();
	}
	
	//Click-Funktionen zum öffnen und schließen
	
	$("#open").click(function() {
		if(actState == 1)
		{
			closeBd();
		}
		else
		{
			openBd();
		}
		return false;
	});
	$("#close").click(function() {
		closeBd();
		return false;
	});
	$("#close_mobile").click(function() {
		closeBd();
		return false;
	});
	$("#x_span").click(function() {
		closeBd();
		return false;
	});
	$("#close_mobile_text").click(function() {
		closeBd();
		return false;
	});
	$("#bdbox").click(function() {
		return false;
	});
	$(".banderole_content_img").click(function() {
		window.location.href = $("#bd").attr('href');
	});
	
	//Funktion um das Banner sichtbar zu machen
	
	function display()
	{
		$(".banderole_content_img").css('display', 'block');
		$("#close_mobile").css('display', 'block');
		$("#x_span").css('display', 'block');
		$("#close_mobile_text").css('display', 'block');
	}
	
	function vanish()
	{
		$(".banderole_content_img").css('display', 'none');
		$("#close_mobile").css('display', 'none');
		$("#x_span").css('display', 'none');
		$("#close_mobile_text").css('display', 'none');
	}
	
	function display_teaser()
	{
		teaserImgHeight = $(".banderole_teaser_img").height();
		if(teaserImgHeight <= 300)
		{
			$(".banderole_teaser_img").css('display', 'block');
		}
		
		if((300 - teaserImgHeight) != 0 || (300 - teaserImgHeight) > 0)
		{
			teaserImgTop = (300 - teaserImgHeight) / 2;
			$(".banderole_teaser_img").css('top', teaserImgTop+"px");
		}
		
	}
	
	function vanish_teaser()
	{
		$(".banderole_teaser_img").css('display', 'none');
	}
	
	//Falls der Kunde das erste mal die Seite besucht wird der Banner automatischs ausgefahren ansonsten nur der Teaser
	
	if(first_display == 1)
	{
		$(popOut).animate({opacity: 1.0}, 1500, "linear", openBdfirstTime);
	}
	else
	{
		display_teaser();
		tease = setInterval(open_close_Tease ,getRandom(5000, 10000));
		$("#open").click( function() {
			clearInterval(tease);
			vanish_teaser();
			display();
		});
	}
	
});
