$(document).ready( function () {

	//width height settings
	$containerHeight = $('div.entry-content').height();
	$containerWidth = $('div.entry-content').width();
	
	$whoGotClickedLast = '';
	$whoGotClicked = '';
	$kompVisible = false;
	$ihelp = 0;
	
	getMarginWidth();
	
	//back next navigation, check if there is more than 1 entry-content
	if ($('#col3_content div.entry-content').length <= 1) {
		$('#backnext div').hide();
	}
	
	//back next click functions
	$('#next, #arrownext').click(function(){
		
		$('#thePost').animate({
			left: '-='+$movingPixels
		}, 500, function() {
			resetStylings('next');
			checkBackNext();
		});		
	});	
	
	$('#back').click(function(){
		
		$('#thePost').animate({
			left: '+='+$movingPixels
		}, 500, function() {
			resetStylings('back');
			checkBackNext();
		});	
	});
	
	checkBackNext();
	
	$('#headline1').click(function(e) {
		backToTheStart();		
	});
	
	$('#eins #einsa').click(function(e) {
		e.preventDefault();
		$('#eins .addon, #eins .bild').toggle();		
	});
	
	$('#zwei #zweia').click(function(e) {
		e.preventDefault();
		$('#zwei .addon, #zwei .bild').toggle();		
	});
	
	$('#drei #fuenfa').click(function(e) {
		e.preventDefault();
		$('#drei .addon, #drei .bild').toggle();		
	});
	
	$('#vier #viera').click(function(e) {
		e.preventDefault();
		$('#vier .addon, #vier .bild').toggle();		
	});
	
	$('#fuenf #dreia').click(function(e) {
		e.preventDefault();
		$('#fuenf .addon, #fuenf .bild').toggle();		
	});
	
	$('#sechs a').click(function(e) {
		e.preventDefault();
		$('#sechs .addon, #sechs .bild').toggle();		
	});
	
	$('#siebenneu a').click(function(e) {
		e.preventDefault();
		$('#siebenneu .addon, #siebenneu .bild').toggle();		
	});
	
	$('#sieben a, #refe').click(function(e) {
		e.preventDefault();
		$whoGotClickedLast = $whoGotClicked;
		$whoGotClicked = 'referenzen';
		resetStylings();
		
		if ($whoGotClickedLast == 'kontakt') {
			
			$('#kontakt').hide();
			$('#kontakt').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		}
		else if ($whoGotClickedLast == 'impressum') {

			$('#impressum').hide();
			$('#impressum').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));		
		} else if (($whoGotClickedLast == 'kompetenzen1') || ($whoGotClickedLast == 'kompetenzen2')) {
			$('#kompetenzen1, #kompetenzen2').hide();
			$kompVisible = false;
			$('#kompetenzen1, #kompetenzen2').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 2 * ($containerWidth + $marginWidth));
		}
		
		//move to pos 7/8
		$('#thePost').animate({
			left: '-'+$movingPixels*7 - 600
		}, 500, function() {
			checkBackNext();
		});		
		
		$('#refe').addClass('hover');		
		$('#referenzen').css('margin-right',$marginWidth+'px');
		$('#referenzen').show();
		$('#referenzen').addClass('entry-content');
		var aktWidth = $('#col3_content #thePost').width();
		$('#col3_content #thePost').width(aktWidth + $containerWidth + $marginWidth);		
	});
	
	$('#acht #achta').click(function(e) {
		e.preventDefault();
		$('#acht .bild').toggle();		
	});
	
	$('#acht #ah, #komp').click(function(e) {
		e.preventDefault();
		$kompVisible = true;
		$whoGotClickedLast = $whoGotClicked;
		$whoGotClicked = 'kompetenzen1';
		resetStylings();
		
		if ($whoGotClickedLast == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));
		} else if ($whoGotClickedLast == 'kontakt') {
			$('#kontakt').hide();
			$('#kontakt').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		} else if ($whoGotClickedLast == 'impressum') {
			$('#impressum').hide();
			$('#impressum').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		
		} else if ($whoGotClickedLast == 'wir') {
			// hide kontakt
			$('#wir').hide();
			$('#wir').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		}

		//move to pos 8/9
		$('#thePost').animate({
			//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			left: '-'+$movingPixels*8 - 600
		}, 500, function() {
			checkBackNext();
		});		
		
		
		$('#komp').addClass('hover');		
		$('#kompetenzen1, #kompetenzen2').css('margin-right',$marginWidth+'px');
		$('#kompetenzen1, #kompetenzen2').show();
		$('#kompetenzen1, #kompetenzen2').addClass('entry-content');
		var aktWidth = $('#col3_content #thePost').width();
		$('#col3_content #thePost').width(aktWidth + 2 * ($containerWidth + $marginWidth));		
	});
	
	$('#acht #ak, #kont').click(function(e) {
		e.preventDefault();
		$whoGotClickedLast = $whoGotClicked;
		$whoGotClicked = 'kontakt';
		resetStylings();
		
		/*$('#thePost').animate({
			left: '-'+$movingPixels*8 - 600
			//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
		}, 500, function() {
			checkBackNext();
		});*/
		
		if ($whoGotClickedLast == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));
		}
		
		
		if ($whoGotClickedLast == 'impressum') {
			// hide kontakt
			$('#impressum').hide();
			$('#impressum').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
			
		} else if ($whoGotClickedLast == 'wir') {
			// hide kontakt
			$('#wir').hide();
			$('#wir').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		
		} else {
			var i = 8;
			if ($kompVisible) i = 10;
			$('#thePost').animate({
				left: '-'+$movingPixels*i - 600
				//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			}, 500, function() {
				checkBackNext();
			});
		}
		
		$('#kont').addClass('hover');
		$('#kontakt').css('margin-right',$marginWidth+'px');
		$('#kontakt').show();
		$('#kontakt').addClass('entry-content');
		var aktWidth = $('#col3_content #thePost').width();
		$('#col3_content #thePost').width(aktWidth + $containerWidth + $marginWidth);		
	});
	
	$('#impr').click(function(e) {
		e.preventDefault();
		$whoGotClickedLast = $whoGotClicked;
		$whoGotClicked = 'impressum';
		resetStylings();
		
		if ($whoGotClickedLast == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));
		}
		
		if (($whoGotClickedLast == 'kompetenzen1') || ($whoGotClickedLast == 'kompetenzen2')) {
			$('#thePost').animate({
				left: '-'+$movingPixels*10 - 600
				//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			}, 500, function() {
				checkBackNext();
			});
			
		} else if ($whoGotClickedLast == 'kontakt') {
			// hide kontakt
			$('#kontakt').hide();
			$('#kontakt').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		
		} else if ($whoGotClickedLast == 'wir') {
			// hide kontakt
			$('#wir').hide();
			$('#wir').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		
		} else {	
			var i = 8;
			if ($kompVisible) i = 10;
			$('#thePost').animate({
				left: '-'+$movingPixels*i - 600
				//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			}, 500, function() {
				checkBackNext();
			});
		}
		$('#impr').addClass('hover');
		$('#impressum').css('margin-right',$marginWidth+'px');
		$('#impressum').show();
		$('#impressum').addClass('entry-content');
		var aktWidth = $('#col3_content #thePost').width();
		$('#col3_content #thePost').width(aktWidth + $containerWidth + $marginWidth);		
	});
	
	$('#wirtop').click(function(e) {
		e.preventDefault();
		$whoGotClickedLast = $whoGotClicked;
		$whoGotClicked = 'wir';
		resetStylings();
		
		if ($whoGotClickedLast == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));
		}
		
		if ($whoGotClickedLast == 'impressum') {
			// hide kontakt
			$('#impressum').hide();
			$('#impressum').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		}
		
		if (($whoGotClickedLast == 'kompetenzen1') || ($whoGotClickedLast == 'kompetenzen2')) {
			$('#thePost').animate({
				left: '-'+$movingPixels*10 - 600
				//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			}, 500, function() {
				checkBackNext();
			});
			
		} else if ($whoGotClickedLast == 'kontakt') {
			// hide kontakt
			$('#kontakt').hide();
			$('#kontakt').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
		
		} else {	
			var i = 8;
			if ($kompVisible) i = 10;
			$('#thePost').animate({
				left: '-'+$movingPixels*i - 600
				//left: '-'+$movingPixels*($('.entry-content').length-1) - 600
			}, 500, function() {
				checkBackNext();
			});
		}
		$('#wirtop').addClass('hover');
		$('#wir').css('margin-right',$marginWidth+'px');
		$('#wir').show();
		$('#wir').addClass('entry-content');
		var aktWidth = $('#col3_content #thePost').width();
		$('#col3_content #thePost').width(aktWidth + $containerWidth + $marginWidth);		
	});
	
	
});

