/* ##########################################################################
    Copyright 2005 BBB Systems, LLC, All rights reserved
########################################################################## */

/*
The other solutions out there are all based off the model of offsetX and offsetY
including the CSS box-shadow.  Well, designers don't always do shadows like that,
and sometimes you need a little more control, and that is what this is for.

the various sizes support negative numbers which will create a similar effect to
the offset style syntax... but if you're going to do that, just use box-shadow:P

*/

bgzDropShadow = function( ){
    return true;
}

bgzDropShadow.prototype = {

init: function(topSize, rightSize, bottomSize, leftSize, shadowColor, borderRadius, elementID){

    this.shadowColor = shadowColor.match(/^\#?[0-9a-fA-F]{6,6}/) ? shadowColor.replace(/^\#/, '') : '000000';
    this.topSize = parseInt(topSize) != NaN ? parseInt(topSize) : 0;
    this.rightSize = parseInt(rightSize) != NaN ? parseInt(rightSize) : 0;
    this.bottomSize = parseInt(bottomSize) != NaN ? parseInt(bottomSize) : 0;
    this.leftSize = parseInt(leftSize) != NaN ? parseInt(leftSize) : 0;
    this.borderRadius =parseInt(borderRadius) != NaN ?  parseInt(borderRadius) : 0;

    this.elementID = elementID ? elementID : '';
    this.baseNode = null;

    this.base_cur_width = 0;
    this.base_cur_height = 0;

    this.baseDiv = null;


    this.applyToElement( );

},

applyToElement: function( ){
    this.baseNode = document.getElementById(this.elementID)
    if(!this.baseNode){ return; }//no shadow for you

/*
    var old_bnode = document.getElementById('bgzDropShadow_baseNode');
    if(old_bnode){
        old_bnode.parentNode.removeChild(old_bnode);
    }
*/



    var numDivs = Math.max(this.topSize, this.rightSize, this.bottomSize, this.leftSize);

    if(numDivs == 0){ return; }//what's the point?

    var baseParent = this.baseNode.parentNode;

    var bn_offsetX = this.baseNode.offsetLeft;
    var bn_offsetY = this.baseNode.offsetTop;

    var bn_width = this.baseNode.clientWidth ? this.baseNode.clientWidth : this.baseNode.offsetWidth;
    var bn_height = this.baseNode.clientHeight ? this.baseNode.clientHeight : this.baseNode.offsetHeight;

    this.base_cur_width = bn_width;
    this.base_cur_height = bn_height;

    var t_width = bn_width + this.leftSize + this.rightSize - 2;
    var t_height = bn_height + this.topSize + this.bottomSize - 1;

    var scale_height = 0;
    if(this.topSize < 0){ scale_height += this.topSize; }
    if(this.bottomSize < 0){ scale_height += this.bottomSize; }
    scale_height += bn_height;

    if(scale_height <= 0){ return; }//no point

    var opacity_step = Math.round(16 / numDivs);

    this.baseDiv = document.createElement('div');
    this.baseDiv.id = 'bgzDropShadow_baseNode';
    var div_offsetX = bn_offsetX - this.leftSize;
    var div_offsetY = bn_offsetY - this.topSize;
    this.baseDiv.style.position = 'absolute';
    this.baseDiv.style.left = div_offsetX + 'px';
    this.baseDiv.style.top = div_offsetY + 'px';
    this.baseDiv.style.zIndex = '1';

    baseParent.appendChild(this.baseDiv);

    var x_adjust = 0;
    var y_adjust = 0;

    var width_adjust = 0;
    var height_adjust = 0;

    for(var i = 0; i < numDivs; i++){
        var div = document.createElement('div');
        div.className = 'bgzDropShadow';
        //div.style.border = '1px solid #' + this.shadowColor;
        //div.style.backgroundColor = '#' + this.shadowColor;

        var opacityIE = Math.round(opacity_step * (i + 1));
        var opacity = opacityIE / 100;

        div.style.position = 'absolute';
        div.style.opacity = opacity;
        div.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + opacityIE + ")";

        div.style.MozBorderRadius = '0px 0px ' + this.borderRadius + 'px ' + this.borderRadius + 'px';


        if(i < this.topSize){
            if(i > 0){
                y_adjust++;
                height_adjust++;
            }

            div.style.borderTop = '1px solid #' + this.shadowColor;
        }
        if(i < this.rightSize){
            if(i > 0){
                width_adjust++;
            }
            div.style.borderRight = '1px solid #' + this.shadowColor;
        }
        if(i < this.bottomSize){
            if(i > 0){
                height_adjust++;
            }
            div.style.borderBottom = '1px solid #' + this.shadowColor;
        }
        if(i < this.leftSize){
            if(i > 0){
                x_adjust++;
                width_adjust++;
            }
            div.style.borderLeft = '1px solid #' + this.shadowColor;
        }

        div.style.left = parseInt(x_adjust) + 'px';
        div.style.top = parseInt(y_adjust) + 'px';

        div.style.width = parseInt(t_width - width_adjust) + 'px';
        div.style.height = parseInt(t_height - height_adjust) + 'px';

        //alert(div.style.width + ' - ' + div.style.height);

        this.baseDiv.appendChild(div);

        //alert(div.style.width + ' - ' + div.style.height);
    }
},


resizeShadow: function( ){

    if(!this.baseNode){ return; }//no shadow for you

    var bn_width = this.baseNode.clientWidth ? this.baseNode.clientWidth : this.baseNode.offsetWidth;
    var bn_height = this.baseNode.clientHeight ? this.baseNode.clientHeight : this.baseNode.offsetHeight;

    if(bn_width == this.base_cur_width && bn_height == this.base_cur_height){
        return;//no change
    }
    else{
        this.base_cur_width = bn_width;
        this.base_cur_height = bn_height;
    }

    var t_width = bn_width + this.leftSize + this.rightSize - 2;
    var t_height = bn_height + this.topSize + this.bottomSize - 1;

    var scale_height = 0;
    if(this.topSize < 0){ scale_height += this.topSize; }
    if(this.bottomSize < 0){ scale_height += this.bottomSize; }
    scale_height += bn_height;

    if(scale_height <= 0){ return; }//no point

    var x_adjust = 0;
    var y_adjust = 0;

    var width_adjust = 0;
    var height_adjust = 0;

    var divs = this.baseDiv.getElementsByTagName('div');

    for(var i = 0; i < divs.length; i++){
        var div = divs[i];

        if(i < this.topSize){
            if(i > 0){
                y_adjust++;
                height_adjust++;
            }
        }
        if(i < this.rightSize){
            if(i > 0){
                width_adjust++;
            }
        }
        if(i < this.bottomSize){
            if(i > 0){
                height_adjust++;
            }
        }
        if(i < this.leftSize){
            if(i > 0){
                x_adjust++;
                width_adjust++;
            }
        }

        div.style.width = parseInt(t_width - width_adjust) + 'px';
        div.style.height = parseInt(t_height - height_adjust) + 'px';

    }
}

}//end prototype
