/* IE6nomore
------------------------------------------------------------------------------------------------ */

$.IE6nomore = function() {
	
    if(!$('#ie6nomore').length)
        return;

    $('#ie6nomore').css({
        'left'      : 0,
        'top'       : 0,
        'bottom'    : 0,
        'right'     : 0,
        'position'  : 'absolute',
        'z-index'   : 9999
    });

};

/* Swap image
------------------------------------------------------------------------------------------------ */

function swapImage(element, newimage) {

    var oldsrc      = element.src;
    element.src     = newimage;

    if (!element.onmouseout) {
        element.onmouseout = function() {
            swapImage(this, oldsrc);
        }
    }

}

/* Get url vars
------------------------------------------------------------------------------------------------ */

$.getUrlVars = function() {

    var vars    = [],
        hash    = '',
        hashes  = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    $(hashes).each(function() {
        hash = this.split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    });

    return vars;
};

/* Spam protection
------------------------------------------------------------------------------------------------ */

function getAdr(prefix, postfix, text) {
    document.write('<a href="mailto:' + prefix + '@' + postfix + '">' + (text ? text.replace(/&quot;/g, '"').replace(/%EMAIL%/, prefix + '@' + postfix) : prefix + '@' + postfix) + '</a>');
}


/* Handle Sizes
-------------------------------------------------------------------------------------------------*/
function handleSizes() {

	//Breite anpassen Menübereich
	var rightwidth = $(window).width() / 2 - 162;
	$('#page_right').width(rightwidth);
	
	
	//Größe Anpassen Graubereich Hintergrund //620
	var greyheight = $(window).height() -30;
	var greywidth = $(window).width() / 2 + 130;

	if ((greywidth < 620) && !$('#top').is('.startpage') ) {
		$('#bg_grey').css('margin-left', '0px');
		$('#author').hide();
		greywidth = 620;	
	} else {
		$('#bg_grey').css('margin-left', '18px');
		$('#author').show();	
	}
	$('#bg_grey').height(greyheight).width(greywidth);
	
	
	// Weißbereich Oben und Unten	
	$('#bg_white_top').width(greywidth);
	$('#bg_white_bottom').width(greywidth);
	
	
	//Weiß-Contentbereich Höhe anpassen
	var pageheight = $(window).height() - 147;
	if ($('#contenttext').height() < pageheight && $('#contenttext').height() > 0) {
		$('#contenttext').height(pageheight);	
	}	
	
	
	//Startseite Breite anpassen
	var startwidth = $(window).width() / 2 + 140;
	if (startwidth < 592) {
		$('.startpage').find('#bg_blue').css('margin-left', '0px');	
		$('.startpage').find('#author').hide();
		startwidth = 620;
	} else {
		$('.startpage').find('#bg_blue').css('margin-left', '30px');
		$('.startpage').find('#author').show();	
	}
	$('.startpage').find('#bg_blue').width(startwidth);
	
	
	//Footer anpassen an Auflösungen < 768px 
	if (pageheight < 470) {
		$('#quotediv').hide();
	} else {
		$('#quotediv').show();
	}
	
	if (pageheight < 420 ) {
		$('#footer_address').hide().nextAll('.links').hide();
		$('#footer_address_small').show();	
	} else {
		$('#footer_address').show().nextAll('.links').show();
		$('#footer_address_small').hide();		
	}
	
	
}

