$j = jQuery.noConflict();
var randomColorEffekt = {
	colors:[],
	cells:[],
}

$j(document).ready(function() {
	correctHeight();	
	
	$j('#backgroundBottom').css('backgroundPosition', '0 '+($j('#content').height()+180)+'px');
	window.onresize = function() {
		$j('#backgroundRight').css('height', '100%');
		$j('#backgroundBottom').css('height', '100%');
		var docHeight = $j(document).height()-38;
		$j('#backgroundRight').css('height', docHeight+'px');
		$j('#backgroundBottom').css('height', docHeight+'px');
	}
	window.onresize();
	
	$j('.colorDefinition').each(function(i) {
		randomColorEffekt.colors.push($j(this).text());
	});
	
	var skipIndices = [];
	var cols = 8;
	var rows = $j('#startPageDiv table tr').length;
	console.log(rows);
	
	for (var c=0;c<cols;++c) randomColorEffekt.cells[c] = [];
	for (var r=0;r<rows;++r) skipIndices[r] = [];
	
	$j('#startPageDiv table tr').each(function(tr) {
		var colIndex = 0;
		$j('td', this).each(function(td) {
			while(skipIndices[tr][colIndex]) {
				colIndex++;
			}
			
			if (!$j(this).hasClass('blockedCell')) {
				$j(this).attr('rel', tr); 
				randomColorEffekt.cells[colIndex].push(this);
				$j(this).html('<div class="colorBlock"><div class="colorBlockInner newColor"></div><div class="colorBlock oldColor"></div></div>');
			}
			
			var rowSpan = $j(this).attr('rowspan');
			if (rowSpan) {
				for (r=1;r<rowSpan;++r) {
					skipIndices[tr+r][colIndex] = true;
				}
			}
			colIndex++;
		});
	});
	
	setRandomColors();
});

var correctHeight = function() {
	$j('#content > .leftCol').css('margin-bottom', (80)+'px');
	
	$j('.contactBox').each(function() {
		$j(this).css('height', $j(this.parentNode.parentNode).height()+'px');
	});
	$j('.headlineWrap').each(function(i) {
		$j(this).css('height', (Math.round($j(this).height()/27)*27)+'px');
	});
	$j('#content > .rightCol > .headlineWrap').each(function() {
		$j(this).css('marginBottom', ((Math.ceil(($j(this).height()+8)/80)*80)-$j(this).height())+'px');
	});
	$j('.twoColumns, .newsContent').each(function() {
		var top = $j(this).offset().top-20;
		var topDiff = Math.round(top/80)*80-top;
		
		var height = $j(this).height();
		var marginBottom = parseFloat($j(this).css('marginBotton'));
		marginBottom = $j(this).outerHeight(true)-height;
		
		if ($j(this).hasClass('newsContent')) {
			while (topDiff<0) {
				topDiff += 17;
				height += 17;
			}
			$j(this).css('marginTop', topDiff+7+'px');
			topDiff = -7;
		}
		
		height += marginBottom<30 ? 30-marginBottom : 0;
		var newHeight = Math.ceil((height+marginBottom-topDiff)/80)*80-marginBottom+topDiff;
		console.log(marginBottom);
		$j(this).css('height', newHeight+'px');
	});
	
	
	$j('.contactBox table').css('position', 'absolute');
	$j('.contactBox table').css('bottom', '0px');
}

var firstRound = true;
var setRandomColors = function() {
	var cells = [];
	var resultCells = [];
	var blockedRows = [];
	
	for (var td=0;td<randomColorEffekt.cells.length;++td) {
		blockedRows[td] = [];
		resultCells[td] = [];
	}
	for (td=0;td<randomColorEffekt.cells.length;++td) {
		cells[td] = [];
		for (var c=0;c<randomColorEffekt.cells[td].length;++c) {
			cells[td][c] = randomColorEffekt.cells[td][c];		
		}
		while(cells[td].length) {
			var randomIndex = Math.floor(Math.random()*(cells[td].length));
			var spliceIndex = Math.max(0, randomIndex-2);
			var randomCell = cells[td][randomIndex];
			var theRow = $j(randomCell).attr('rel');
			
			var blocked = false;
			for (var b=0;b<blockedRows[td].length;++b) {
				blocked = blocked || blockedRows[td][b] == theRow;
			}
			
			if (!blocked) {;
				cells[td].splice(spliceIndex, 5);
				if (td<blockedRows.length-1) blockedRows[td+1].push(theRow, theRow-1, theRow+1);
				if (td<blockedRows.length-2) blockedRows[td+2].push(theRow);
				if (!$j(randomCell).hasClass('tempBlocked')) resultCells[td].push(randomCell);
			} else {
				cells[td].splice(randomIndex, 1);
			}
		}
	}
	
	var index = 0;
	for (r=0;r<resultCells.length;++r) {
		for (c=0;c<resultCells[r].length;c++) {
			$j(resultCells[r][c]).addClass('tempBlocked');
			$j('.newColor', resultCells[r][c]).css('backgroundColor', randomColorEffekt.colors[Math.floor(Math.random()*(randomColorEffekt.colors.length))]);
			$j('.newColor', resultCells[r][c]).fadeOut(0);
			$j('.newColor', resultCells[r][c]).delay(index*50).fadeIn(6000, resetColor);
			++index;
		}
	}
	setTimeout('setRandomColors()', firstRound ? 2000 : 3000);
	firstRound = false;
}


var resetColor = function(e) {
	$j('.oldColor', this.parentNode).css('backgroundColor', $j(this).css('backgroundColor'));
	$j('.oldColor', this.parentNode).fadeOut(6000, resetOldColor);
	$j(this).css('backgroundColor', 'transparent');
	$j(this).fadeOut(0);
}

var resetOldColor = function(e) {
	$j(this.parentNode.parentNode).removeClass('tempBlocked');
	$j(this).css('backgroundColor', 'transparent');
	$j(this).fadeIn(0);
}
