﻿//* simple modal plugin

jQuery.fn.showModal = function(options) {
    //get the height and width of the page   
    //var window_width = $(window).width();   
    //var window_height = $(window).height(); 

    return this.each(function() {
        var modalDiv = $('#modal');
        if (modalDiv.length == 0) {

            // put this stuff pretty much at the end of the dom
            $("#h-wrapper").append('<div class="overlay"></div>');
            modalDiv = $('<div id="modal" class="modalDiv"></div>').appendTo("#h-wrapper");

        }
        //* you can remove this if you don't have to support IE6
        if ($.browser.msie && $.browser.version == "6.0") {
            $('select').hide();
            modalDiv.css({ 'position': 'absolute', 'height': $(document).height() - 5, 'width': $(window).width() }).show();
        }
        else
            modalDiv.css({ 'position': 'fixed' }).show();

        var x = $(window).width() / 2 - $(this).outerWidth() / 2;
        var y = $(window).height() / 2 - $(this).outerHeight() / 2;


        $(".overlay")
            .css({ 'width': $(document).width(), 'height': $(document).height(), 'background': $('body').css('background-color'), 'position': 'fixed', 'left': '0px', 'top': '0px', 'z-index': '10000' })
            .fadeTo(0, 0.5);

        var modalHeight = $('.h-popup modal').height();
        var modalWidth = $('.h-popup modal').width();
        var modalHeightCentered = ($(window).height() - modalHeight) / 2;
        var modalWidthCentered = ($(window).width() - modalWidth) / 2;

        $(this)
            .appendTo("form") // yeah, put that near the end of the dom also. or IE7 will crack
            .css({ 'position': 'fixed', 'left': x, 'top': y, 'z-index': '100001' })
            .focus()
            .fadeIn('fast');
        //$(this).css({ 'position': 'absolute', 'top': top , 'left': left,'z-index': '100001'}).focus().slideDown();

    });
};


jQuery.fn.hideModal = function(options) {
	return this.each(function() {
		//* you can remove this if you don't have to support IE6
		if ($.browser.msie && $.browser.version == '6.0')
			$('select').show();
        $(this).slideUp(function() { $('#modal').remove(); });
        $(".overlay").fadeOut(function() { $('.overlay').remove(); });
	});
};


var resizeTimer = null;
$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
	resizeTimer = setTimeout(doResize, 10);
});

function doResize() {
    if (typeof $(window).width == 'function' && typeof $(window).height == 'function') {
        var x = $(window).width() / 2 - $(".h-popup").outerWidth() / 2;
        var y = $(window).height() / 2 - $(".h-popup").outerHeight() / 2;
        $(".overlay")
            .fadeTo(0, 0.5)
            .css({ 'width': $(document).width(), 'height': $(document).height(), 'background': $('body').css('background-color'), 'position': 'fixed', 'left': '0px', 'top': '0px', 'z-index': '10001' });
        $(".h-popup")
            .css({ 'left': x, 'top': y });
    }
}
