/*
Copyright 2008-2011 by PE Eliseev Stanislav
The program is distributed under the terms of the GNU General Public License version 3.

This file is part of ForSiter http://forsiter.com

    ForSiter 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 version 3 of the License.

    ForSiter 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 ForSiter.  If not, see <http://www.gnu.org/licenses/>.
 */

rShadowBox = function (settings)
{
    this.settings = $.extend ({}, rShadowBox.defaultsSettings, settings);
    this.create ();
    
    this.onClick = [];
    this.addOnClick (this.settings.onClick);
};

rShadowBox.prototype.addOnClick = function (func)
{
    if (func) {
        this.onClick.push (func);
    }
    
};

rShadowBox.prototype.create = function ()
{
    $ ('#' + this.settings.id).remove ();
    
    $ ('<div id="' + this.settings.id + '"></div>').appendTo ('body');
    this.setStartSettings ();
    
    var cmp = this;
    $ (window).resize (function ()
    {
        cmp.resize (cmp);
    });
    
    $ ('#' + this.settings.id).bind ('click', function ()
    {
        cmp.click (cmp);
    });
};

rShadowBox.prototype.setStartSettings = function ()
{
    this.css ({
        zIndex : this.settings.zIndex,
        width : '0px',
        height : '0px',
        position : 'absolute',
        top : this.settings.top,
        left : this.settings.left,
        opacity : this.settings.opacity,
        backgroundColor : this.settings.backgroundColor,
        display : 'none',
        cursor : this.settings.cursor
    });
};

rShadowBox.prototype.css = function (cssObject)
{
    if ($ ('#' + this.settings.id).get (0)) {
        $ ('#' + this.settings.id).css (cssObject);
    }
};

rShadowBox.prototype.resize = function (cmp)
{
    if (cmp.css) {
        cmp.css ({
            width : '0',
            height : '0'
        });
        cmp.css ({
            width : $ (document).width (),
            height : $ (document).height ()
        });
    }
};

rShadowBox.prototype.show = function (cssObject)
{
    if (cssObject) {
        this.css (cssObject);
    }
    $ ('#' + this.settings.id).show ();
    this.css ({
        width : $ (document).width (),
        height : $ (document).height ()
    });
};

rShadowBox.prototype.hide = function ()
{
    this.css ({
        width : '0',
        height : '0'
    });
    $ ('#' + this.settings.id).hide ();
    this.setStartSettings ();
};

rShadowBox.prototype.click = function (cmp)
{
    if (cmp.settings.hideOnClick) {
        cmp.hide ();
    }
    
    for ( var i = 0; i < cmp.onClick.length; i ++ ) {
        var func = cmp.onClick[ i ];
        func ();
    }
};

rShadowBox.defaultsSettings = {
    id : 'rShadowBox',
    zIndex : 999,
    width : null,
    height : null,
    opacity : 0.7,
    backgroundColor : "#ffffff",
    cursor : "wait",
    top : '0',
    left : '0',
    hideOnClick : true,
    
    onClick : null
};

