function debug(text) {
	$('#debug').html($('#debug').html()+'<br/>'+text);
}

var png_selector = 'a.button';

// Bilderwechsler
var timer_started = 0;
var timer_delay = 10000;
var active_element_id = 0;
var force_element_id = -1;

function pictureChanger() {
	var new_date = new Date();
	timer_started = new_date.getTime();
	var $element = $('div.stageContainer');
	var $active_element = $element.find('div.stage:eq('+active_element_id+')').addClass('activePicture');
	var next_element_id = active_element_id+1;
	if(active_element_id==2) {
		next_element_id = 0;
	}
	
	var $new_element = $element.find('div.stage:eq('+next_element_id+')').addClass('newPicture').removeClass('hidden');
	active_element_id = next_element_id;
	if($.browser.msie) {
		$('div#stage_vertrauen').removeAttr('id').attr('id','stage_vertrauen_mod');
	}
	$active_element.fadeOut(2500,function(){
		$active_element.removeClass('activePicture').addClass('hidden').removeAttr('style');
		$new_element.removeClass('newPicture').removeAttr('style').removeClass('hidden');
		DD_belatedPNG.fix(png_selector);
	});
}

function pictureChangerForce() {
	if(active_element_id != force_element_id) {
		var new_date = new Date();
		timer_started = new_date.getTime();
		var $element = $('div.stageContainer');
		var $active_element = $element.find('div.stage:eq('+active_element_id+')').addClass('activePicture');
		var next_element_id = force_element_id;
		var $new_element = $element.find('div.stage:eq('+next_element_id+')');
		if($.browser.msie) {
			$('div#stage_vertrauen').removeAttr('id').attr('id','stage_vertrauen_mod');
		}
		
		$new_element.removeAttr('style').removeClass('activePicture').removeClass('hidden').removeClass('newPicture');
		$active_element.removeAttr('style').removeClass('newPicture').removeClass('activePicture').addClass('hidden');
		active_element_id = force_element_id;
		DD_belatedPNG.fix(png_selector);
	}
}

$(document).ready(function(){
	$('li:nth-child(1)').addClass('first');
	$('div#contentContainer > .csc-header > h1, div#contentContainer > h1').addClass('grid_12');
	$('div.stage:not(:first)').addClass('hidden');
	
	$("div#contentContainer div.grid_4 a:contains('mehr')").prepend('/&thinsp;').addClass('more').parent().addClass('endWrapper').each(function(i){
		if($(this).prevUntil('h4,h5,h6').length) {
			$(this).prevUntil('h4,h5,h6').prev().addClass('startWrapper');
		} else {
			$(this).prev('h4,h5,h6').addClass('startWrapper');
		}
	});
	
	$('div#contentContainer div.grid_4 > .startWrapper').each(function(){
		$(this).nextUntil('.endWrapper').andSelf().add($(this).nextAll('.endWrapper:first')).wrapAll('<div class="contentWrapper"></div>');
	});
	
	$('div.contentWrapper').each(function(){
		if($(this).find('a.more').length) {
			$(this).addClass('contentTeaser');
		}
	});
	if($('div.contentWrapper').length) {
		$("div#contentContainer div.grid_4 > *").not('.contentWrapper').wrap('<div class="contentWrapper"></div>');
	} else {
		$('div#contentContainer div.grid_4 > *').wrap('<div class="contentWrapper"></div>');
	}
	
	//$('a.more').parentsUntil('div.contentWrapper').parent('div.contentWrapper').addClass('contentTeaser');
	/* Ganz Bloecke verlinken */
	$('div.contentTeaser').live('mouseover',function(){
		$(this).addClass('active');
	});
	$('div.contentTeaser').live('mouseout',function(){
		$(this).removeClass('active');
	});
	$('div.contentTeaser').live('click',function(){
		$(location).attr('href',$(this).find('a.more').attr('href'));
	});
	/*$('div#contentContainer div.grid_4 div').hover(function(){
		$(this).addClass('active');
	},function(){
		$(this).removeClass('active');
	});
	$('div#contentContainer div.grid_4 div').click(function(){
		$(location).attr('href',$(this).find('a.more').attr('href'));
	});*/
	$('input.csc-mailform-submit').addClass('formButtonSpan').wrap('<div class="formButton"></div>');
	
	if($.browser.msie && ($.browser.version.substr(0,1)==6)) {
		var tmp_width = 0;
		$('div#navigationContainer > ul > li').each(function(){
			tmp_width += $(this).width();
		});
		tmp_width += 34;
		$('div#navigationContainer ul li ul').css('left',-tmp_width+'px');
		
		DD_belatedPNG.fix(png_selector);
	}
	if(($('div.home').length || $('div.start').length)) {
		/* Bilderwechsler */
		$('div.stageContainer').everyTime(timer_delay,'pictureChange',pictureChanger);
		var new_date = new Date();
		timer_started = new_date.getTime();
		$('div.stageContainer').hover(
			function () {
				$(this).stopTime('pictureChange');
			}, 
			function () {
				$(this).stopTime('pictureChange');
				var new_date = new Date();
				var new_timer_started = new_date.getTime();
				new_timer_started = timer_started + timer_delay - new_timer_started ;
				if(new_timer_started<=0) {
					new_timer_started = 10;
				}
				$(this).oneTime(new_timer_started,'pictureChange',function(){
					pictureChanger();
					$(this).everyTime(timer_delay,'pictureChange',pictureChanger);
				});
			}
		);
		$('a.logo_vertrauen,a.logo_effizienz,a.logo_entwicklung').hover(function(event){
			event.stopPropagation()
			$('div.stage').stop();
			var $timer_element = $('div.stageContainer');
			force_element_id = $('div#headContainer a').index($(this))-1;
			$timer_element.stopTime('pictureChange').oneTime(50,'pictureChangeForce',pictureChangerForce);
		},function(){
			event.stopPropagation()
			$('div.stageContainer').everyTime(timer_delay,'pictureChange',pictureChanger);
		});
	}
	
	$('a[href*=pdf]').addClass('pdf');

});
