;(function ($) {
	
    $.floatbox = function (options) {
	    
	    if ((navigator.appName == "Microsoft Internet Explorer") && (parseFloat(navigator.appVersion.split("MSIE")[1])) < 8) {	
		    var pri = 20;
	    }else{
		    var pri = 3;
	    }
	    
        var settings = $.extend({
            bg : "floatbox-background",
            box : "floatbox-box",
            content : "",
			button: "<a role='button' href='javascript:void(0);' class='close-floatbox'><img src='images/closebox.gif' style='height: 12px; width: 12px; border: 0px; position: absolute; top: 3px; right: "+pri+"px;'></a>",
			desc: "This is a popup box, press esc key to close.",
            fade : false,
			ajax: null,
            bgConfig : {
                position: ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 8,
                width: "100%",
                height: "100%",
                top:  "0px",
                left: "0px",
                backgroundColor: "#000",
                opacity: "0.75",
                display: "none"
            },
            boxConfig : {
                position : ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 9,
                //width: ($(window).width() / 2) + "px",
                padding: "15px "+options.mright+"px 5px 15px",
                border: "1px solid #a2a2a2",
                width: options.plotis+"px",
            //    marginLeft: "-" + (($(window).width()-400) / 4) + "px",
                marginLeft: "-"+(options.plotis / 2)+"px",
                height: options.baukstis,
                overflow: "auto",
                overflowX: "hidden",
                top: "50%",
                left: "50%",
                backgroundColor: "#fff",
                display: "none"
            }
        }, options);

        //inserts floatbox and sets its content
        var showBox = function () {
			var content = typeof settings.content === "string" ? settings.content : settings.content.clone();
            //inserts the background element in the document
            $("<div></div>")
                .bind("click", function () {
                    closeBox();
                })
                .attr("id", settings.bg)
                .css(settings.bgConfig)
                .width(($.browser.msie) ? document.body.clientWidth : "100%")
                .height(options.aukstis)
                .appendTo("body");
            //inserts the floating box in the document
            $("<div></div>")
                .attr({id: settings.box, role: "alertdialog"}) 
                .html("<div class='wysiwyg opened'>"+content+"</div>")
                .append(settings.button)
                .css(settings.boxConfig)
                .appendTo("body")
                .css("margin-top", "-" + $("#" + settings.box).height() / 2 + "px")
                .find(".close-floatbox").bind("click", function () {
                    closeBox();
                })
                .end();
            //checks if it needs to fade or not
            if (settings.fade) {
                $("#" + settings.bg)
                .fadeIn(200, function () {
                    $("div#" + settings.box).fadeIn(200);
                    scroll(0,options.pozicija);
                    scroll(0,options.pozicija2);
                });
            } else {
                $("#" + settings.bg)
                .show()
                .parent().find("#" + settings.box).show();
            }
			//sets if ajax is needed(already detectets if it is POST or GET)
			if (settings.ajax) {
				$.ajax({
					type: settings.ajax.params === "" ? "GET" : "POST",
					url: settings.ajax.url,
					data: settings.ajax.params,
					
					beforeSend: function () {
						$("#" + settings.box).html(settings.ajax.before);
					},
					
					success: function (data) {
						$("#" + settings.box)
							.html(data)
							.append(settings.button)							
							.find(".close-floatbox").bind("click", function () {
								closeBox();
							});
					},
					contentType: "html"
				});
			}
        };
        //hides floatingbox and background
        var closeBox = function () {
            if (settings.fade) {
                $("#" + settings.box).fadeOut(200, function () {
                     $("#" + settings.bg).fadeOut(200, function () {
						$("#" + settings.box).remove();
						$("#" + settings.bg).remove();
                    });
                });
            } else {
				//for opera issues hide first and a timeout is needed to remove the elements
				$("#" + settings.box + ",#" + settings.bg).hide();
				setTimeout(function () {
					$("#" + settings.box).remove();
					$("#" + settings.bg).remove();
				}, 500);
            }
        };
        //inits the floatbox
        var init = function () {
            //shows box
            showBox();
			//adds cross browser event to esc key to hide floating box
            $(document).one("keypress", function (e) {
                var escKey = $.browser.mozilla ? 0 : 27;
                if (e.which === escKey) {
                    closeBox();
                }
            });
			//if msie6, adds event to browser scroll to keep floatbox ina fixed position and uses css hack for full background size
	        if ($.browser.msie) {
	            $("body, html").css({height: "100%", width: "100%"});
	            $(window).bind("scroll", function () {
	                $("#" + settings.box).css("top", document.documentElement.scrollTop +  ($(window).height() / 2) + "px");
	            });
	        }
        };
        //starts the plugin
        init();
    };
})(jQuery);