var Seed = {
    init            : function() {
        var x=$('meta[@http-equiv=refresh]');
        if(x.length) {
            x=x.get(0).content.split(';')[0];
            if(parseInt(x)) {
                setTimeout(Seed.logoutAlert,(x-60)*1000);
            }
        }
        Seed.fixHeight();
    },

    logoutAlert : function() {
        alert("You've been idle for 59 minutes - you will be logged out in 1 minute");
    },

    getWindowHeight : function() {
        var windowHeight=0;
        if (typeof(window.innerHeight)=='number') {
            windowHeight=window.innerHeight;
        } else {
            if (document.documentElement&& document.documentElement.clientHeight) {
                windowHeight= document.documentElement.clientHeight;
            } else {
                if (document.body&&document.body.clientHeight) {
                    windowHeight=document.body.clientHeight;
                }
            }
        }
        return windowHeight;
    },


    // Set height of box to full height of window when content is less
    // than a window's height
    fixHeight : function() {

        var wh = Seed.getWindowHeight(),
            bh = $("#box1").get(0).clientHeight;

        if(bh<wh) {
            $("#box1").css("height",wh + "px");
            $("#box2").css("height",wh + "px");
            if(!document.all) {
                // fuck you ie
                $("#footer").css("position","absolute");
                $("#footer").css("bottom","0");
            }
        } else {
            $("#box1").css("height",bh + "px");
            $("#box2").css("height",bh + "px");
        }
    },

    // Create an smoked overlay
    // html is the content for the overlay; redirect is the
    // URL for success
    overlay : function(url,callback) {
        var screen = document.getElementById('screen');
        screen.style.display='block';
        screen.style.height=document.body.clientHeight+ 'px';
        $(screen).html('<div id="overlay"></div>');
        $('#overlay').html('<img id="loading" src="img/ajax-loader.gif" alt="Loading" />');
        if(url) {
            $.get(url,function(xml) {
                $("#overlay").html(xml);
                $("#overlay").append('<p id="error" class="alert"></p>');
                $("#overlay").prepend('<p id="titlebar"></p>');
                $('#overlay #cancel').click(Seed.removeOverlay);
                $('#overlay form').submit(Seed.overlaySubmit);
                $("#overlay").Draggable({handle:"#titlebar",zIndex:99,containment:'document'});
                if(typeof(callback)=='function') {
                    callback();
                }
            });
        }
    },

    overlaySubmit : function() {
        var o = $(this).fastSerialize();
            a = this.action;
        $.post(a,o,function(xml) {
            if(xml=='OK') {
                window.location.reload();
            } else {
                Seed.overlayError(xml);
            }
        });
        return false;
    },

    removeOverlay : function() {
        $("#overlay").DraggableDestroy().remove();
        $("#screen").hide();
    },

    overlayError : function(msg) {
        $("#overlay #error").html(msg);
    },

    // For simple errors
    overlayAlert : function(msg,nocancel) {
        Seed.removeOverlay();
        Seed.overlay();
        $('#overlay').html('<div class="alert"><p>'+msg+'</p></div>');
        if(!nocancel) {
            $('#overlay div.alert').append('<p><input type="button" id="cancel" value="Cancel" /></p>');
        }
        $('#overlay #cancel').click(Seed.removeOverlay);
    }
}
$(document).ready(Seed.init);

