$(function(){
	$('a.email').nospam({
		replaceText: true
	});
	if ($('img').length > 0 ){
		$('body').onImagesLoad({
	        selectorCallback: selectorImagesLoaded 
	    });
	}else{
		selectorImagesLoaded();
	}
    //the selectorCallback function, invoked once when all images contained within $('body') have loaded 
    function selectorImagesLoaded($selector){ 
        //note: $selector is $('body') here
        $('#loading').detach();
        intiializeScrollBars();
        var startfelder = "#"+$("#one .artikel")[1].id;
        $('embed, object').attr('width', '100%').width('100%');
        $('embed, object').attr('height','95%').height('95%');
        goToArticle(startfelder);
    }
	
	$('#one .artikel').click( function(){
		reSizeA();
	});
	
	$('#two .artikel').click( function(){
		reSizeB();
	});
	
	$('#three .artikel').click( function(){
		reSizeC();
	});

	$('#four .artikel').click( function(){
		reSizeD();
	});
	
	author();
	
	$('.play').click( function(event) {
		event.stopPropagation();
		var vimeourl = $(this).parent().next('.video_file_empty').attr('rel');
		$(this).parent().hide();
		if ($(this).parent().parent().width() < 500 ){
			$(this).parent().next('.video_file_empty').show().removeClass('video_file_empty').addClass('video_file').oembed(vimeourl, 
	           {
	           embedMethod: "fill",
	           width: 380,
	           height: 220, 
	           vimeo: { autoplay: true, width: 380, height: 220}                 
	        }, oembedcallback());
		} else {
			$(this).parent().next('.video_file_empty').show().removeClass('video_file_empty').addClass('video_file').oembed(vimeourl, 
	           {
	           embedMethod: "fill",
	           width: 780,
	           height: 440, 
	           vimeo: { autoplay: true, width: 780, height: 440}                 
	        }, oembedcallback());
		}
	});
	
	$('.video_file').click( function(event) {
		event.stopPropagation();
	});
	
	$('.authored').click( function() {
		author();
	});
	$('.unlinked').click( function() {
		unlink();
	});
	
	$('.menupt').mouseover(function() {
		var mnuid = "#mtitel_"+$(this).attr('rel');
		$(mnuid).css({'visibility':'visible'});
	}).mouseout(function(){
		var mnuid = "#mtitel_"+$(this).attr('rel');
		$(mnuid).css({'visibility':'hidden'});
	});
	
/*	$('#closeR, #closeL').click( function() {
		if ( $('#oneContainer').width() > 600) {
			reSizeA();
		} else if ( $('#twoContainer').width() > 600) {
			reSizeB();
		} else if ( $('#threeContainer').width() > 600) {
			reSizeC();
		} else if ( $('#fourContainer').width() > 600) {
			reSizeD();
		}
	});*/
	
	$('#oneContainer').scroll( function(){
		var curID = currentID('#oneContainer');
		$('#menuAlist .menupt').css({'color':'#ccc'});
		$('#mpt_'+curID).css({'color':'#000'});
	});
	
	$('#twoContainer').scroll( function(){
		var curID = currentID('#twoContainer');
		$('#menuBlist .menupt').css({'color':'#ccc'});
		$('#mpt_'+curID).css({'color':'#000'});
	});
	
	$('#threeContainer').scroll( function(){
		var curID = currentID('#threeContainer');
		$('#menuClist .menupt').css({'color':'#ccc'});
		$('#mpt_'+curID).css({'color':'#000'});
	});
	
	$('#fourContainer').scroll( function(){
		var curID = currentID('#fourContainer');
		$('#menuDlist .menupt').css({'color':'#ccc'});
		$('#mpt_'+curID).css({'color':'#000'});
	});
			
});

