﻿/// <reference path="jquery-intellisense.js" />

if (typeof SiteBuilder == 'undefined') {
    SiteBuilder = {};
}

// Center an element classed "dialog".
SiteBuilder.repositionDialog = function() {
    var dialog = $('.dialog');
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var offset = dialog.offset();
    var windowScrollTop = $(window).scrollTop();

    if (offset) {
        var width = dialog.width();
        var height = dialog.height() + 50;
        var x = (windowWidth - width) / 2;
        var y = (windowHeight - height) / 2 + windowScrollTop;

        dialog.css('left', x - offset.left + 'px').css('top', y - offset.top + 'px');
    }
}

SiteBuilder.focusFirstDialogInput = function() {
    $('.dialog input:text').eq(0).focus();
}

// Store the y-scroll position of the given element.
SiteBuilder.holdScrollPosition = function(elem) {
    // When the element scrolls...
    elem.scroll(function() {
        // ...store the scroll value at the body level.
        $('body').data('scroll-y', $(this).scrollTop());
    });

    // If we want to reset the scroll, the server should mark it "no-scroll-hold". This wipes the stored position.
    if (elem.hasClass('no-scroll-hold')) {
        $('body').data('scroll-y', '');
    }

    // When this function is called on pageLoad, if the scroll position has been stored, apply it.
    var scrollY = $('body').data('scroll-y');

    if (scrollY) {
        elem.scrollTop(scrollY);
    }
}
