jQuery(document).ready(function(){

	/*** png fix for IE <= 6 ***/
	/* for background images use bounding element */
	jQuery('#sg-carousel').pngFix();

	/*** menu ***/
	var isOverSubmenu = new Array();
	jQuery('#sg-mainmenu td > a, ul.sg-submenu').hover(
		function() {
			var iActiveParent = jQuery(this).parents('td').index();
			if (isOverSubmenu[iActiveParent]) {
				clearTimeout(isOverSubmenu[iActiveParent]);
			}
			jQuery(this).next('ul').slideDown(50);
		},
		function() {
			var iActiveParent = jQuery(this).parents('td').index();
			isOverSubmenu[iActiveParent] = setTimeout(function() {
				jQuery('#sg-mainmenu td:eq('+iActiveParent+') > ul').slideUp(50);
	    }, 1);
	  }
	);

	/*** magnify image ***/
	if (jQuery('a.sg-zoom').length) {
		jQuery('a.sg-zoom, a.sg-fancy').colorbox({
			transition:'none',
			scrolling:false,
			opacity:.3,
			close:''
		});
	}

	/*** carousel ***/
	jQuery('#sg-carousel').jCarouselLite({
		btnNext: ".sg-carousel-right",
    btnPrev: ".sg-carousel-left",
    mouseWheel: true,
    visible: 4
	});
	/*** carousel alt text switcher ***/
	var defaultText = '';
	var lastText = '';
	jQuery('#sg-carousel img').hover(
		function() {
			// get default info text for mouse out info
			if (defaultText == '') defaultText = jQuery('#sg-carousel-info').text();
			// get current text
			jQuery(this).addClass('hover');
			var curText = jQuery(this).attr('title');
			// get and set current text to info div
			jQuery('#sg-carousel-info').addClass('hover').text(curText);
/*			jQuery('#sg-carousel-info').fadeOut(25, function() {
				jQuery(this).text(curText).fadeIn(25);
			});*/
		},
		function() {
			jQuery(this).removeClass('hover');
			jQuery('#sg-carousel-info').text(defaultText).removeClass('hover');
		}
	);

	/*** set elements to equal heights ***/
	function setEqualHeights(elems) {
		var maxHeight = 0;
		var sameelems = new Array();
		for (elem in elems) {
			if (jQuery(elems[elem]).length) {
				if (jQuery(elems[elem]).innerHeight() > maxHeight)
					maxHeight = jQuery(elems[elem]).height();
				sameelems[elem] = elems[elem];
			}
		}
		if (sameelems.length) {
			for (elem in sameelems) {
				jQuery(sameelems[elem]).height(maxHeight);
			}
		}
	}
	// call the function with json-encoded elements
	setEqualHeights({0:'#sg-sameheightbox-0', 1:'#sg-sameheightbox-1'});
	setEqualHeights({0:'#sg-sameheightbox-2', 1:'#sg-sameheightbox-3'});
	setEqualHeights({0:'#sg-sameheightbox-4', 1:'#sg-sameheightbox-5'});
	setEqualHeights({0:'#sg-sameheightbox-6', 1:'#sg-sameheightbox-7'});
	setEqualHeights({0:'#sg-sameheightbox-8', 1:'#sg-sameheightbox-9'});

	/*** "more" subnavi ***/
	jQuery('a.sg-more').click(function() {
		jQuery(this).next('.sg-more-menu').toggle();
		return false;
	});

	/*** tippgemeinschaft ***/
	jQuery('.sg-inlinemenu TD:eq(0)').addClass('active');
	jQuery('.sg-inlinemenu TD').click(function(){
		var clickedIdx = jQuery(this).index();
		jQuery(this).parent().children('TD').removeClass('active');
		jQuery(this).closest('TABLE').nextAll('TABLE').hide();
		jQuery(this).addClass('active');
		jQuery(this).closest('TABLE').nextAll('TABLE:eq('+clickedIdx+')').show();
	});

	/*** home page teaser changer ***/
	if (jQuery('.sg-hometopgal').length > 0) {
		var teaserHop = setInterval( "teaserChanger()", 5000 );
		jQuery('.sg-hometopgal').find('a').live("click", function(event) {
			event.preventDefault();
			clearInterval(teaserHop);
			var aTI = jQuery(this).attr('rel').split('_');
			var teaserIndex = aTI[1];
			setTeaser(teaserIndex);
		});
	}

});

var teaserId = 0;
function teaserChanger() {
	teaserId = (teaserId < jQuery('.sg-hometopgal').find('a').length) ? teaserId+1 : 0;
	setTeaser(teaserId);
}

function setTeaser(elem){
	var textWidth = jQuery('.sg-hometoptext').width();
	var aTI = jQuery('.sg-hometopgal').find('a:eq('+((elem-1 >= 0)?elem-1:0)+')').attr('rel').split('_');
	var teaserIndex = elem;
	jQuery('.sg-hometoptext').fadeOut(10, function() { // {right:(textWidth+30), opacity:0}
		jQuery(this).find('.date').text(oTeaser[teaserIndex].release_date);
		jQuery(this).find('h1, h2').detach();
		jQuery(this).find('.sg-topnavi').after(oTeaser[teaserIndex].category + oTeaser[teaserIndex].headline);
		jQuery(this).find('p').text(oTeaser[teaserIndex].text);
		jQuery(this).find('.sg-btn-194x28').attr('href', oTeaser[teaserIndex].link);
	});
	jQuery('.sg-hometoppic').fadeOut(10, function() {
		jQuery(this).find('img').detach();
		jQuery(this).find('a').attr({
			href: oTeaser[teaserIndex].link,
			title: oTeaser[teaserIndex].image_teaser
		}).prepend(oTeaser[teaserIndex].image);
		jQuery(this).find('em').text(oTeaser[teaserIndex].image_teaser);
		jQuery(this).fadeIn(10);
		jQuery('.sg-hometoptext').fadeIn(10, function() { // {right:0, opacity:1}
			if (jQuery.browser.msie) {this.style.removeAttribute('filter');}
		});
	});
	jQuery('.sg-hometopgal').fadeOut(10, function() {
		jQuery(this).find('a').detach();
		for (teaser in oTeaser) {
			if (teaser != teaserIndex) {
				jQuery('<a/>').attr({
					rel: aTI[0]+'_'+teaser,
					title: oTeaser[teaser].image_teaser,
					href: oTeaser[teaser].link
				}).append(oTeaser[teaser].thumb).appendTo(this);
			}
		}
		jQuery(this).fadeIn(10);
	});

	return false;
}