function startanimation() {

	if ($('#top').is('.startpage') ) {
	
		var h = 0;
		
		$('#content').add('.bg_color').show();
		$('#sitetitle').css('opacity', '0').animate({
			opacity: '1'
		}, 500);
	
	
		$('#animatelist > .startpage_pic').hide();
		var intervalID = window.setInterval(function() {
		
			//--------------Animation------------------	
				h = $('#animatelist > .startpage_pic:hidden').last().height();	
				
				$('#animatelist > .startpage_pic:hidden').last().delay(100).height(0).show().animate( {
					height: h + 4
					}, 90, function () {
						//Animation complete
						$(this).animate( {
							height: h
						}, 200, function () {});
					});
					
				if (!$('#animatelist > .startpage_pic').is(':hidden')) {
					window.clearInterval(intervalID);
				}
			//------------------------------------------		
		}, 1000);

		$('.startpage').find('a').not('#quote').not('#author').click(function() {
				
				var linkhref=this.href;
				
				//Links zu den Detailansichten abfangen
				if ( $('#nav > li:not(.active) > ul > li > a').is(this) || $('#nav > #nav_e40189 > a').is(this) || $('#page_right_footer').find('a').is(this) ) {
					$('#content').animate({	
						opacity: '0'
					}, 300, function() {
						showLoading();
					});
					
					$('#bg_blue').animate({	
						marginLeft: '-878px'/*'-839px'*/
					}, 1000, function() {
						//URL Variable übergeben		
						location.href =linkhref + '?toDetail=1';
					});	
				} else {
					$('#content').animate({	
						opacity: '0'
					}, 300, function() {
						showLoading();
					});
					
					$('#bg_blue').animate({	
						marginLeft: '-878px'/*'-839px'*/
					}, 1000, function() {
						//URL Variable übergeben		
						location.href =linkhref;
					});	
				}
				return false;
			});
	}
}

function infolistSubmenu() {

	$('.buchhaltung').find('#contenttext > table:first').hover(function() {
		$('.buchhaltung').find('#content_submenu ').css('z-index', '3').css('opacity', '0.2');
	}, function() {
		$('.buchhaltung').find('#content_submenu ').css('z-index', '6').animate({
			opacity:1
		}, 200);
	});
	
	
	$('.buchhaltung').find('#contenttext > table').is(function() {
		if ($(this).width() > 410) {
			$(this).addClass('big');
		}
	});
	
	
	$('.steuern').find('#contenttext > table').is(function() {
		if ($(this).width() > 410) {
			$(this).addClass('big');
		}
	});
		
	
	$('.steuern').find('#contenttext > table.big:first').hover(function() {
		$('.steuern').find('#content_submenu ').css('z-index', '3').css('opacity', '0.2');
	}, function() {
		$('.steuern').find('#content_submenu ').css('z-index', '6').animate({
			opacity:1
		}, 200);
	});
}


function pageanimation() {

	//---------------Übersichtsseite-----------------
	if ( $('#top').is('.overviewpage') ) {
		
		if ( !$('#top').is('.startpage') ) {	//nicht die Startseite
		
			showContent();
		
			var entrylinks = $('#overview > .entry').find('a');
			
			
			$('.overviewpage').find('#page_right').find('a').not('#quote').add(entrylinks).click(function() {
				//hideContent();
			
				//Links zu den Detailansichten abfangen
				if ( $('#nav > li:not(.active) > ul > li > a').is(this) || $('#nav > #nav_e40189 > a').is(this) || $('#page_right_footer').find('a').not('.startpagelink').is(this) ||$(entrylinks).is(this) ) {
					hideContentAndSetVariable(this.href, 1);
				//Links zur Startseite abfangen
				} else if( $(this).is('.startpagelink') ) {
					showStartpage(this.href);
				//Links zu Übersichtsseiten 
				} else {
					hideContentAndLoad(this.href);
				}
				
				return false;
			});
		}
	
	} else {
	//------------------Detailseite ------- mit Animation einblenden		
		var urlVars = $.getUrlVars();
		
		if (urlVars['toDetail'] == 1) {
			//alert("startAnimation");
			$('#page_left').hide();
			showDetailView();	
		} else {
			$('#content').add('.bg_color').show();
			onPageDisplay();	
		}
		
		
		//Link zu den Übersichtsseiten abfangen
		$('#nav > li:not(#nav_e40189) > a').click(function() {
			hideDetailView(this.href);
			return false;			
		});
		
		//Links zu den Detailansichten abfangen
		$('#nav > li:not(.active) > ul > li > a').click(function() {
			hideDetailViewAndSetVariable(this.href, 1);
			return false;
		});
		$('#nav > #nav_e40189 > a').click(function() {
			hideDetailViewAndSetVariable(this.href, 1);
			return false;
		});
		$('#page_right_footer').find('a').not('#quote').not('.startpagelink').click(function() {
			hideDetailViewAndSetVariable(this.href, 1);
			return false;
		});	
		
		//Schließen Links abfangen
		$('#close_button_link').click(function() {
			hideDetailView(this.href);
			return false;
		});
		
		//Zur Startseite-Link abfangen
		$('.startpagelink').click(function() {
			var detail = $('#page_left');
			var linkhref = this.href;
			
			$(detail).animate({
				marginLeft: '156px'
			}, 500, function() {
				//Animation complete.
				showLoading();
				showStartpage(linkhref);		
			});
			return false;	
		});	
	}
}

