jQuery.noConflict();
(function($){
var COLUMN_MIN = 4;
var COLUMN_WIDTH = 180;
var COLUMN_MARGIN = 10; 

var _offset_x=0;
var _offset_y=0;

$.grid = {
	init: function() {
		for (module in $.grid) {
			if ($.grid[module].init)
				$.grid[module].init();
		}
	}
};

$(document).ready($.grid.init);
$.event.add(window, "load", function(){adjust(false);});

$.grid.window = {
	init: function() {
		$(window)
			.bind('resize', this.resize)
			.each(this.preload);
	},
	preload: function() {
		_offset_y = $('.grid').offset().top;
		_offset_x = $('.grid').offset().left;
		adjust(false);
	},
	resize: function() {
		adjust(true);
	}
};

function adjust(mode){

	var _max_y = new Array();
	var _max_h = 0;
	var _max_col = 0;

	var _window_h= $(window).height();
	var _window_w= $(window).width();

	var _menu_w=$('.menu').outerWidth();
	var _target_w=_window_w-_menu_w;

	var _footer_y=0;
	var columns = Math.max(COLUMN_MIN, parseInt(_target_w / (COLUMN_WIDTH+COLUMN_MARGIN)));
//	$('.grid > div.item').css('width',COLUMN_WIDTH*2+COLUMN_MARGIN);
	$('.grid > .item_half').css('width',COLUMN_WIDTH);
	$('.grid .item_2w').css('width',COLUMN_WIDTH*2+COLUMN_MARGIN*1);
	$('.grid .item_3w').css('width',COLUMN_WIDTH*3+COLUMN_MARGIN*2);
	$('.grid .item_4w').css('width',COLUMN_WIDTH*4+COLUMN_MARGIN*3);
	var _auto=Math.floor((_target_w)/(COLUMN_WIDTH+COLUMN_MARGIN));
	_auto=(_auto<10)?10:_auto;
	var _autoW = (COLUMN_WIDTH+COLUMN_MARGIN)*_auto-COLUMN_MARGIN;

	for (x=0;x<columns;x++) _max_y[x] = 0;
	$('.grid > .item_box').each(function(i) {
		var pos, cursor, width, height= 0;
		var item_x=item_y=0;
		width=(this.className.match(/item_auto/))?_auto:(Math.floor($(this).outerWidth()/COLUMN_WIDTH));
		cursor=0;
		if (width>1) {
			for (x=0;x<columns-(width-1);x++) cursor=(_max_y[x]<_max_y[cursor])?x:cursor;
			pos=cursor;
			for(var x=0; x<width; x++) height=Math.max(height, _max_y[pos+x]);
			for(var x=0; x<width; x++) _max_y[pos+x]=parseInt($(this).outerHeight())+COLUMN_MARGIN+height;
			item_x=pos*(COLUMN_WIDTH+COLUMN_MARGIN);
			item_y=height+_offset_y;
		}else{
			for (x=0;x<columns;x++) cursor=(_max_y[x]<_max_y[cursor])?x:cursor;
			item_x=cursor*(COLUMN_WIDTH+COLUMN_MARGIN);
			item_y=_max_y[cursor]+_offset_y;
			_max_y[cursor] += $(this).outerHeight()+COLUMN_MARGIN;
		}
		_max_h=(_max_y[cursor]>_max_h)?_max_y[cursor]:_max_h;
		_footer_y=(_footer_y<_max_h)?_max_h:_footer_y;
		if(!mode){
			$(this).css('left', item_x + 'px').css('top',item_y+COLUMN_MARGIN+'px');
			if(this.className.match(/item_auto/)) $(this).css('width',_autoW+'px');
		}else{
			$(this).stop();
			if(this.className.match(/item_auto/)){
				$(this).animate({left: item_x + 'px',top:item_y+COLUMN_MARGIN+'px',width:_autoW+'px'},500,'easeInOutCubic');
			}else{
				$(this).animate({left: item_x + 'px',top:item_y+COLUMN_MARGIN+'px'},500,'easeInOutCubic');
			}
		}
		_max_col=(_max_col<cursor)?cursor:_max_col;
	});

	var target_w=columns*(COLUMN_WIDTH+COLUMN_MARGIN)-COLUMN_MARGIN;

	var target_x=(_window_w-((columns+1)*(COLUMN_WIDTH+COLUMN_MARGIN)))/2;
	target_x=(0<target_x)?target_x:0;
	target_x=(0<target_x)?target_x:0
	offset_x=(0<target_x)?+5:0;
	var grid_x=(_window_w-target_w-_menu_w-COLUMN_MARGIN)/2;
	grid_x=(0<grid_x)?grid_x:0;

	$('.grid').stop();
	$('.menu').stop();
	$('body').stop();
	$('.footer').stop();
	var header_x = target_x+target_w+COLUMN_MARGIN+offset_x;
	if(!mode){
		$('.grid > div.block').css('width', target_w).css('top',COLUMN_MARGIN);
		$('.grid').css('left',grid_x);
		$('.menu').css('left',header_x);
		$('body').css('background-position',header_x);
		$('.footer').css('left',grid_x).css('top',_footer_y).css('width',columns*COLUMN_WIDTH);
	}else{
		$('.grid').animate({left:grid_x+'px'},500,'easeInOutCubic');
		$('.grid > div.block').animate({width:target_w+'px'},500,'easeInOutCubic');
		$('.menu').animate({left: header_x + 'px'},500,'easeInOutCubic');
		$('body').animate({backgroundPosition: header_x+'px'+' 0px'},500,'easeInOutCubic')
		$('.footer').animate({left:grid_x+'px',top:_footer_y+'px',width:columns*COLUMN_WIDTH+'px'},500,'easeInOutCubic');
	}
	$('.grid > div.item_on').mouseover(function() {
		$(this).stop().animate({backgroundColor: '#ffffcc'}, 'normal');
	});
	$('.grid > div.item_on').mouseout(function() {
		$(this).stop().animate({backgroundColor: '#fff'}, 'normal');
	});
	$('.grid > div.item_on').click(function() {
		$(this).css({'background-color':'#e0b051'})
	});
};
})(jQuery);

