	

$(document).ready(function(){


	$('.rock').ceebox({html:false});
	$('.project').ceebox({html:false});
	$('.post').ceebox({html:false});
	
	$('#microblog').ceebox({html:false});

	if(!currentKolumn){
		var currentKolumn = parseInt($("#cat").text());
	}

	var postid = parseInt($("#post-id").text());

	remMessage();
	setMessage('Processing');
	
	var numberOfKolumns = 0;

	$('.kolumn').addClass(function() {
	  return 'item-' + $(this).index();
	});
	$('.item-'+currentKolumn).addClass('active');
	
	
	
	$('#menu > div > ul > li').addClass(function() {
			  return 'menu-' + ($(this).index() + 2);
	});
	$('.menu-'+currentKolumn).addClass('selected');
	
	
	
	
	
	
	var offset = $('.active').offset();
		
	$('.kolumn').animate({left: '+=' + ((f_clientWidth() - $('.active').outerWidth(true) ) / 2 - offset.left) } , 1000 );
	$('.active').animate({opacity: 1}, 500);
	
	updateY();
	updateMenu();
	scrollIt();
	
	$('#goleft').click(function () { 
		nextKolumn(-1); 
	});
	
	$('#goright').click(function () { 
		nextKolumn(1); 
	});


	$('.menu-2').click(function () { 
		nextKolumn(2); 
	});
	$('.menu-3').click(function () { 
		nextKolumn(3); 
	});
	$('.menu-4').click(function () { 
		nextKolumn(4); 
	});
	$('.menu-5').click(function () { 
		nextKolumn(5); 
	});
	


	$(window).scroll(function() {
		scrollIt(0);
	});

	remMessage();	



	$(window).resize(function() {
		var offset = $('.active').offset();
		$('.kolumn').animate({left: '+=' + ((f_clientWidth() - $('.active').outerWidth(true) ) / 2 - offset.left) } , 1000 );
		nextKolumn(currentKolumn);
	});		
	
	$(window).keydown(function(event) {
	  if (event.keyCode == '37' || event.keyCode == '39') {
	     event.preventDefault();
	   }
	});
	
	
	

	function scrollIt(animate) {
		
		$('.kolumn').each(function(index) {
			if (index != (currentKolumn-2)) {;
				$('> .block',this).each(function(index) {
					var offset = $(this).offset();
					
					//var newoffset = offset.left + (offset.top - (f_scrollTop() + 40) );
					//$(this).offset({ left: newoffset });
					
					var speed = index * 0.7;
					
					var newoffset = speed * (offset.top - (f_scrollTop() + 40));
					
					if(animate){
						$(this).animate({ marginLeft: newoffset, marginRight: -newoffset }, 500);
					}
					else {
						$(this).css('marginLeft', function(index) {
							return newoffset + 'px';
						});
						$(this).css('marginRight', function(index) {
							return -newoffset + 'px';
						});
					}
				});
			}
			else {
				$('> .block',this).each(function(index) {
					
					if(animate){
						$(this).animate({ marginLeft: 0, marginRight: 0 }, 500);
					}
					else {
						$(this).css('marginLeft', function(index) {
							return 0 + 'px';
						});
						$(this).css('marginRight', function(index) {
							return 0 + 'px';
						});
					}
				});
			}
		});
	}
	

	function nextKolumn(direction)
	{
		if (direction < 2 ) {
			if (currentKolumn + direction < 2 || currentKolumn + direction > 5) {
				return;				
			}

			var offSet = direction*($('.item-'+currentKolumn).width() + $('.item-'+ (currentKolumn+direction) ).width())/2;
		}
		else {
			var start = $('.item-'+currentKolumn).offset();
				
			var end = $('.item-'+direction).offset(); 
				
			var offSet = end.left - start.left;
			
			direction = direction-currentKolumn;
		}
		
		if (direction != 0) {
			$('.active').animate({opacity: 0.2}, 500);
			$('.kolumn').animate({left: '-='+offSet}, 500);


			$('.item-'+currentKolumn).toggleClass('active');
			$('.menu-'+currentKolumn).toggleClass('selected');

			currentKolumn += direction;
			$('.item-'+currentKolumn).toggleClass('active');
			$('.menu-'+currentKolumn).toggleClass('selected');

			$('.active').animate({opacity: 1}, 500);
		}
		updateMenu();
		
		updateY();
		scrollIt(1);
	}
	
	function updateY() {
		var solidHeight = $('#solid').outerHeight(true);
		var projectHeight = $('#projects').outerHeight(true);
		var blogHeight = $('#blog').outerHeight(true);
		var twitterHeight = $('#twitter').outerHeight(true);	
		var microHeight = $('#microblog').outerHeight(true);
		var theHeight = $('.active').outerHeight(true);			
		
		var addedMargin = (theHeight - solidHeight) / ($('.rock').length -1) -1;

		$('.rock').each(function(index) {
			if (index > 0) {				
				var offset1 = $('.rock:eq(' + (index-1) +') ').offset();
				var offset2 = offset1.top + $('.rock:eq('+(index-1)+ ')').outerHeight(true) + addedMargin;
				$(this).offset({ top: offset2 }); 
				//$(this).animate({ marginTop : addedMargin }, 500);
			}
		});
		
		addedMargin = (theHeight - projectHeight) / ($('.project').length -1) -1;
		
		$('.project').each(function(index) {
			if (index > 0) {
				var offset1 = $('.project:eq(' + (index-1) +') ').offset();
				var offset2 = offset1.top + $('.project:eq('+(index-1)+ ')').outerHeight(true) + addedMargin;
				$(this).offset({ top: offset2 }); 
				//$(this).animate({ marginTop : addedMargin }, 500);
			}
		});
		


		addedMargin = (theHeight - blogHeight)  / ( $('.post').length -1 ) -1;		

		$('.post').each(function(index) {
			if (index > 0) {
				var offset1 = $('.post:eq(' + (index-1) +') ').offset();
				var offset2 = offset1.top + $('.post:eq('+(index-1)+ ')').outerHeight(true) + addedMargin;
				$(this).offset({ top: offset2 }); 
				//$(this).animate({ top : offset2 }, 500);
			}						
		});
		

		addedMargin = (theHeight - microHeight)  / ( $('.micropost').length -1 ) -1;		

		$('.micropost').each(function(index) {
			if (index > 0) {
				var offset1 = $('.micropost:eq(' + (index-1) +') ').offset();
				var offset2 = offset1.top + $('.micropost:eq('+(index-1)+ ')').outerHeight(true) + addedMargin;
				$(this).offset({ top: offset2 }); 
				//$(this).animate({ top : addedMargin }, 500);
			}						
		});
		
		
	}
	
	function updateMenu() {
		//$('.selected').animate({ fontSize : "36px" }, 1000);
		var offset3 = $(".selected").offset();
		var n = offset3.left + ( $('.selected').outerWidth(true) /2 );
		var m = f_clientWidth() / 2 - n;
		$('#menu > div').animate({ left:'+='+m }, 1000, function() {
			if (postid > 0){
				var offset4 = $("#"+postid).offset();
				$("html").scrollTop(offset4.top);
				postid = 0;
			}		
		});		
	}
	
	function setMessage(message) {
		$('<div id="message"><h1>' + message + '</h1></div>').appendTo('#messages');
	}

	function remMessage() {
		$('div').remove('#message');	
	}
	
	//http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html

	function f_clientWidth() {
		return f_filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);
	}
	
	function f_scrollTop() {
		return f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}

	function f_filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}

});
