var IE6 = $.browser.msie && $.browser.version.substr(0,1) == 6 && !window.XMLHttpRequest;
$.fn.init_ecpopup = function() {
	 html = '<div id="ecpopup_overlay"></div>';
	 html += '<div id="ecpopup">';
	 html += '    <div id="ecpopup_content"></div>';
     html += '    <div id="ecpopup_toolbar"><a href="#" class="ecpopup_close">Закрыть</a></div>';
     html += '</div>';
     html += '<div id="ecpopup_small">';
     html += '    <div id="ecpopup_small_content"></div>';
     html += '    <div id="ecpopup_small_toolbar"><a href="#" class="ecpopup_close">Закрыть</a></div>';
     html += '</div>';
	 $(html).appendTo("body");
}
$.fn.ecpopup = function(settings){

	var defaults = {
		content: null,
		url: null,
		small : false,
        nocache : true
	}
	
	var settings = $.extend({}, defaults, settings);
	
	$(window).bind("resize.fb scroll.fb", $.fn.ecpopup.scroll);
	if (IE6) {
		$('embed, object, select').css('visibility', 'hidden');
		$("#ecpopup_overlay").css('height', $(document).height());
	}
	if (!IE6) {
		$("#ecpopup_overlay").css({
			'background-color': "#666",
			'opacity': "0.3"
		}).show();
	}
	if (settings.small) {
		id = "ecpopup_small";
	} else {
		id = "ecpopup";
	}
	if (settings.url) {
	    $("#"+id+"_content").html("Загрузка...");
        if (settings.nocache) {
            if (settings.url.indexOf("?") >= 0) {
                url = settings.url + "&random=" + Math.random() * 9999999;
            }
            else {
                url = settings.url + "?random=" + Math.random() * 9999999;
            }
        } else {
            url = settings.url;
        }
	    $("#"+id+"_content").load(url);
	} else {
		$("#"+id+"_content").html($(settings.content));
	}
    $("#"+id).fadeIn();
	
	
    $(document).bind("keydown.fb", function(e) {
        if (e.keyCode == 27) {
            $.fn.ecpopup.close();
        }
    });
    $("#ecpopup_overlay").bind("click", $.fn.ecpopup.close);
	$(".ecpopup_close").bind("click", $.fn.ecpopup.close);
    $.fn.ecpopup.scroll();   
}
$.fn.ecpopup.close = function () {
	$(document).unbind("keydown.fb");
    $(window).unbind("resize.fb scroll.fb");
    
	$("#ecpopup_content").empty();
	$("#ecpopup_small_content").empty();
	
	$('embed, object, select').css('visibility', '');
    $("#ecpopup_overlay").fadeOut("fast");    
    $("#ecpopup").fadeOut("fast");
    $("#ecpopup_small").fadeOut("fast");
    return false;
}


$.fn.ecpopup.scroll = function() {
        var w = [$(window).width(), $(window).height(), $(document).scrollLeft(), $(document).scrollTop() ];
        
        if ($("#ecpopup").is(':visible')) {
            var ow  = $("#ecpopup").outerWidth();
            var oh  = $("#ecpopup").outerHeight();

            var pos = {
                'top'   : (oh > w[1] ? w[3] : w[3] + Math.round((w[1] - oh) * 0.5)),
                'left'  : (ow > w[0] ? w[2] : w[2] + Math.round((w[0] - ow) * 0.5))
            };

            $("#ecpopup").css(pos);
        }
        
		if ($("#ecpopup_small").is(':visible')) {
            var ow  = $("#ecpopup_small").outerWidth();
            var oh  = $("#ecpopup_small").outerHeight();

            var pos = {
                'top'   : (oh > w[1] ? w[3] : w[3] + Math.round((w[1] - oh) * 0.5)),
                'left'  : (ow > w[0] ? w[2] : w[2] + Math.round((w[0] - ow) * 0.5))
            };

            $("#ecpopup_small").css(pos);
        }
		
        if (IE6 && $("#ecpopup_overlay").is(':visible')) {
            $("#ecpopup_overlay").css({
                'height' : $(document).height()
            });
        }
    };


$(document).ready(function() {
    $.fn.init_ecpopup();
});
