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

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

var _offset_x=_offset_y=0;

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

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() {
		_offset_y = 0;
		_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;

	}
};

function adjust(mode){
	var _max_y = new Array();
	var _max_h = 0;
	var _max_col = 0;
	
	var _window_h = grid$(window).height();

	if ( grid$(window).width() >960 )
	{
		var _window_w = grid$(window).width()-454;
	} else {
		var _window_w = 485;
	}

	

	var W_Defini = grid$(window).width();
	var _target_w = _window_w;

	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));
	grid$('#grid div.item_tripple').css('width',(COLUMN_WIDTH*3+COLUMN_MARGIN*2));
	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)-0;
	grid$('#grid').stop();



	if(!mode){
		grid$('div.SeparaBlocos').css({'height':_footer_y});	
		if (target_x < 227)
		{
			grid$('#grid').css('left',10);
		} else {
			grid$('#grid').css('left',target_x-227);
		}

		if (grid$('#FiltroEsquerda').height()>grid$('#ColunaCentral').height())
		{
			grid$('#FiltroEsquerda').css({'height':grid$('#FiltroEsquerda').height()});	
		}else {
			grid$('#FiltroEsquerda').css({'height':grid$('#ColunaCentral').height()});	
		}

	}else{
		grid$('div.SeparaBlocos').css({'height':_footer_y});	
		if (target_x < 227)
		{
			grid$('#grid').animate({left:10},500,'easeInOutCubic');
		} else {
			grid$('#grid').animate({left:target_x-227},500,'easeInOutCubic');
		}
		
		if (grid$('#FiltroEsquerda').height()>grid$('#ColunaCentral').height())
		{
			grid$('#FiltroEsquerda').css({'height':grid$('#FiltroEsquerda').height()});	
		}else {
			grid$('#FiltroEsquerda').css({'height':grid$('#ColunaCentral').height()});	
		}


	}

};
