var gridManager = {

	columns: 	new Array(),
	elements: 	new Array(),
	marginChanges: {},

	getColumnWidth: function() {
		return 240;
	},

	// quick way to create a function that calls
	// fixPositions from a disignated start point
	fixFromThis: function(startPoint) {
		return function() {
			gridManager.fixPositions(startPoint);
		};
	},

	_fixBottomOverlap: function(start) {

		$$('div.element').each(function(element) {
			if (element.up('div.column').getWidth() >= element.getWidth()) return false;

			// we''ve got bottom overlapping going on as well
			$$('div.element').each(function(el) {

				if (el == element) return false;
				//if (el.id != 'element_290') return false;

				var widePos        = Position.cumulativeOffset(element);
				var wideTopLeftY= widePos[1];

				var pos            = Position.cumulativeOffset(el);
				var bottomLeftY    = pos[1] + el.getHeight();
				var bottomLeftX    = pos[0];  

				if (Position.within(element, bottomLeftX, bottomLeftY)) {

				var change         = parseInt(bottomLeftY - wideTopLeftY) + 5;
				var currentMargin  = parseInt(element.style.marginTop.replace(/px/, ''));
				element.style.marginTop = (currentMargin + change) + 'px';

				}
			});
		});
   },

	fixPositions: function(start) {

		var cinel_layout 	= $$('div.cinel_layout').shift();

		// Move ColB down (and for long titles ColC and ColD) if Subheader is shown
		if (cinel_layout && $$('div.subheader').length == 1) {

			// Margin top for cinek_layout is calculated there

			var cinelLayoutTop	= Position.cumulativeOffset(cinel_layout)[1];	
			var margin 			= cinel_layout.getStyle('margin-top').replace(/px/, '') * 1;
			var subHeader		= $$('div.subheader').shift();
			var subheaderBottom	= Position.cumulativeOffset(subHeader)[1] + subHeader.getHeight() + 10;
		

			// Adjust the margin top for educatie only
			if ($$('div.pagina_kop_educatie').length == 1) {
				var subheaderBottom	= Position.cumulativeOffset(subHeader)[1] + subHeader.getHeight() + 40;
			}
			
			if (cinelLayoutTop < subheaderBottom) cinel_layout.style.marginTop = (subheaderBottom - cinelLayoutTop + margin) + 'px';
			
			
		}

		// move ColA down if top-left submenu is shown and overlaps
		if ($$('div.left_menu').length == 1) {

			try {
				var colA 			= start ? $(start).getElementsBySelector('div.ColumnA').shift() : $$('div.ColumnA').shift();
				var colATop			= Position.cumulativeOffset(colA)[1];
				var leftMenu		= $$('div.left_menu').shift();
				var leftMenuBottom	= Position.cumulativeOffset(leftMenu)[1] + leftMenu.getHeight() + 10;
				var margin 			= colA.getStyle('margin-top').replace(/px/, '') * 1;
				
			
				if (colATop < leftMenuBottom) colA.style.marginTop = (leftMenuBottom - colATop + margin) + 'px';
			} catch(e) {

			}
		}
		
		var columns 	= start ? $(start).getElementsBySelector('div.column') : $$('div.column');
		var elements	= start ? $(start).getElementsBySelector('div.column div.element') : $$('div.column div.element');

		// if there are no columns, there's nothing for us to do here
		if (columns.length == 0) return false;

		// clear all margins first
		elements.each(function(el) {
			el.style.marginTop = '0px';
		});


		// go through the columns
		columns.each(function(col) {

			// fix the height of the columns
			if ($(col.parentNode).getHeight() < col.getHeight()) $(col.parentNode).style.height = (col.getHeight() + 40) + 'px';

			// go through all elements in this column
			col.getElementsBySelector('div.element').each(function(element) {

				// keep track of changes
				gridManager.marginChanges[element.id] = {};
				changeList = gridManager.marginChanges[element.id];
				
				var withinCheckElement	= element;
				var elWidth 			= element.getWidth();
				var imgReps 			= element.getElementsBySelector('div.rep img');

				if (imgReps.length > 0) {

					withinCheckElement	= imgReps.shift();
					elWidth 			= withinCheckElement.getWidth();
					//element = imgReps.shift();
				}

				/*
				// if the element fits, no worries mate
				if (element.id == 'element_1186') {
					alert(elWidth + ' <= ' + col.getWidth());
				}
				*/
				var colWidth = col.getStyle('width').replace(/px/, '') * 1;
				if (elWidth <= colWidth) return true;
				//alert('Check: ' + element.inspect() + ': ' + element.getStyle('width'));

				// but if it doesn\'t, what\'s next
				var colIndex	= columns.indexOf(col);

				// get basic information about this element
				var height 	= element.getHeight();
				var right	= Position.cumulativeOffset(element)[0] + elWidth;
				var bottom	= Position.cumulativeOffset(element)[1] + height;

				for(i = 1; columns[colIndex + i] != undefined; i++) {

					var nextSib = columns[colIndex + i];
					var sibPos	= Position.cumulativeOffset(nextSib);

					// if the element doesn\'t go into this sibling, no more worries
					if (sibPos[0] >= right) {
						return true;
					}

					nextSib.getElementsBySelector('div.element').each(function(el) {

						var pos 	= Position.cumulativeOffset(el);
						if (Position.within(withinCheckElement, pos[0], pos[1]+(height/2))) {

							changeList[el.id]	= change;
							var change 			= bottom - pos[1];
							var currentMargin	= el.style.marginTop.replace(/px/, '')*1;
							el.style.marginTop 	= (currentMargin + change) + 'px';

						} else {
							//alert('no overlap: ' + el.inspect());
						}

					}.bind(this));
				};

				// we\'ve got bottom overlapping going on as well
				$$('div.element').each(function(el) {

					var widePos        = Position.cumulativeOffset(element);
					var wideTopLeftY= widePos[1];    

					var pos            = Position.cumulativeOffset(el);
					var bottomLeftY    = pos[1] + el.getHeight();
					var bottomLeftX    = pos[0];

					if (Position.within(element, bottomLeftX, bottomLeftY)) {

						var change         = parseInt(bottomLeftY - wideTopLeftY);
						var currentMargin  = parseInt(element.style.marginTop.replace(/px/, ''));
						element.style.marginTop = (currentMargin + change) + 'px';
						Debug.write("Moving " +element.inspect()+ " down by: " + change + ", to: " + element.style.marginTop + " because of overlap with " + el.inspect());

					}
				});
				/****/

			}.bind(this));
		
			//alert(pos);
		}.bind(this));
	
		this._fixBottomOverlap(start);

		// [Loek] Deze doet het nog niet helemaal goed ...
		var footer = start ? $(start).getElementsBySelector('div.page_footer').shift() : $$('div.page_footer').shift();
		if (footer) {
			//footer.style.top = ($$('div.cinel_layout').shift().getHeight() + 400) + 'px';
		}
	}

};

