/*//////////////////////////////////////////////////////////
//  * jQuery Grid Layout
//
//  * Requirement:
//   - jQuery v1.3.2 or higher
//     URL: http://jquery.com/
//   - jQuery Easing v1.3 or higher
//     URL: http://gsgd.co.uk/sandbox/jquery/easing/
//   - Prototype v1.6 or higher
//     URL: http://www.prototypejs.org/
//
//////////////////////////////////////////////////////////*/

jQuery.noConflict();


var BLOCK_WIDTH = 185;
var BLOCK_MARGIN = 10;
var BLOCK_SIZE = BLOCK_WIDTH + BLOCK_MARGIN;
var BLOCK_MINLIMIT = 3;
var BLOCK_ANIMATE_DURATION = 300;
var BLOCK_ANIMATE_TYPE = 'easeInOutCubic';
var FONTSIZE_CHECK_INTERVAL = 3000;
var REMIX_INTERVAL = 1500;


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('#modal').offset().left;
		area_offset_y = jQuery('#modal').offset().top;

		jQuery('#blocks > div.block').css('width', BLOCK_WIDTH + 'px');
		jQuery('#blocks div.doublesize').css('width', BLOCK_WIDTH*2 + BLOCK_MARGIN + 'px');

		remix();
		setInterval(remix, REMIX_INTERVAL);

		if (jQuery('#modal')){
			blind_timerid = setTimeout('hide_blind()',1000);
			hide_blind();
		}
});


function hide_blind(){
	jQuery('#modal').fadeOut(1000);
	clearTimeout(blind_timerid);
}


function remix(){

		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));

		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;
		}

		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){

						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];

						jQuery(this).animate({
										left: startpos_x + 'px',
										top: startpos_y + BLOCK_MARGIN + 'px',
										borderWidth: BLOCK_MARGIN + 'px'
								},
								BLOCK_ANIMATE_DURATION,
								BLOCK_ANIMATE_TYPE
						);

						maxpos_y[block_row] = maxpos_y[block_row] + item_height + BLOCK_MARGIN;
				}else if(block_rowsize == 2){

						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];
						}

						jQuery(this).animate({
										left: startpos_x + 'px',
										top: startpos_y + BLOCK_MARGIN + 'px',
										borderWidth: BLOCK_MARGIN + 'px'
								},
								BLOCK_ANIMATE_DURATION,
								BLOCK_ANIMATE_TYPE
						);

						maxpos_y[block_row] = startpos_y + item_height + BLOCK_MARGIN;
						maxpos_y[block_row+1] = startpos_y + item_height + BLOCK_MARGIN;
				}
		});

		var renderpos_x = parseInt((layoutarea_w - (BLOCK_SIZE * block_row_max))/2 );

		var footer_h = Math.max(maxpos_y.max(), sidebar_h);
		var footer_w = BLOCK_SIZE * block_row_max + sidebar_w;
/*		var footer_w = BLOCK_SIZE * block_row_max;*/
/*		if(footer_w <= 790) footer_w = 790;*/
		jQuery('#footer').animate({
				top: 120 + 10 + footer_h + 'px',
				left: renderpos_x + 'px',
				width: footer_w
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);

		jQuery('#header').animate({
				top: '20px',
				left: renderpos_x + 'px'
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);

		jQuery('#blocks').animate({
				top: '120px',
				left: renderpos_x + 'px'
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);

/*
		jQuery('#sidebar').animate({
				top: '120px',
				left: renderpos_x + block_row_max * BLOCK_SIZE + 'px'
				},
				BLOCK_ANIMATE_DURATION,
				BLOCK_ANIMATE_TYPE
		);
*/

}
