﻿/// <reference path="./jquery-1.3.2-vsdoc.js" />
/// <reference path="./Agility.js" />

// Scrolling code adapted from http://www.switchonthecode.com/tutorials/javascript-tutorial-the-scroll-wheel

Agility.RegisterNamespace("Participaction");

(function(Participaction, $) {

    Participaction.FeaturedStoryPopup = function (containerID) {
        this.containerID = containerID;
        this.scrollEventsBound = false;
    };
    
    Participaction.FeaturedStoryPopup.prototype.bind = function () {
        var closeButton = $("#" + this.containerID + "_CloseButton"),
            obj = this;
        closeButton.click(function () {
            obj.hide();
        });
    };
    
    Participaction.FeaturedStoryPopup.prototype.show = function () {
        var scrollingContainer = document.getElementById(this.containerID + "_ScrollingContainer"),
            scrollingInterior = $("#" + this.containerID + "_ScrollingInterior"),
            modalBlocker = $(".ModalPopupBackground").get(0),
            container = $("#" + this.containerID).get(0);
        
        $find("ShowFeaturedStory").show();
        
        if (!this.scrollEventsBound) {
            addScrollEvent(scrollingContainer, scrollingInterior, container, modalBlocker);
            this.scrollEventsBound = true;
        }
    };
    
    Participaction.FeaturedStoryPopup.prototype.hide = function () {
        $find("ShowFeaturedStory").hide();
    };
    
    function addScrollEvent(scrollingContainer, scrollingInterior, container, modalBlocker) {
        function cancelScroll(e) {
            e = e || window.event;
            if (e.stopPropagation) {
                e.stopPropagation();
            }
            if (e.preventDefault) {
                e.preventDefault();
            }
            e.cancelBubble = true;
            e.cancel = true;
            e.returnValue = false;
            return false;        
        }
        
        function scrollingContainerCallback(args) {
            var e = args ? args : window.event,
                scrollAmount = args.detail ? args.detail * -1 : args.wheelDelta / 40;
            if (atBottom(scrollingContainer, scrollingInterior) && scrollAmount < 0
                || atTop(scrollingContainer) && scrollAmount > 0) {
                cancelScroll(args);    
            } else {
                if (e.stopPropagation) {
                    e.stopPropagation();
                }
                e.cancelBubble = true;
            }
        }
        
        function addListeners(domObj, callback) {
            if (domObj.addEventListener) {
                domObj.addEventListener("DOMMouseScroll", callback, false);
                domObj.addEventListener("mousewheel", callback, false);
            } else if (domObj.attachEvent) {
                domObj.attachEvent("onmousewheel", callback);
            }
        }
        
        addListeners(scrollingContainer, scrollingContainerCallback);
        addListeners(container, cancelScroll);
        addListeners(modalBlocker, cancelScroll);
    }
    
    function atBottom(scrollingContainer, scrollingInterior) {
        return $(scrollingContainer).scrollTop() >= scrollingInterior.height() - $(scrollingContainer).height();
    }
    
    function atTop(scrollingContainer) {
        return $(scrollingContainer).scrollTop() === 0;
    }

})(Participaction, jQuery);
