 $(document).ready( function() {
	 		if($('.player-visual').length){
	 			preloadPlayerVisual();
	 		}
			initSponsorsFade();		 
			
			/*
			if($('#spieleTeaser').length){
				jQuery('#mycarousel1').jcarousel();
			    jQuery('#mycarousel2').jcarousel();
				
				var slg = $('#mycarousel3 li').length;
			    jQuery('#mycarousel3').jcarousel({
			    	start: slg,
			    	//size: slg,
			    	itemFallbackDimension: 260
			    });
			    	
				$('.jcarousel-prev, .jcarousel-next').hover(
					function(){
						if(!$(this).hasClass('jcarousel-prev-disabled') && !$(this).hasClass('jcarousel-next-disabled')){				
							$(this).addClass('jcarousel-hover');
						}
			      	},
			      	function(){
						$(this).removeClass('jcarousel-hover');
			        }
				);	
				$("#spieleTeaser ul").idTabs("tabs11");
				$("#spieleTeaser ul").idTabs("tabs22");
				$("#spieleTeaser ul").idTabs("tabs33");			
			}
			*/
			
			$('#newsslider').hoverAccordion({
                keepHeight:true,
                speed: 400 
            });		    		   
		    
			$('#dateslider').hoverAccordion({
                keepHeight:true,
                speed: 400
            });
			
            $('#dateslider, #newsslider').children('li:first').addClass('firstitem');
            $('#dateslider, #newsslider').children('li:last').addClass('lastitem');
			$('#newsslider').children('li:first').addClass('firstitem');
            $('#newsslider').children('li:last').addClass('lastitem');
                        
            /* erstes element offen! activateItem funzt nicht */
			$('#newsslider li.firstitem span.news-title a.header').removeClass('closed').addClass('opened');
		    $('#newsslider li.firstitem ul li').show();					
		    
		    $('#dateslider li.firstitem a.header').removeClass('closed').addClass('opened');
		    $('#dateslider li.firstitem ul li').show();			
			            
			$('#termineContainer, #ticketsContainer, #shopContainer, #fade, #fade1').hide();
//			$('#ticketsContainer, #fade, #fade1').hide();
			
			// termine tab hover  			
			$('.termine, .datePickerWrap, #fade').hover(function(){
				$('.navigation').addClass('up');		
				$('#termineContainer, #fade').show();
				
				//$('#fade').show();				
				//$('#termineContainer').fadeIn('fast');
				
				$('.termine a').addClass('active');
				$('.lefttabs ul li.termine a.active').css({zIndex: '101', position:'relative'});
				$('.lefttabs ul li.tickets a').css({zIndex: '0', position:'relative'});
				$('.lefttabs ul li.shop a').css({zIndex: '0', position:'relative'});
			},function(){
				$('#termineContainer, #fade').hide();
				
				$('.termine a').removeClass('active');
				$('.navigation').removeClass('up');	
				$('.lefttabs ul li.tickets a').css({zIndex: '0', position:'relative'});			  			  
			});  			  			  			  			
  			
			
			
			// tickets tab hover
			$('.tickets, .datePickerWrap1, #fade').hover(function(){
				$('.navigation').addClass('up');					
				$('#ticketsContainer, #fade').show();
				
				//$('#fade').show();
				//$('#ticketsContainer').fadeIn('fast');
				
				$('.tickets a').addClass('active');
				$('.lefttabs ul li.tickets a.active').css({zIndex: '101', position:'relative'});
				$('.lefttabs ul li.termine a').css({zIndex: '0', position:'relative'});
				$('.lefttabs ul li.shop a').css({zIndex: '0', position:'relative'});
		  	},function(){
			  	$('#ticketsContainer, #fade').hide();

			  	$('.tickets a').removeClass('active');
			  	$('.navigation').removeClass('up');
			  	$('.lefttabs ul li.termine a').css({zIndex: '0', position:'relative'});
			});
			
			/*
			// shop tab hover
			$('.shop, .datePickerWrap2, #fade').hover(function(){
				$('.navigation').addClass('up');	
				$('#shopContainer, #fade').show();
				
				//$('#fade').show();
				//$('#shopContainer').fadeIn('fast');
				
				$('.shop a').addClass('active');
				$('.lefttabs ul li.shop a.active').css({zIndex: '2', position:'relative'});
				$('.lefttabs ul li.termine a').css({zIndex: '0', position:'relative'});
				$('.lefttabs ul li.tickets a').css({zIndex: '0', position:'relative'});
		  	},function(){
			  $('#shopContainer, #fade').hide();
			  			  
			  $('.shop a').removeClass('active');
			  $('.navigation').removeClass('up');
			   $('.lefttabs ul li.shop a').css({zIndex: '0', position:'relative'});
			});
			*/
			
			// overlay hover
			$('#fade').hover(function(){
				$('#termineContainer, #ticketsContainer, #shopContainer, #fade').hide();
				$('.termine a, .tickets a, .shop a').removeClass('active');
				$('.navigation').removeClass('up');
				$('.lefttabs ul li.termine a.active').css({zIndex: '0', position:'relative'});
				$('.lefttabs ul li.tickets a.active').css({zIndex: '0', position:'relative'});
			},function(){
			}); 
		
		var menuDown = 0;
			
		//menu hover
		function megaHoverOver(){
			var menu = $(this).find(".sub, .sub1");
			if (menu.length>0) {
				menuDown ++;
				$('#fade1').stop().show();
				menu.stop().show();
			}
			
		
/* Simon: useless code!?		
		
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
		//	$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
		//	$(this).find(".sub").css({'width' : rowWidth});
		}
		
	*/
	}
	function megaHoverOut(){ 
		var menu = $(this).find(".sub, .sub1");
		if (menu.length>0) {
			menu.hide();
			menuDown --;
			if (menuDown == 0) $('#fade1').stop().hide();
		}
	}


	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	if($('ul#nav').length){
		$("ul#nav li .sub").hide();
		$("ul#nav li").hoverIntent(config);
	}

		var el = $('#wrapper').height();
	//	var elw = $(window).width();
		$(".overlay, .overlaym").css("height", el+"px");
	//	$(".overlay, .overlaym").css("width", elw+"px");

	
	//	news block hover
	 $('#tmpSlideshowControl-1, #tmpSlideshowControl-2, #tmpSlideshowControl-3, .tmpSlideCopy').hover(function(){
		$('.tmpSlideCopy').css({bottom: '60px'});
		  },function(){
		  $('.tmpSlideCopy').css({bottom: '0'});
    	});
	 
	  $('#tmpSlideshowControl-1').hover(function(){
		$(this).addClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-2').removeClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-3').removeClass('tmpSlideshowControlActive');
		  },function(){
		$(this).removeClass('tmpSlideshowControlActive');
    	});
	  
	  $('#tmpSlideshowControl-2').hover(function(){
		$(this).addClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-1').removeClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-3').removeClass('tmpSlideshowControlActive');
		  },function(){
		$(this).removeClass('tmpSlideshowControlActive');
    	});
	  
	   $('#tmpSlideshowControl-3').hover(function(){
		$(this).addClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-1').removeClass('tmpSlideshowControlActive');
		$('#tmpSlideshowControl-2').removeClass('tmpSlideshowControlActive');
		  },function(){
		$(this).removeClass('tmpSlideshowControlActive');
    	});
	   
	   $('#aktuelles div.sub').css({marginLeft: '-25px'});
	   $('#saison div.sub').css({marginLeft: '-143px'});
	   $('#tickets div.sub').css({marginLeft: '-235px'});
	   $('#team div.sub').css({marginLeft: '-338px'});
	   $('#jugend div.sub').css({marginLeft: '-411px'});
	   $('#fans div.sub').css({marginLeft: '-489px'});
	   $('#verein div.sub').css({marginLeft: '-610px'});
	   $('#sponsoren div.sub').css({marginLeft: '-687px'});
	   $('#presse div.sub').css({marginLeft: '-776px'});
	   
	 /*
	 //	menu hover
	 $('#aktuelles').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();
		$('#aktuelles div.sub').css({marginLeft: '-25px'});
      },function(){$('#aktuelles div.sub').css({marginLeft: '-25px'}); 
	  	$('#fade1').hide();
	  });
	 
	   $('#saison').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();
	  	$('#saison div.sub').css({marginLeft: '-145px'});
      },function(){$('#saison div.sub').css({marginLeft: '-145px'});
      	$('#fade1').hide();
	  });
	  
	 $('#tickets').hover(function(){
		 $('#fade1').css({zIndex:'3'}).show();
	 	$('#tickets div.sub').css({marginLeft: '-239px'});
      },function(){$('#tickets div.sub').css({marginLeft: '-239px'});
        $('#fade1').hide();
	  });
	  
	  $('#team').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();
		$('#team div.sub').css({marginLeft: '-338px'});
      },function(){$('#team div.sub').css({marginLeft: '-338px'});
        $('#fade1').hide();
	  });
	  
	   $('#jugend').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();		   
		$('#jugend div.sub').css({marginLeft: '-419px'});
      },function(){$('#jugend div.sub').css({marginLeft: '-419px'});
        $('#fade1').hide();
	  });
	   
	   $('#fans').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();
		$('#fans div.sub').css({marginLeft: '-499px'});
      },function(){
    	$('#fans div.sub').css({marginLeft: '-499px'});
        $('#fade1').hide();
	 });
	 
	   $('#verein').hover(function(){
	    $('#fade1').css({zIndex:'3'}).show();
		$('#verein div.sub').css({marginLeft: '-595px'});
      },function(){$('#verein div.sub').css({marginLeft: '-595px'});
        $('#fade1').hide();
	   });
		
		$('#sponsoren').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();
		$('#sponsoren div.sub').css({marginLeft: '-674px'});
      },function(){$('#sponsoren div.sub').css({marginLeft: '-674px'});
        $('#fade1').hide();
	  });
		
		$('#presse').hover(function(){
		$('#fade1').css({zIndex:'3'}).show();			
		$('#presse div.sub').css({marginLeft: '-765px'});
      },function(){$('#presse div.sub').css({marginLeft: '-765px'});
        $('#fade1').hide();
	});		
	*/
});

