var flip_contentBefore = '';

soundManager.url = 'fileadmin/template/swf/';
soundManager.debugMode = false;

jQuery(function($) {
	
	//besseres styling der überschriften
	$(':header', '#content')
		.filter(':first').addClass('first').end()
		.filter(':last').addClass('last');
	
	//Olis Notizblog
	if($('.olisblog', '#right').length == 1) {
		$('.olisblog', '#right').hover(function() {
			if($(this).find('img').is(':visible')) {
				$(this)
					.find('img').fadeOut(500).end()
				  .find('.content').fadeIn(500, function() {
			  		//$(this).closest('.olisblog').find('.jeder').hide().text('Das kann doch jeder lesen.').fadeIn(1000);
			  	}).end()
			  	.find('.jeder').hide().text('Das kann doch jeder lesen.').fadeIn(500);
			}
		}, function() {
			$(this)
				.find('img').fadeIn(1000).end()
			  .find('.content').fadeOut(1000).end()
			  .find('.jeder').hide().text('Das kann doch keiner lesen!').fadeIn(500);
		});
	}
	
	//Lavalamp im Hauptmenü
	/*$('#mainmenu').lavaLamp({
		speed: 500
	});*/
	
	if(!$.browser.msie) {
		//Links zu MP3s als Player darstellen
		$('a[href$=.MP3],a[href$=.mp3]', '#contentwrap').mp3player();
	}
	
	//Slider um durch relevante Records zu scrollen
	$recordsliders = $('.recordslider');
	if($recordsliders.length > 0) {
		makeRecordsliders($recordsliders);
	}
	
	//Cooler Flipeffekt beim Login
	flip_contentBefore = $('#head #login').html();
	$('#head #login a.login').live('click', function() {
		$('#head #login').flip({ 
			direction: 'rl', 
			color: '#000000',
			content: $('.loginform', '#head'),
			onEnd: function() {
				$('#head #login form input:first').focus();
				$('#head #login').css('backgroundColor', '#ffffff');
			}
		}); 
		return false;
	});
	
	//Zurück vom Loginformular
	$('#head #login a.back').live('click', function() {
		$('#head #login').flip({
			direction: 'lr',
			color: '#000000',
			content: flip_contentBefore,
			onEnd: function() {
				$('#head #login').css('backgroundColor', '#ffffff');
			}
		});
		return false;
	});
	
	//Klick auf "Nein"
	var href = '';
	$('#login li.no', '#head').live('click', function() {
		href = $(this).find('a').attr('href');
		$('#head #login').flip({
			direction: 'rl',
			color: '#000000',
			content: $('<div><h1 class="nofriend">Oliver Rohrbeck</h1></div>'),
			onEnd: function() {
				$('#head #login').css('backgroundColor', '#ffffff');
				window.setTimeout(function() { window.location = href; }, 1000);
			}
		});
		return false;
	});
	

	
	/* Mouseover für Sprecher (Name einblenden) */
	$('.sitemap li').each(function() {
		h = -$(this).height();
		$('a.name', this).css({
				top: h
			})
			.parent().hover(function() {
				$('a.name', this).stop().animate({top: 0}, 500);
			}, function() {
				$('a.name', this).stop().animate({top: h}, 500);
			});
		
		if($(':has(img)', this).length == 0) {
			$(this).remove();
		}
	});
	
});


function makeRecordsliders($elements) {
	$elements.each(function() {
		var $parent = $(this);
		$parent.data('element_width', $('li:first', this).width());
		$parent.data('element_count', $('li', this).length);
		$parent
			.find('ul')
				.css('width', ($parent.data('element_width')*$parent.data('element_count'))+'px')
				.before('<div class="move left" style="display:none">zurück</div>')
				.after('<div class="move right" style="'+($parent.data('element_count') > 3 ? 'display:block;' : 'display:none')+'">weiter</div>')
			.end()
			.find('.move')
				.hover(function() {
					//$('.showTitle', $parent).html('<i>'+($(this).is('.right') ? 'weiter' : 'zurück')+'</i>');
				}, function() {
					$('.showTitle', $parent).text('');
				})
				.click(function() {
					element_count = $parent.data('element_count');
					element_width = $parent.data('element_width');
					
					if($parent.find('ul:animated').length == 1) {
						$parent.find('ul').stop().css('left', $parent.find('ul').data('left'));
					}				
					var left = $parent.find('ul').position().left;
					if($(this).is('.right') && left > ((element_count-3)*element_width)*-1) {
						left-= element_width;
						$parent.find('.move.left').fadeIn();
					}
					if($(this).is('.left') && left < 0) {
						left+= element_width;
					}
					$parent.find('ul').data('left', left);
					
					$parent.find('ul').animate({left: left}, 500, function() {
						if(left <= ((element_count-3)*element_width)*-1) {
							$parent.find('.move.right').fadeOut();
							$parent.find('.move.left').fadeIn();
						}
						else if(left >= 0) {
							$parent
								.find('.move.left').fadeOut().end()
								.find('.move.right').fadeIn().end()
								.find('ul').css('left', 0);
						}
						else {
							$parent.find('.move.right').fadeIn();
						}
	 				});
				}).end()
				.find('.title').hide().end()
				.append('<div class="showTitle"><span></span></div>')
				.find('li:not(.showTitle)').hover(function() {
					$('.showTitle', $parent).text($(this).find('.title').text());
				}, function() {
					$('.showTitle', $parent).text('');
				});
		});
}

