// JavaScript Document

var dcount=0;	//DebugOutput-Zaehler

var rec=0;		//cycle: Zähler für Recursionen
cswitch=1;		//cycle: aktiviert

function mydebug(debugstring){
	
	if(!document.all && !(typeof(console) == 'undefined'))
	{
		console.log(dcount++ +' | ' + debugstring);
		}
	return debugstring;
	}

mydebug('ich lebe');


/* FormHandling */

function doformelem(elem){
	/*
	Leert Input-Feld, wenn User Eingaben machen will (onfocus, onblur)
	elem: this
	*/
	
	if(elem.defaultValue==elem.value)
		{
		elem.value="";
		//alert(elem.style.getAttribute("color",false));
		elem.className="formelem_active";
		return(0);
		}
		
	if(elem.value=="")
	{
		elem.value=elem.defaultValue;
		elem.className="formelem";
		return(0);
	}
	
	
}


function cycle(elem,fspeed,cwait){
	
	/*
	element:	selector
	fspeed:		fading speed
	cwait:		cycle wait: time between fadings; 0 => no recursion
	*/
		
	//mydebug('cycle()['+ rec++ +'] '+fspeed+' | '+cwait);
	
	$(elem+':first')
		.animate({
			opacity: 0,
			height: ['toggle', 'swing'],
			'margin-bottom': 'toggle'
			},fspeed,function(){
				
				//move li
				saveme=$(this);									//li zwischenspeichern
				$(this).remove();
				$('#sbb_events').append(saveme);				//ToDo: Abhaengigkeit loesen!!!
				
				$(this).css({
					opacity: 1,
					display: 'block',
					'margin-bottom': '0.5em'
					})
				
				if(cwait!=0 && cswitch!=0)
					ctimer=window.setTimeout(function(){cycle(elem,fspeed,cwait);}, cwait);	//rekursiv aufrufen
			})
	}

$(document).ready(function() {
   
	/* hintere Weather-Sheets ausblenden */
	$('.sbb_weather_sheet').css({
			opacity: 0,
			'filter': 'alpha(opacity=0)'
			});		//alle auf null setzen

	$('.sbb_weather_sheet:first').css({				//heute
			opacity: 1,
			'filter': 'alpha(opacity=100)'
			})										//sichtbar und ganz vorn
		.addClass('active');						//aktiver
   
   
   
	/* WF-Online oben*/
   
   $('#wfonline, #catchme')
   .bind("mouseenter", function(){
		//reingegangen
		mydebug('wfonline');
		
		//$('body').css('height',"50px")
		
		$('body').stop().animate(
			{"margin-top":20}, 
			{duration:400})
		

		})
   .bind("mouseleave", function(){
		//rausgegangen
		
		//$('#catchme').css('height',"35px")
		
		$('body').stop().animate(
			{"margin-top":-15}, 
			{duration:400})
					  			})
   
	/* SIDEBARs */
   
	/*** Wetter ***/
	
	$('#sbb_weather_selector a')
		.bind("mouseenter", function(){
			
			//mydebug('mouseenter');
			
			if($(this).hasClass('active'))		//schon aktiv: nichts tun
			{
				mydebug('das bin ich');
				return 0;
			}
			else
			{
				mydebug('------');
				//Link-Klassen tauschen
				$('#sbb_weather_selector a').removeClass('active');
				$(this).addClass('active');
				
				//welcher Link gewählt?
				num=String(this.id).charAt(String(this.id).length-1);mydebug('Auswahl: '+num);
				
				//Layer identifizieren
				neu = $('.sbb_weather_sheet').eq(num);mydebug('Neu: '+neu.attr('id'));
				alt = $('.sbb_weather_sheet.active');mydebug('Alt: '+alt.attr('id'));
				
				//Layer hinter altem positionieren
				neu.css({
					'z-index':'99'
					});	
							
				//alten Layer ausblenden
				alt.animate({
					opacity: 0
					}, 1200, function() {
						mydebug(alt.attr('id')+" ausgeblendet");
					})
				//alten Layer ausblenden
				neu.animate({
					opacity: 1
					}, 800, function() {
						mydebug(neu.attr('id')+" eingeblendet");
					})
				
				//Klassen tauschen
				alt.removeClass('active').css({
					'z-index':50
					 });
				neu.addClass('active').css('z-index',100);		
				
			}//schon active?
		})
	
	
	/*** Events ***/
	
	$('#sbb_events_next, #sbb_events')
	.bind('mouseenter',function(){
		
		cswitch=0;				//naechste rekursion verhindern
		clearTimeout(ctimer);	//aktueller timer
		
		mydebug('manuell: enter | cswitch='+cswitch);
		
		})
	.bind('mouseleave',function(){
		mydebug('manuell: leave');
		ctimer=window.setTimeout(function(){cycle('#sbb_events li',3000,7000)},300);	//sofort normal weiter
		cswitch=1;				//naechste rekursion erlauben

		})
	$('#sbb_events_next')
	.bind('click',function(){
		mydebug('manuell: click next');
		cycle('#sbb_events li',500,0)		//0-> no recursion		
		})
	
	//dothemagic
	ctimer=window.setTimeout(function(){cycle('#sbb_events li',3000,7000);}, 1000);		//fspeed, cwait, 1st delay
   
   
   /* PNGfix für IE5.5+ */
	$(document).pngFix();
   
   
 });	//document.ready()
