/**
 * Grid-A-Licious(tm)
 * Copyright (c) 2008 Suprb - info(at)suprb(dot)com
 *
 * License Agreement: By downloading Grid-A-Licious(tm),
 * you agree to the following: The copyright information 
 * must remain intact in the product.
 *
 * The product may be used for personal use only, no 
 * commercial projects. You are not free to remove the 
 * copyright information (anywhere).
 * 
 * You are not free to use or copy any of the 
 * "grid-a-licious.js" (this file) code on your own products
 * without asking for permission.
 * 
 * Thanks for understanding.
 */

	var MIN_COLS = 2;
	var COL_WIDTH = 220;
	var GAP = 10; 
	
	var offx, offy = 0;
	maxy = new Array();
	
	function randomBg() {
	var hex=new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
	var bg="#";
	for (var i=0; i < 6; i++) bg+=hex[Math.floor(Math.random()*hex.length)];
	return bg;
	}
	
	// on site load (DOM READY)
	$(function() { 
		handleFocus();
		offy = $('#allposts').offset().top;
		offx = $('#allposts').offset().left;
		arrange();		
	//	$('#sidebar ul .eachpost').each(function(i){$(this).css('background-color', randomBg())});
	//	$('body').css('background-color', randomBg());
	});
	
	// on window resize, call again
	$(window).resize( function() { arrange(); } );
	
	//arrange();
	var loadingText = 'Laddar';
	var newText = '';
	var loopCnt = 0;
	var loaded = false;
	loopLoader();
	
	
	function loopLoader (){
		if(!loaded){
			window.setTimeout(function() {
			newText+='.';
			 $('.loading').text(loadingText+newText);
			loopCnt++;
			if (loopCnt >= 4) {
				$('.loading').text(loadingText);
				newText = '';
				loopCnt = 0;
				};
			loopLoader();
			}, 400);
		}
	}
	
	
	function arrange() {
	
		// how many columns fits here?
		var columns = Math.max(MIN_COLS, parseInt($('body').innerWidth() / (COL_WIDTH+GAP)));
		$('.eachpost').css('width',COL_WIDTH  + 'px');
		$('.twocols').css('width', COL_WIDTH*2 + GAP  );
		$('.threecols').css('width', COL_WIDTH*3 + GAP*2);

		for (x=0; x < columns; x++) {
			maxy[x] = 0;
		}
		
		// lets iterate over all posts
		$('.eachpost').each(function(i) {

			var pos, cursor, w , altura= 0;
	
			w = (Math.floor($(this).outerWidth() / COL_WIDTH));
			cursor = 0;

			if (w>1) {
				for (x=0; x < columns-(w-1); x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}
				pos = cursor;
				
				for (var x=0; x<w; x++) {
					altura = Math.max(altura, maxy[pos+x]);
				}
				for (var x=0; x<w; x++) 
					maxy[pos+x] = parseInt($(this).outerHeight()) + GAP + altura;
					
				$(this).css('left', pos*(COL_WIDTH+GAP) + offx).css('top',altura + offy);
			}
			else {
			
				for (x=0; x < columns; x++) {
					cursor = maxy[x] < maxy[cursor] ? x : cursor;
				}

				$(this).css('left', cursor*(COL_WIDTH+GAP) + offx).css('top',maxy[cursor] + offy);
				maxy[cursor] += $(this).outerHeight() + GAP;
			}
			//$(this).css('visibility', 'visible');
			$(this).css('margin-left', '0px');
			/*
			$(this).css('-webkit-transform', 'rotate('+Math.round(Math.random()*4-2)+'deg)');
			$(this).css('-moz-transform', 'rotate('+Math.round(Math.random()*4-2)+'deg)');
			*/
			
		});
		loaded = true;
		$('.loading').css('visibility', 'hidden');
		
	}
	
	// comments focus handling
	function handleFocus(){
		var authorText = 'Namn';
		var emailText = 'Mail (obligatoriskt)';
		var urlText = 'URL';
		$("#author").focus(function () { 
			if ($(this).val() == authorText) {$(this).val('')}; 
		});
		$("#email").focus(function () { 
			if ($(this).val() == emailText) {$(this).val('')}; 
		});
		$("#url").focus(function () { 
			if ($(this).val() == urlText) {$(this).val('')}; 
		});

		$("#author").blur(function () { 
			if ($(this).val() == '') {$(this).val(authorText)}; 
		});
		$("#email").blur(function () { 
			if ($(this).val() == '') {$(this).val(emailText)}; 
		});
		$("#url").blur(function () { 
			if ($(this).val() == '') {$(this).val(urlText)}; 
		});
	}
	
