/*********************************************************
* Kompatibilitätsmodus
*********************************************************/

jQuery.noConflict();


/*********************************************************
* Scrolllinks
*********************************************************/

function scrollinterna()
{
	jQuery('a[href*=#]').click(function() {
		if( location.pathname.replace( /^\//,'' ) == this.pathname.replace( /^\//,'' ) && location.hostname == this.hostname && jQuery(this).attr('href').replace( /#/,'' ) != '' )
		{
			var jQuerytarget = jQuery(this.hash);
			jQuerytarget = jQuerytarget.length && jQuerytarget || jQuery('[name=' + this.hash.slice(1) +']');
			if (jQuerytarget.length)
			{
				var targetOffset = jQuerytarget.offset().top;
				jQuery('html,body').animate({scrollTop: targetOffset}, 800, 'swing');
				return false;
			}
		}
	});
};


/*********************************************************
* Drucken
*********************************************************/

function printMe()
{
	jQuery('#printMe').click(function(){
		window.print();
		return false;
	});
};


/*********************************************************
* Menü-Slider
*********************************************************/

function scrollposition()
{
	var scrollX = parseInt('0')
	var scrollY = parseInt('0');

	if( document.pageYOffset )
	{
		scrollX = document.pageXOffset;
		scrollY = document.pageYOffset;
	}
	else if( document.documentElement && document.documentElement.scrollTop )
	{
		scrollX = document.documentElement.scrollLeft;
		scrollY = document.documentElement.scrollTop;
	}
	else if( document.body )
	{
		scrollX = document.body.scrollLeft;
		scrollY = document.body.scrollTop;
	}

	return ({scrollX: scrollX, scrollY: scrollY })
}


function menuSlider()
{
	if( jQuery('#home').attr('id') != 'home' )
	{
		var tolerance = parseInt('150');
		if( jQuery('#menu-slider').attr('id') == 'menu-slider' )
		{
			var offsetY = jQuery('#submenu').offset().top;
			var uLimit = offsetY + jQuery('#submenu').height();

			if( jQuery.browser.msie && jQuery.browser.version < 7 )
				jQuery('#menu-slider').css({ display: 'none', position: 'absolute', top: (uLimit + 50) });

			jQuery(window).scroll(function(){
				var scrollPos = scrollposition();
				var scrollPosY = scrollPos.scrollY;

				if( scrollPosY > uLimit - tolerance && uLimit < scrollPosY + tolerance )
				{
					if( jQuery('#menu-slider').css('display') == 'none' )
						jQuery('#menu-slider').fadeIn('slow');

					if( jQuery.browser.msie && jQuery.browser.version < 7 )
						jQuery('#menu-slider').css({ top: ( scrollPosY + jQuery(window).height() - 80 ) });

					/*slideCoordY = scrollPosY + jQuery(window).height() - 40;
					if( !scrollReset)
						window.setTimeout( 'smoothscroller(' + uLimit + ')', 100 );*/
				}
				else
				{
					if( jQuery('#menu-slider').css('display') != 'none' )
						jQuery('#menu-slider').fadeOut('fast');
				}

			});
		}
	}
}

/*function smoothscroller( startPos )
{
	if( !scrollReset )
	{
		scrollReset = true;
		jQuery('#menu-slider').animate({ top: + slideCoordY }, 800, 'easeOutCubic', function(){
			scrollReset = false;
		});
	}
	else
		window.setTimeout( 'smoothscroller(' + startPos + ')', 1000 );
}*/


/*********************************************************
* Suchform befüllen
*********************************************************/

function searchString()
{
	if( jQuery('#searchstring').attr('id') )
	{
		var searchElement = jQuery('#searchstring');
		var searchStringContent = getSearchStringContent(querySt('L'));

		if( !searchElement.attr('value') )
			searchElement.attr('value', searchStringContent);

		searchElement.focus(function(){
			if( searchElement.attr('value') == searchStringContent )
				jQuery(this).attr('value', '');
		});
		searchElement.blur(function(){
			if( !searchElement.attr('value') )
				jQuery(this).attr('value', searchStringContent);
		});
	}
}

function querySt(ji) // http://ilovethecode.com/Javascript/Javascript-Tutorials-How_To-Easy/Get_Query_String_Using_Javascript.shtml
{
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for( i=0; i < gy.length; i++ )
	{
		ft = gy[i].split("=");
		if( ft[0] == ji )
		{
			var result = false;
			result = ft[1];
			if( !result ) result = 0;
		}
	}
	return result;
}

function getSearchStringContent( langId )
{
	switch( langId )
	{
		case '0': // English
			return 'Enter keyword';
			break;
		case '1': // Deutsch
			return 'Suchbegriff eingeben';
			break;
		default:
			return 'Enter keyword';
	}
}


/*********************************************************
* Liquide Stütze
*********************************************************/

function liquidSupportPrep() {
	jQuery('#right, .dependence .right').css({
		left: '100%',
		position: 'absolute'
	});
}

function liquidSupport( constantWidth ) {
	if( !(jQuery.browser.msie && jQuery.browser.version <= 7 )) {
		var contentWidth = parseInt(jQuery('#wrapper').css('width')) - parseInt(jQuery('#content').css('padding-left'));
	}
	else {
		if( jQuery('.depended-multi-column').size() > 0 ) {
			var contentWidth = parseInt(jQuery('#wrapper').css('width')) - parseInt(jQuery('.depended-multi-column .right').outerWidth());
		}
		else {
			var contentWidth = parseInt(jQuery('#wrapper').css('width')) - parseInt(jQuery('#submenu').outerWidth()) - parseInt(jQuery('#content').css('padding-left')) - parseInt(jQuery('#right').css('margin-right'));
		}
	}

	constantWidth = ( !constantWidth ) ? parseInt(jQuery('#right, .dependence .right').outerWidth()) + parseInt(jQuery('#right, .dependence .right').css('margin-left')) + parseInt(jQuery('#right, .dependence .right').css('margin-right')) : constantWidth;
	var liquidWidth = contentWidth - constantWidth;

	if( liquidWidth > 0 ) {
		jQuery('#left, .dependence .left').css('width', liquidWidth + 'px');
		jQuery(window).unbind('resize').resize(function(){
			liquidSupport( constantWidth );
		});

	}
}

function liquidSupportAfter() {
	jQuery('#right, .dependence .right').each(function(){
		var curEl = jQuery(this);
		var curParent = curEl.parent();
		var rightHeight = parseInt(curEl.css('height'));
		if( parseInt(curParent.css('min-height')) < rightHeight ) {
			curParent.css('min-height', rightHeight + 'px');
		}
	});
}


/*********************************************************
* Hintergrund-Slider für die Startseite
*********************************************************/

var backgroundHolderList = [];
var stopTime = parseInt('8000');

function homepageBackgroundSlider() {
	if( jQuery('#home').size() > 0 ) {
		var curBackgroundHolder = jQuery('#content');

		curBackgroundHolder.children().css({
			'position': 'relative',
			'z-index': '9999'
		});

		window.setTimeout('slideBackgrounds()', stopTime);
	}
}

function slideBackgrounds( curBackgroundIteration, stopSearching ) {
	var curBackgroundHolder = jQuery('#content');
	var curBackgroundHolderPaddingTop = curBackgroundHolder.css('padding-top');
	curBackgroundIteration = ( !curBackgroundIteration ) ? 2 : curBackgroundIteration;
	var curBackground = curBackgroundHolder.css('background-image').replace(/^url|[\(\)]/g, '').replace(/"|'/g, '');
	var nextBackground = curBackgroundIteration;

	if( !stopSearching ) {
		imgPreloader = new Image();

		imgPreloader.onerror = function() {
			curBackgroundHolder.find('.dynamicBackground-' + parseInt(curBackgroundIteration-1)).fadeOut(2000, function(){
				window.setTimeout('slideBackgrounds(' + false + ', ' + true + ')', stopTime);
			});
		}

		imgPreloader.onload = function() {
			if( curBackgroundHolder.find('.dynamicBackground-' + curBackgroundIteration).size() < 1 ) {
				curBackgroundHolder.append('<div class="dynamicBackground-' + curBackgroundIteration + '" />');

				var newCurBackgroundHolder = curBackgroundHolder.find('.dynamicBackground-' + curBackgroundIteration);
				var oldCurBackgroundHolder = curBackgroundHolder.find('.dynamicBackground-' + parseInt(curBackgroundIteration-1));
				backgroundHolderList['bg-' + curBackgroundIteration] = newCurBackgroundHolder;

				newCurBackgroundHolder.css({
					'background-image': 'url("' + curBackground.replace(/\.jpg/, '-' + curBackgroundIteration + '.jpg') + '")',
					'background-repeat': 'no-repeat',
					'width': '100%',
					'height': '100%',
					'position': 'absolute',
					'top': '0',
					'left': '0',
					'z-index': '1',
					'display': 'none'
				});

				newCurBackgroundHolder.fadeIn(2000, function(){
					oldCurBackgroundHolder.hide();
					window.setTimeout('slideBackgrounds(' + (curBackgroundIteration+1) + ')', stopTime);
				});
			}
		}

		imgPreloader.src = curBackground.replace(/\.jpg/, '-' + curBackgroundIteration + '.jpg');
	}
	else {
		if( backgroundHolderList['bg-' + curBackgroundIteration] ) {
			var newCurBackgroundHolder = curBackgroundHolder.find('.dynamicBackground-' + curBackgroundIteration);
			var oldCurBackgroundHolder = curBackgroundHolder.find('.dynamicBackground-' + parseInt(curBackgroundIteration-1));

			newCurBackgroundHolder.fadeIn(2000, function(){
				oldCurBackgroundHolder.hide();
				window.setTimeout('slideBackgrounds(' + (curBackgroundIteration+1) + ', ' + true + ')', stopTime);
			});
		}
		else {
			curBackgroundHolder.find('.dynamicBackground-' + parseInt(curBackgroundIteration-1)).fadeOut(2000, function(){
				window.setTimeout('slideBackgrounds(' + false + ', ' + true + ')', stopTime);
			});
		}
	}
}


/*********************************************************
* IE fixen
*********************************************************/

function fixIE() {
	jQuery('.open').live('click', function(){
		if( jQuery('#lbIeFix').size() == 0 && jQuery.browser.msie ) {
			jQuery('#lbImage').wrap('<div id="lbIeFix" />');
		}
	});

	// if( jQuery.browser.msie ) {
	// 	jQuery('.myGallery .slideElement').each(function(){
	// 		this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' + jQuery(this).css('background-image').replace(/url|"|\(|\)/g, '') + '", sizingMethod="scale")';
	// 		this.style.background = 'none';
	// 	});
	// }

}


/*********************************************************
* Document-Ready
*********************************************************/

jQuery(document).ready(function() { // Wenn DOM geladen, dann ...
	scrollinterna(); // Scrollinks
	printMe(); // Drucken-Funktion
	menuSlider(); // Menü-Button
	searchString(); // Suchform befüllen

	if( !jQuery.browser.opera || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) ) {
		liquidSupportPrep(); liquidSupport(); // Liquide Stütze
	}
});

window.onload=function() {
	if( !jQuery.browser.opera || (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) ) {
		liquidSupportAfter(); // Liquide Stütze
	}
	homepageBackgroundSlider(); // Hintergrund-Slider für die Startseite
	fixIE(); // IE fixen
}
