var $j = jQuery.noConflict();

$j(document).ready(function(){
	// Selectfield Start ----------------------------------------
		$j("ul.select > li.parent").click(function(){
			$j(this).children("ul").toggle();
		});	
		$j("ul.value > li:last-child").css("border", "none"); 
	// Selectfield End ----------------------------------------
	
	$j('#topic > .sbcnt').height( $j('#topic > .sbcnt').children().height() );
	
	$j('#meta > li:last').add('#navigation > li:last').css({border:'0px','margin-right':'0px','padding-right':'0px'})
	
	if ($j('.startpage').size() > 0) {
		$j('#foot').addClass('startpage');
	}
	
	if ($j('.cntteaser').size() > 0) {
		
		$j('.cntteaser > div:first:not(".topnews") > p').css({'padding-left':35});
		
		$j('.cntteaser').each(function(){
			var teasrows = $j(this).children('div').size();
			
			if (teasrows > 1) {
				var teaserow_width = (960 / teasrows) - 70;
				$j(this).children('div').each(function(){
					$j(this).css({
						width: teaserow_width
					});
				});
			}
		});
		$j('.cntteaser > div:last').removeClass('wline');
	}

	$j("a.lightbox").fancybox({
		'titlePosition' : 'over',
		'transitionIn'	: 'fade',
		'transitionOut'	: 'fade',
		'speedIn'		: 600, 
		'speedOut'		: 200, 
		'overlayShow'	: true
	});
	
	$j('#navigation > li').bind('mouseenter mouseleave',function(event){
		
		if ($j(this).children('div.subbox').size() > 0) {
			$j(this).children('div.subbox').toggle()
			
			if (event.type == 'mouseenter') {
				$j(this).addClass('open');
				openWidth = $j('#navigation > li.open').outerWidth();
				$j('.openover', '#navigation > li.open').width(openWidth + 11);
			}
			else 
				$j(this).removeClass('open');
		}
	});
	
	$j('#controlpanel > .cpcnt > .empty').parent().parent().remove();
	
	if ($j('#topic > .sbcnt > div > .csc-textpic-clear').size() > 1) {
		$j('#topic > .sbcnt > div > .csc-textpic-clear:first').remove();			
				
		if ($j('#topic > .sbcnt > div > .csc-textpic:first > .csc-textpic-imagewrap').width() >= 644) {		
			// cach elements
			slideshowWrap = $j('#topic > .sbcnt > div > .csc-textpic:first > .csc-textpic-imagewrap');
			slideshowList = $j('#topic > .sbcnt > div > .csc-textpic:first > .csc-textpic-imagewrap > ul');
			anzElements	= slideshowList.children('li').size();
			
			// init wrap width and height
			slideshowWrap.width(644).css({
				'overflow': 'hidden',
				'height': '238px'
			});
			$j('#topic > .sbcnt').height(238);
			slideshowList.children('li').css({
				position: 'absolute',
				top: 0,
				display: 'none'
			});
			slideshowList.children('li:first').css({display: 'block'});
			
			// build and init controlpanel
			$j('.cpcnt').prepend('<a href="#next" class="cparrowright"></a><a href="#last" class="cparrowleft"></a>');
			for(i=0; i < anzElements; i++){
				$j('.cpcnt').prepend('<span><img src="fileadmin/templates/img/icons/pixel.gif" /></span>');
			}
			$j('div.cpcnt > span:first').addClass('act');
			$j('.cparrowleft').css({opacity: 0.2}, 500).addClass('blind');
			
			function imgChange(nextIndex,currIndex,option){		
				slideshowList.children("li:eq(" + nextIndex + ")").css({
					'z-index': 5
				}).show();
				slideshowList.children("li:eq(" + currIndex + ")").css({
					'z-index': 10
				}).fadeOut('slow');
				
				$j('.cpcnt > span:eq(' + currIndex + ')').removeClass('act');
				$j('.cpcnt > span:eq(' + nextIndex + ')').addClass('act');
				
				if(option == "auto" && slideshowList.hasClass('auto')){
					currIndex = nextIndex;
					nextIndex = currIndex + 1;
					
					if (nextIndex == $j('.cpcnt > span').size()) {
						// allright this is the end of list - just move to the first
						currIndex = nextIndex - 1;
						nextIndex = 0;
						
						// ok now the visitor is able to control the pictures
						$j('.cparrowright').animate({opacity: 1}, 500).removeClass('blind');
						$j('.cparrowleft').animate({opacity: 1}, 500).removeClass('blind');
					}
					
					setTimeout(function(){
						imgChange(nextIndex, currIndex, "auto");
					}, 2000);		
				}		
			}
						
			$j('.cparrowright').add('.cparrowleft').bind("click", function(){
				// stop the auto
				slideshowList.removeClass('auto');
				
				// swap the image
				if( $j(this).hasClass('cparrowright') ){
					
					// next image
					currIndex = $j('.cpcnt > span.act').index();
					if( $j('.cparrowleft').hasClass('blind') ) 
						$j('.cparrowleft').animate({opacity: 1}, 500).removeClass('blind');
											
					if (currIndex + 1 != anzElements) {	
						nextIndex = currIndex + 1;

						imgChange(nextIndex,currIndex,"clicked");
						
						if (nextIndex + 1 == anzElements) 
							$j('.cparrowright').animate({opacity: 0.2}, 500).addClass('blind');
					}
				}else {
					
					// prev image
					currIndex = $j('.cpcnt > span.act').index();
					if( $j('.cparrowright').hasClass('blind') )  
						$j('.cparrowright').animate({opacity: 1}, 500).removeClass('blind');
					
					if (currIndex != 0) {	
						nextIndex = currIndex - 1;

						imgChange(nextIndex,currIndex,"clicked");
						
						if (nextIndex == 0) 
							$j('.cparrowleft').animate({opacity: 0.2}, 500).addClass('blind');
					}					
				}
			});
			
			// run on load
			$j('.cparrowleft').animate({opacity: 0.2}, 500).addClass('blind');
			$j('.cparrowright').animate({opacity: 0.2}, 500).addClass('blind');
			slideshowList.addClass('auto');
			setTimeout(function(){
				imgChange(1,0,"auto");
			}, 2000);
		}
	}	


		$j(function () {
	        $j('.bubbleInfo').each(function () {
	            var distance = 15;
	            var time = 150;
	            var hideDelay = 150;
	
	            var hideDelayTimer = null;
	
	            var beingShown = false;
	            var shown = false;
	            var trigger = $j('.trigger', this);
	            var info = $j('.popup', this).css('opacity', 0);
				var bubbleWidth = 200;
				var triggerWidth = trigger.width();
				var bubblepos = (bubbleWidth / 2) - (triggerWidth / 2); 
	
	            $j([trigger.get(0), info.get(0)]).mouseenter(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                if (beingShown || shown) {
	                    // don't trigger the animation again
	                    return;
	                } else {
	                    // reset position of info box
	                    beingShown = true;
	
	                    info.css({
	                        top: -170,
	                        left: -bubblepos,
	                        display: 'block'
	                    }).animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 1
	                    }, time, 'swing', function() {
	                        beingShown = false;
	                        shown = true;
	                    });
	                }
	
	                return false;
	            }).mouseleave(function () {
	                if (hideDelayTimer) clearTimeout(hideDelayTimer);
	                hideDelayTimer = setTimeout(function () {
	                    hideDelayTimer = null;
	                    info.animate({
	                        top: '-=' + distance + 'px',
	                        opacity: 0
	                    }, time, 'swing', function () {
	                        shown = false;
	                        info.css('display', 'none');
	                    });
	
	                }, hideDelay);
	
	                return false;
	            });
	        });
	    });

	
		//DD_belatedPNG.fix('#logo, #claim, .png');
		
});
