$(document).ready(function(){
	WebFont.load({
	  google: {
	    families: ['FF Meta Web Pro']
	  }
	});
	
	
	try {
	   Typekit.load({
	     loading: function() {
	       // Javascript to execute when fonts start loading
	     },
	     active: function() {
	     	prev = null;
			$('h3 span').each(function(index) {

				if(prev != null && prev.position().top < $(this).position().top){
				
						$(prev).css('padding-right', '10px');
						$(this).css('padding-left', '10px');

					return false;
				}

				prev = $(this);
			});
	     

	     },
	     inactive: function() {
	       // Javascript to execute when fonts become inactive
	     }
	   })
	 } catch(e) {}
			
	$('#bmwplayer').pixelplayer();
	$('#slider').headerslideshow();
	$('#news .post a.showmore').click(show_more);
	
	function show_more(e){
		e.preventDefault();
		this.blur();
		$(this).next('div.more').slideDown();

	}
	
	$("a.newsletterbox").fancybox({
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	400, 
		'speedOut'			:	200,
		'showCloseButton'	: 	false,
		'titleShow'			: 	false,
		'padding'			: 	0,
		'overlayShow'		:	true,
		'overlayColor'		: 	'#002c41',
		'overlayOpacity'	: 	0.8,
		'scrolling'			: 	'no',
		'centerOnScroll'	: 	true
	});
	
	
	$("a.gewinnspielbox").fancybox({
		'transitionIn'		:	'elastic',
		'transitionOut'		:	'elastic',
		'speedIn'			:	400, 
		'speedOut'			:	200,
		'showCloseButton'	: 	false,
		'titleShow'			: 	false,
		'padding'			: 	0,
		'overlayShow'		:	true,
		'overlayColor'		: 	'#002c41',
		'overlayOpacity'	: 	0.8,
		'scrolling'			: 	'no',
		'centerOnScroll'	: 	true
	});
	
	
	$('a.closeNL').click(function(e){
		e.preventDefault();
		$.fancybox.close();
	});
	
	
	$('#newsletterbox a.radiostyler').click(function(e){
		e.preventDefault();
		this.blur();
		
		$('input[name=' + $(this).attr('rel') + ']').each(function(){
			$(this).attr('checked', false);
			$(this).removeClass('active');
		});
		$('a[rel=' + $(this).attr('rel') + ']').each(function(){
			$(this).removeClass('active');
		});
		
		$(this).addClass('active');
		$('#' + $(this).attr('rev')).attr('checked', 'checked');
	});
	
	$('#newsletterSubmit').click(function(e) {
		e.preventDefault();
		this.blur();
		
		$.post('/newsletter', $('#nlForm').serialize(), function(data){
		if (data=="true") {
			$('#nlFormContainer').fadeOut('slow', function() {
				nlFirst = $('#nlFirst').val();
				if (nlFirst != '') {
					$('#nlThanksFirst').html(' ' + nlFirst);
				}
				
				$('#nlRegister').hide();
				$('#nlThanks').show();
				
				$('#nlThanksContainer').fadeIn('slow');
			});
		}
		else if (data == "wrongMail") {
			$('#nlText').fadeOut('slow', function() {
				myText = (BMWlang=="de") ? "<b>Fehler:</b><br />Bitte trage Deine korrekte E-Mail Adresse ein." : "<b>Error:</b><br />Please enter your correct e-mail address.";
				$('#nlText').html(myText).fadeIn('slow');
				$('#nlText').addClass("error");
				$('#nlMailLabel').addClass("error");
				$('#nlMail').select();
				$('#nlMail').focus();
			});
		}
		else if (data == "mailAlreadyInDB") {
			$('#nlText').fadeOut('slow', function() {
				myText = (BMWlang=="de") ? "<b>Fehler:</b><br />Die angegebene E-Mail Adresse ist bereits in unserem Verteiler." : "<b>Error:</b><br />The given e-mail address is already stored in our recipient list.";
				$('#nlText').html(myText).fadeIn('slow');
				$('#nlText').addClass("error");
				$('#nlMailLabel').addClass("error");
				$('#nlMail').select();
				$('#nlMail').focus();
			});
		}
		else {
			$('#nlText').fadeOut('slow', function() {
				myText = (BMWlang=="de") ? "<b>Fehler:</b><br />Leider ist ein Fehler aufgetreten. Bitte versuche es sp&auml;ter noch einmal, vielen Dank." : "<b>Error:</b><br />Unfortunately an error occured. Please try again later.";
				$('#nlText').html(myText).fadeIn('slow');
				$('#nlText').addClass("error");
			});
		}
	});
	return(false);
		
	});
});
/**
*
*
*
*/
(function($){
	
	
	if(window.console && window.console.firebug)
		FIREBUG = true;
	
	var HeaderSlideShow = function(element, options) {
		
		var elem = element;
		var obj = this;
		var timer = null;
		
		// Default
		var settings = $.extend({
			duration: 				5
			
		}, options || {});



		var fade = function(e) {
			
			stopSlide();

			var next = $('#slider > .current').next();

			if(next.length > 0){

				$('#slider > .current').animate({
					opacity: 0
				}, 1000, function(){
					$(this).removeClass('current');
				});
				
				next.addClass('current').css('opacity',0).animate({opacity: 1}, 1000, function(){
					
					startSlide();
				});

			}
			else{
				$('#slider > .current').animate({
					opacity: 0
				}, 1000, function(){
					$('#slider img').last().removeClass('current');
				});
						
						
				$('#slider img').first().addClass('current').animate({opacity: 1}, 1000, function(){
			
					startSlide();
				});
			}
		}

		
		
		var fadeWebkit = function(e) {
				
			stopSlide();

			var next = $('#slider > .current').next();

			if(next.length > 0){
				$('#slider > .current').removeClass('current');
				next.addClass('current');
				startSlide();

			}
			else{
				$('#slider > .current').removeClass('current');
				$('#slider img').first().addClass('current');
				startSlide();
			}

		}

		var startSlide = function(e) {
			this.timer = setInterval(next, settings.duration * 1000);
		}

		var stopSlide = function(e) {
				clearInterval(this.timer);
			}



		var next = function(e) {

			if($.browser.webkit){
				fadeWebkit();

			}else{
				fade();
			}

		}
		
		/**
		* Initialisierung
		*
		*
		*/
		var init = function() {
			startSlide();
	    };
	   
		$(init);
		
		$(".chzn-select").chosen();
   };

	/**
	*
	*
	*
	*/
	$.fn.headerslideshow = function(options){
		return this.each(function() {
			var element = $(this);
			
			if (element.data('headerslideshow')) return;
			
			var headerslideshow = new HeaderSlideShow(this, options);
			element.data('headerslideshow', headerslideshow);

       });
   };
})(jQuery);
