/*
Border - An element decorator for adding a border of images around HTML elements.
Copyright (C) 2005 Thomas Meyer

http://seal.ifi.unizh.ch/fileadmin/User_Filemount/Publications/meyer-thesis05.pdf

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

function DecoBorder(node, arg) {

    this.node = node;
	this.node.DecoBorder = this;
	
    // arguments
    
    var w = arg && arg.width ? arg.width : 0;
	
    this.topWidth = arg && arg.topWidth ? arg.topWidth : w;
    this.rightWidth = arg && arg.rightWidth ? arg.rightWidth : w;
    this.bottomWidth = arg && arg.bottomWidth ? arg.bottomWidth : w;
    this.leftWidth = arg && arg.leftWidth ? arg.leftWidth : w;
    
    this.offset = arg && arg.offset ? arg.offset : 0;

    this.topOffset = arg && arg.topOffset ? arg.topOffset : this.offset;
    this.rightOffset = arg && arg.rightOffset ? arg.rightOffset : this.offset;
    this.bottomOffset = arg && arg.bottomOffset ? arg.bottomOffset : this.offset;
    this.leftOffset = arg && arg.leftOffset ? arg.leftOffset : this.offset;

    var idPrefix = arg && arg.idPrefix ? arg.idPrefix : '';
    var idSuffix = arg && arg.idSuffix ? arg.idSuffix : '';

    // define border tiles
    this.border = new Array();
    var dir = new Array('n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw');
    
    var i;
    
    for (i = 0; i < dir.length ; ++i )
    {
	var tile = document.createElement('div');
	
	tile.id = idPrefix + dir[i] + idSuffix;
	tile.style.position = 'absolute';
	
	tile.DecoBorder = this;
	
//	tile.innerHTML = '&nbsp;';

	this.border[dir[i]] = tile;
    }
    
    var b = this.border;
    var tw = this.topWidth;
    var rw = this.rightWidth;
    var bw = this.bottomWidth;
    var lw = this.leftWidth;
    var o = this.offset;
    var br = 'backgroundRepeat';
    var x = 'px';
    
    b.n.style.left = o + x;
    b.n.style.top = (o - tw) + x;
    b.n.style.right = o + x;
    b.n.style.height = tw + x;
    b.n.style[br] = 'repeat-x';
	
    b.ne.style.right = (o - rw) + x;
    b.ne.style.top = (o - tw) + x;
    b.ne.style.width = rw + x;
    b.ne.style.height = tw + x;
    b.ne.style[br] = 'no-repeat';
	
    b.e.style.right = (o - rw) + x;
    b.e.style.top = o + x;
    b.e.style.width = rw + x;
    b.e.style.bottom = o + x;
    b.e.style[br] = 'repeat-y';

    b.se.style.right = (o - rw) + x;
    b.se.style.bottom = (o - bw) + x;
    b.se.style.width = rw + x;
    b.se.style.height = bw + x;
    b.se.style[br] = 'no-repeat';
    
    b.s.style.left = o + x;
    b.s.style.bottom = (o - bw) + x;
    b.s.style.right = o + x;
    b.s.style.height = bw + x;
    b.s.style[br] = 'repeat-x';
    
    b.sw.style.left = (o - lw) + x;
    b.sw.style.bottom = (o - bw) + x;
    b.sw.style.width = lw + x;
    b.sw.style.height = bw + x;
    b.sw.style[br] = 'no-repeat';

    b.w.style.left = (o - lw) + x;
    b.w.style.top = o + x;
    b.w.style.width = lw + x;
    b.w.style.bottom = o + x;
    b.w.style[br] = 'repeat-y';
    
    b.nw.style.left = (o - lw) + x;
    b.nw.style.top = (o - tw) + x;
    b.nw.style.width = lw + x;
    b.nw.style.height = tw + x;
    b.nw.style[br] = 'no-repeat';
    
    // add border tiles to node
    for (i in this.border)
    {
	this.node.appendChild(this.border[i]);
    }
    
    // parameter: position
    if (arg && arg.position) {
	this.setPosition(arg.position);
    } else {
	this.setPosition();
    }
    
    var imagePrefix = arg && arg.imagePrefix ? arg.imagePrefix : '';
    var imageSuffix = arg && arg.imageSuffix ? arg.imageSuffix : '';
    
    if (imagePrefix || imageSuffix) {
	this.setImages(imagePrefix, imageSuffix);
    }
}


DecoBorder.prototype = {
    
    setStyle: function(property, value)
    {
	for (i in this.border)
	{
	    this.border[i].style[property] = value;
	}
    }

    ,setImages: function(prefix, suffix)
    {
	for (i in this.border)
	{
	    this.border[i].style.backgroundImage = 'url(' + prefix + i + suffix + ')';
	}
    }

    ,setPosition: function(position)
    {
	var b = this.border;
	var p = 'backgroundPosition';
	
	switch(position)
	{
	    case 'inside':
		b.nw.style[p] = 'left top';
		b.n.style[p] = 'top';
		b.ne.style[p] = 'right top';
		b.w.style[p] = 'left';
		b.e.style[p] = 'right';
		b.sw.style[p] = 'left bottom';
		b.s.style[p] = 'bottom';
		b.se.style[p] = 'right bottom';
		break;

	    default:
		b.nw.style[p] = 'right bottom';
		b.n.style[p] = 'bottom';
		b.ne.style[p] = 'left bottom';
		b.w.style[p] = 'right';
		b.e.style[p] = 'left';
		b.sw.style[p] = 'right top';
		b.s.style[p] = 'top';
		b.se.style[p] = 'left top';
		break;
	}
    }

	,remove: function()
	{
		for (i in this.border)
    	{
			this.node.removeChild(this.border[i]);
    	}

		this.node.DecoBorder = null;
	}
	
};
