var AutoGrid = {
    baseColWidth: 313,
    gutterSize: 25,
    sizes: [313, 651, 989],
    leeway: 80,
    leewayIncrement: 80,
    elem: new Object(),
    items: [],
    colStatus: [],
    numCols: 0,
    bodyWidth: 0,
    bodyHeight: 0,
    gridWidth: 0,
    construct: function(a){
        this.elem = $(a);
        this.items = this.elem.children();
        this.bodyWidth = $(document.body).width();
        this.bodyHeight = $(document.body).height();
        this.bindHandlers();
        return this
    },
    init: function(){
        this.bodyWidth = $(document.body).width();
        this.bodyHeight = $(document.body).height();
        this.gridWidth = (this.bodyWidth - 30) >= 1019 ? this.bodyWidth - 30 : 1019;
        var a = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        if (a > 3) {
            if ($.cookie("gobig") == null) {
                this.gridWidth = this.sizes[2];
                $("#goBig a").show()
            }
        }
        else {
            this.gridWidth = this.sizes[2]
        }
        this.numCols = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        for (i = 0; i < this.numCols; i++) {
            this.colStatus[i] = {
                x: i * (this.baseColWidth + this.gutterSize),
                h: 0,
                position: i
            }
        }
    },
    bindHandlers: function(){
        var a = this;
        $("#searchform").submit(function(){
            $('input[type="image"]', $(this))[0].disabled = true
        });
        $("#goBig a").click(function(){
			var date = new Date();
			date.setTime(date.getTime() + (31536000000));
			var expires = date.toGMTString();
			document.cookie = "gobig=true; expires="+expires+"; path=/";
            location.reload()
        });
        $(".videoArea a span").each(function(){
            var b = (355 - $(this).height()) / 2;
            $(this).css("padding-top", b)
        });
        $(".read-more").click(function(){
            $(this).siblings("p.moreText").show();
            $(this).siblings("a.read-less").show();
            $(this).hide();
            a.render();
            return false
        });
        $(".read-less").click(function(){
            $(this).siblings("p.moreText").hide();
            $(this).siblings("a.read-more").show();
            $(this).hide();
            a.render();
            return false
        })
    },
    filter: function(c, d){
        var b = this;
        var a = "";
        var e = $("#loading");
        d = d.join(",");
        e.show();
        $.post(c, {
            runAjaxPost: true,
            cat_list: d
        }, function(f){
            $("#main").html(f);
            b.items = b.elem.children();
            b.render();
            e.hide()
        }, "html");
        return false
    },
    findShortestCol: function(b){
        var a = 0;
        for (i = 0; i < b.length; i++) {
            if (b[i].h < b[a].h) {
                a = i
            }
        }
        return {
            orig: b[a].position,
            reduced: a
        }
    },
    canFit: function(c, a){
        if (c + a > this.numCols) {
            return false
        }
        var b;
        for (i = c + 1; i < c + a; i++) {
            b = Math.abs(this.colStatus[i].h - this.colStatus[c].h);
            if (b > this.leeway) {
                return false
            }
        }
        return true
    },
    getInsertionInfo: function(c, e, b){
        if (b.length == 0) {
            return null
        }
        var f = this.findShortestCol(b);
        var d = (c + this.gutterSize) / (this.baseColWidth + this.gutterSize);
        if (this.canFit(f.orig, d)) {
            var a = f.orig;
            for (i = f.orig; i < f.orig + d; i++) {
                if (this.colStatus[i].h > this.colStatus[a].h) {
                    a = i
                }
            }
            return {
                x: this.colStatus[f.orig].x,
                y: this.colStatus[a].h,
                p: f.orig,
                s: d
            }
        }
        b.splice(f.reduced, 1);
        return this.getInsertionInfo(c, e, b)
    },
    updateStatus: function(d, b, c, a){
        for (i = d; i < d + b; i++) {
            this.colStatus[i].h = c + a + this.gutterSize
        }
        this.leeway = this.leewayIncrement
    },
    findCorrectWidth: function(a){
        var b = 0;
        for (i = 1; i < this.sizes.length; i++) {
            if (Math.abs(a - this.sizes[i]) < Math.abs(a - this.sizes[b])) {
                b = i
            }
        }
        return this.sizes[b]
    },
    getGridDimensions: function(){
        var b = 0;
        var a = false;
        var c = this.colStatus;
        for (i = 0; i < c.length; i++) {
            if (c[i].h > c[b].h) {
                b = i
            }
            if (c[i].h == 0 && !a) {
                x = i;
                a = true
            }
        }
        if (!a) {
            x = this.numCols
        }
        w = (x * this.baseColWidth) + ((x - 1) * this.gutterSize);
        return {
            w: w,
            h: c[b].h
        }
    },
    setSiteItems: function(c){
        var b = 0;
        $("#banner").children().not("#loveFootballSwf, .itemShadow").each(function(){
            b += $(this).outerWidth()
        });
        $("#loveFootballSwf").width(c - b);
        $("#header .itemShadowMid").width(c - 24);
        $("#banner .itemShadowMid").width(c - 24);
        var a = 0;
        $(".paginationInner").children().not(".paginationCenter").each(function(){
            a += $(this).outerWidth()
        });
        var h = c - a;
        $(".paginationCenter").width(h);
        var d = 0;
        $(".paginationCenter ul").children().each(function(){
            d += $(this).outerWidth()
        });
        $(".paginationCenter ul").css("padding-left", Math.floor((h - d) / 2));
        $("#footerTopFiller").width(c - 14);
        var f = $.getDocHeight() - $("#main").outerHeight();
        if (f > 0) {
            var g = $("#footer");
            var e = parseInt(g.css("margin-top")) + f;
            g.css("margin-top", e)
        }
    },
    renderTeamsGrid: function(){
        this.baseColWidth = 236;
        this.gutterSize = 15;
        this.bodyWidth = $(document.body).width();
        this.gridWidth = (this.bodyWidth - 30) >= 1019 ? this.bodyWidth - 30 : 1019;
        var b = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        if (b > 4) {
            if ($.cookie("gobig") == null) {
                this.gridWidth = this.sizes[2];
                $("#goBig a").show()
            }
        }
        else {
            this.gridWidth = this.sizes[2]
        }
        this.numCols = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        $("#nationalTeams div:nth-child(" + this.numCols + "n)").css("margin-right", "0px");
        $("#clubTeams div:nth-child(" + this.numCols + "n)").css("margin-right", "0px");
        var a = (this.numCols * this.baseColWidth) + ((this.numCols - 1) * this.gutterSize);
        $("#main").width(a);
        this.setSiteItems(a)
    },
    renderProductsGrid: function(){
        this.baseColWidth = 236;
        this.gutterSize = 15;
        this.bodyWidth = $(document.body).width();
        this.gridWidth = (this.bodyWidth - 30) >= 1019 ? this.bodyWidth - 30 : 1019;
        var b = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        if (b > 4) {
            if ($.cookie("gobig") == null) {
                this.gridWidth = this.sizes[2];
                $("#goBig a").show()
            }
        }
        else {
            this.gridWidth = this.sizes[2]
        }
        this.numCols = Math.floor((this.gridWidth + this.gutterSize) / (this.baseColWidth + this.gutterSize));
        $("#productGrid div:nth-child(" + this.numCols + "n)").css("margin-right", "0px");
        var a = (this.numCols * this.baseColWidth) + ((this.numCols - 1) * this.gutterSize);
        $("#main").width(a);
        this.setSiteItems(a)
    },
    render: function(){
        this.init();
        var b;
        var l;
        var f;
        var k = 0;
        var g = 0;
        var e = 0;
        var m = this;
        var c;
        this.items.each(function(d){
            l = m.findCorrectWidth($(this).outerWidth());
            f = $(this).outerHeight();
            $(".itemShadowMid", $(this)).height(f - 24);
            do {
                m.leeway += m.leewayIncrement;
                c = clone(m.colStatus);
                b = m.getInsertionInfo(l, f, c)
            }
            while (b == null);
            g = b.x + (Math.floor(Math.random() * 19) - 9);
            e = b.y + (Math.floor(Math.random() * 19) - 9);
            $(this).css("left", g).css("top", e);
            $(this).css("visibility", "visible");
            m.updateStatus(b.p, b.s, b.y, f)
        });
        var j = this.getGridDimensions();
        var a = (j.w < 989) ? 989 : j.w;
        $("#main").width(a);
        this.elem.height(j.h);
        this.setSiteItems(a)
    }
};
var myGrid;
$(document).ready(function(){
    myGrid = AutoGrid.construct("#grid");
    if ($("#teamsGrid").length > 0) {
        myGrid.renderTeamsGrid()
    }
    else {
        if ($("#productGrid").length > 0) {
            myGrid.renderProductsGrid()
        }
        else {
            myGrid.render()
        }
    }
    $("#header").css("visibility", "visible");
    $("#banner").css("visibility", "visible");
    $("#footer").css("visibility", "visible")
});
$(window).resize(function(){
    if ($("#teamsGrid").length > 0) {
        myGrid.renderTeamsGrid()
    }
    else {
        if ($("#productGrid").length > 0) {
            myGrid.renderProductsGrid()
        }
        else {
            myGrid.render()
        }
    }
});
