/**
 * パズル関連スクリプト
 */
/**
 * Avoid conflict with Prototype.【プロトタイプ無いと動かないよ】
 */
jQuery.noConflict();
/**
 * Settings【ここで動き設定】
 */
var BLOCK_WIDTH = 230;
var BLOCK_MARGIN = 10;
var BLOCK_SIZE = BLOCK_WIDTH + BLOCK_MARGIN;
var BLOCK_MINLIMIT = 2;
var BLOCK_ANIMATE_DURATION = 500;
var BLOCK_ANIMATE_TYPE = 'easeInOutCubic';
var FONTSIZE_CHECK_INTERVAL = 2000;
var REMIX_INTERVAL = 1500;
/**
 * Init【内容】
 */
var area_offset_x = area_offset_y = 0;
var basic_fontsize = current_fontsize = 0;
var blind_timerid;
jQuery(window).ready(function(){
		area_offset_x = jQuery('#blocks').offset().left;
		area_offset_y = jQuery('#blocks').offset().top;
		jQuery('#blocks > div.block').css('width', BLOCK_WIDTH + 'px');
		jQuery('#blocks div.doublesize').css('width', BLOCK_WIDTH*2 + BLOCK_MARGIN + 'px');
		jQuery('div#footer table.search').hide(BLOCK_ANIMATE_DURATION, BLOCK_ANIMATE_TYPE);
		remix();
		// Fontsize Check【フォントサイズチェック】
		// basic_fontsize = current_fontsize = jQuery('div#fontsizeCheck').width();【基本フォントサイズ】
		// setInterval(fontsizeCheck, FONTSIZE_CHECK_INTERVAL);【フォントサイズ関連】
		// Remixing by interval【リミックス？これが意味不明】
		setInterval(remix, REMIX_INTERVAL);
});
function remix(){
		// Size define【サイズ確認】
		var windowsize_h = jQuery(window).height();
		var windowsize_w = jQuery(window).width();
		var sidebar_h = jQuery('#sidebar').height();
		var sidebar_w = jQuery('#sidebar').outerWidth();
		var layoutarea_w = windowsize_w - sidebar_w;
		var block_row_max = Math.max(BLOCK_MINLIMIT, parseInt(layoutarea_w / BLOCK_SIZE));
		// Position define
		var block_row = 0;
		var startpos_x = startpos_y = 0;
		var maxpos_y = Array();
		for(i=0; i<block_row_max; i++){
				maxpos_y[i] = 0;
		}
		// Each cards calculating layouts.
		jQuery('#blocks > div.block').each(function() {
				var item_width = jQuery(this).outerWidth();
				var item_height = jQuery(this).outerHeight();
				var block_rowsize = Math.floor(item_width / BLOCK_WIDTH);
				if(block_rowsize == 1){
						// Detect block position.【ポジション探索】
						// Scan for all rows, detect min height.【最低高さを探索】
						block_row = 0;
						var minpos_y = maxpos_y.min();
						for(i=block_row_max; i>=0; i--){
								if(minpos_y == maxpos_y[i]){
										block_row = i;
							}
						}
						startpos_x = block_row * BLOCK_SIZE;
						startpos_y = maxpos_y[block_row];
						// Animation.【アニメ】
						jQuery(this).animate({
										left: startpos_x + 'px',
										top: startpos_y + BLOCK_MARGIN + 'px',
										borderWidth: BLOCK_MARGIN + 'px'
								},
								BLOCK_ANIMATE_DURATION,
								BLOCK_ANIMATE_TYPE
						);
						// Lock field for this block.【このブロックをロックする】
						maxpos_y[block_row] = maxpos_y[block_row] + item_height + BLOCK_MARGIN;
				}else if(block_rowsize == 2){
						// Detect block position.【ブロック位置を探索】
						// Scan for all rows, detect min height with doublesized block.【ダブルサイズブロック関連】
						block_row = 0;
						var pos_pair_max = Array();
						for(i=0; i<(block_row_max-1); i++){
								var pair = new Array(maxpos_y[i],  maxpos_y[i+1]);
								pos_pair_max[i] = pair.max();
						}
						var pos_pair_min = pos_pair_max.min();
						for(i=(block_row_max-1); i>=0; i--){
								if(pos_pair_min == pos_pair_max[i]){
										block_row = i;
								}
						}
						startpos_x = block_row * BLOCK_SIZE;
						if(maxpos_y[block_row] >= maxpos_y[block_row+1]){
								startpos_y = maxpos_y[block_row];
						}else{
								startpos_y = maxpos_y[block_row+1];
						}
						// Animation.【動き？】
						jQuery(this).animate({
										left: startpos_x + 'px',
										top: startpos_y + BLOCK_MARGIN + 'px',
										borderWidth: BLOCK_MARGIN + 'px'
								},
								BLOCK_ANIMATE_DURATION,
								BLOCK_ANIMATE_TYPE
						);
						// Lock fields for this blocks.【このブロック関連をロックする】
						maxpos_y[block_row] = startpos_y + item_height + BLOCK_MARGIN;
						maxpos_y[block_row+1] = startpos_y + item_height + BLOCK_MARGIN;
				}
		});
		// Calculating rendering startpoint.【スタート位置を計算】
		var renderpos_x = parseInt((layoutarea_w - (BLOCK_SIZE * block_row_max))/2 );
		// Animate displayareas.【アニメーションを動かすエリアを探索】
		var footer_h = Math.max(maxpos_y.max(), sidebar_h);
		var footer_w = BLOCK_SIZE * block_row_max + sidebar_w;
        if(footer_w <= 790) footer_w = 790;
		jQuery('#footer').animate({
				top: footer_h + 'px',
				left: renderpos_x + 'px',
                width: footer_w
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);
		jQuery('#blocks').animate({
				left: renderpos_x + 'px'
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);
		jQuery('#sidebar').animate({
				left: renderpos_x + block_row_max * BLOCK_SIZE + 'px'
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);
}