function author() {
	
	$('.authored').css({'text-decoration':'underline'});
	$('.unlinked').css({'text-decoration':'none'});
	
	$('.menupt').unbind('click');
	$('.menupt').bind('click', function() {
		var wohin = "#"+$(this).attr('rel');
		goToArticle(wohin);
	});
	
	
	$('#oneContainer').bind('mousewheel', function(event, delta) {
		
		if ( $(this).find('.video_file').length > 0 ){
				$(this).find('.video_thumb').show();
				$(this).find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
	
		var contoben = Math.round($(this).position().top*-1);
		
		$("#oneContainer .artikel").each(function (i) {
			var hoehe = Math.round($(this).height());
			var oben = Math.round($(this).position().top);
			var unten = hoehe + oben;
			
			if ( delta < 0 ){
				var oben = oben - 150;
				var unten = unten - 150;
			} else if ( delta > 0 ){
				var oben = oben + 150;
				var unten = unten;
			}
						
			if ( oben <= contoben && contoben < unten ) {
				var rel = $(this).attr("rel").split(",");
				scrollMit('A', rel[0], rel[1], rel[2], rel[3]);
				return false;
			}
			
		});
	});
	$('#twoContainer').bind('mousewheel', function(event, delta) {
		
		if ( $(this).find('.video_file').length > 0 ){
				$(this).find('.video_thumb').show();
				$(this).find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
	
		var contoben = Math.round($(this).position().top*-1);
		
		$("#twoContainer .artikel").each(function (i) {
			var hoehe = Math.round($(this).height());
			var oben = Math.round($(this).position().top);
			var unten = hoehe + oben;
			
			if ( delta < 0 ){
				var oben = oben - 150;
				var unten = unten - 150;
			} else if ( delta > 0 ){
				var oben = oben + 150;
				var unten = unten;
			}
						
			if ( oben <= contoben && contoben < unten ) {
				var rel = $(this).attr("rel").split(",");
				scrollMit('B', rel[0], rel[1], rel[2], rel[3]);
				return false;
			}
			
		});
	});
	$('#threeContainer').bind('mousewheel', function(event, delta) {
		
		if ( $(this).find('.video_file').length > 0 ){
				$(this).find('.video_thumb').show();
				$(this).find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
	
		var contoben = Math.round($(this).position().top*-1);
		
		$("#threeContainer .artikel").each(function (i) {
			var hoehe = Math.round($(this).height());
			var oben = Math.round($(this).position().top);
			var unten = hoehe + oben;
			
			if ( delta < 0 ){
				var oben = oben - 150;
				var unten = unten - 150;
			} else if ( delta > 0 ){
				var oben = oben + 150;
				var unten = unten;
			}
						
			if ( oben <= contoben && contoben < unten ) {
				var rel = $(this).attr("rel").split(",");
				scrollMit('B', rel[0], rel[1], rel[2], rel[3]);
				return false;
			}
			
		});
	});
	$('#fourContainer').bind('mousewheel', function(event, delta) {
		
		if ( $(this).find('.video_file').length > 0 ){
				$(this).find('.video_thumb').show();
				$(this).find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
	
		var contoben = Math.round($(this).position().top*-1);
		
		$("#fourContainer .artikel").each(function (i) {
			var hoehe = Math.round($(this).height());
			var oben = Math.round($(this).position().top);
			var unten = hoehe + oben;
			
			if ( delta < 0 ){
				var oben = oben - 150;
				var unten = unten - 150;
			} else if ( delta > 0 ){
				var oben = oben + 150;
				var unten = unten;
			}
						
			if ( oben <= contoben && contoben < unten ) {
				var rel = $(this).attr("rel").split(",");
				scrollMit('B', rel[0], rel[1], rel[2], rel[3]);
				return false;
			}
			
		});
	});
}

function unlink() {
	
	$('.authored').css({'text-decoration':'none'});
	$('.unlinked').css({'text-decoration':'underline'});
	
	$('.menupt').unbind('click');
	$('.menupt').bind('click', function() {
		var wohin = "#"+$(this).attr('rel');
		if ( $(this).parent().parent().is("#menuAlist") ) {
			$('#oneContainer')[0].scrollTo(wohin);
		} else if ( $(this).parent().parent().is("#menuBlist") ) {
			$('#twoContainer')[0].scrollTo(wohin);
		} else if ( $(this).parent().parent().is("#menuClist") ) {
			$('#threeContainer')[0].scrollTo(wohin);
		} else if ( $(this).parent().parent().is("#menuDlist") ) {
			$('#fourContainer')[0].scrollTo(wohin);
		}
	});
	
	$('#oneContainer').unbind('mousewheel');
	$('#twoContainer').unbind('mousewheel');
	$('#threeContainer').unbind('mousewheel');
	$('#fourContainer').unbind('mousewheel');
}

function intiializeScrollBars() {
	$(".container").jScrollPane({animateTo:true});
	$(".jScrollPaneTrack").css({'visibility' : 'hidden'});
}

function oembedcallback(){
	$('.video_file').click( function(event) {
		event.stopPropagation();
	});
}
	

function goToArticle(trgt) {
	var targ = $(trgt);	
	var reltxt = targ.attr("rel");
	var related = reltxt.split(",");
	
	if ( $(trgt).parent().is("#oneContainer") ) {
		if ( $('#oneContainer').find('.video_file').length > 0 ){
				$('#oneContainer').find('.video_thumb').show();
				$('#oneContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		$('#oneContainer')[0].scrollTo(trgt);
	} else if (related[0] != 'e') {
		if ( $('#oneContainer').find('.video_file').length > 0 ){
				$('#oneContainer').find('.video_thumb').show();
				$('#oneContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		var relA = "#"+related[0];	
		$('#oneContainer')[0].scrollTo(relA);
	}
	if ( $(trgt).parent().is('#twoContainer') ) {
		if ( $('#twoContainer').find('.video_file').length > 0 ){
				$('#twoContainer').find('.video_thumb').show();
				$('#twoContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		$('#twoContainer')[0].scrollTo(trgt);
	} else if (related[1] != 'e') {
		if ( $('#twoContainer').find('.video_file').length > 0 ){
				$('#twoContainer').find('.video_thumb').show();
				$('#twoContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		var relB = "#"+related[1];
		$('#twoContainer')[0].scrollTo(relB);
	}
	if ( $(trgt).parent().is('#threeContainer') ) {
		if ( $('#threeContainer').find('.video_file').length > 0 ){
				$('#threeContainer').find('.video_thumb').show();
				$('#threeContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		$('#threeContainer')[0].scrollTo(trgt);
	} else if (related[2] != 'e') {	
		if ( $('#threeContainer').find('.video_file').length > 0 ){
				$('#threeContainer').find('.video_thumb').show();
				$('#threeContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		var relC = "#"+related[2];
		$('#threeContainer')[0].scrollTo(relC);
	}
	if ( $(trgt).parent().is('#fourContainer') ) {
		if ( $('#fourContainer').find('.video_file').length > 0 ){
				$('#fourContainer').find('.video_thumb').show();
				$('#fourContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		$('#fourContainer')[0].scrollTo(trgt);
	} else if (related[3] != 'e') {
		if ( $('#fourContainer').find('.video_file').length > 0 ){
				$('#fourContainer').find('.video_thumb').show();
				$('#fourContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
		}
		var relD = "#"+related[3];	
		$('#fourContainer')[0].scrollTo(relD);
	}

}

function scrollMit(wer, wo1, wo2, wo3, wo4) {
	var wohin1 = "#"+wo1;
	var wohin2 = "#"+wo2;
	var wohin3 = "#"+wo3;
	var wohin4 = "#"+wo4;
	if ( wer != 'A' ){
		if ( $(wohin1).parent().is('#oneContainer') || wo1 == 'w' ) {
			if ( $('#oneContainer').find('.video_file').length > 0 ){
				$('#oneContainer').find('.video_thumb').show();
				$('#oneContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
			}
			$('#oneContainer')[0].scrollTo(wohin1);
		}
	}
	if ( wer != 'B' ){
		if ( $(wohin2).parent().is('#twoContainer') || wo2 == 'w' ) {
			if ( $('#twoContainer').find('.video_file').length > 0 ){
				$('#twoContainer').find('.video_thumb').show();
				$('#twoContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
			}
			$('#twoContainer')[0].scrollTo(wohin2);
		}
	}
	if (wer != 'C'){
		 if ( $(wohin3).parent().is('#threeContainer') || wo3 == 'w' ) {
		 	if ( $('#threeContainer').find('.video_file').length > 0 ){
				$('#threeContainer').find('.video_thumb').show();
				$('#threeContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
			}
			$('#threeContainer')[0].scrollTo(wohin3);
		 }
	}
	if (wer != 'D' ){
		if ( $(wohin4).parent().is('#fourContainer') || wo4 == 'w' ) {
			if ( $('#fourContainer').find('.video_file').length > 0 ){
				$('#fourContainer').find('.video_thumb').show();
				$('#fourContainer').find('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
			}
			$('#fourContainer')[0].scrollTo(wohin4);
		}
	}
}

function currentID(feld){
	var container = $(feld).position();
	var contoben = Math.round(container.top*-1);
	$(feld+" .artikel").each( function() {
		var oben = Math.round($(this).position().top);
		var hoehe = Math.round($(this).height());
		var unten = hoehe + oben;
		if ( oben <= contoben && contoben < unten ) {
			curID = $(this).attr('id');
			return false;
		}
	});
	return curID;
}

function reSizeA() {
	
	
	if ( $('.video_file').length > 0 ){
			$('.video_thumb').show();
			$('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
	}
    
    trgt = "#"+currentID('#oneContainer');
    		
	$("#one").css({'z-index' : '50'});
	$("#two, #three, #four").css({'z-index' : '1'});
	$('#oneContainer').jScrollPaneRemove();
	if ( $('#oneContainer').width() < 600) {
		$('.menu, .menulist').css({'visibility' : 'hidden'});
		$('#menuA, #menuAlist').css({'visibility' : 'visible'});
		$('#oneContainer').animate({reSizeOne: trgt}, {duration: 500});
		var reinitialize = function() {
	   		$(".text0hidden, .text1hidden").css({'visibility' : 'visible', 'display' : 'block'});
	   		$('#closeR').css({'visibility' : 'visible'});
			$('#oneContainer').jScrollPane();
			$('#oneContainer')[0].scrollTo(trgt);
			$('#oneContainer').jScrollPane({animateTo:true});
	   	}
	} else {
		$(".text0hidden, .text1hidden").css({'visibility' : 'hidden', 'display' : 'none'});
		$('#closeR').css({'visibility' : 'hidden'});
		$('#oneContainer').animate({reSizeOneSmall: trgt}, {duration: 500});
		var reinitialize = function() {
			$('#oneContainer').jScrollPane();
			$('#oneContainer')[0].scrollTo(trgt);
			$('#oneContainer').jScrollPane({animateTo:true});
			$(".jScrollPaneTrack").css({'visibility' : 'hidden'});
			$('.menu, .menulist').css({'visibility' : 'visible'});
	   	}
	}
	window.setTimeout(reinitialize, 501);
}
function reSizeB() {
	
	if ( $('.video_file').length > 0 ){
			$('.video_thumb').show();
			$('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
	}
	
	trgt = "#"+currentID('#twoContainer');
    
	$("#two").css({'z-index' : '50'});
	$("#one, #three, #four").css({'z-index' : '1'});
	$('#twoContainer').jScrollPaneRemove();
	if ( $('#twoContainer').width() < 600) {
		$('.menu, .menulist').css({'visibility' : 'hidden'});
		$('#menuB, #menuBlist').css({'visibility' : 'visible'});
		$('#twoContainer').animate({reSizeTwo: trgt}, {duration: 500});
		var reinitialize = function() {
	   		$(".text0hidden, .text1hidden").css({'visibility' : 'visible', 'display' : 'block'});
	   		$('#closeL').css({'visibility' : 'visible'});
			$('#twoContainer').jScrollPane();
			$('#twoContainer')[0].scrollTo(trgt);
			$('#twoContainer').jScrollPane({animateTo:true});
	   	}
	} else {
		$(".text0hidden, .text1hidden").css({'visibility' : 'hidden', 'display' : 'none'});
		$('#closeL').css({'visibility' : 'hidden'});
		$('#twoContainer').animate({reSizeTwoSmall: trgt}, {duration: 500});
		var reinitialize = function() {
			$('#twoContainer').jScrollPane();
			$('#twoContainer')[0].scrollTo(trgt);
			$('#twoContainer').jScrollPane({animateTo:true});
			$(".jScrollPaneTrack").css({'visibility' : 'hidden'});
			$('.menu, .menulist').css({'visibility' : 'visible'});
	   	}
	}
	window.setTimeout(reinitialize, 501);
}
function reSizeC() {
	
	if ( $('.video_file').length > 0 ){
			$('.video_thumb').show();
			$('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
	}
	
	trgt = "#"+currentID('#threeContainer');
    
	$("#three").css({'z-index' : '50'});
	$("#one, #two, #four").css({'z-index' : '1'});
	$('#threeContainer').jScrollPaneRemove();
	if ( $('#threeContainer').width() < 600) {
		$('.menu, .menulist').css({'visibility' : 'hidden'});
		$('#menuC, #menuClist').css({'visibility' : 'visible'});
		$('#threeContainer').animate({reSizeThree: trgt}, {duration: 500});
		var reinitialize = function() {
	   		$(".text0hidden, .text1hidden").css({'visibility' : 'visible', 'display' : 'block'});
	   		$('#closeR').css({'visibility' : 'visible'});
			$('#threeContainer').jScrollPane();
			$('#threeContainer')[0].scrollTo(trgt);
			$('#threeContainer').jScrollPane({animateTo:true});
	   	}
	} else {
		$(".text0hidden, .text1hidden").css({'visibility' : 'hidden', 'display' : 'none'});
		$('#closeR').css({'visibility' : 'hidden'});
		$('#threeContainer').animate({reSizeThreeSmall: trgt}, {duration: 500});
		var reinitialize = function() {
			$('#threeContainer').jScrollPane();
			$('#threeContainer')[0].scrollTo(trgt);
			$('#threeContainer').jScrollPane({animateTo:true});
			$(".jScrollPaneTrack").css({'visibility' : 'hidden'});
			$('.menu, .menulist').css({'visibility' : 'visible'});
	   	}
	}
	window.setTimeout(reinitialize, 501);
}
function reSizeD() {
	
	if ( $('.video_file').length > 0 ){
			$('.video_thumb').show();
			$('.video_file').removeClass('video_file').addClass('video_file_empty').hide().empty();
	}
	
	trgt = "#"+currentID('#fourContainer');
    
	$("#four").css({'z-index' : '50'});
	$("#one, #two, #three").css({'z-index' : '1'});
	$('#fourContainer').jScrollPaneRemove();
	if ( $('#fourContainer').width() < 600) {
		$('.menu, .menulist').css({'visibility' : 'hidden'});
		$('#menuD, #menuDlist').css({'visibility' : 'visible'});
		$('#fourContainer').animate({reSizeFour: trgt}, {duration: 500});
		var reinitialize = function() {
	   		$(".text0hidden, .text1hidden").css({'visibility' : 'visible', 'display' : 'block'});
	   		$('#closeR').css({'visibility' : 'visible'});
			$('#fourContainer').jScrollPane();
			$('#fourContainer')[0].scrollTo(trgt);
			$('#fourContainer').jScrollPane({animateTo:true});
	   	}
	} else {
		$(".text0hidden, .text1hidden").css({'visibility' : 'hidden', 'display' : 'none'});
		$('#closeR').css({'visibility' : 'hidden'});
		$('#fourContainer').animate({reSizeFourSmall: trgt}, {duration: 500});
		var reinitialize = function() {
			$('#fourContainer').jScrollPane();
			$('#fourContainer')[0].scrollTo(trgt);
			$('#fourContainer').jScrollPane({animateTo:true});
			$(".jScrollPaneTrack").css({'visibility' : 'hidden'});
			$('.menu, .menulist').css({'visibility' : 'visible'});
	   	}
	}
	window.setTimeout(reinitialize, 501);
}
	