var columnWidth = 330;
var minColumnHeight = 200;
var maxColumnHeight = 667;
var footerHeight = 80;
var hc = 0;

function calcSize() {
	if (typeof (window.innerWidth) == 'number') {
		// Non-IE
		hc = window.innerHeight;
	} else if (document.documentElement	&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		hc = document.documentElement.clientHeight;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		hc = document.body.clientHeight;
	}
	var minHeight = 0;
	$('#sidebar').find('div').each(function() {
		minHeight += $(this).outerHeight();
	});
	hc = Math.max(Math.max(minHeight, minColumnHeight), hc - $('#container').offset().top - footerHeight);
	hc = Math.min(maxColumnHeight, hc);
};

function setupContent() {
	$("#container").css("height", hc + "px");
	// Hangs for cforms :/
	//$("form").addClass('dontsplit');
	$(".content_org form").find("li").addClass('dontsplit');
	$(".content_org form").find("img, object, embed, iframe").addClass('dontscale');
	$(".content_org").find("img, object, embed, iframe").each(function() {
		if (!$(this).hasClass("dontscale")) {
			var w = $(this).width() || this.width || $(this).get(0).getAttribute('width');
			var h = $(this).height() || this.height || $(this).get(0).getAttribute('height');
			var r = hc / h;
			$(this).width(Math.round(w * r));
			$(this).height(hc);
		}
	});
  /*
  $("form").each(function(i) {
    if (!$(this).id)
      $(this).id = "form_" + i;
  });
  */
}

function columnize() {
	$(".content").children().remove();
	$(".content_org").each(function(i) {
		$(this).columnize({
			buildOnce: true,
			width: columnWidth,
			// doneFunc: restructure,
			ignoreImageLoading: true,
			lastNeverTallest: false,
			height: hc,
			target : $(".content")[i]
		});
	});
}

function restructure() {
	$(".content").each(function() {
		var forms = $(this).find("form");
		if (forms.length > 1) {
			var form = $(forms).get(0).cloneNode(false);
			$(forms).each(function() {
				$(this).children().insertBefore(this);
				$(this).remove();
			});
			$(this).children().appendTo(form);
			$(this).prepend(form);
		}
	});
	
	
	$(".content").each(function() {
		// remove br
		$(this).find("br:last").remove();

		// remove empty columns
		$(this).find(".column").each(function() {
      if ($(this).find("img, object, embed, iframe").length < 1 && $(this).text().match(/^(&nbsp;|\s)*$/)) {
        $(this).remove();
      }
		});
		// re-width
		$(this).css("width", "auto");
	});
  
	// resize img columns to content
	$(".content").find("img, object, embed, iframe").each(function() {
		if (!$(this).hasClass('dontscale')) {
      //var me = this;
      var parent = $(this).parents(".column");
			parent.width($(this).width());
      parent.find('p').each(function() {
        if (jQuery.trim($(this).html()) == "") {
          $(this).remove();
        }
      });
      // remove a tags surrounding images
      $(this).parent('a').each(function() {
        $(this).children().insertBefore(this);
        $(this).remove();
      });
    }
	});
	// expand page
	$("body, #container, html").width(1048576);
	
	$('#backLink').hide();

	// page Width
	var w = $("#tracer").offset().left;
	
	var wc = 0;
	if (typeof (window.innerWidth) == 'number') {
		// Non-IE
		wc = window.innerWidth;
	} else if (document.documentElement	&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		wc = document.documentElement.clientWidth;
	} else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		wc = document.body.clientWidth;
	}
	
	//console.log(w + '\t' + wc);
	if (w > wc) {
		$('#backLink').show();
		// page Width
		w = $("#tracer").offset().left;
	}
	
	//console.log(w);
	var minPageWidth = $('#menubar').offset().left + $('#menubar').outerWidth(); 
	w = Math.max(w, minPageWidth);
	$("body, #container, html").width(w);
}

$(document).ready(function() {
	$('#backLink').click(function() {
		$.scrollTo( 0, 500 );
		return false;
	});
	//console.log('ready');
	calcSize();

	// resize images
	setupContent();

	// columnize
	columnize();

	// restructure
	restructure();

	// fade in
	$("#container").animate({
		"opacity": 1
	});

	$("#cover").fadeOut();
});

/*
$(window).load(function() {
  //console.log('load');
});
*/

$(window).resize(function() {
  //console.log('resize');

	var oldHc = hc;
	calcSize();
	if (hc == oldHc) return;
	
	$("#container").css('opacity', 0);
	
	// resize images
	setupContent();
	// columnize
	columnize();
	// restructure
	restructure();

	$("#container").animate({
		"opacity": 1
	});
});

