﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference path="~/js/jquery-1.2.6-vsdoc.js" />
jQuery.browser.avoidTransparentOverlays = (function() {
    var output = false;
    if (jQuery.browser.mozilla && (navigator.userAgent.indexOf("Macintosh") > -1)) {
        var majorVersion = 3.0;
        try {
            majorVersion = parseFloat(jQuery.browser.version.substr(0, 3));
        }
        catch (e) {}
        if (majorVersion < 1.9) {
            output = true;
        }
    }
    return output;
})();

jQuery.browser.avoidPositionFixed = (function() {
    var output = false;
    if (jQuery.browser.msie) {
		if(jQuery.browser.version < 7)
            output = true;
    }
    return output;
})();

jQuery.getGuid = function() {
    var sb = new Sys.StringBuilder();
    for (var i = 0; i < 32; i++) {
        var rnd = Math.round(Math.random() * 15);
        sb.append(rnd > 9 ? String.fromCharCode("A".charCodeAt(0) + (rnd - 10)) : rnd);
    }
    return sb.toString();
};

jQuery.semiTransparentPNG = null;

jQuery.fn.showModal = function() {
    if (!jQuery.semiTransparentPNG) {
        throw new Error('You must define the global jQuery.semiTransparentPNG property to use this method.');
    }
    var $el = $(this);
    var guid = $.getGuid();
    $el.data('guid', guid);

    var width = $el.width();
    var height = $el.height();
    var totalWidth = $(window).width();
    var totalHeight = $(window).height();
    var scrollTop = document.documentElement.scrollTop;


    var newLeft = Math.floor((totalWidth - width) / 2);
    var newTop = Math.floor((totalHeight - height) / 2);

    var $screen = $('<div id="divModalScreen_' + guid + '">');
    $screen.css({
        'position': 'absolute',
        'left': '0px',
        'top': '0px',
        'z-index': '1000',
        'width': $(document).width(),
        'height': $(document).height()
    });

    if (jQuery.browser.avoidTransparentOverlays) {
        $screen.css({
            'background-image': 'url(' + jQuery.semiTransparentPNG + ')',
            'background-repeat': 'repeat'
        });
    }
    else {
        $screen.css({
            'background-color': '#FFF',
            'opacity': 0
        });
    }

    $('body').append($screen);
    //$('body').css('overflow', 'hidden');

    if (!jQuery.browser.avoidTransparentOverlays) {
        $screen.fadeTo('fast', 0.5);
    }

    $el.css({
        "left": newLeft,
        "top": newTop,
        "display": "none",
        'z-index': '1001',
        'position': 'fixed'
    });
	
	if(jQuery.browser.avoidPositionFixed) {
		 $el.css({
			'position': 'absolute'	 
		 });

	}
    $el.fadeIn("fast");
};

jQuery.fn.hideModal = function() {
    var $el = $(this);
    //$('body').css('overflow', '');
    $el.fadeOut("fast");
    var $screen = $('#divModalScreen_' + $el.data('guid'));
    if (jQuery.browser.avoidTransparentOverlays) {
        $screen.remove();
    }
    else {
        $screen.fadeTo("fast", 0.0, function() { $(this).remove() });
    }
};

