$(document).ready(function() {
	/* START ANIMATION */
	$('#contentList').show();
	$('#contentList li').addClass('displayNone').slideToggle(1750).delay(1000);
	$('#imprint').slideDown('slow').delay(2000);
	$('#contact').slideDown('slow').delay(2000);
	
	
	/* INHALTE LADEN */				   
	$('#contentList a').click(function(){
		var toLoad = 'load.php?url=' + $(this).attr('href');
		var newObject = this;
		
		if($(newObject).is('.open')) {
		    $(newObject).removeClass('open');
			$('#insert').slideUp('normal', function() { $(this).remove(); });
		} else if( $('#insert').length < 1 ){
			getNewContent();
		} else {
			$('#contentList a').removeClass('open');
			$('#insert').slideUp('normal', function() { $(this).remove(); getNewContent(); });
		}
		
		function getNewContent() {
			if($(newObject).hasClass('creation')){
				newColorClass = 'creation';
				}
			if($(newObject).hasClass('agency')){
				newColorClass = 'agency';
				}
			if($(newObject).hasClass('news')){
				newColorClass = 'news';
				}
			if($(newObject).hasClass('voice')){
				newColorClass = 'voice';
				}
			$(newObject).after('<div id="insert" class="' + newColorClass + '"><img src="img/loader.gif" width="32" height="32" class="insertLoader" border="0" alt="" /><div id="insertContainer"></div></div>');
			$(newObject).addClass('open');
			$('#insert').slideDown('normal',loadContent);
			$(newObject).intoViewport({duration:500});
		}
		function loadContent() {
			$('#insertContainer').load(toLoad,'',showNewContent());
		}
		function showNewContent() {
			
			$('#insert .insertLoader').slideUp('fast', function() { $('#insertContainer').slideDown('slow'); replaceVideoContent() });
			
		}	
		function replaceVideoContent() {
		 	$('.video a').each(function(index) {
				thisHref = $(this).attr('href');
				posterHref = $(this).children().attr('src');				
				//$(this).replaceWith('<video preload controls poster="' + posterHref + '"><source src="' + thisHref + '"  type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'></video>');
				$(this).replaceWith('<video controls preload="none" poster="' + posterHref + '" webkit-playsinline><source src="' + thisHref + '"  type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\'></video>');
	 		});
		
		}
		
		return false;
		
	});
	
	/* INHALTE FILTERN */
	$('#Lgo').click( function () { filterContent('') } );
	$('#kr a').click( function () { filterContent('creation') } );
	$('#ne a').click( function () { filterContent('news') } );
	$('#ag a').click( function () { filterContent('agency') } );
	
	function filterContent( ele ) {
		if(ele == 'creation')
		{
			$('#contentList .creation').slideDown('fast');
			$('#contentList .news').slideUp('fast');
			$('#contentList .agency').slideUp('fast');
			$('#insert').slideUp('fast', function() { $(this).remove(); });
			$('#imprint p').slideUp('normal');
			$('#contact p').slideUp('normal'); 
		} 
		else if(ele == 'news')
		{
			$('#contentList .creation').slideUp('fast');
			$('#contentList .news').slideDown('fast');
			$('#contentList .agency').slideUp('fast');
			$('#insert').slideUp('fast', function() { $(this).remove(); });
			$('#imprint p').slideUp('normal');
			$('#contact p').slideUp('normal'); 
		} 
		else if(ele == 'agency')
		{
			$('#contentList .creation').slideUp('fast');
			$('#contentList .news').slideUp('fast');
			$('#contentList .agency').slideDown('fast');
			$('#insert').slideUp('fast', function() { $(this).remove(); });
			$('#imprint p').slideUp('normal');
			$('#contact p').slideUp('normal'); 
		}
		else
		{
			$('#contentList .creation').slideDown('fast');
			$('#contentList .news').slideDown('fast');
			$('#contentList .agency').slideDown('fast');
			$('#insert').slideUp('fast', function() { $(this).remove(); });
			$('#imprint p').slideUp('normal');
			$('#contact p').slideUp('normal'); 
		}
	}
	
	/* IMPRESSUM & KONTAKT LADEN */
	$('#imprint').click( function (){ 
		var newObject = this;
		$('#insert').slideUp('fast', function() { $(this).remove(); });
		$('#imprint p').slideToggle('normal'); 
		$('#contact p').slideUp('normal'); 
		$(newObject).intoViewport(); 
	});
	$('#contact').click( function (){ 
		var newObject = this;
		$('#insert').slideUp('fast', function() { $(this).remove(); });
		$('#imprint p').slideUp('normal'); 
		$('#contact p').slideToggle('normal'); 
		$(newObject).intoViewport();
	});
});