/*-----------------------Functions-------------------------*/
function hideDetailView(linkhref) {
	var detail = $('#page_left');
			
	$(detail).animate({
		marginLeft: '156px'
	}, 500, function() {
		//Animation complete.
		showLoading();
		location.href=linkhref;	
	});		
}

function hideDetailViewAndSetVariable(linkhref, toDetail) {	
	var detail = $('#page_left');
	
	$(detail).animate({
		marginLeft: '156px'
	}, 500, function() {
		//Animation complete.
		showLoading();
		location.href =linkhref + '?toDetail=' + toDetail;	
	});
}

function showDetailView() {
	var detail = $('#page_left');
			
	$(detail).css('margin-left', '156px').show();
	$('#content').add('.bg_color').show();
	hideLoading();
	onPageDisplay();
			
	$(detail).delay(300).animate({
		marginLeft: '-481px'
	}, 500, function() {
		//Animation complete.	
	});
}

function hideContentAndLoad(linkhref) {	
	$('#content').animate({
		opacity: '0'
	}, 300, function() {
		showLoading();	
		location.href=linkhref;
	});
}

function hideContentAndSetVariable(linkhref, toDetail) {	
	$('#content').animate({
		opacity: '0'
	}, 300, function() {
		showLoading();	
		//URL Variable übergeben		
		location.href =linkhref + '?toDetail=' + toDetail;
	});	
}

function showContent() {	
	$('#content').css('opacity', '0').show();
	
	hideLoading();
	$('#content').delay(300).animate({
		opacity: '1'
	}, 300, function() {	
	});	
}

function showLoading() {
	$('#loaderanimation').show();
}

function hideLoading() {
	$('#loaderanimation').hide();	
}

function showStartpage(linkhref) {
	var newWidth = $(window).width() / 2 + 140;
	
	$('#bg_white_top').add('#bg_white_bottom').hide('fast');
	$('#page_left').animate({
		'opacity': '0'
	}, 100);
	$('#right_border').animate({
		'opacity': '0'
	}, 100);
	$('#menu').css('background-color', 'transparent');
	
	$('#bg_blue').animate({
		width: newWidth 
	}, 300, function() {
		$('#bg_blue').animate({
			marginLeft: '30px'
		}, 100, function() {
			location.href =linkhref;
		});		
	});	
}



/* DOM
------------------------------------------------------------------------------------------------ */

$(document).ready(function() {

    // IE6nomore

    $.IE6nomore();

	//Klassen vergeben
	$('#overview').parents('#top').addClass('overviewpage');
	$('#top').not('.overviewpage').addClass('detailviewpage');

	//Inhalte für Animation bereits jetzt ausblenden
	if ( $('#top').is('.startpage') ) {
		$('#sitetitle').css('opacity', '0');
		$('#animatelist > .startpage_pic').hide();
	} else if ( $('#top').is('.overviewpage') ) {
		$('#content').css('opacity', '0');
	} else if ($.getUrlVars()['toDetail'] == 1) {
		$('#page_left').css('margin-left', '156px').show();
	}
	
	//Funktionen ausführen
	onPageDisplay();

    // Menu
    $('#nav').menu({
        'removeTitle' : false
    });

	$('#nav > li > ul').prepend('<div class="bg">')
		
	//Startpageanimation
	startanimation();
	
	
	//Infolisten Seiteninterne-Links
	$('#contenttext > div > ul > li > a[href*=#]').parent().parent().addClass('dictionary_list');
	
	//Steuernews
	$('#news_page > p:first').addClass('black');
	
	//Back-Link anpassen
	var homelink = $('#logo > a').attr('href');
	$('.ueberuns').find('#close_button_link').attr('href', homelink);
	$('.sitemap').find('#close_button_link').attr('href', homelink);
	
	//Seiten-Animationen
	pageanimation();
});

function onPageDisplay() {
	//Größen anpassen
	handleSizes();
	//Infolisten-HOverSubmenü
	infolistSubmenu();
}


$(window).resize(function() {
	handleSizes();
});

