var imgs = 30;
var thumbwidth = 200;
var maxcols = 6;
var mincols = 3;
var defcols = 5;
var adWidth = 0;

function findWidth()
{
    var width = 1200;
    if (typeof(window.innerWidth) == 'number')
        width = window.innerWidth;
    else if (document.documentElement &&
         (document.documentElement.clientWidth)) 
        width = document.documentElement.clientWidth;
    else if (document.body && (document.body.clientWidth))
        width = document.body.clientWidth;
    return width;
}

function findParam(param, def)
{
    param += "=";
    var res = def;
    URL = self.location.href;
    var p = URL.indexOf(param);
    if (p == -1)
    {
        return res;
    } else
    {
        var temp = URL.substring(p + param.length);
        pe = temp.indexOf("&");
        if (pe == -1) pe = temp.length;
        res = temp.substring(0, pe);
    }
    return Number(res);
}

function setParam(URL, param, value)
{
    param += "=";
    var p = URL.indexOf(param);
    if (p == -1)
    {
        URL += "&";
        URL += param;
        URL += String(value);
        return URL;
    } else
    {
        p += param.length;
        var pre = URL.substring(0, p);
        post = URL.substring(p);
        pe = post.indexOf("&");
        if (pe == -1) post = "";
        else post = post.substring(pe);
        pre += String(value);
        return (pre + post);
    }
}

function reSizeTable()
{
    var width = findWidth();
        width = width - adWidth
    cols = parseInt(width / thumbwidth);
    if (cols < mincols) cols = mincols;
    if (cols > maxcols) cols = maxcols;
    if (findParam("cols", defcols) != cols)
    {
        rows = parseInt(imgs / cols);
        if (rows < 1) rows = 1;
        URL = self.location.href;
        URL = setParam(URL, "cols", cols);
        URL = setParam(URL, "imgs", cols * rows);
        if (document.images)
            window.location.replace(URL);
        else
            window.location.href = URL;
    }
}

function addColParams()
{
    var width = findWidth();
    width = width - adWidth
    cols = parseInt(width / thumbwidth);
    if (cols < mincols) cols = mincols;
    if (cols > maxcols) cols = maxcols;
    var rows = parseInt(imgs / cols);
    if (rows < 1) rows = 1;
    document.query.cols.value = cols;
    document.query.imgs.value = rows * cols;
        return true;
}

