$(function(){
// Start_Opacity
});


// EntryBox
var _offset_x=_offset_y=0;
var grid = {
	init: function() {
		for (module in grid) {
			if (grid[module].init)
				grid[module].init();
		}
	}
};
grid.window = {
	init: function() {
		$(window)
			.bind('load', this.load)
			.bind('resize', this.resize)
			.bind('scroll', this.scroll)
	},

	load: function() {
		adjust(true);
	},
	resize: function() {
		adjust(true);
	},
	scroll: function() {
		var st=$(document.body).scrollTop();
		st = (!st)?document.documentElement.scrollTop:st;
		$('#header').css({top: st+10});
	}
};
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 limit=$('#header div.inner').height();
	if(limit+32<_window_h){
		$('#header').height(_window_h-8);
	}
	var _header_w=$('#header').outerWidth();
	var _target_w = _window_w-$('#header').outerWidth();
	$('#pagebody').width(_target_w-16);

	var _footer_y=0;
	var columns = Math.max(2, parseInt(_target_w / 200));
	$('#boxContent > .box').css('width',190+'px');
	$('#boxContent .box_double').css('width',390);
	$('#boxContent .box_tripple').css('width',590);
	for (x=0;x<columns;x++) _max_y[x] = 0;
	$('#boxContent > .box').each(function(i) {
		var pos, cursor, width, height= 0;
		var target_x=target_y=0;
		width=(Math.floor($(this).outerWidth()/190));
		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())+10+height;
			target_x=pos*(200)+_offset_x;
			target_y=height+_offset_y;
			_max_h=(height > _max_h)?_max_y[pos+width-1]:_max_h;
		}else{
			for (x=0;x<columns;x++) cursor=(_max_y[x]<_max_y[cursor])?x:cursor;
			target_x=cursor*(200)+_offset_x;
			target_y=_max_y[cursor]+_offset_y;
			_max_y[cursor] += $(this).outerHeight()+10;
			_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', target_x).css('top',target_y+10);
		}else{
			$(this).stop();
			$(this).animate({left: target_x + 'px',top: target_y+10 + 'px',borderWidth: "10px"},700,'easeInOutSine');
		}
		_max_col=(_max_col<cursor)?cursor:_max_col;
	});
	var target_x=parseInt(($('body').innerWidth()-(200)*(_max_col+1))/2)-0;
	$('#boxContent').stop();
	if(!mode){
		$('#boxContent').css('left',target_x);
	}else{
		$('#boxContent').animate({left:target_x},700,'easeInOutSine');
	}
};
$(document).ready(grid.init);

