
/** NAMESPACE **/
var FSN = {};


/*********************************************/
/*******   PAGE CONTROLLER   *****************/
/*********************************************/

jQuery(function(){

    /*** INIT ***/
        var UPDATE_PROGRESS_MIN_SHOW_TIME = 500;
        var updateProgress = jQuery(".UpdateProgress");
        var requestStartTime = 0;
        
        initPage();
        
    /*** FUNCTIONS ***/
        function initPage() {
            initUpdateProgress();
            initInfoBox();
            addDotNetRequestHandlers();
        }
        function addDotNetRequestHandlers() {
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(handleBeginRequest);
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
        }
        function handleBeginRequest(sender, args) {
            layoutUpdateProgress();
            updateProgress.show();
            requestStartTime = getTime();
        } 
        function handleEndRequest(sender, args) {
            layoutInfoBox();
            var requestTime = getTime() - requestStartTime;
            var wait = Math.max( 0, UPDATE_PROGRESS_MIN_SHOW_TIME - requestTime );
            window.setTimeout(function(){updateProgress.hide()},wait);
        }
        function initUpdateProgress() {
            updateProgress._bg = updateProgress.find(".background");
            updateProgress._bg_width  = updateProgress._bg.width();
            updateProgress._bg_height = updateProgress._bg.height();
            updateProgress._icon = updateProgress.find(".icon");
            updateProgress._icon_width  = updateProgress._icon.width();
            updateProgress._icon_height = updateProgress._icon.height();
            updateProgress._text = updateProgress.find(".text");
            updateProgress._text_width  = updateProgress._text.width();
            updateProgress._text_height = updateProgress._text.height();
            updateProgress.hide();
            layoutUpdateProgress();
        }
        function layoutUpdateProgress() {
            var wp = jQuery.viewportSize();
            updateProgress.fillViewport();
            updateProgress.css({
                top: getScroll() + "px"
            });
            updateProgress._bg.css({
                left: (wp.width/2) - (updateProgress._bg_width/2) + "px",
                top: (wp.height/2) - (updateProgress._bg_height/2) + "px"
            });
            updateProgress._icon.css({
                left: (wp.width/2) - (updateProgress._icon_width/2) + "px",
                top: (wp.height/2) - (updateProgress._icon_height*1.5) + "px"
            });
            updateProgress._text.css({
                left: (wp.width/2) - (updateProgress._text_width/2) + "px",
                top: (wp.height/2) - (updateProgress._text_height/2) + "px"
            });
        }
        function initInfoBox() {
            jQuery(document).bind("click",function(event){
                var clicked = jQuery(event.target);
                if( isLinkOrDescendantOfLink(clicked) && !clicked.hasClass("close") ) { return; }
                var box = jQuery(".BehandlingsstedInfo");
                var isBoxOpen = (new String(box.html())).length > 20;
                if( isBoxOpen ) {
                    event.preventDefault();
                    var closeScript = box.find("a.close").attr("href").replace("javascript:","");
                    eval(closeScript);
                }
            });
        }
        function isLinkOrDescendantOfLink(clicked) {
            var result = false;
            if(clicked.is("a")) {
                result = true;
            } else if(clicked.parents("a").size() > 0) {
                result = true;
            }
            return result;
        }
        function layoutInfoBox() {
            var box = jQuery(".BehandlingsstedInfo");
            if( box.size() == 0 ) { return; }
            var body = jQuery(document.body);
            var scroll = getScroll();
            var width = box.width();
            var height = box.height();
            var wp = jQuery.viewportSize();
            var top = (scroll + (wp.height/2) - (height/2) );
            if( top < 0 ) { top = 0; }
            box.css({
                left: ( (wp.width/2) - (width/2) ) + "px",
                top:  top + "px"
            });
        }
        function getTime() {
            return (new Date()).getTime();
        }
        function getScroll() {
            if (self.pageYOffset) {
	            return self.pageYOffset;
            } else if (document.documentElement && document.documentElement.scrollTop) {
	            return document.documentElement.scrollTop;
            } else if (document.body) {
	            return document.body.scrollTop;
            }
        }
});


/*********************************************/
/*******   JQUERY PLUGINS    *****************/
/*********************************************/

jQuery.fn.fillViewport = function() {
    var element = jQuery(this.eq(0));
    if( "absolute" != element.css("position") ) { return; }
    var wp = jQuery.viewportSize();
    element.css({
        left: 0,
        top:  0,
        width: wp.width+"px",
        height: wp.height+"px"
    });
    
}


jQuery.viewportSize = function() {
    var height = 0;
    var width = 0;
    if( document.documentElement != null ) {
        height = document.documentElement.offsetHeight;
        width = document.documentElement.offsetWidth - 50;
    } else {
        var body = jQuery(document.body);
        width = body.width();
        height = body.height();
    }
    return {width:width,height:height};
}