function embedSWFBanner(id, swf, width, height, url)
{
	var flashvars = {url : url};
	swfobject.embedSWF(swf, id, width, height, "8", "/custom/js/expressInstall.swf", flashvars);
}

$.easing.custom = function (x, t, b, c, d, s) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
}


var alternateSidebar = {
	
	'speed': 2000,

	'start': function()
	{
		var date = new Date();
		var now = date.getTime()/1000;
		
		var session_length = now - session_start;
		var delay = 30;
		var timeout =  delay - session_length;
		
		if(timeout < 3)
			timeout = 3;
		
		setTimeout(function()
		{
			alternateSidebar.show();
		}, timeout*1000);
	},

	'show': function()
	{
		// stop rotator
		rotator.stop();
		
		setTimeout(function()
		{
			alternateSidebar.hideOldSidebar();
		}, 2000);
	},
	
	'hideOldSidebar': function()
	{
		//console.log();
		$('#alternate_sidebar').height($('#sidebar').height());
		
		$('#alternate_sidebar').animate(
			{
				'width': 218,
				'marginLeft': 0
			},
			alternateSidebar.speed,
			'custom',
			function()
			{
				$('#normal_sidebar').hide();
			}
		);
	}
}


var rotator = {
	
	'count': 1,
	'container': '#rotatorCont',
	'speed': 2000,
	'wait': 4000,
	'stopped': false,
	'timeout': false,
	
	'init': function()
	{
		var type = ($(rotator.container).parent().hasClass("start")) ? "start" : "other";
		$(rotator.container).addClass("enabled");
		
		$.getJSON("/cms/rotator/"+type+"/json/", function(data)
		{
			for(var i in data)
			{
				var headline_size = (data[i].title.length > 26) ? "small" : "";
				
				var html = '<li id="rotator_item_'+data[i].id+'">\
					<h4 class="'+headline_size+'"><a href="'+data[i].link+'">'+data[i].title+'</a></h4>\
					<a href="'+data[i].link+'"><img src="'+data[i].image+'" /></a>\
					<p>'+data[i].text+' <a href="'+data[i].link+'">weiter&nbsp;»</a></p>\
				</li>';
				
				rotator.count++;
				$(rotator.container).append(html);
			}
			
			$(rotator.container+" li").click(function()
			{
				var link = $("#"+this.id+" a").first().attr("href");
				document.location = link;
			});

			rotator.timeout = setTimeout(rotator.loadNext, rotator.wait);
		})
	},
	
	'loadNext': function()
	{
		if(!rotator.stopped)
		{
			$(rotator.container+" li").first().animate(
				{
					'margin-top': "-100px"
				}, 
				rotator.speed,
				'custom',
				function()
				{
					var item = $(rotator.container+" li").first().detach();
					item.css('margin-top', "0");
					$(rotator.container).append(item);

					if(!rotator.stopped)
						rotator.timeout = setTimeout(rotator.loadNext, rotator.wait);
				}
			);
		}
	},
	
	'stop': function()
	{
		clearTimeout(rotator.timeout);
		rotator.stopped = true;
	},
	
	'start': function()
	{
		rotator.stopped = false;
		rotator.loadNext();
	}
}


var slideshow = {
	
	'imageCount': 0,
	'currentImage': 0,
	'speed': 3000,
	'transition': 1000,
	
	'init': function()
	{
		this.imageCount = $('ul.slideshow li').length;
		this.currentImage = 0;
		$('ul.slideshow li').hide();
		
		if(this.imageCount > 1)
		{
			$('ul.slideshow li:first').show();

			$('ul.slideshow').height($('ul.slideshow li:first img').height());


			setTimeout(slideshow.showNextImage, slideshow.speed);
		}
		else
		{
			$('ul.slideshow li:first').fadeIn(slideshow.transition);
		}
	},
	
	'showNextImage': function()
	{
		var next = (slideshow.currentImage + 1 < slideshow.imageCount) ? slideshow.currentImage + 1 : 0;
		
		$('ul.slideshow li').eq(next).fadeIn(slideshow.transition);
		$('ul.slideshow li').eq(slideshow.currentImage).fadeOut(slideshow.transition);
		
		slideshow.currentImage = next;
		setTimeout(slideshow.showNextImage, slideshow.speed+slideshow.transition);
	}
}

