$(document).ready(function() 
{	
	var transTime = 500;
	var bgColor = '#d5d5d5';
	
	$("#topNav").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$("#topNav").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: '#111',
        color: '#fff'
      }, transTime );
    });
	
	$(".home_post").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".home_post").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$("#sidebar").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$("#sidebar").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$(".archive_header").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".archive_header").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$("#postnavigation").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$("#postnavigation").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$(".postnavigation").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".postnavigation").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$(".page_content").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".page_content").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$(".post").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
	  
	  $(this).find('#postmeta').stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".post").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
	  
	  $(this).find('#postmeta').stop().animate({ 
        backgroundColor: '#ccc',
        color: '#111'
      }, transTime );
    });
	
	$(".search_result").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$(".search_result").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
	
	$("#socialStatik").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$("#socialStatik").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
    
    $("#notFound").mouseenter(function(){
	  var color = getRandomColorScheme();							   
      $(this).stop().animate({ 
        backgroundColor: color.bg,
        color: color.fg
      }, transTime );
    });
	
	$("#notFound").mouseleave(function(){
      $(this).stop().animate({ 
        backgroundColor: bgColor,
        color: '#111'
      }, transTime );
    });
    
    
    /*$(".home_post").each(function(){
    	var color = getRandomColorScheme();
    	$(this).css({"backgroundColor":color.bg, "color":color.fg});
    })*/
    
    $(window).resize( function() { positionPosts(); } );
	
	positionPosts();
});

$(window).load(function(){
	positionPosts();
});

function getRandomColorScheme()
{
	var colors = [{'bg':'#7d0080', 'fg':'#fb00ff'}, {'bg':'#ff1248', 'fg':'#ffb8c4'}, {'bg':'#1b76ff', 'fg':'#0eeaff'}, {'bg':'#179400', 'fg':'#3eff19'}, {'bg':'#fff401', 'fg':'#5c5800'}];
	var ran = Math.floor(Math.random() * colors.length);
	return colors[ran];
}

function positionPosts()
{	
	var xPos = 40;
	var yPos = 80;
	var spacing = 5;
	var w = 330 + spacing;
	var count = 0;
	var cols = Math.floor($('body').innerWidth() / w);
	var heights = [];
	var postsExist = false;
	
	//console.log("cols: "+cols);
	
	for(var i = 0; i < cols; i++){
		heights[i] = yPos;
	}
	
	$('.home_post').each(function(i) {
		$(this).css({"left":xPos, "top":heights[count]});
		heights[count] += $(this).outerHeight() + spacing; 
		//xPos += w;
		xPos += ($(this).outerWidth() + spacing); 
		count++;
		if(count == cols){
			count = 0;
			xPos = 40;
		}
	});
	
	$('.post').each(function(i) {
		$(this).css({"left":xPos, "top":heights[count]});
		heights[count] += $(this).outerHeight() + spacing; 
		//xPos += w;
		xPos += ($(this).outerWidth() + spacing); 
		count++;
		if(count == cols){
			count = 0;
			xPos = 40;
		}
		postsExist = true;
	});
	
	$('.page_content').each(function(i) {
		$(this).css({"left":xPos, "top":heights[count]});
		heights[count] += $(this).outerHeight() + spacing; 
		//xPos += w;
		xPos += $(this).outerWidth(); 
		count++;
		if(count == cols){
			count = 0;
			xPos = 40;
		}
	});
	
	if($('#postnavigation')){
		$('#postnavigation').css({"left":xPos, "top":heights[count]});
		heights[count] += $('#postnavigation').outerHeight() + spacing; 
		xPos += ($('#postnavigation').outerWidth() + spacing);
		count++;
		if(count == cols){
			count = 0;
			xPos = 40;
		}
	}
	
	if($('.search_result')){
		xPos += $('.search_result').outerWidth();
	}
	
	if($('#notFound')){
		xPos += $('#notFound').outerWidth();
	}
	
	if(!postsExist){
		$('#sidebar').css({"left":xPos, "top":heights[count]});
	}else{
		$('#sidebar').css({"left":635, "top":80});
	}
	heights[count] += $('#sidebar').outerHeight() + spacing;
	xPos += ($('#sidebar').outerWidth() + spacing);
	count++;
	if(count == cols){
		count = 0;
		xPos = 40;
	}
	
	$('#socialStatik').css({"left":xPos, "top":heights[count]});
	
	// bottom spacing
	var bodyHeight = 0;
	$('div').each(function(i){
		var h = $(this).offset().top + $(this).height();
		if(h > bodyHeight){
			bodyHeight = h;
		}
	});
	
	$('body').height(bodyHeight);
}