;(function ($) {
	
    $.floatbox = function (options) {
	    var naukstis = options.baukstis;
	    var koksplotis = options.wdth*1+500;
	    var nuokaires = 100-(koksplotis*50/500);
        var settings = $.extend({
            bg : "floatbox-background",
            box : "floatbox-box",
            content : "",
			button: "<div style='position: absolute; top: 1px; right: 2px'><a role='button' href='javascript:void(0);' class='close-floatbox'><img src='/images/closebox.png' style='height: 15px; width: 15px; border: 0px;'></a></div>",
			desc: "This is a popup box, press esc key to close.",
            fade : false,
			ajax: null,
            bgConfig : {
                position: ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 200,
                width: "100%",
                height: "100%",
                top:  "0px",
                left: "0px",
                backgroundColor: "#000",
                opacity: "0.35",
                display: "none"
            },
            
            boxConfig : {
                position : ($.browser.msie) ? "absolute" : "fixed",
                zIndex: 201,
                //width: ($(window).width() / 2) + "px",
                padding: "15px 1px 15px 15px",
                border: "1px solid #cdcfd4",
                width: koksplotis+"px",
               // marginLeft: "-" + (($(window).width()-koksplotis) / 2) + "px",
               left: (($(window).width()/2-koksplotis/2) ) + "px",
                height: options.baukstis,
                overflow: "auto",
                overflowX: "hidden",
                top: "45%",
                //left: nuokaires+"%",
                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 () {
				   if (document.getElementById('partn')) {
	                if (document.getElementById('partn').style.display == 'none') {
		                 document.getElementById('partn').style.display='block';
		                 document.getElementById('whol').style.height='';
		            }
	         	   }   
                    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 style="overflow: auto; overflow-x: hidden; width:'+options.pwidth+'px; height: '+naukstis+'px; padding: 0px '+options.mright+'px 0px 1px" class="opened">'+content+'</div>')
                .bind("mouseenter", function(ev){
	               
	               $(this).find('.laikas').bind("click", function(e){
		            if ($(this).parent().find("#laiko_paaiskinimas").css('display')=="none"){
		             $(this).parent().find("#laiko_paaiskinimas").animate({opacity:'show'},20);
		             var lf = 100;
		             var tp = $(this).offset().top;
		             
		             if($.browser.msie){lf=lf-15; tp=tp-40}
		             
		             $(this).parent().find("#laiko_paaiskinimas").css({left:lf,top:tp})
	           	   }	   
		             
		           })
	           })
                .bind("mouseout", function(ev){
	               $(this).find("#laiko_paaiskinimas").animate({opacity:'hide'},20); 
	          //     $(this).find('.laikas').bind("mouseout", function(e){
		      //       $(this).parent().find("#laiko_paaiskinimas").animate({opacity:'hide'},20);
		      //     })
	            })	            
	            
	            
                .append(settings.button)
                .css(settings.boxConfig)
                .appendTo("body")
                .css("margin-top", "-" + $("#" + settings.box).height() / 2 + "px")
                .find(".close-floatbox").bind("click", function () {
				   if (document.getElementById('partn')) {
	                if (document.getElementById('partn').style.display == 'none') {
		                 document.getElementById('partn').style.display='block';
		                 document.getElementById('whol').style.height='';
		            }
	         	   }   	                
                    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);
