function fixer_manager()
{
    var spans = document.getElementsByTagName('span');
    for(var i = 0; i < spans.length; i++) {
        if(spans[i].className.match(/fixer_(cornerseal|tape)_([0-4]+)(_center)?/)) {
            var fixer = new Fixer(spans[i]);
            fixer.setType(RegExp.$1);
            if(RegExp.$1 == 'cornerseal') fixer.setBorder(4);
            fixer.setLoc(RegExp.$2);
            if(RegExp.$3 == '_center') fixer.centering();
            fixer.fix();
        }
    }

}

var Fixer = function(container)
{
    this.container = container;
    this.image = container.firstChild;
}

Fixer.prototype =
{
    center: false,
    hasBorder: false,
    border: 0,
    loc: ['1','2','3','4'],

    setType: function(type)
    {
        switch(type) {
        case 'cornerseal':
            this.item = new Corner;
            break;
        case 'tape':
            this.item = new Tape;
            break;
        }
    },

    setBorder: function(border)
    {
        this.hasBorder = true;
        this.border = border;
    },

    setLoc: function(loc)
    {
        if(loc != 0) this.loc = loc.split('');
    },

    centering: function()
    {
        this.center = true;
    },

    fix: function()
    {
        this.width = this.image.width + this.border * 2;
        this.height = this.image.height + this.border * 2;

        var s = this.container.style;
        s.display = 'block';
        s.position = 'relative';

        this.image.style.position = 'relative';

        if(this.hasBorder) {
            s.border = 'solid 1px #A0816C';
            s.backgroundColor = '#FFF';
            s.width = this.width + 'px';
            s.height = this.height + 'px';
            this.image.style.top = this.border + 'px';
            this.image.style.left = this.border + 'px';
        }

        if(this.center) {
            s.marginLeft = 'auto';
            s.marginRight = 'auto';
            this.image.style.left = '0';
        }

//        for(var i = 1; i < 5; i++) {
        while(loc = this.loc.pop()) {
            var item = this.item.create(loc, this.width, this.height);
            this.container.appendChild(item);
        }

    }

}

var Corner = function(){}

Corner.prototype =
{
    margin: 3,
    width: 31,
    height: 31,
    alpha: 9,
    color: 'brown',

    create: function(loc, fwidth, fheight)
    {
        var item = document.createElement('span');
        var s = item.style;
        s.position = 'absolute';
        s.width = this.width + 'px';
        s.height = this.height + 'px';

        s.width = this.width + 'px';
        s.height = this.height + 'px';
        s.backgroundRepeat = 'no-repeat';
        s.backgroundImage = 'url("/resources/images/common/corners/' + this.color + '/' + loc + '.png")';

        if(this.alpha != 10) {
            s.filter = 'alpha(opacity=' + this.alpha * 10 + ')';
            s['-moz-opacity'] = '0.' + this.alpha;
            s['opacity'] = '0.' + this.alpha;
        }

        switch(loc) {
        case '1':
            s.top = '-' + this.margin + 'px';
            s.left = '-' + this.margin + 'px';
            break;
        case '2':
            s.top = '-' + this.margin + 'px';
            s.left = (fwidth - this.width + this.margin) + 'px';
            break;
        case '3':
            s.top = (fheight - this.height + this.margin) + 'px';
            s.left = '-' + this.margin + 'px';
            break;
        case '4':
            s.top = (fheight - this.height + this.margin) + 'px';
            s.left = (fwidth - this.width + this.margin) + 'px';
            break;
        }

        return item;

    }
}

var Tape = function(){}

Tape.prototype =
{
    width: 43,
    height: 53,
    margin: 16,

    create: function(loc, fwidth, fheight)
    {
        var item = document.createElement('span');
        var s = item.style;
        s.position = 'absolute';
        s.width = this.width + 'px';
        s.height = this.height + 'px';
        s.backgroundRepeat = 'no-repeat';

        s.filter = 'alpha(opacity=70)';
        s['-moz-opacity'] = '0.7';
        s['opacity'] = '0.7';

        switch(loc) {
        case '1':
            s.backgroundImage = 'url("/resources/images/common/tape1.png")';
            s.top = '-' + this.margin + 'px';
            s.left = '-' + this.margin + 'px';
            break;
        case '2':
            s.backgroundImage = 'url("/resources/images/common/tape2.png")';
            s.top = '-' + this.margin + 'px';
            s.left = (fwidth - this.width + this.margin) + 'px';
            break;
        case '3':
            s.backgroundImage = 'url("/resources/images/common/tape2.png")';
            s.top = (fheight - this.height + this.margin) + 'px';
            s.left = '-' + this.margin + 'px';
            break;
        case '4':
            s.backgroundImage = 'url("/resources/images/common/tape1.png")';
            s.top = (fheight - this.height + this.margin) + 'px';
            s.left = (fwidth - this.width + this.margin) + 'px';
            break;
        }

        return item;

    }
}
