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

var COLUMN_MIN = 2;
var COLUMN_WIDTH = 190;
var COLUMN_MARGIN = 10; 

var _offset_x=_offset_y=0;

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

cell$(document).ready(cell$.cell.init);

var cookie="";

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

	preload: function() {
		//cookie = cell$.cookie('ready');
		
		//if(!cookie){
			cell$('body').append('<div id="loading" style="position:absolute;top: 0 ;left:0; margin:0; padding:0; background: #181818; width:'+cell$(window).width()+'px;height:'+cell$(window).height()+'px;z-index:9999;"></div>');
			//cell$.cookie('ready','true',{expires:30,path:'/'});
			cell$.timer(2000,function(timer){
				cell$('#loading').animate({'opacity':0},3000,'easeOutCubic',function(){cell$(this).remove();});
			});
		//}
		cell$('#container, #tnav, #footer').css({'visibility':'visible'});
		
		_offset_y = cell$('#cell').offset().top;
		_offset_x = cell$('#cell').offset().left;
		
		//topics();
		adjust(false);
	},
	resize: function() {
		adjust(true);
	},
	scroll: function() {
		var st=cell$(document.body).scrollTop();
		st = (!st)?document.documentElement.scrollTop:st;
		cell$('#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 = cell$(window).height();
	var _window_w = cell$(window).width();
	/*
var limit=cell$('#header div.inner').height();
	if(limit+32<_window_h){
		cell$('#header').height(_window_h-8);
	}
	var _header_w=cell$('#header').outerWidth();
	var _target_w = _window_w-cell$('#header').outerWidth();
*/
	var _target_w = _window_w;
	
	cell$('#pagebody').width(_target_w-16);

	var _footer_y=0;
	var columns = Math.max(COLUMN_MIN, parseInt(_target_w / (COLUMN_WIDTH+COLUMN_MARGIN)));
	cell$('#cell div.item').css('width',COLUMN_WIDTH+'px');
	cell$('#cell div.item_double').css('width',(COLUMN_WIDTH*2+COLUMN_MARGIN));
	cell$('#cell div.item_tripple').css('width',(COLUMN_WIDTH*3+COLUMN_MARGIN*2));
	for (x=0;x<columns;x++) _max_y[x] = 0;
	
	cell$('#cell > div.item').each(function(i) {
		var pos, cursor, width, height= 0;
		var target_x=target_y=0;
		width=(Math.floor(cell$(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(cell$(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] += cell$(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){
			cell$(this).css('left', target_x).css('top',target_y+COLUMN_MARGIN);
		}else{
			cell$(this).stop();
			cell$(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((cell$('body').innerWidth()-(COLUMN_WIDTH+COLUMN_MARGIN)*(_max_col+1))/2)-0;
	cell$('#cell').stop();
	cell$('#header').stop();
	cell$('#hp_cell').stop();
	cell$('#footer').stop();
	if(!mode){
		cell$('#header').css('right',target_x);
		cell$('#cell').css('left',target_x);
		cell$('#hp_cell').css({'left':target_x,'top':_footer_y, 'width':columns*COLUMN_WIDTH});
		cell$('#footer').css({'left':target_x,'top':_footer_y, 'width':columns*COLUMN_WIDTH});
	}else{
		cell$('#cell').animate({left:target_x},500,'easeInOutCubic');
		cell$('#header').animate({right:target_x},500,'easeInOutCubic');
		cell$('#hp_cell').animate({left:target_x,top:_footer_y, width:columns*COLUMN_WIDTH},500,'easeInOutCubic');
		cell$('#footer').animate({left:target_x,top:_footer_y, width:columns*COLUMN_WIDTH},500,'easeInOutCubic');
	}
	cell$('#cell > div.item').mouseover(function() {
		cell$(this).css({'background-color':'#000'})
	});
	cell$('#cell > div.item').mouseout(function() {
		cell$(this).css({'background-color':'#000'})
	});
};