﻿Type.registerNamespace('mps');

window.name = "desktop";

mps.guid = (function() {
    var guid_counter = 0;
    return function() { return "guid_"+guid_counter++; };
})();

(function($) {

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

    mps.openHelp = function(e)
    {
        mps.popupOpen(e, 600, 400, mps.AppPath + 'pages/HelpPopup.aspx?pg=@' + escape(window.location.href));
    }

    mps.popupOpen = function(e, width, height, url)
    {
        var posx = 0;
        var posy = 0;
        if (!e) e = window.event;
        if (e) {
            if (e.pageX || e.pageY) 	{
                posx = e.pageX;
                posy = e.pageY;
            }
            else if (e.clientX || e.clientY) 	{
                posx = e.clientX + document.body.scrollLeft
					+ document.documentElement.scrollLeft;
                posy = e.clientY + document.body.scrollTop
					+ document.documentElement.scrollTop;
            }
        }
        else {
            var wnd = $(window), doc = $(document);
            posy = doc.scrollTop() + Math.floor(wnd.height()/2); 
            posx = doc.scrollLeft() + Math.floor(wnd.width()/2);
        }
        mps.popupOpenXY(posx, posy, width, height, url);
    };

    mps.popupOpenXY = function(ex, ey, width, height, url)
    {
        var wnd = $(window), doc = $(document), top = doc.scrollTop(), left = doc.scrollLeft();
        top += (wnd.height() / 2) - (height / 2);
        left += (wnd.width() / 2) - (width / 2);
        var id = mps.guid();
        var boxid = mps.guid();

        $('div.ui-dialog').css({zIndex:9998});

        // start the dashed box animation
        $('<div id="' + boxid + '" style="position:absolute;top:'+ey+'px;left:'+ex+'px;width:10px;height:10px;border:dashed 1px black;z-index:9999"></div>')
            .appendTo('#windows')
            .animate({top:top-32, left:left-9, width:width+20, height:height+43}, 600, "easeinout", function(){ $('#'+boxid).remove(); $('#'+id).dialogOpen(); });

        // create the iframe and make it a dialog
        var iframe = $('<iframe class="ui-dialog-iframe" style="width:100%;height:100%;" frameborder="0"></iframe>').appendTo('#windows').dialog({width:width, height:height, openOnCreate:false});
        var dialog = iframe.parents(".ui-dialog:first");
        iframe.attr('src',url);

        // set the id's so we can find it later
        dialog.attr('id', id);
        iframe.attr('dialog', id);

        var titlebar = $('.ui-dialog-titlebar', dialog)
            .append('<div class="ui-dialog-titlebar-help"></div>')
            .append('<div class="ui-dialog-icon"></div>');

		$('.ui-dialog-titlebar-help', titlebar)
			.hover(function() { $(this).addClass('ui-dialog-titlebar-help-hover'); }, 
			       function() { $(this).removeClass('ui-dialog-titlebar-help-hover'); })
			.mousedown(function(ev) {
				ev.stopPropagation();
			})
			.click(function(ev) {
                mps.popupOpen(ev, 600, 400, mps.AppPath + 'pages/HelpPopup.aspx?pg=@' + escape(url));
			});
        
    };
    mps.popupClose = function(id)
    {
        $('#'+id).fadeOut(400, function() { jQuery('#'+id).dialogClose(); } );
    };
    mps.popupCancel = function(id)
    {
        mps.popupClose(id);
    };
    mps.popupTitle = function(id, title)
    {
        $('#'+id+' span.ui-dialog-title').html(title);
    };
    mps.popupIconClass = function(id, iconClass)
    {
        $('#'+id+' div.ui-dialog-icon').className = "ui-dialog-icon " + iconClass
    };
    mps.popupIconClassAdd = function(id, iconClass)
    {
        $('#'+id+' div.ui-dialog-icon').addClass(iconClass);
    };
    mps.popupIconClassRemove = function(id, iconClass)
    {
        $('#'+id+' div.ui-dialog-icon').removeClass(iconClass);
    };
    mps.desktopEval = function(script)
    {
        if (window.execScript) window.execScript(script);
        else eval(script);
    };

    mps.refreshSnipit = function(id, name)
    {
        window.setTimeout("mps.refreshSnipitCall(\"" + id + "\",\"" + name + "\");");
    };
    mps.refreshSnipitCall = function(id, name)
    {
        mps.ScriptServices.GetSnipit(id, name, mps.refreshSnipitCallback);
    };
    mps.refreshSnipitCallback = function(result, eventArgs)
    {
        $j('#'+result[0])[0].innerHTML = result[1];
    };

})(jQuery);


