function resize_image(id,maxWidth,maxHeight) {
    var image = $("#" + id);
    var width  = image.attr('width');
    var height = image.attr('height');

    if( width <= maxWidth && height <= maxHeight) {
        return {'width': width, 'height': height};
    }

    var deltaW = image.attr('width')/ maxWidth;
    var deltaH = image.attr('height')/maxHeight;

    if(deltaW > deltaH) {
        width = image.attr('width')/ deltaW;
        height = image.attr('height')/deltaW;
    } else {
        width = image.attr('width') / deltaH;
        height = image.attr('height')/deltaH;
    }

    image.attr('width',width);
    image.attr('height',height);

    return {'width': width, 'height': height};
}

function document_width() {
    // handle IE 6
    if ($.browser.msie && $.browser.version < 7) {
        var scrollWidth = Math.max(
            document.documentElement.scrollWidth,
            document.body.scrollWidth
        );
        var offsetWidth = Math.max(
            document.documentElement.offsetWidth,
            document.body.offsetWidth
        );

        if (scrollWidth < offsetWidth) {
            return $(window).width() + 'px';
        } else {
            return scrollWidth + 'px';
        }
    // handle "good" browsers
    } else {
        return $(document).width() + 'px';
    }
}

function document_height() {
    // handle IE 6
    if ($.browser.msie && $.browser.version < 7) {
        var scrollHeight = Math.max(
            document.documentElement.scrollHeight,
            document.body.scrollHeight
        );
        var offsetHeight = Math.max(
            document.documentElement.offsetHeight,
            document.body.offsetHeight
        );

        if (scrollHeight < offsetHeight) {
            return $(window).height() + 'px';
        } else {
            return scrollHeight + 'px';
        }
    // handle "good" browsers
    } else {
        return $(document).height() + 'px';
    }
}

function element_position (element,pos) {
    var wnd = $(window), doc = $(document),
        pTop = doc.scrollTop(), pLeft = doc.scrollLeft(),
        minTop = pTop;

    if ($.inArray(pos, ['center','top','right','bottom','left']) >= 0) {
        pos = [
            pos == 'right' || pos == 'left' ? pos : 'center',
            pos == 'top' || pos == 'bottom' ? pos : 'middle'
        ];
    }
    if (pos.constructor != Array) {
        pos = ['center', 'middle'];
    }
    if (pos[0].constructor == Number) {
        pLeft += pos[0];
    } else {
        switch (pos[0]) {
            case 'left':
                pLeft += 0;
                break;
            case 'right':
                pLeft += wnd.width() - element.outerWidth();
                break;
            default:
            case 'center':
                pLeft += (wnd.width() - element.outerWidth()) / 2;
        }
    }
    if (pos[1].constructor == Number) {
        pTop += pos[1];
    } else {
        switch (pos[1]) {
            case 'top':
                pTop += 0;
                break;
            case 'bottom':
                pTop += wnd.height() - element.outerHeight();
                break;
            default:
            case 'middle':
                pTop += (wnd.height() - element.outerHeight()) / 2;
        }
    }

    // prevent the dialog from being too high (make sure the titlebar
    // is accessible)
    pTop = Math.max(pTop, minTop);
    return {top: pTop, left: pLeft};
}

function toggle_module(id,isToggle) {
    if(isToggle == "0") {
        $("#toggle_panel_" + id).hide();
    } else {
        $("#toggle_panel_" + id).show();
    }

    $("#toggle_panel_head_" + id).click(function(){
            $("#toggle_panel_" + id).slideToggle("normal");
            return false;
    });
}

$(document).ready(function(){

    $('form').submit(function(){
        $('body').startWaiter();
    });

    $('body').ajaxStop(function(){
       $('body').destroyWaiter();
    });

    $('body').ajaxStart(function(){
       $('body').startWaiter();
    });
});

function message(title,message) {
    if($("body").find("div").attr("id") != "system_message") {
        var my_div = $("<div id=\"system_message\" title=\"\"><p></p></div>");
        my_div.appendTo("body");
    }

    $("#system_message").attr('title',"" + title);
    $("#system_message").text("" + message);

    $("#system_message").dialog({
			bgiframe: true,
			height: 300,
                        width:  400,
			modal: true,
            autoOpen: false,
            resizable: false,
            buttons: {
				Ok: function() {
					$(this).dialog('close');
				}
			}

		});

    $("#system_message").dialog('open');
}

function refresh_module(id,time) {
   $("body").everyTime(parseInt(time), function(){
       xajax_ajaxRefreshModule(id,"hello");
   });
}

xajax.loadingFunction = function() {
    if(function_name != 'ajaxRefreshModule') {
        $('body').startWaiter();
    }
};
xajax.doneLoadingFunction = function ()
{
    if(function_name != 'ajaxRefreshModule') {
        $('body').destroyWaiter();
    }
}


