/*
* jQuery Floatbox Plugin 1.0
* Copyright (c) 2008 Leonardo Rossetti (motw.leo@gmailcom)
* Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
(function ($) {
    $.floatbox = function (options) {
        var settings = $.extend({
            bg : "floatbox-background",
            box : "floatbox-box",
            content : "",
            fade : true,
            bgConfig : {
                position : ($.browser.msie && $.browser.version === "6.0") ? "absolute" : "fixed",
                zIndex : "8",
                width : "100%",
                height : "100%",
                top : "0px",
                left : "0px",
                backgroundColor : "#000",
                opacity : "0.75",
                display : "none"
            },
            boxConfig : {
                position : ($.browser.msie && $.browser.version === "6.0") ? "absolute" : "fixed",
                zIndex : "9",
                width : ($(window).width() / 4) + "px",
                marginLeft: "-" + ($(window).width() / 4) + "px",
                height : "auto",
                top : "50%",
                left: "50%",
                backgroundColor : "#fff",
                display : "none",
            }
        }, options);
        //inserts the background element in the document
        if (($("#" + settings.bg).size() === 0)) {
            $("<div></div>")
                .attr("id", settings.bg)
                .css(settings.bgConfig)
                .appendTo("body");
        }
        //inserts the floating box in the document
        if (($("#" + settings.box).size() === 0)) {
            $("<div></div>")
                .attr("id", settings.box)
                .css(settings.boxConfig)
                .appendTo("body");
        }
        //shows floating box with background
        var showBox = function () {
            if (settings.fade) {
                $("#" + settings.bg)
                .width(($.browser.msie && $.browser.version === "6.0") ? document.body.clientWidth : "100%")
                .height(($.browser.msie && $.browser.version === "6.0") ? document.body.clientHeight : "100%")
                .fadeIn(200, function () {
                    $("div#" + settings.box).fadeIn(200);
                });
            } else {
                $("#" + settings.bg)
                .width(($.browser.msie && $.browser.version === "6.0") ? document.body.clientWidth : "100%")
                .height(($.browser.msie && $.browser.version === "6.0") ? document.body.clientHeight : "100%")
                .show()
                .parent().find("#" + settings.box).show();
            }
        };
        //hides floatingbox and background
        var closeBox = function () {
            if (settings.fade) {
                $("#" + settings.box).fadeOut(200, function () {
                    $("#" + settings.bg).fadeOut(200);
                });
            } else {
                $("#" + settings.box).hide();
                $("#" + settings.bg).hide();
            }
        };
	//sets html floating box content
        var setBoxContent = function () {
            $("#" + settings.box)
            .html(settings.content)
            .append("<p><a href='javascript:void(0);' class='close-floatbox'>Close</a></p>");
        };
        //inits the floatbox
        var init = function () {
        setBoxContent();
        //adds close box event to link inside floatbox
        $("#" + settings.box)
            .find("a.close-floatbox").click(function () {
                closeBox();
            })
        .end()
        .parent()
        .find("#" + settings.bg).click(function () {
            closeBox();
        });
        //adds cross browser event to esc key to hide floating box
        $(document).keypress(function (e) {
            var escKey = $.browser.mozilla ? 0 : 27;
            if (e.which === escKey) {
                closeBox();
            }
        });
        //sets margin-top according to div height to display box in the middle of the page
        $("#" + settings.box).css("margin-top", "-" + $("#" + settings.box).height() / 2 + "px");
            showBox();
        };
        //if msie6, adds event to browser scroll to keep floatbox ina fixed position and uses css hack for full background size
        if ($.browser.msie && $.browser.version === "6.0") {
            $("body, html").css({height: "100%", width: "100%"});
            $(window).scroll(function () {
                $("#" + settings.box).css("top", document.documentElement.scrollTop +  ($(window).height() / 2) + "px");
            });
        }
        //starts the plugin
        init();
    };
})(jQuery);


