
$(document).ready(function(){

    if (document.location.pathname.indexOf("/new/") < 0) return;

    
    $("DIV#multicol").each(function() {
        $mc = $(this);
        var n = $mc.children().length;
        var w = 560;
        var pw = Math.floor((w - ((n-1) * 13)) / n );
        $mc.find("DIV").width( pw ).css("float","left").css("padding-left","6px").css("padding-right","6px").css("border-right","1px dotted black");
        $mc.find("DIV:first").css("padding-left","0");
        $mc.find("DIV:last").css("padding-right","0").css("border","0");
    });
    
    //resizeMultis();
});


var resizeMultis = function() {

    $("DIV#multicol").each(function() {
        $mc = $(this);
        var n = $mc.children().length;
        var w = 560;
        var pw = Math.floor((w - ((n-1) * 13)) / n );
        $mc.find("DIV").width( pw ).css("float","left").css("padding-left","6px").css("padding-right","6px").css("border-right","1px dotted black");
        $mc.find("DIV:first").css("padding-left","0");
        $mc.find("DIV:last").css("padding-right","0").css("border","0");
        
        $mc.find("IMG").load( function() {
            //var ih = resizeCols(this)
            var h=0;
            var ih = 0;
            $mc.find("DIV").each(function() {
                h = Math.max(h, $(this).height());
                ih = Math.max(ih, $(this).find("IMG").height());
            });
        
            $mc.find("DIV").height(h);
        
            var pges = (ih - $(this).height() )
            var pt =  Math.floor(pges / 2);
            var pb = pges - pt;
            $(this).css("padding-top", pt).css("padding-bottom", pb);
        });
        $mc.find("IMG").load();
        
    });
}


var resizeCols = function(obj) {
        $mc = $(obj).parents('DIV#multicol');

        var h=0;
        var ih = 0;
        $mc.find("DIV").each(function() {
            h = Math.max(h, $(this).height());
            ih = Math.max(ih, $(this).find("IMG").height());
        });
    
        $mc.find("DIV").height(h);
        
        return ih;
}


$(window).load(function() {banners()});

var bInit = false;
var banners = function() {
    if (window != top)  return;
    if (bInit) return;
    bInit = true;

    resizeMultis();
    
    $("P.c_multicol_link A").prepend("» ");

    if ($("DIV.topimage").length < 1) return;

    
    $("DIV.topimage").css("position","relative").height(function() {
        var i=0, maxh=0, $t=$(this);
        for (i=0; i < $t.children().length; i++) {
            var h = $($t.children()[i]).height();
            maxh = Math.max(maxh, h);
        }
        return maxh;
    });
      
    $("DIV#t_image > DIV").css("display","none").css("top","0px").css("left","0").css("position","absolute");
    
    var curshown = -1;
    
    var show = function() {
        var p = $("DIV.topimage").children().length;
        var n = curshown;
        var maxtrys = 10;
        while (--maxtrys > 0 && n == curshown) {
            n = Math.floor(Math.random()*p + 1);
        }
        if (curshown >= 0) {
            if (typeof(console) != "undefined") console.log( "hide:" + curshown);
            $("DIV.topimage DIV:nth-child(" + curshown + ")").fadeOut(500);
        }
        curshown = n;
        if (typeof(console) != "undefined") console.log( "show:" + n);
        $("DIV.topimage DIV:nth-child(" + n + ")").fadeIn(500, function() {
                window.setTimeout(show, 20000);
            });
    };
    
    show();
    //window.setInterval(show, 20000);
    
}
