var stopScroll=false;
var current = 1;
var last = 0;

function scrollEvent(container){
	var events = $(container).children('dl');
 	$(events[current]).addClass('selected');
 	$(events[last]).removeClass('selected');
 	$("#event").scrollTo($(events[current]),800);
    if ((current + 1) < events.length) {
    	current = current + 1;
    	last = current - 1;
    } else {
    	current = 0;
    	last = events.length - 1;       
    }
}

(function($) { 
		// for when you're calling a flash method

		$.fn.getFlashObj = function(options){   // does NOT return a jQuery object. cannot take multiple elements., just one!
		  if (!this[0].id){ this[0].id='flashobj'+(+new Date); }
		  return (jQuery.browser.msie) ? window[this[0].id] : document[this[0].id];
		  // return window[this[0].id] || document[this[0].id]; // would that work?
		};
		$.fn.eventfade = function(options) {
			return this.each(function() {
				$.eventfade(this, options);
			});
		}; 
		$.eventfade = function(container, options){
			var settings = {
				'speed': 'normal',
				'timeout': 2000,
				'containerheight': '280px',
				'runningclass': 'eventfade',
				'children': 'dl',
				'containertarget': container,
				'container': container
			};
			if (options)
				$.extend(settings, options); 
			var event_list = $(container).children(settings.children);
			var event_flyer = $(settings.containertarget);
			$("#event").scrollTop(0);
			var top_flash = $('#flash_zone').children('li');
			if (top_flash.length > 1) {
				$(top_flash).css('position', 'relative').css('height', '145px').addClass(settings.runningclass);
				for (var i = 0; i < top_flash.length; i++) {
					$(top_flash[i]).css('z-index', String(top_flash.length - i)).css('position', 'absolute').hide();
				}; 
				$(top_flash[0]).addClass('selected');
				$(top_flash[0]).show();
			}
			if (event_list.length > 1) {					
				 setTimeout(function(){
					 $.eventfade.next(event_flyer, settings, top_flash, 1, 0, 1);
				 }, settings.timeout);
				$(event_list[0]).addClass('selected');
			}
		};
		
			
	    $.eventfade.next = function(flyers, settings, flashs, fcurrent, flast, rotate) {  
            if(rotate%2 == 0){
				$(flashs[flast]).fadeOut(settings.speed);
            	$(flashs[fcurrent]).fadeIn(settings.speed, function() {
						removeFilter($(this)[0]);
					});
				if ((fcurrent + 1) < flashs.length) {
	                flast = fcurrent;
					fcurrent = fcurrent + 1;
					rotate = rotate +1;
	            } else {
	                fcurrent = 0;
	                flast = flashs.length - 1;
	                rotate = 1;
	            }
			}else{
				rotate = rotate +1
			}
            //window.console && console.log && console.log("events: %o - current: %o - last: %o - mod: %o", events.length, current, last );
            //window.console && console.log && console.log("Scrolling event - %o",events[current]);
            if(!stopScroll){
            	scrollEvent(settings.container);
            }
	        setTimeout((function() {
	            $.eventfade.next(flyers, settings, flashs, fcurrent, flast, rotate);
	        }), settings.timeout);

		};
	
	})(jQuery);

$(document).ready(function(){	
	// **** remove Opacity-Filter in ie ****
	function removeFilter(element) {
		if(element.style.removeAttribute){
			element.style.removeAttribute('filter');
		}
	}
	$("#header_music_player").hover(
	function(){
		$("#flashPlayer").getFlashObj().showPlaylist();
	}, 
	function(){
		$("#flashPlayer").getFlashObj().hidePlaylist();	
	});
	$("#events #scroller").after("<a id='toggleScroll' style='cursor:pointer;'>Stop Scrolling</a>");
	$("#toggleScroll, .eventScroll").click(function(){
		if(stopScroll){
			stopScroll=false;
			$("#toggleScroll").html('Stop Scrolling');
		}
		else{
			stopScroll=true;
			$("#toggleScroll").html('Resume Scrolling');
		}
	});
	$("#scroller").click(function(){
		stopScroll=true;
		$("#toggleScroll").html('Resume Scrolling');
	});
	$("#scrollerUp").click(function(){
		stopScroll=true;
		$("#toggleScroll").html('Resume Scrolling');
		if(last != 0){
			current = last-1;
			scrollEvent('#event');
		}
	});
	$("#scrollerDown").click(function(){
		stopScroll=true;
		$("#toggleScroll").html('Resume Scrolling');
		scrollEvent('#event');
	});
});

var flashvars = {
  xmlPath: BASE+"/flash/playlist.xml",
  autoStart: "true",
  showDownload: "true",
  repeatPlay: "false",
  songVolume: "75"
};
var params = {
  menu: "false",
  wmode: "transparent",
  allowScriptAccess: "always",
  swliveconnect: "true"
};
var attributes = {
  id: "flashPlayer",
  name: "flashPlayer"
};


$(window).load(function () {
	$("#event").scrollTop(0);
	$('#event').eventfade({'timeout': 2500,'containertarget': '.eventFlyer'});
	swfobject.embedSWF(BASE+"/flash/spacemp3player.swf", "player", "300", "134", "8.0.0","expressInstall.swf", flashvars, params, attributes);

});