$(document).ready(function()
{
	// alternateSidebar.start();
	
	if($('#big_teaser').length && $('#big_teaser').attr("data-swf"))
	{
		embedSWFBanner("big_teaser", $('#big_teaser').attr("data-swf") , 486, 430, $('#big_teaser').attr("data-link"))
	}
	
	if($('a#embed_video').length)
	{
		// get width and hight from image
		var image = $('a#embed_video').children().first();
		var video_image_width = image.attr("width");
		var video_image_height = image.attr("height");
		var video_url = $('a#embed_video').attr("href");
		
		// for browsers supporting html5 video with h.264
		if($("body").hasClass("safari") || $("body").hasClass("chrome"))
		{
			var button_pos = [ video_image_width/2-22, video_image_height/2-22 ];

			$('a#embed_video').prepend("<img src=\"/custom/media/swf/play_button.png\" style=\"margin:"+button_pos[1]+"px 0 0 "+button_pos[0]+"px\" class=\"video_play\"/>");

			$('a#embed_video').click(function()
			{
				if($("#html5_video").length == 0)
				{
					var video_url = this.href;

					// prepare html
					var html = $("<video src=\""+video_url+"\" id=\"html5_video\" width=\""+video_image_width+"\" height=\""+video_image_height+"\" controls=\"controls\" />");
					
					// replace link and image with video
					$(this).replaceWith(html);

					// force video to play
					$('#html5_video').get(0).play();
				}
				
				return false;
			});
		}
		else
		{
			
			var flashvars = {
				width: video_image_width,
				height: video_image_height,
				image: image.attr("src"),
				file: video_url
			}
			
			var params = {
				allowfullscreen: "true",
				wmode: "transparent"
			}
			
			var attributes = {}
			
			swfobject.embedSWF("/custom/media/swf/mediaplayer.swf", "embed_video", video_image_width, video_image_height, "9.0.98", "/custom/js/expressInstall.swf", flashvars, params, attributes);
		}
	}
	
	$('a.content_toggle').show();
	
	$('a.content_toggle').each(function()
	{
		var content_class = $(this).attr('href').split("#").pop();
		$('.'+content_class).hide();
	});
	
	$('a.content_toggle').click(function()
	{
		var clicked = $(this);
		
		var content_class = clicked.attr('href').split("#").pop();
		var animation_speed = 500;
		
		if(clicked.hasClass("opened"))
		{
			$('.'+content_class).fadeOut(animation_speed);
			clicked.removeClass("opened");
		}
		else
		{
			$('.'+content_class).fadeIn(animation_speed);
			clicked.addClass("opened");
		}
		
		return false;
	});
	
	$('a.back').click(function()
	{
		history.back();
		return false;
	});
	
	
	$('input.check_jpeg').change(function()
	{
		var filename = this.value;
		var extension = filename.substr(filename.lastIndexOf('.')+1, filename.length).toLowerCase();
		
		if(extension != 'jpeg' && extension != 'jpg')
		{
			alert("Bitte waehlen Sie eine JPEG-Datei aus.");
			this.value = "";
			return false;
		}
		else
		{
			return true;
		}
	});

	
	$('a.blank').click(function()
	{	
		window.open(this.href);
		return false;
	});
	
	$('#small_teasers .small').click(function()
	{
		var link_element = $("#"+this.id+"_link");
		
		if(link_element.length)
		{
			var link = $("#"+this.id+"_link").attr('href');
			document.location = link;
		}
	});
	
	$('div.overlay').hide();
	$('a.overlay').click(function()
	{
		var url = this.href.split("#");
		if(url.length == 2)
		{
			var id = url[1];
			var element = $('#'+id+".overlay");
			
			if(element.length)
			{
				$.fancybox(
					element.html(),
					{
			        	'autoDimensions'	: false,
						'width'         	: element.width(),
						'height'        	: 'auto'
					}
				);
				
				return false;
			}
		}
		
	})
	
	$('#small_teasers .small:not(.nolink)').addClass("javascript");
	
	if($('#rotatorCont').length)
	{			
		rotator.init();
	}
	
	slideshow.init();
	
	var fancy_images = $('a.fancybox:not(.no_link) img');
	var link = "<div class=\"fancy_image_link\"><a href=\"\">Bild vergrößern</a></div>";
	$(link).insertAfter(fancy_images);
	
	$('a.fancybox').fancybox({
		'titleShow'     : true,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'titlePosition' : 'over'
	});
	
});
