$(document).ready(function() {

	
						   
	// CHANGE HREF-ATTRIBUTE OF A.AZSHOW WHEN JS ACTIVE						   

	$("a.azshow").attr("href", "#");
	
	// AZ-BOX
	
	$("a.azshow").live("click",function() {					 
		$("#azbox").toggleClass("visible");
		$(".overlay").toggleClass("visible");
		$("a.azshow").toggleClass("clicked");
		$(this).blur();
		return false;
	});
	
	/*$("a.azshow.clicked").live("click",function() {
		$("#azbox").removeClass("visible");
		$(".overlay").removeClass("visible");
		$("a.azshow").toggleClass("clicked");
		$(this).blur();
	});*/
	
	$("#azbox a.all").click(function() {
			$(this).parent().addClass("active");
			$("#azbox a.sit").parent().removeClass("active");
			$(".azbox_body.all").addClass("visible");
			$(".azbox_body.sit").removeClass("visible");
	 });
	
	$("#azbox a.sit").click(function() {
			$(this).parent().addClass("active");
			$("#azbox a.all").parent().removeClass("active");
			$(".azbox_body.all").removeClass("visible");
			$(".azbox_body.sit").addClass("visible");
	 });
	
	$("#azbox a.close, #azbox a.close_bottom, .overlay").click(function() {
			$("#azbox").removeClass("visible");
			$(".overlay").removeClass("visible");
			$("a.azshow").toggleClass("clicked");
			$(this).blur();
	 });

	// IE6 METANAV

	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6)
	{
		try
			{
				$("#metanavigation ul li").hover(function() {
					$(this).toggleClass("hover");
				})
			}
			catch (err)
			{
		}
	}
	
	// FONT-SIZE-TRIGGER

	var fontsize_normal = "100%";
	var fontsize_bigger = "105%";
	var fontsize_biggest = "110%";
	
	$("a.small").click(function() { $("body").css('font-size', fontsize_normal); });	
	$("a.normal").click(function() { $("body").css('font-size', fontsize_bigger); });
	$("a.big").click(function() { $("body").css('font-size', fontsize_biggest); });
	
	// EASYSLIDER
	
	if ( $("#refresh").length > 0 ) {
		$("#refresh").easySlider({
			prevId: 'prevBtn',
			prevText: 'Previous',
			nextId: 'nextBtn',	
			nextText: 'Next',
			controlsShow: true,	
			speed: 350,
			auto: true,
			pause: 6000,
			continuous: true
		});

    $("#stopBtn.play").click(function() {
        location.reload(); 
    });
	
	if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 7)
		{
			try
				{
					$("#contentarea").css("padding","0");
				}
				catch (err)
				{
			}
		}

	}

	// CHECK SIDEBAR BOX FOR EMPTY LIS

	//if ( $(".sidebar .box ul").length > 0 ) {
	//	$(".sidebar .box ul li:first a").css("border-top","none");
	//	$(".sidebar .box ul li:empty").hide();
	//}
	
	if ( $(".sidebar .box ul").length ) 
	{
	$(".sidebar .box ul li:first a").css("border-top","none");
	$(".sidebar .box ul li:empty").hide();
	var sidebarul = $(".sidebar .box ul");
	var ulchildren = sidebarul.children().length;
	for ( i=0; i<ulchildren; i++ ) {
		var cli = $("li:eq("+i+")",sidebarul);
		var clicss = cli.css("display");
		if ( clicss != "none" ) {
			break;
		} else {
			sidebarul.parent().hide();
		}
	}
	}


	// WAI ARIA STUFF

	if ( $("div#content").length > 0 ) {
        $("div#content").attr("role","article");
    }

	if ( $("div#search").length > 0 ) {
        $("div#search").attr("role","search");
    }

	if ( $("div#metanavigation").length > 0 ) {
        $("div#metanavigation").attr("role","navigation");
    }

	// LANGUAGE ATTRIBUTES 
	
	if ( $("a:contains('English')") ) {
			$("a:contains('English')").wrap("<blockquote xml:lang='EN' />")
	}
	
	if ( $("a:contains('eGovernment')") ) {
			$("a:contains('eGovernment')").wrap("<blockquote xml:lang='EN' />")
	}

	
	
	// ABBR / ACRONYM
		
		if ( $.browser.msie ) {

			if ( $("a.rss").length ) {
				var x = $("a.rss");
				x.replaceWith("<blockquote xml:lang='EN'></blockquote><a href='#' class='rss'>Newsletter &amp; <acronym title='Really Simple Syndication'>RSS</acronym></a></blockquote>");
			}

		} else {

			if ( $("a:contains('Newsletter & <abbr title='Really Simple Syndication'>RSS</abbr>')") ) {
				$("a:contains('Newsletter & <abbr title='Really Simple Syndication'>RSS</abbr>')").wrap("<blockquote xml:lang='EN' />");
			}

		}

	if ( $("a:contains('Sitemap')") ) {
			$("a:contains('Sitemap')").wrap("<blockquote xml:lang='EN' />")
	}
	
	if ( $("a:contains('Disclaimer')") ) {
			$("a:contains('Disclaimer')").wrap("<blockquote xml:lang='EN' />")
	}
		
	// MENU
	
	if ( $("#metanavigation").length ) {
		$("#metanavigation ul li:last-child").css("border-right","1px solid #d2d2d2");
	}

	// CONTENTPAGE REMOVE HEADER IF THERES NO IMAGE
	
	if ( $("#content .header .image").length ) {
		var image = $("#content .header .image img");
		if ( image.length == 0 ) {
			$("#content .header").remove();	
		}		
	}
	
	// BACKEND REMOVE IMG
	
	if ( $(".subteaser.first").length ) {
	
		var editmode = false;
	
		if ( $(".portaltext ul a img").length ) {
			var editmode = true;
		}
	
		if ( editmode == true ) {
	
			$("<div class='trenner' style='clear: both; height: 50px'></div>").insertBefore($(".teaser.events.twocols a:eq(5)"));
			$(".teaser.events.twocols .subteaser").css("width","0px");
			$(".teaser.events.twocols").css("margin-top","30px");
			$(".teaser.events.twocols .headline").css("margin-bottom","30px");
		
		}
		
	}
	
	// SIDEBAR BOX EMPTY?
		
	if ( $(".sidebar .box").length ) {
	
		var color = $(".sidebar .box").css("color");
		if ( color != "#333333" ) {
			//alert("farbe ist nicht #33333");
		}
		else {			
			if ($(".sidebar .box ul li").text() == "" ) {
				$(".sidebar .box").remove();
			}	
		}
		
	}
	
	// SIDEBAR IMAGES EMPTY?
		
	if ( $(".teaser.right .content .subteaser").length ) {
	
		
	
		var fontsize = $("#content .sidebar .teaser .content").css("font-size");
		//alert(fontsize);
		if ( fontsize != "12px" ) {
		}
		else {			
			var contcount = $("#content .sidebar .teaser.right").length;
			var contcount = contcount - 1;
			//alert(contcount);
			//alert(imagecount);
			
			$("#content .sidebar .teaser").each(function() {
				var thisteaser = $(this);
				if ( $("img",thisteaser).length > 0 ) {
					//
				}
				else {
					$(this).remove();
				}
			});
			/*
			for ( i=0; i <= contcount; i++  ) {
				if ($("#content .sidebar .teaser.right:eq("+i+") .content img").length < 1 ) {
					alert(i);
					$("#content .sidebar .teaser.right:eq("+i+")").remove();
				}
			}*/
			
			
		}
		
	}
	
	
	
	// PUNKT 1 - PORTAL IMAGE CENTERING

	if ( $("#content .textarea .teaserrow .teaser.news .content .image a img").length ) {

		var standardwidth = $("#content .textarea .teaserrow .teaser.news .content .image").width();
		var standardheight = $("#content .textarea .teaserrow .teaser.news .content .image").height();
		var img = $("#content .textarea .teaserrow .teaser.news .content .image a img");
		
		var imgheight = img.height();
		var imgwidth = img.width();
		
		if ( imgheight > imgwidth ) {
			img.height(standardheight);
			var imgwidth = img.width();
			console.log(imgwidth);
			var marginfromleft = ( standardwidth - imgwidth ) / 2;
			img.css("margin-left",marginfromleft);
		}
		else {
			img.width(standardwidth);
		}
	}
	
	// PUNKT 2 - METANAV DROPOUT CALCULATION

	if ( $("#metanavigation").length ) {

		$("#metanavigation > ul > li").mouseenter(function() {
			
			if ( $("ul",this).length ) {

				var thisposfromleft = $(this).offset().left;				
				var pagewidth = 1000;
				var menuwidth = 800;
				var diff = (pagewidth - menuwidth) / 2;
				var thisposfrompage = $(this).position().left + 200;
				var dropdowndiff = (thisposfrompage - diff) * -1;
				
				var windowwidth = $(window).width();
				var margintoright = windowwidth - thisposfromleft;
				
					if ( margintoright < 600 ) {
						var childul = $("ul:first",this);
						var childulchild = $("ul",childul);
						$(childul).css("right","0px").css("left","auto");
						$(childulchild).css("right","200px").css("left","auto");
					}
			}
		}).mouseleave(function() {
			$(this).removeClass("toleft");
		});
	}

});




