var animationActive = false;

jQuery.fn.findMaxItemWidthForList = function(){
	//creating a temporary element to work with
	$(this).after($(this).clone().empty().append(document.createElement("li")).addClass("temptopic").addClass("hidden"));
	
	var maxItemWidth = 0, itemWidth = 0;

	$(this).children("li").each(function(){
		$("ul.temptopic > li").html($(this).html());
		itemWidth = $("ul.temptopic > li").outerWidth();
		if(itemWidth > maxItemWidth)
			maxItemWidth = itemWidth;
	});

	$("ul.temptopic").remove();
	return maxItemWidth;
}

function findColumnNumber(itemNumber, numberOfItems, numberOfColumns)
{
	var maxItemsPerColumn = Math.ceil(numberOfItems/numberOfColumns);
	var numberOfFullColumns = numberOfColumns-(maxItemsPerColumn*numberOfColumns - numberOfItems);
	var numberOfItemsInFullColumns = maxItemsPerColumn*numberOfFullColumns;
	var columnNumber = 0;

	if(itemNumber+1 <= numberOfItemsInFullColumns)
	{
		columnNumber = Math.floor(itemNumber/maxItemsPerColumn);
	}
	else
	{
		var numberOfItemsInHalfColumns = numberOfItems-numberOfItemsInFullColumns;
		var itemNumberInHalfColumns = itemNumber - numberOfItemsInFullColumns;
		columnNumber = numberOfFullColumns + Math.floor(itemNumberInHalfColumns/(maxItemsPerColumn-1));
	}
	return columnNumber;
}

jQuery.fn.getOffset = function(){
	var absTop = 0;
	var absLeft = 0;


	var element = $(this).get(0);
	while( element != null ) {
		absTop += element.offsetTop;
		absLeft += element.offsetLeft;
		element = element.offsetParent;
	}
	return { "y":absTop, "x":absLeft };
}

var debugLog = "";
var startTime = 0;

function startLog(){
	;//startTime = (new Date()).getTime();
}

function writeLog(s){
	;//debugLog += ((new Date()).getTime() - startTime) + " :: " + s + "\n";
}

function printLog(){
	$("<div class=\"log\"></div>").appendTo($("body")).html(debugLog);
}


jQuery.fn.buildColumns = function(animate){
	startLog();
	var numberOfTopics = $(this).size();
	$(this).each(function(topicNumber){
		writeLog("Starting topic");
		var listWidth = $(this).innerWidth();
		writeLog("Starting findMaxItemWidthForList");
		var itemMinWidth = $(this).findMaxItemWidthForList();
		writeLog("Finished findMaxItemWidthForList");
		var itemPadWidth = parseInt($(this).children("li").css("border-left-width")) + parseInt($(this).children("li").css("padding-left")) + parseInt($(this).children("li").css("border-right-width")) + parseInt($(this).children("li").css("padding-right"));
		
		var numberOfColumns = Math.floor(((listWidth / itemMinWidth) > maxColumns) ? maxColumns : (listWidth / itemMinWidth));
	
		var itemRealWidth = Math.round(listWidth/numberOfColumns);
		var itemActiveWidth = Math.round(itemRealWidth - itemPadWidth);
		
		$(this).children("li").css({"width" : parseInt(itemActiveWidth)+"px", "position":"absolute"});
		$(this).attr({"numberOfColumns" : numberOfColumns, "itemActiveWidth":itemActiveWidth, "itemRealWidth": itemRealWidth});

		var numberOfItems = $(this).children("li").size();
		var columnNumber = 0;
		var columnNumberNew = 0;
		var columnHeight = 0;
		var maxColumnHeight = 0;
		var itemLeft = 0;
		var itemTop = 0;

		writeLog("Starting items");
		$(this).children("li").each(function(i){
			writeLog("Item :: Starting");
			writeLog("Item :: Starting findColumnNumber");
			columnNumberNew = findColumnNumber(i, numberOfItems, numberOfColumns);
			$(this).attr("col", columnNumberNew);
			writeLog("Item :: Finished findColumnNumber");
			if(columnNumberNew > columnNumber)
				columnHeight = 0;
			columnNumber = columnNumberNew;
			
			itemLeft = itemRealWidth*columnNumber;
			itemTop = columnHeight;

			if(animate)
			{
				writeLog("Item :: Starting animate");
				animationActive = true;
				$(this).animate({"left": itemLeft + "px", "top":itemTop+"px"}, animationSpeed, "easeInOutSine", function(){
					animationActive = false;
				});
				writeLog("Item :: Finished animate");
			}
			else
			{
				writeLog("Item :: Starting CSS");
				//using oldschool DOM access because it's a little bit faster
				$(this).get(0).style.left = itemLeft + "px";
				$(this).get(0).style.top = itemTop + "px";
				writeLog("Item :: Finished CSS");
			}
			columnHeight += $(this).outerHeight();
			if(columnHeight > maxColumnHeight)
				maxColumnHeight = columnHeight;

			writeLog("Item :: Finished");
		});

		writeLog("Finished items");
		if(animate)
		{
			if(topicNumber+1 == numberOfTopics)
			{
				writeLog("Starting topic animate with setParameters()");
				animationActive = true;
				$(this).animate({"height":maxColumnHeight + "px"}, animationSpeed, "swing", function(){
					$("ul.topic").setParameters(); 
					animationActive = false;
				});
				writeLog("Finished topic animate with setParameters()");
			}
			else
			{
				writeLog("Starting topic animate without setParameters()");
				animationActive = true;
				$(this).animate({"height":maxColumnHeight + "px"}, animationSpeed, "easeInOutSine", function(){
					animationActive = false;
				});
				writeLog("Finished topic animate without setParameters()");
			}
		}
		else
		{
			
			writeLog("Starting topic CSS");
			$(this).css({"height":maxColumnHeight + "px"});
			if(topicNumber+1 == numberOfTopics)
			{
				writeLog("Calling setParameters()");
				$("ul.topic").setParameters();
			}
			writeLog("Finished topic CSS");
		}
		writeLog("Finished topic");
	});
	//printLog();
}

function getWindowWidth() 
{
	return document.getElementById("head-container").offsetWidth;
	/*var myWidth = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth) ) {
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		myWidth = document.body.clientWidth;
	}
	return myWidth;*/
}

function resizePage()
{
	var totalScreenWidth = getWindowWidth();
	var tanA = (1500-800) / (1920-800)
	var contentWidth = Math.round((totalScreenWidth <= 800)?(totalScreenWidth-20):(totalScreenWidth-800)*tanA+800);
	$("#head").css({"width":contentWidth+"px"});
	$("ul.container").css({"width":contentWidth+"px"});
}