// calendar events
var events;
var now = new Date();                	   
var month = now.getMonth()+1;
var day = now.getDate();
var year = now.getFullYear();
var id = now.getFullYear()+':'+month;
var month_label = {1:'Januar',2:'Februar',3:'März',4:'April',5:'Mai',6:'Juni',7:'Juli',8:'August',9:'September',10:'Oktober',11:'November',12:'Dezember'};
YAHOO.namespace("example.calendar");


YAHOO.example.calendar.init = function() {
    YAHOO.example.calendar.cal1 = new YAHOO.widget.Calendar("cal1","cal1Container");
    YAHOO.example.calendar.cal1.cfg.setProperty("START_WEEKDAY",  1);
    
    setEventsAndRenderCalendar(month,id);    	     	   
   
    YAHOO.example.calendar.cal1.selectEvent.subscribe(handleSelect, YAHOO.example.calendar.cal1, true);
    YAHOO.example.calendar.cal1.changePageEvent.subscribe(setEventsAndRenderCells, YAHOO.example.calendar.cal1, true);
}

function setEventsAndRenderCalendar(_month,_id){
	$.ajax({
		//async: false,
	    url: '/?type=1338&tx_k5hsvhajax_pi1[cmd]=getevents&tx_k5hsvhajax_pi1[data]='+_id,    		    
	    success: function(result){
			events = result;    
			if(events){
				renderCells(_month,_id);
				setCurrentCell(day,month,year,id);
			}
			YAHOO.example.calendar.cal1.render();
	    }
	});
}

