// fsOverlay plugin v0.9.4.12 for jQuery; Code & concept by Adrian Voica 'Adana' (adrian.voica@gmail.com); Varagus(TM)Technologies (http://www.varagustechnologies.com);
(function ($) {
    $.fsoverlay = function (options) {
        $.fsoverlay.options = $.fn.extend({}, $.fsoverlay.defaults, options);
    };

    $.fsoverlay.show = function (htmlContent, callBackFunction, options) {
        if ($.fsoverlay.options == undefined) $.fsoverlay.options = $.fsoverlay.defaults;
        if (options != undefined) $.fsoverlay.options = $.fn.extend($.fsoverlay.options, options);
        if (htmlContent == undefined || callBackFunction == undefined) return false;
        var o = $.fsoverlay.options;
        $(window).unbind(".fsoverlay");
        $("#" + o.container).remove();

		$(document.body).append("<div id='" + o.container + "' style='position: absolute; display: none; left: 0; top: 0px; background-color: " + o.bgcolor + "; z-index: " + o.zindex + ";'></div>");
		if ($.isPlainObject(htmlContent) == 'false'){
			$("#" + o.content).remove();
			$(document.body).append("<div id='" + o.content + "' style='position: absolute; display: none; left: 0; top: 0; " + (o.contentbgcolor != '' && o.contentbgcolor != null ? 'background-color: ' + o.contentbgcolor + ';' : '') + "z-index: " + (parseInt(o.zindex) + 1) + ";'></div>");
			o.contentObj = $("#" + o.content);
			o.contentObj.append(htmlContent);
			o.contentType = 'html';
		} else {
			o.contentObj = $(htmlContent);
			o.contentType = 'object';
		}

		$('select').css('visibility', 'hidden');
		o.contentObj.find('select').css('visibility', '');
		o.contentObj.css('position', 'absolute').css('display', 'none').css('zIndex', (parseInt(o.zindex) + 1));

		var fsoverlayPosition = function () {
			var st = parseInt($(window).scrollTop());
			var ww = $(window).width(), hh = $(window).height();

			$("#" + o.container).css("top", st);
			$("#" + o.container).width(ww).height(hh);
			o.contentObj.css("left", (ww - o.contentObj.outerWidth(true)) / 2).css("top", ((hh - o.contentObj.outerHeight(true)) / 2) + st);
        };

        $(window).bind("resize.fsoverlay", fsoverlayPosition).trigger("resize.fsoverlay");
		if (o.moveOverlay){
			$(window).bind("scroll.fsoverlay", fsoverlayPosition);
		}

		if (o.fadein > 0) o.contentObj.fadeTo(o.fadein, o.opacity);
		$("#" + o.container).fadeTo(o.fadein, o.opacity, function () {
			if (o.fadein > 0) o.contentObj.fadeTo(0, 1);
			else o.contentObj.show();
			o.contentObj.css('opacity', null);
			callBackFunction(o.contentObj, o.container);
		});



        return false;
    };
	

    $.fsoverlay.hide = function () {
        if ($.fsoverlay.options == undefined) return false;
        var o = $.fsoverlay.options;

		o.beforeHide(o.contentObj, o.container);

		var afterHide = function () {
            $(window).unbind(".fsoverlay");
			$("#" + o.container).remove();
			if (o.contentType == 'object'){
				o.contentObj.css('opacity', null);
				o.contentObj.hide();
			} else {
				o.contentObj.remove();
			}
			$('select').css('visibility', '');
        };

		if (o.fadeout > 0){
	        o.contentObj.fadeTo(o.fadeout, 0);
	        $("#" + o.container).fadeTo(o.fadeout, 0, afterHide);
		} else {
			afterHide();
		}

        return true;
    };
    $.fsoverlay.defaults = {
		'moveOverlay': true,
        'beforeHide': function () {},
        'container': 'fsoverlaycontainer',
        'content': 'fsoverlaycontent',
        'zindex': 1001,
        'opacity': 0.7,
        'bgcolor': '#000000',
        'contentbgcolor': '#FFFFFF',
        'fadein': 200,
        'fadeout': 200
    };
})(jQuery);
