jQuery.noConflict();
jQuery(document).ready(function() {
	jQuery(window).unbind("resize");
	jQuery(window).bind("resize", setBackgroundSize);
	
	// leere Newsbox ausblenden
	var newsText = jQuery('div.news_text p');
	if (newsText.html() == '') jQuery('div#newsbox').hide();

	var BGimage = jQuery('div#background img');
	var initImageHeight = BGimage.height();
	var initImageWidth = BGimage.width();
	var imgRatio = initImageWidth/initImageHeight;

	// Initialisierung des Hintergrundbildes
	function setBackgroundSize() {
		var windowWidth = jQuery(window).width();
		var windowHeight = jQuery(window).height();
		var leftMargin = (windowWidth-980)/2;
		if (leftMargin<0) leftMargin = 0;
		
		if(windowWidth>initImageWidth) {
			BGimage.width(windowWidth);
			BGimage.height(windowWidth/imgRatio);
		}
		
		if(windowHeight>BGimage.height()) {
			BGimage.height(windowHeight);
			BGimage.width(windowHeight*imgRatio);
		}

		// Inhalte zentrieren
		jQuery('div#mainNavi').css('left', leftMargin);		
		jQuery('div#logo').css('left', leftMargin);		
		jQuery('div#languagebox').css('left', leftMargin);		
		//jQuery('div#sidebar').css('left', leftMargin);	
		//jQuery('div#frame').css('margin-left', leftMargin+220);		
		jQuery('div#frame').css('margin-left', leftMargin);
		
		//if ((jQuery('div#sidebar').height()+220) > windowHeight) {
		//	jQuery('div#sidebar').css('position', 'relative');
		//}else {
	//		jQuery('div#sidebar').css('position', 'fixed');
		//}
	}
	

	// Funktion beim ersten Start ausführen und dann nochmal, wenn alles geladen ist
	jQuery(function() {
    	setBackgroundSize();
	});
	window.onload = function() {setBackgroundSize();setBackgroundSize();}
	

	  /////////////////////
	 // Gallery-Skripts //
	/////////////////////
	
	// items abfragen
	if(jQuery('div#startgallery')) {
		itemCount = 0; var itemLinkCount = 0;
		var thisTitle, thisClass;
		jQuery('div.galleryItem').each(function(e) {
			itemDisplay = 'none';
			jQuery(this).hide();
			if (itemCount == 0) {
				itemDisplay = 'block';
				itemActive = 'div.galleryItem.no_1';
				itemActiveNo = 1;
				jQuery(this).show();
			}
			itemCount++;
			jQuery(this).addClass('no_'+itemCount);
			thisTitle = jQuery(this).children('div.galleryHeader').html();
			itemLinkCount = 0;
			
			// navi-links erstellen
			jQuery('div.galleryNavi').each(function(e) {
				itemLinkCount++;
				thisClass = '';
				if (itemLinkCount == itemCount) {
					thisClass = 'class="active" ';
				}
				jQuery(this).append('<div class="galleryButton"><a href="javascript:showItem('+itemCount+');" title="'+thisTitle+'" '+thisClass+'><img src="clear.gif" width="10" height="10" alt="'+thisTitle+'" /></a></div>');
			});
		});	
	}
	
	// einmal aufrufen
	// setTimeout('showItem(2)', 7500);
	timer = setInterval('showItem(2)', 7500);
	
	// PNG-Fix
	//jQuery(document).pngFix();
});


	// Gallery-Itemwechsel
	var itemDisplay, itemActive, itemActiveNo, itemTemp, itemCount, timer;
	function showItem(itemNo) {
		clearInterval(timer);
		itemTemp = 'div.galleryItem.no_' + itemNo;
		//jQuery(itemActive).fadeOut('slow', function() {
        	jQuery(itemTemp).fadeIn('slow');
        	jQuery(itemActive).fadeOut('slow');
        	var itemTempHeight = jQuery(itemTemp).height()+15;
        	jQuery('div#startgallery').animate({
    			'height': itemTempHeight
  			}, 500);
      	//});
		itemActive = itemTemp;
		itemActiveNo = itemNo+1;
		if (itemActiveNo>itemCount) itemActiveNo=1;
		//setTimeout('showItem(itemActiveNo)', 7500);
		timer = setInterval('showItem(itemActiveNo)', 7500);
	}
	
jQuery(window).scroll(function() {
	jQuery('div#sidebar').css('padding-top', '220');
	var scrollYPos = jQuery(window).scrollTop();
	var sidebarHeight = jQuery('div#sidebar').height();
	var contentHeight = jQuery('div#content').height()+220+jQuery('div#imprint').height();
	var windowH = jQuery(window).height();
	var newSidebarPad = scrollYPos+220;
	var actSidebarPad = sidebarHeight+220-scrollYPos;
	var padValue = windowH-sidebarHeight+scrollYPos;

	if ((sidebarHeight+220) < jQuery(window).height()) {
		//if ((newSidebarPad+sidebarHeight) > contentHeight) newSidebarPad = contentHeight - sidebarHeight;
		//jQuery('div#sidebar').css('padding-top', newSidebarPad);
		jQuery('div#sidebar').css('position', 'fixed');
		//jQuery('div#sidebar').css('padding-top', '220');
		jQuery('div#sidebar').css('top', '0');		
	} else {
		jQuery('div#sidebar').css('position', 'relative');
		jQuery('div#sidebar').css('top', '0');			
		if (actSidebarPad<windowH) {
			//jQuery('div#sidebar').css('padding-top', padValue);
			jQuery('div#sidebar').css('position', 'fixed');
			var sidebarTop = windowH-sidebarHeight-220;
			jQuery('div#sidebar').css('top', sidebarTop);
		}
	}
});