function setEventsAndRenderCells(type,args,obj){
	var _month= args[1].getMonth()+1;
	var _id = args[1].getFullYear()+':'+_month; 		
	     
	$.ajax({
	    url: '/?type=1338&tx_k5hsvhajax_pi1[cmd]=getevents&tx_k5hsvhajax_pi1[data]='+_id,    		    
	    success: function(result){
			events = result;
			if(events){
				renderCells(_month,_id);    				    				
			}
			YAHOO.example.calendar.cal1.render();    				    			
	    }
	});
}  
 
function renderCells(_month,_id){
    for (var _day in events[_id]){	
    	YAHOO.example.calendar.cal1.addRenderer(_month+'/'+_day, YAHOO.example.calendar.cal1.renderCellStyleHighlight1);    	    	    	    
    }			
}

function handleSelect(type,args,obj){
	var _dates = args[0];    		
	var _date = _dates[0];
	
	var _year = _date[0], _month = _date[1], _day = _date[2];
	var _id = _year+':'+_month;

	setCurrentCell(_day,_month,_year,_id);    		    	
} 

function setCurrentCell(_day,_month,_year,_id){
	
	html = events[_id][_day] ? events[_id][_day].html : '';
	$('.currentDayContent').html(html);
	
	label = _day+'. '+month_label[_month]+' '+_year;
	$('.curDate').html(label);
	$('#dateslider').hoverAccordion({
        keepHeight:true,
        activateItem: 1,
        speed: 400
    });
	$('#dateslider').children('li:first').addClass('firstitem');
    $('#dateslider').children('li:last').addClass('lastitem');
    
    /* erstes element offen! activateItem funzt nicht */
    $('#dateslider li.firstitem a.header').removeClass('closed').addClass('opened');
    $('#dateslider li.firstitem ul li').show();
}

