var itemWidth;
var itemCount;
var contentWidth;
var theBox;
var i;
var j;
var itemHeight;
var list;
var curid;
var oldid = 0;
var SlideActive = false;
var tltr;
var blbl;
var all;
var $_GET;

function setGET() {
    var s = window.location.search.substring(1).split('&');
    if(!s.length) return;
    window.$_GET = {};
    for(var i  = 0; i < s.length; i++) {
        var parts = s[i].split('=');
        window.$_GET[unescape(parts[0])] = unescape(parts[1]);
    }
}


tltr = {
        tl: { radius: 4 },
        tr: { radius: 4 },
        bl: { radius: 1 },
        br: { radius: 1 },
        antiAlias: true,
        autoPad: true,
        validTags: ["div"]
    };
blbr = {
        tl: { radius: 0 },
        tr: { radius: 0 },
        bl: { radius: 4 },
        br: { radius: 4 },
        antiAlias: true,
        autoPad: true,
        validTags: ["div"]
    };
all = {
        tl: { radius: 4 },
        tr: { radius: 4 },
        bl: { radius: 4 },
        br: { radius: 4 },
        antiAlias: true,
        autoPad: true,
        validTags: ["div"]
    };

function menufunc() {
		$('ul#leftmenu>li.itemwithsub').find('div').hide();

		$('ul#leftmenu>li:last-child').css({backgroundPosition: '0px -40px'});
		$(this).find('div').show();

		//$('ul#leftmenu>li.itemwithsub').bind("click", menufunc);
		if ($(this).html() === $('ul#leftmenu>li:last-child').html()) {
			$(this).css({backgroundPosition: '0px -81px'});
		}
		return false;
		
	}

function Slide(object, pageid, _oldid) {
	if (SlideActive === false) {
		if (pageid != _oldid) {
			oldid = pageid;
			SlideActive = true;
			//if (_oldid != 0) {
				object.children('.content[pageid=' + _oldid + ']').animate({marginLeft: - itemWidth}, 500, "swing", function () {
					//object.children('.content[pageid=' + _oldid + ']').css({marginLeft: itemWidth});
					object.children('.content[pageid=' + _oldid + ']').css({display: 'none'});
					//object.children('.content[pageid=' + _oldid + ']').css({width: '470'});
				});		
			//} else {
				//object.children('.content[pageid!=' + pageid + ']').css({display: 'none'});
			//}
		
			//object.children('.content[pageid!=' + pageid + ']').css({display: 'none'});	
			object.children('.content[pageid=' + pageid + ']').css({display: 'block'});
			object.children('.content[pageid=' + pageid + ']').animate({marginLeft: 0}, 500, "swing", function () {
				object.children('.content[pageid!=' + pageid + ']').css({marginLeft: itemWidth, width: 430});
				SlideActive = false;
			});
			object.children('.title').children('ul').children('li[pageid!=' + pageid + ']').removeClass('active');
			object.children('.title').children('ul').children('li[pageid=' + pageid + ']').addClass('active');
		
		}
	}
}


function IE6() {
	$('ul#leftmenu>li:first-child').css({lineHeight: '39px'});
	$('.tabmenu').css({display: 'block'});
	$('ul#leftmenu>li:first-child').css({backgroundPosition: '0 0'});	
	$('ul#leftmenu>li:first-child').hover(function(){$(this).css({backgroundPosition: '-260px 0'})},function(){$(this).css({backgroundPosition: '0px 0'})});

	


	
	$('ul#leftmenu>li.active:first-child').css({backgroundPosition: '-260px 0'});
	$('ul#leftmenu>li:last-child').css({backgroundPosition: '0 -40px'});
	$('ul#leftmenu>li:last-child').hover(function(){$(this).css({backgroundPosition: '-260px -40px'})},function(){$(this).css({backgroundPosition: '0px -40px'})});


}

