(function(){
	var $ = document.id;
	
	//General useful stuff
	Element.implement({
		wrapsContent: function(el) {
			this.adopt(document.id(el, true).childNodes).inject(document.id(el, true));
			return this;
		}
	});

	//Pink Floyd
	Mansonry = new Class({
		//Define Defaults
		Implements: Options,
		options: {
			bricks: '.article',
			wall: new Element('div', {
				'class': 'mansonry clear'
			}),
			maxCols: 100,
			minColWidth: 320,
			colMargin: 50
		},
		element: {},
		wallSize: 0,
		cols: [],
		bricks: [],
		colCount: 0,
		initialize: function(element, options) {
			this.setOptions(options);
			this.bricks = $(element).getChildren(this.options.bricks);
			this.element = $(element);
			
			//Don't use more columns than bricks
			if (this.options.maxCols > this.bricks.length) {
				this.options.maxCols = this.bricks.length;
			};
			
			//Keep container unique
			if ( !this.element.hasChild(this.options.wall) ) {
				this.options.wall = this.options.wall.clone();
				this.options.wall.inject(this.element);
			};
			
			this.options.wall.adopt(this.bricks);

			//Make that wall
			this.build();
		},
		build: function() {
			this.wallSize = this.options.wall.getSize().x;

			//Check how many columns we need
			for (var i = this.options.maxCols - 1; i >= 0; i--) {
				colW = Math.floor((this.wallSize - (i * 25)) / (i + 1));
				if (colW < this.options.minColWidth) {
					continue;
				};
				this.colCount = i + 1;
				break;
			};

			if(this.colCount > 1 || $defined($('respond'))){
				//Reset columns
				if(this.cols.length != 0){
					this.bricks.each(function(brick){
						brick.inject(this.options.wall);
					}.bind(this));
					this.cols.each(function(col){
						col.destroy();
					});
				}

				//Create columns
				for (k = 0; k < this.colCount; k++) {
					this.cols[k] = new Element('div',{
						'class': 'col'
					}).inject(this.options.wall,'bottom');
				}

				//Make that wall, this time for real.
				this.lay();
			
			}
		},
		lay: function() {
			//Size columns	
			var colWidth = Math.floor((this.wallSize - this.options.colMargin * (this.colCount-1)) / this.colCount);
			var colHeight = [];

			//Setup columns
			for (l = 0; l < this.colCount; l++) {
				colHeight[l] = 0;
				this.cols[l].setStyles({
					'width': colWidth,
					'margin-right': ( l < this.colCount-1 ) ? this.options.colMargin : 0
				});
			}

			//Here come the bricks
			this.bricks.each(function(brick, c) {
				//Decide where to lay the next brick
				for (var n = this.colCount - 1; n > -1; n--) {
					if (colHeight[n] == colHeight.min()) {
						var thisCol = n;
					}
				};

				//Setup bricks
				brick.inject( this.cols[thisCol] );

				//Add height
				colHeight[thisCol] += brick.getSize().y;
			}.bind(this));
		}
	});

	var days = [];
	var walls = [];

	window.addEvent('domready', function() {
		//THIS IS THE EVIL FRAME BREAKER
		//if (top.location != location) {
		//	top.location.href = window.location.href ;
		//}
		
		//Prepare walls on page load
		days = $('content').getElements('div.wall');
		if(  days.length > 0 ){
			days.each(function(day,i){
				walls[i] = new Mansonry(day);
			});
		}		

		//Prepare backgrounds
		var body = $(document.body);
		var time = 3000;
		var site2 = new Element('div',{
			'id': 'site2'
		}).inject($('site'), 'top');
		var magic = new Fx.Morph(site2, {
			duration: time,
			link: 'chain',
			transition: Fx.Transitions.Sine
		})

		$('site').setStyle('background','transparent');
		
		var c = 0;
		x = 0;
		y = x;
		b = true;
		
		//Add that fancy background stuff
		var interference = function(){
			if( b ){
				x += 50;
				y = x;
				if ( x == 100 ){
					b = false;
				}
			}else{
				x -= 50;
				y = x;
				if ( x == 0 ){
					b = true;
				}
			}
			
			var tx = Math.floor(-.8*x);
			var ty = Math.floor(-.8*y-10);
			
			magic.resume().start({right: tx, top: ty});
			c++;
		}
		interference();
		var run = interference.periodical(time-50);
		
		$('content').addEvents({
			'mouseenter': function(){
				$clear(run);
				magic.pause()
			},
			'mouseleave': function(){
				interference();
				run = interference.periodical(time-50);
			}
		})
		
	});
	
	window.addEvent('load', function() {
		if( days.length > 0 ){
			days.each(function(day,i){
				walls[i].build();
			});
		}
	});

	//Adjust on resize
	window.addEvent('resize', function() {
		if( days.length > 0 ){
			days.each(function(day,i){
				walls[i].build();
			});
		}
	});
})();