$(window).resize(function() {
	getMarginWidth();
});

function resetStylings(backnext) {
	if (backnext == 'back') {
	
		if ($whoGotClicked == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($whoGotClicked == 'kompetenzen1') {
			$('#kompetenzen1, #kompetenzen2').hide();
			$kompVisible = false;
			$('#kompetenzen1, #kompetenzen2').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 2 * ($containerWidth + $marginWidth));
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($whoGotClicked == 'kompetenzen2') {
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = 'kompetenzen1';
			
		} else if ($whoGotClicked == 'kontakt') {
			$('#kontakt').hide();
			$('#kontakt').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($whoGotClicked == 'impressum') {
			$('#impressum').hide();
			$('#impressum').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($whoGotClicked == 'wir') {
			$('#wir').hide();
			$('#wir').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - 1 * ($containerWidth + $marginWidth));
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($kompVisible) {
			$ihelp ++;
			if ($ihelp == 2) {
				$('#kompetenzen1, #kompetenzen2').hide();
				$kompVisible = false;
				$ihelp = 0;
				$('#kompetenzen1, #kompetenzen2').removeClass('entry-content');
				
				var aktWidth = $('#col3_content #thePost').width();
				$('#col3_content #thePost').width(aktWidth - 2 * ($containerWidth + $marginWidth));
			}
		}
		
	} else if (backnext == 'next') {
	
		if ($whoGotClicked == 'referenzen') {
			$('#referenzen').hide();
			$('#referenzen').removeClass('entry-content');
			
			var aktWidth = $('#col3_content #thePost').width();
			$('#col3_content #thePost').width(aktWidth - ($containerWidth + $marginWidth));	
			var aktLeft = $('#thePost').position();
			$('#thePost').css('left', aktLeft.left+$movingPixels+'px');
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = '';
			
		} else if ($whoGotClicked == 'kompetenzen1') {
			$whoGotClickedLast = $whoGotClicked;
			$whoGotClicked = 'kompetenzen2';
		}
	
	}
	
	$('.addon, .bild').hide();
	$('#topnav a').removeClass('hover');
	//console.log('last: ' + $whoGotClickedLast + ' now: ' + $whoGotClicked + ' x:' + $('#col3_content div.entry-content').length);
}

function getMarginWidth() {
	//3 containers
	//$marginWidth = ($(window).width() - (2 * $containerWidth + $('#col1').width())) / 2;
	//console.log($containerWidth);
	//$marginWidth = ($(window).width() - (2 * $containerWidth)) / 2;
	
	//2 containers 
	$marginWidth = $(window).width() - ($containerWidth + 330);
	if ($marginWidth < 50) {
		//alert('smaller');
		$marginWidth = 50;
	}
	$('#col3_content div.entry-content:not(".first")').css('margin-right',$marginWidth);
	$('#col3_content div.entry-content.first').css('margin-right',39);
	
	$howMany = $('#col3_content div.entry-content').length;
	$('#col3_content #thePost').width($howMany * ($containerWidth + $marginWidth));
	$movingPixels = $containerWidth + $marginWidth;
	//$('#col3_content #thePost').css('left','0px');
	$('#thePost').css('left','-600px'); //starte versetzt
	var navpadding = $movingPixels - 600 + $('#col1').width(); //nav anpassen
	//$('#nav').css('padding-left',navpadding+'px');	
	
	//position arrow
	//$('#arrownext').css('left', $containerWidth + $marginWidth - 25);
	$('#arrownext').css('width', $(window).width() - (100 + 645));
	
	if(isiPhone()){
		$('#arrownext').css('display', 'none');
		$('div.einszwei').css('display', 'none');
		$('#col3_content div.entry-content:not(".first")').css('margin-right',200);
	}
	
	//setbacknext area
	$('#backnext #next').css('padding-right','600px');
	$('#backnext #back').css('padding-left','600px');
	
	checkBackNext();
}

function isiPhone(){
		var deviceAgent = navigator.userAgent.toLowerCase();
		var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);
		
		return agentID;
}

function checkBackNext() {	
	//alert($('.entry-content').length);
	$howMany = $('#col3_content div.entry-content').length;
	//$('#nav h1').html($howMany);
	var actualPosition = $('#col3_content #thePost').position();
	var actualLeft = actualPosition.left;
	if (actualLeft == '-600') {
		$('#back').hide();
	}
	else if ((actualLeft*(-1)) >= $movingPixels*($howMany-2)) {
		//$('#next').hide();
		backToTheStart()
		//console.log('ENDE');
	} else {
		$('#backnext div').show();
	}
}

function backToTheStart() {
	//resetall addons and then getmarginwidth!
	$('.add-entry-content').each(function(){
		$(this).hide();	
		$(this).removeClass('entry-content');
	});

	$howMany = $('#col3_content div.entry-content').length;
	//console.log($howMany);
	$('#col3_content #thePost').width($howMany * ($containerWidth + $marginWidth));
	
	$whoGotClickedLast = '';
	$whoGotClicked = '';
	$kompVisible = false;
	$ihelp = 0;
	
	getMarginWidth();
}
