var threshHoldHt = 550; //The threshhold height
var newColCreated = false;
var whichCol = {};
var maxCols = 3;
var hasAdvert = true;
var bareMinHt = 500;
var arrNoAdCategories = ["Network Management", "Security", "Voice Services", "Internet / Data Networking"];
function checkCategories(){
    var categoryID = jQuery("h1").text();
    jQuery.each(arrNoAdCategories, function(){
        if (categoryID == this) {
            hasAdvert = false;
            maxCols = maxCols - 1;
            return false;
        }
    })
    genThresholdHt();
}
function genThresholdHt(){
    var eachBlockHt1 = 0;
    $("div.container").find(".rows").each(function(i, obj){
        eachBlockHt1 += $(this).outerHeight();
        //alert($(this).outerHeight());
        bareMinHt = ($(this).outerHeight() + 20) > bareMinHt ? $(this).outerHeight() + 80 : bareMinHt;
        return;
    });
    maxCols = hasAdvert == true ? maxCols - 1 : maxCols + 1;
    threshHoldHt = Math.round((eachBlockHt1 / maxCols) + 30) > bareMinHt ? Math.round((eachBlockHt1 / maxCols) + 30) : bareMinHt;
}
function computeBlockHeight(n){
    //return .browser ? (n.outerHeight(true) - 5) : n.outerHeight(true);//get values for each blocks height, almost browser-agnostic
}
function arrangeCols(bool, col){ //pass in a boolean and a param object(which will later be used for generated <div>'s)
    if (bool) {
        if (col == '') {
            $("div.container").each(function(){
                arrangeBlocks($(this).find("div.cols"));
            });
        }
        else {
            $("div.container").each(function(){
                arrangeBlocks($(whichCall));
            });
        }
    }
    else {
        return false;
    }
}
function arrangeBlocks(n){
    var eachBlockHt = 0; //initializing block height
    n.find(".rows").each(function(i, obj){
        eachBlockHt += $(this).outerHeight();
        if (eachBlockHt > threshHoldHt) {
            var newDiv = $(document.createElement("div"));
            newDiv.addClass("col_33 cols col_01 bmg_catg_sect");
            n.after(newDiv);
            newColCreated = true;
            $(this).nextAll().appendTo(newDiv);
            $(this).prependTo(newDiv);
            whichCall = $(newDiv);
            return false;
        }
        else {
            newColCreated = false;
            whichCall = {};
            //return false;
        }
    });
    if (newColCreated) {
        arrangeCols(true, $(whichCall));
    }
    else {
        if (hasAdvert) {
            var colCount = $("div.container").find("div.cols").length;
            //$("div.container").find("div.cols:last").after('<div class="adverts" style="height:'+threshHoldHt+'px"> Advert</div>');
            $("div.container").find("div.ads").show().css({
                "width": (colCount > 1 ? "33%" : "33%"),
                "height": +threshHoldHt - 50
            });
        }
        return false;
    }
};
$(document).ready(function(){						   
    checkCategories();
    arrangeCols(true, '');
    //$("div.container").css("min-height",threshHoldHt);
    $("div.adjustHeight").css({
        "height": +threshHoldHt + 90,
	    "min-height": +threshHoldHt + 90
    });
	if(jQuery("h1").length=="1" && (jQuery("h1").text()=="Internet / Data Networking")){
	var moveThisBlock = jQuery("div.cols:last").find("div.rows:last");
	moveThisBlock.appendTo(jQuery("div.cols:eq(1)"));
	}
});