$(document).ready(function () {

		$('a').bind('click', function(){
			setGET();
			$.cookie('refID', $_GET['id']);
		});
	
	
	$("p").filter( function() {
	    return $.trim($(this).html()) == '';
	}).remove();
	//ContentSwitcher
	$.each($('.greyheadlineteaserbox'), function () {
		itemCount = $( "> div.content",this).length;
		if (itemCount > 1) {
			theBox = $(this);
			contentWidth =  $(this).children('div.content').width();
			itemWidth = $(this).children('div.content').outerWidth();
			itemHeight = $(this).children('div.content').outerHeight();
			theBox.height(theBox.height());
			$(this).children('div.content').width($(this).children('div.content').width());
			$(this).children('div.content').css({display:'inline-block'});
			$(this).prepend('<hr style="position:absolute; width:' + itemWidth + 'px; border:0; border-bottom: 1px solid #CCCCCC; margin:37px 0 0;height: 0px;"/>');
			//$(this).append('<hr style="position:absolute; width:' + String(parseInt(itemWidth+1 , 10)) + 'px; border:0; border-bottom: 1px solid #CCCCCC; top: '+ String(parseInt(itemHeight +18 , 10)) + 'px; margin: 0;height: 0px;"/>');
			$(this).css({overflow: 'hidden'});
			j = 0;
			$.each($(this).children('.content'), function () {
				$(this).attr('pageid', j);
				j = j + 1;
				
			});
			list = '<ul class="pagination">';
			for (i = 0; i < itemCount; i++) {
				list += '<li pageID="' + i + '"></li>';
			}
			list += '</ul>';
			$(this).children('.title').prepend(list);
			$(this).children('.title').children('ul').children('li:first-child').addClass('active');
			$(this).children('.content[pageid=0]').css({marginLeft: '0'});
			$(this).children('.content[pageid!=0]').css({marginLeft: itemWidth});
			var Timer = window.setInterval(function () {
				curid = parseInt($('div.content[pageid=0]', theBox).parent().children('.title').children('ul').children('li.active').attr('pageid'), 10);
				oldid = curid;
				if ($('div.content[pageid=0]', theBox).parent().children('div.content').size() === curid + 1) {
					oldid = curid;
					curid = -1;
				}
				Slide($('div.content[pageid=0]', theBox).parent(), curid + 1, oldid);
			}, 10000);
			$.each($(this).children('.title').children('ul').children('li'), function () {
				$(this).bind("click", function () {
					window.clearInterval(Timer);
					Slide($(this).parent().parent().parent(), $(this).attr('pageid'), oldid);
				});
			});
		}
	});

	
	if ($('.openSub').length) {

		$('.itemwithsub').not('.openSub').children('div').css({height: 0, paddingTop: 0});
		if($.cookie('refID')){
			if ($('.openSub').find('a[href*='+$.cookie('refID')+']').length == 0	) {
				$('.itemwithsub').not('.openSub').children('div').height(0);
				var sheight;
				sheight = $('.openSub>div>ul').height() + 7;
				$('.openSub>div').animate({height: sheight}, 500, 'swing');
			}
		}else{
			$('.itemwithsub').not('.openSub').children('div').height(0);
			var sheight;
			sheight = $('.openSub>div>ul').height() + 7;
			$('.openSub>div').animate({height: sheight}, 500, 'swing');
		}
	}
	
	$('.itemwithsub>a').bind('click', function(){
		if(!$(this).parent().hasClass('openSub')){
			var clickedSub = $(this);
			$('.openSub').children('div').animate({height:0,paddingTop:0}, function(){
				$('.itemwithsub').not('.openSub').children('div').css({display: 'none',borderWidth:0});
				var sheight;
				$('.itemwithsub').removeClass('openSub');
				clickedSub.parent().addClass('openSub');
				$('.openSub').children('div').css({display: 'block',borderLeftWidth: '1px',borderRightWidth: '1px',borderBottomWidth: '1px'});
				sheight = clickedSub.parent().find('ul').height() + 7;
				$('.openSub').children('div').animate({height: sheight, paddingTop: 7}, 500, 'swing');
			});
			
			
		}
		return false;
	});


	
	
	
	if ($.browser.msie && ($.browser.version <= 7)) {
		IE6();
		node = $("ul#mainmenu li");
	    node.mouseover(function() {
	    if (this.className != 'active'){
		this.className+=" over";
	    }
    });
    node.mouseout(function() {this.className=this.className.replace(" over", "");});
	}

    //last-child fixes
	if ($.browser.msie) {
		$('ul#leftmenu li.sub:last-child').css({backgroundColor: '#fff',borderBottom:'1px solid #fff',marginBottom:'-1px'});
		$('ul#leftmenu li.itemwithsub.active:last-child').css({backgroundPosition: '-260px -80px '});
		$('ul#leftmenu>li:last-child').css({backgroundPosition: '0px -40px'});
		$('ul#leftmenu>li:last-child').hover(function(){$(this).css({backgroundPosition: '-260px -40px'})},function(){$(this).css({backgroundPosition: '0px -40px'})});
		
		$('ul#leftmenu>li.active:last-child').css({backgroundPosition: '-260px -40px'});
		$('ul#leftmenu>li.openSub:last-child').css({backgroundPosition: '0 -81px'});
		$('ul#leftmenu>li.openSub:last-child').hover(function(){$(this).css({backgroundPosition: '-260px -81px'})},function(){$(this).css({backgroundPosition: '0 -81px'})});
	}
	
	//Kontaktseite
	$("body#contact div.smallcolumn>div>div>div").height(53);
	$("body#contact div.smallcolumn>div:first-child>div>div.smallcolumnbox").height(parseInt($('body#contact div.smallcolumn>div:first-child>div>div>p').height(),10) + 63);
	
	$('body#contact div.smallcolumn div.smallcolumnbox h3').bind("click", function () {
		if ($(this).parent().height() == 53) {
		clickedPlace = $(this);
		
		sheight = clickedPlace.parent().children('p').height() + 63;
		$("div.smallcolumn>div>div>div").animate({height:53});
		clickedPlace.parent().animate({height: sheight});
		}
		
	});
	//CurvyCorners
	if ($('.box').length>0){curvyCorners(tltr,'.box');}
	if ($('.greyheadlineteaserbox').length>0){curvyCorners(tltr,'.greyheadlineteaserbox');}
	//if ($('#gut_beraten_in_sachen').length>0){$('.content','#gut_beraten_in_sachen').css({borderBottom: '1px solid #ccc'})}
	//if ($('ul#leftmenu>li:last-child div').length>0){ curvyCorners(blbr,'ul#leftmenu>li:last-child div');}
	if ($('.teaserbox').length>0){curvyCorners(all,'.teaserbox');}
	if ($('#mailform').length>0){curvyCorners(all,'#mailform');}
	if ($('#mailform .formBox').length>0){curvyCorners(all,'#mailform .formBox');}
});