YAHOO.util.Event.onDOMReady(function(){         
	YAHOO.example.calendar.init();	
});

function initSponsorsFade(){
	if($('.sponsors').length){
		var h = $('.sponsors').height;
		$('.sponsors').innerfade({
			containerheight: h,
			timeout: 5000
		});
	}
}

function preloadPlayerVisual(){	
	// bild muss png sein! css dazu muss vorhanden sein (name des bildes)
	//var bg_images = ['b_gille','bitter2','bitter','g_gille2','g_gille3','g_gille','hans_lindberg','hans_lindberg2','hens'];
	//var bg_images = ['g_gille3'];
	//var bg_images = ['hans_lindberg'];
	//var bg_images = ['pascal-hens_2012','bitter_2012'];	
	var bg_images = ['b_gille','bitter','bitter_2','g_gille_1','g_gille_2','g_gille_3','lindberg','pascal-hens_2012'];
	
	rnd = (parseInt(Math.random() * bg_images.length));	
	$('.player-visual').addClass('player-visual-'+bg_images[rnd]);
	
  	var bgimg = $('.player-visual').css('background-image');
  	rawurl = bgimg.replace(/"/g,"").replace(/url\(|\)$/ig, "");
  	
	var bg = new Image();  	
	bg.src = rawurl;

    if (imageIsOk(bg)) {    	
    	//setTimeout(function(){$('#mg .png').fadeIn('slow');}, 1000);
    	$('.player-visual').fadeIn(900);
    	//setTimeout(function(){$('#mg').removeClass('loading').addClass('loaded');}, 900);    	
    } else {
    	$(bg).load(function(){
    		$('.player-visual').fadeIn(900);    		
    		//setTimeout(function(){$('#mg').removeClass('loading').addClass('loaded');}, 900);
      	});
    }		  	
}
function imageIsOk(img) {
    // During the onload event, IE correctly identifies any images
    // that weren't downloaded as not complete. Others should too.
    // Gecko-based browsers act like NS4 in that they report this
    // incorrectly: they always return true.
    if (!img.complete) {
        return false;
    }

    // However, they do have two very useful properties: naturalWidth
    // and naturalHeight. These give the true size of the image. If
    // it failed to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
        return false;
    }

    // No other way of checking: assume it's ok.
    return true;
}
