jQuery.noConflict();
var grid$ = jQuery;

var COLUMN_MIN = 4;
var COLUMN_WIDTH = 190;
var COLUMN_MARGIN = 5; 

var _offset_x=_offset_y=0;

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

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {window.location = "./mobile_safari/index.html";}
grid$(document).ready(grid$.grid.init);

var cookie="";
grid$.grid.window = {
	init: function() {
		grid$(window)
			.bind('resize', this.resize)
			.bind('scroll', this.scroll)
			.each(this.preload);
	},

	preload: function() {
		cookie = grid$.cookie('ready');
		grid$('body').append('<div id="loading" style="position:absolute;top:0;left:0;margin:0;padding:0;width:'+grid$(window).width()+'px;height:'+grid$(window).height()+'px;background-color:#FFF;"></div>');
		grid$.timer(500,function(timer){
			grid$('#loading').animate({'opacity':0},50,'easeOutCubic',function(){grid$(this).remove();});
		});
		grid$('#container').css({'visibility':'visible'});
		_offset_y = grid$('#grid').offset().top;
		_offset_x = grid$('#grid').offset().left;
		adjust(false);
	},
	resize: function() {
		adjust(true);
	},
	scroll: function() {
		var st=grid$(document.body).scrollTop();
		st = (!st)?document.documentElement.scrollTop:st;
		grid$('#header').css({top: st+COLUMN_MARGIN});
	}
};

function adjust(mode){
	var _max_y = new Array();
	var _max_h = 0;
	var _max_col = 0;

	var _window_h=grid$(window).height();
	var _window_w=grid$(window).width();
	var limit=grid$('#header div.inner').height();
	if(limit+32<_window_h){
		grid$('#header').height(_window_h-8);
	}
	var _header_w=grid$('#header').outerWidth();
	var _target_w = _window_w-grid$('#header').outerWidth();
	grid$('#pagebody').width(_target_w-16);

	var _footer_y=0;
	var columns = Math.max(COLUMN_MIN, parseInt(_target_w / (COLUMN_WIDTH+COLUMN_MARGIN)));
	grid$('#grid > div.item').css('width',COLUMN_WIDTH+'px');
	grid$('#grid > div.item_double').css('width',COLUMN_WIDTH*2+COLUMN_MARGIN+'px');
	grid$('#grid > div.item_head').css('width',COLUMN_WIDTH*4+COLUMN_MARGIN*3+'px');
	grid$('#grid > div.item_3').css('width',COLUMN_WIDTH*3+COLUMN_MARGIN*2+'px');
	grid$('#grid > div.item_4').css('width',COLUMN_WIDTH*4+COLUMN_MARGIN*3+'px');
	grid$('#grid > div.item_5').css('width',COLUMN_WIDTH*5+COLUMN_MARGIN*4+'px');
		
	
	for (x=0;x<columns;x++) _max_y[x] = 0;
	grid$('#grid > div.item').each(function(i) {
		var pos, cursor, width, height= 0;
		var target_x=target_y=0;
		width=(Math.floor(grid$(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(grid$(this).outerHeight())+COLUMN_MARGIN+height;
			target_x=pos*(COLUMN_WIDTH+COLUMN_MARGIN)+_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*(COLUMN_WIDTH+COLUMN_MARGIN)+_offset_x;
			target_y=_max_y[cursor]+_offset_y;
			_max_y[cursor] += grid$(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){
			grid$(this).css('left', target_x).css('top',target_y+COLUMN_MARGIN);
		}else{
			grid$(this).stop();
			grid$(this).animate({left: target_x + 'px',top: target_y+COLUMN_MARGIN + 'px',borderWidth: "10px"},500,'easeInOutCubic');
		}
		_max_col=(_max_col<cursor)?cursor:_max_col;
	});
	var target_x=parseInt((grid$('body').innerWidth()-(COLUMN_WIDTH+COLUMN_MARGIN)*(_max_col+1))/2);
	grid$('#grid').stop();
	grid$('#header').stop();
	grid$('#footer').stop();
	if(!mode){
		grid$('#header').css('right',target_x);
		grid$('#grid').css('left',target_x);
		grid$('#footer').css({'left':target_x,'top':_footer_y, 'width':columns*COLUMN_WIDTH});
	}else{
		grid$('#grid').animate({left:target_x},500,'easeInOutCubic');
		grid$('#header').animate({right:target_x},500,'easeInOutCubic');
		grid$('#footer').animate({left:target_x,top:_footer_y, width:columns*COLUMN_WIDTH},500,'easeInOutCubic');
	}
	
	

	
	
	/**/
	/*▽オーバー時の設定*/
	grid$('#grid > div.column_over').mouseover(function() {
		grid$(this).css({'background-color':'#ececec'});
		grid$(this).css({'background-image':'url(common/image/bg_column_top_over.gif)'});
		grid$(this).children('.body').css({'background-image':'url(common/image/bg_column_bottom_over.gif)'});
	});
	/*▽ロールアウト時の設定　デフォ#0F0F0F*/
	grid$('#grid > div.column_over').mouseout(function() {
		grid$(this).css({'background-color':'#FFFFFF'});
		grid$(this).css({'background-image':'url(common/image/bg_column_top.gif)'});
		grid$(this).children('.body').css({'background-image':'url(common/image/bg_column_bottom.gif)'});
	});
	/*▽オーバー時の設定*/
	grid$('#grid > div.column_over2').mouseover(function() {
		grid$(this).css({'background-color':'#ececec'});
		grid$(this).css({'background-image':'url(common/image/bg_column_top_over.gif)'});
		grid$(this).children('.body').css({'background-image':'url(common/image/bg_column_bottom_over.gif)'});
	});
	/*▽ロールアウト時の設定*/
	grid$('#grid > div.column_over2').mouseout(function() {
		grid$(this).css({'background-color':'#FFFFFF'});
		grid$(this).css({'background-image':'url(common/image/bg_column_top.gif)'});
		grid$(this).children('.body').css({'background-image':'url(common/image/bg_column_bottom.gif)'});
	});
	
	
	/**/
	/*
	grid$('#grid > div.column_over').mouseenter(function() {
		grid$(this).children('.body').children('.thumbnail').animate({'opacity':0.5},1000,'easeOutCubic');
		grid$(this).children('.body').children('.thumbnail_2').animate({'opacity':0.5},1000,'easeOutCubic');
	});
	grid$('#grid > div.column_over').mouseleave(function() {
		grid$(this).children('.body').children('.thumbnail').animate({'opacity':1},1000,'easeOutCubic');
		grid$(this).children('.body').children('.thumbnail_2').animate({'opacity':1},1000,'easeOutCubic');
	});
	*/
	/**/
	grid$('#grid > div.column_over').click(function(ev){
		
		
		
		if (navigator.platform.indexOf("Win") != -1) {
			/*WIN*/
			if(navigator.userAgent.indexOf("Firefox") != -1){

				if(ev.target != '[object HTMLEmbedElement]'){
														
					window.location=grid$(this).find("a").attr("href");
				}

			}else{
				window.location=grid$(this).find("a").attr("href");
			}

		}
		else {
			if(ev.target != '[object HTMLObjectElement]'){
														
				window.location=grid$(this).find("a").attr("href");
			}
		}
													
	});
	
	
	
	
	
};

/*
grid$('#grid').animate({'opacity':0},0,'easeOutCubic');
grid$('#grid').animate({'opacity':1},1000,'easeOutCubic');
*/
