//jQuery.noConflict();
//var jQuery = jQuery;

var COLUMN_MIN = 3;
var COLUMN_WIDTH = 188;
var COLUMN_MARGIN = 10; 
var COLUMN_MARGINw = 10;
var COLUMN_MARGINh = 10;

var _offset_x = _offset_y= 0;

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

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

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

    preload: function() {
        cookie = $.cookie('ready');
        if(!cookie){
            //$('body').append('<div id="loading" style="position:absolute;top:0;left:0;margin:0;padding:0;background:url(./images/logo.gif) 50% 50% no-repeat;width:'+$(window).width()+'px;height:'+$(window).height()+'px;z-index:100;background-color:#fff;"></div>');
            $.cookie('ready','true',{
                expires:30,
                path:'/'
            });
            $.timer(200,function(timer){
                $('#loading').animate({
                    'opacity':0
                },1000,'easeOutCubic',function(){
                    $(this).remove();
                });

            });
        }
        $('#ctr-enc, #ctr-cuerpo, #ctr-pie').css({
            'visibility':'visible'
        });
        _offset_y = $('#grilla').offset().top;
        _offset_x = $('#grilla').offset().left;
        /*topics();*/
        adjust(false);
    },
    resize: function() {
        adjust(true);
    },
    scroll: function() {
        var st=$(document.body).scrollTop();
        st = (!st)?document.documentElement.scrollTop:st;
//        $('#ctr-enc').css({
//            top: st+COLUMN_MARGIN
//        });
    }
};



var rss_data = "";
function replaceBlank(str) {
    str = str.replace(/^　/, "");
    str = str.replace(/^ /, "");
    return str;
}

function convertFormat(str) {
    var ary = str.split("/");
    var d = ary[0] + "." + ary[1] + "." + ary[2];
    return "<span>" + d + "</span>";
}

function adjust(mode){
    if(seccion == 'inicio' || seccion == 'links' ) {
        var _max_y = new Array();
        var _max_h = 0;
        var _max_col = 0;
	
//        var _window_h = $(window).height();
        var _window_w = $(window).width();
//        var limit = $('#ctr-enc div.inner').height();
//        if(limit+32<_window_h){
//            $('#ctr-enc').height(_window_h-8);
//        }
        //var _header_w = $('#ctr-enc').outerWidth();
        var _target_w = _window_w;
        
        $('#pagebody').width(_target_w-20);
		
        var _footer_y=0;
        var columns = Math.max(COLUMN_MIN, parseInt(_target_w / (COLUMN_WIDTH+COLUMN_MARGINw)));
        $('#grilla > div.item').css('width',COLUMN_WIDTH+'px');
        $('#grilla div.item_double').css('width',COLUMN_WIDTH*2+COLUMN_MARGINw);
        $('#grilla div.item_tripple').css('width',COLUMN_WIDTH*3+COLUMN_MARGINw*2);
        for (x=0;x<columns;x++) _max_y[x] = 0;
        $('#grilla > div.item').each(function(i) {
            var pos, cursor, width, height= 0;
            var target_x=target_y=0;
            width=(Math.floor($(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($(this).outerHeight())+COLUMN_MARGINh+height;
                target_x=pos*(COLUMN_WIDTH+COLUMN_MARGINw)+_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_MARGINw)+_offset_x;
                target_y=_max_y[cursor]+_offset_y;
                _max_y[cursor] += $(this).outerHeight()+COLUMN_MARGINh;
                _max_h=(_max_y[cursor]>_max_h)?_max_y[cursor]:_max_h;
            }
            _footer_y=(_footer_y<_max_h)?_max_h:_footer_y;
            if(!mode){
                $(this).css('left', target_x).css('top',target_y+COLUMN_MARGINh);
            }else{
                $(this).stop();
                $(this).animate({
                    left: target_x + 'px',
                    top: target_y+COLUMN_MARGINh + 'px',
                    borderWidth: "0px"
                },500,'easeInOutCubic');
            }
            _max_col=(_max_col<cursor)?cursor:_max_col;
        });
        
        var factorAncho = 1.05;
        var anchoPie = columns*COLUMN_WIDTH*factorAncho;
        var anchCalcCol = (COLUMN_WIDTH+COLUMN_MARGINw)*(_max_col+1);
        
        if(anchCalcCol < anchoPie) {
            anchCalcCol = anchoPie;
        }
        var target_x=parseInt(($('body').innerWidth()-anchCalcCol)/2)-0;
        
        $('#grilla').stop();
        $('#ctr-enc').stop();
        $('#enunciados').stop();
        $('#ctr-pie').stop();
        
        if(!mode){
            //$('#ctr-enc').css('right',target_x);
            $('#grilla').css('left',target_x);
            $('#enunciados').css({
                'left':target_x,
                'top':_footer_y, 
                'width':columns*COLUMN_WIDTH*factorAncho
                });
            $('#ctr-pie').css({
                'left':target_x,
                'top':_footer_y, 
                'width':columns*COLUMN_WIDTH*factorAncho
                });
        }else{
            $('#grilla').animate({
                left:target_x
            },500,'easeInOutCubic');
            //$('#ctr-enc').animate({right:target_x},500,'easeInOutCubic');
            $('#enunciados').animate({
                left:target_x,
                top:_footer_y, 
                width:columns*COLUMN_WIDTH*factorAncho
                },500,'easeInOutCubic');
            $('#ctr-pie').animate({
                left:target_x,
                top:_footer_y, 
                width:columns*COLUMN_WIDTH*factorAncho
                },500,'easeInOutCubic');
        }
        $('#grilla > div.item').mouseover(function() {
            $(this).css({
                'background-color':'#090906'
            })
        });
        $('#grilla > div.item').mouseout(function() {
            $(this).css({
                'background-color':'#000000'
            })
        });	
    }
    else {
        var _footer_y = 0;
        var _ancho_pie_fijo = 940;
        var target_x = parseInt(($('body').innerWidth()-_ancho_pie_fijo)/2);
		
        if (!mode) {
            $('#ctr-pie').stop();
            $('#ctr-pie').css({
                'left':target_x,
                'top':_footer_y, 
                'width':_ancho_pie_fijo
            });
        }
        else {
            $('#ctr-pie').animate({
                left:target_x,
                top:_footer_y, 
                width:_ancho_pie_fijo
            },500,'easeInOutCubic');
        }
    }
}

