var COLUMN_MIN=5;
var COLUMN_WIDTH=180;
var COLUMN_MARGIN=10;

$.slidegrid = {
	init: function() {
		for (module in $.slidegrid) {
			if ($.slidegrid[module].init)
				$.slidegrid[module].init();
		}
	}
};

$(document).ready($.slidegrid.init);
$.event.add(window, "load", function(){adjust(false);});

$.slidegrid.window = {
	init: function() {
		$(window)
			.bind('resize', this.resize)
			.each(this.preload);
	},
	preload: function() {
		_content_y = $('#slidegrid').offset().top;
		_content_x = $('#slidegrid').offset().left;
		var _side_height=$('#side').outerHeight();
		var _detail_height=$('#shopping-detail').outerHeight();
		adjust(false);
	},
	resize: function() {
		adjust(true);
	}
};

function adjust(mode){

	var _window_height=$(window).height();
	var _window_width=$(window).width();
	var _max_y=new Array();
	var _max_h=0;
	var _max_col=0;
	var _side_width=$("#side").outerWidth();
	var _side_height=$("#side").outerHeight();
	var _detail_height=$("#shopping-detail").outerHeight();
	var _outline_width=_window_width;/*-_side_width*/
	var _cell=Math.max(COLUMN_MIN,parseInt(_outline_width/(COLUMN_WIDTH+COLUMN_MARGIN)));
	//$(".cell").css("width",COLUMN_WIDTH+"px");
	$(".cell-single").css("width",COLUMN_WIDTH+"px");
	$(".cell-double").css("width",(COLUMN_WIDTH*2+COLUMN_MARGIN));
	$(".cell-triple").css("width",(COLUMN_WIDTH*3+COLUMN_MARGIN*2));
	$(".cell-quartet").css("width",(COLUMN_WIDTH*4+COLUMN_MARGIN*3));
	var _auto=Math.floor((_outline_width)/(COLUMN_WIDTH+COLUMN_MARGIN));
	_auto=(_auto<5)?5:_auto;
	var _autoW = (COLUMN_WIDTH+COLUMN_MARGIN)*_auto-COLUMN_MARGIN;
	
	/*サイドバーの長さ*/
	if(_detail_height>=_side_height){
		$("#side").css("height",_detail_height);
	}else{
		$("#side").css("height","auto");
	};
	
	for(x=0;x<_cell;x++){
		_max_y[x]=0;
	}
	
	$(".cell").each(function(i){
		var _pos,_cursor,_width,_height=0;
		var _cell_x=_cell_y=0;
			_width=(this.className.match(/cell-auto/))?_auto:(Math.floor($(this).outerWidth()/COLUMN_WIDTH));
			_cursor=0;
			
		if(_width>1){
			for(x=0;x<_cell-(_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;
			}
			_cell_x=_pos*(COLUMN_WIDTH+COLUMN_MARGIN)+_content_x;
			_cell_y=_height+_content_y;
			
		}else{
			for(x=0;x<_cell;x++){
				_cursor=(_max_y[x]<_max_y[_cursor])?x:_cursor;
			}
			_cell_x=_cursor*(COLUMN_WIDTH+COLUMN_MARGIN)+_content_x;
			_cell_y=_max_y[_cursor]+_content_y;
			_max_y[_cursor]+=$(this).outerHeight()+COLUMN_MARGIN;
			
		}
		
		_max_h=(_max_y[_cursor]>_max_h)?_max_y[_cursor]:_max_h;
		
		if(!mode){
			$(this).css("left", _cell_x + "px").css("top",_cell_y+COLUMN_MARGIN+"px");
			if(this.className.match(/cell-auto/)) $(this).css("width",_autoW+"px");
			if(this.className.match(/cell-header/)) $(this).css("top",_cell_y+"px");
		
		}else{
			$(this).stop();	
			
			if(this.className.match(/cell-auto/)){
				if(this.className.match(/cell-header/)){
					$(this).animate({left:_cell_x+"px",top:_cell_y+"px",width:_autoW+"px"},500,"easeInOutCubic");
				}else{
					$(this).animate({left:_cell_x+"px",top:_cell_y+COLUMN_MARGIN+"px",width:_autoW+"px"},500,"easeInOutCubic");
				};
			}else{
				if(this.className.match(/cell-header/)){
					$(this).animate({left:_cell_x+"px",top:_cell_y+"px"},500,"easeInOutCubic");
				}else{
					$(this).animate({left:_cell_x+"px",top:_cell_y+COLUMN_MARGIN+"px"},500,"easeInOutCubic");
				};
			}
		}
		_max_col=(_max_col<_cursor)?_cursor:_max_col;
	});
	
	
	var _side_height=$("#side").outerHeight();
	var _detail_height=$("#shopping-detail").outerHeight();
	
	/*サイドバーの長さ*/
	if(_detail_height>=_side_height){
		$("#side").css("height",_detail_height);
	}else{
		$("#side").css("height","auto");
	};
	
	
	var _outline_width=_cell*(COLUMN_WIDTH+COLUMN_MARGIN)-COLUMN_MARGIN;	
	var _outline_x=(_window_width-((_cell+1)*(COLUMN_WIDTH+COLUMN_MARGIN)))/2;
	_outline_x=(0<_outline_x)?_outline_x:0;
	_outline_x=(100<_outline_x)?_outline_x:0
	_content_x=(0<_outline_x)?-100:0;
	var _grid_x=(_window_width-_outline_width)/2;
	_grid_x=(0<_grid_x)?_grid_x:0;
	
	
	$("#slidegrid").stop();
	$("body").stop();
	
	if(!mode){
		$("#slidegrid > div.block").css("width", _outline_width).css("top",COLUMN_MARGIN);
		$("#slidegrid").css("left",_grid_x);
	}else{
		$("#slidegrid").animate({left:_grid_x+"px"},500,"easeInOutCubic");
		$("#slidegrid > div.block").animate({width:_outline_width+"px"},500,"easeInOutCubic");
	};
	
	$('.cell > div.hoverBtn').mouseover(function() {
	$(this).stop().animate({backgroundColor: '#E0E0D9'}, 'normal');
	});
	$('.cell > div.hoverBtn').mouseout(function() {
	$(this).stop().animate({backgroundColor: '#fff'}, 'normal');
	});
	$('.cell > div.hoverBtn').click(function() {
	$(this).css({'background-color':'#ddd'})
	});

};

