YAHOO.namespace("SW.local.CommunityArea");
(function(){
    var yuiDom = YAHOO.util.Dom;
    var yuiEvent = YAHOO.util.Event;

    var nHContainer = null;
    var nGSContainer = null;
    var nGSContainerTimeOut = null
    var nHContainerTimeOut = null;
    var normalizerTimeout = null

    SW.local.CommunityArea = {
        init: function() {
            nHContainer = yuiDom.get("newHotelsSection");

            var nHNormalizer = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsSection"),
                showAttributes: {width:{to: 278}},
                hideAttributes: {width:{to: 0}},
                showOnStart: function(config) {
                    nHContainer.contentNormalizer.show();
                    nHContainer.openToggler.hide();
                    nHContainer.closeToggler.hide();
                },
                showDuration:.500,
                hideDuration:.500
            });
            var nHContentNormalizer = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsContentContainer"),
                showAttributes: {width:{to: 278}},
                showOnStart: function () {
                    yuiDom.addClass(yuiDom.get("newHotels_leftContent"), "indicator");
                },
                showDuration:.500,
                hideDuration:.500
            });

            var nHToggler = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsSection"),
                showAttributes: {width:{to: 535}},
                hideAttributes: {width:{to: 23}},
                showOnStart: function(config) {
                    nHContainer.openToggler.hide();
                    nHContainer.contentToggler.show();
                    nGSContainer.toggler.hide();
                    yuiDom.removeClass(yuiDom.get("newHotels_leftContent"), "indicator");
                },
                showOnComplete: function() {
                    nHContainer.closeToggler.show();
                },
                hideOnStart: function() {
                    nHContainer.contentToggler.hide();
                    nHContainer.closeToggler.hide();
                },
                hideOnComplete: function () {
                    nHContainer.openToggler.show();
                },
                showDuration:.500,
                hideDuration:.500
            });

            var nHContentToggler = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsContentContainer"),
                showAttributes: {width: {to: 515}},
                hideAttributes: {width: {to: 0}},
                showDuration:.800,
                hideDuration:.500
            });

            var nHOpenHandleToggler = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsOpenHandle"),
                showAttributes: {width: {to: 11}, height: {to: 100, unit: "%"}},
                hideAttributes: {width: {to: 0}, height: {to: 0}},
                showDuration:.300,
                hideDuration:.300
            });

            var nHCloseHandleToggler = SW.widget.displayToggler({
                element: yuiDom.get("newHotelsCloseHandle"),
                showAttributes: {width: {to: 11}, height: {to: 100, unit:"%"}},
                hideAttributes: {width: {to: 0}, height: {to: 0}},
                showDuration:.300,
                hideDuration:.300
            });

            nHContainer.normalizer = nHNormalizer;
            nHContainer.contentNormalizer = nHContentNormalizer;
            nHContainer.toggler = nHToggler;
            nHContainer.contentToggler = nHContentToggler;
            nHContainer.openToggler = nHOpenHandleToggler;
            nHContainer.closeToggler = nHCloseHandleToggler;

            nGSContainer = yuiDom.get("newStoriesSection");

            var nGSNormalizer = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesSection"),
                showAttributes: {width:{to: 278}},
                hideAttributes: {width:{to: 0}},
                showOnStart: function(config) {
                    nGSContainer.contentNormalizer.show();
                    nGSContainer.openToggler.hide();
                    nGSContainer.closeToggler.hide();
                },
                showDuration:.250,
                hideDuration:.500
            });
            var nGSContentNormalizer = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesContentContainer"),
                showAttributes: {width:{to: 278}},
                showOnStart: function () {
                    yuiDom.addClass(yuiDom.get("newStories_leftContent"), "indicator");
                },                
                showDuration:.250,
                hideDuration:.500
            });

            var nGSToggler = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesSection"),
                showAttributes: {width:{to: 533}},
                hideAttributes: {width:{to: 21}},
                showOnStart: function(config) {
                    nGSContainer.openToggler.hide();
                    nHContainer.openToggler.show();
                    nHContainer.toggler.hide();
                    nGSContainer.contentToggler.show();
                    yuiDom.removeClass(yuiDom.get("newStories_leftContent"), "indicator");
                },
                showOnComplete: function() {
                    nGSContainer.closeToggler.show();
                },
                hideOnStart: function() {
                    nGSContainer.contentToggler.hide();
                    nGSContainer.closeToggler.hide();
                },
                hideOnComplete: function () {
                    nGSContainer.openToggler.show();
                },
                showDuration:.500,
                hideDuration:.500
            });

            var nGSContentToggler = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesContentContainer"),
                showAttributes: { width: {to: 512}},
                hideAttributes: {width: {to: 0}},
                showDuration:.800,
                hideDuration:.500
            });

            var nGSOpenHandleToggler = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesOpenHandle"),
                showAttributes: {width: {to: 11}, height: {to: 100, unit: "%"}},
                hideAttributes: {width: {to: 0}, height: {to: 0}},
                showDuration:.300,
                hideDuration:.300
            });

            var nGSCloseHandleToggler = SW.widget.displayToggler({
                element: yuiDom.get("newStoriesCloseHandle"),
                showAttributes: {width: {to: 11}, height: {to: 100, unit: "%"}},
                hideAttributes: {width: {to: 0}, height: {to: 0}},
                showDuration:.300,
                hideDuration:.300
            });


            nGSContainer.normalizer = nGSNormalizer;
            nGSContainer.contentNormalizer = nGSContentNormalizer;
            nGSContainer.toggler = nGSToggler;
            nGSContainer.contentToggler = nGSContentToggler;
            nGSContainer.openToggler = nGSOpenHandleToggler;
            nGSContainer.closeToggler = nGSCloseHandleToggler;

            yuiEvent.addListener(yuiDom.get("newHotelsContentContainer"), "mouseover", SW.local.CommunityArea.toggleNewHotels, nHContainer);
            yuiEvent.addListener(yuiDom.get("newStoriesContentContainer"), "mouseover", SW.local.CommunityArea.toggleNewStories, nGSContainer);

            yuiEvent.addListener(yuiDom.get("newHotelsOpenHandle"), "mouseover", SW.local.CommunityArea.toggleNewHotels, nHContainer);
            yuiEvent.addListener(yuiDom.get("newStoriesOpenHandle"), "mouseover", SW.local.CommunityArea.toggleNewStories, nGSContainer);

            yuiEvent.addListener(yuiDom.get("newHotelsCloseHandle"), "click", SW.local.CommunityArea.normalize, nHContainer);
            yuiEvent.addListener(yuiDom.get("newStoriesCloseHandle"), "click", SW.local.CommunityArea.normalize, nGSContainer);
//              stop the body event from triggering when we're inside area

            yuiEvent.addListener(yuiDom.get("communityArea"), "mouseover", function(e) {yuiEvent.stopPropagation(e);});
            yuiEvent.addListener(document.body, "mouseover", SW.local.CommunityArea.normalize);

        },
        toggleNewHotels: function(e) {
//                console.log("hover nh")
            if(nHContainerTimeOut == null) {
                if(nGSContainerTimeOut != null) {
                    clearTimeout(nGSContainerTimeOut);
                    nGSContainerTimeOut = null;
                }
                if(normalizerTimeout != null) {
                    clearTimeout(normalizerTimeout);
                    normalizerTimeout = null;
                }
//                    nHContainerTimeOut = setTimeout("console.log(\""+"show nH"+"\")", 1750);
                nHContainerTimeOut = setTimeout(SW.local.CommunityArea.showNewHotels, 500);
            }
            yuiEvent.stopPropagation(e);
        },
        showNewHotels: function () {
//                console.log("show nH");
            nHContainer.toggler.show();
        },
        toggleNewStories: function(e) {
//                console.log("hover ngs")
            if(nGSContainerTimeOut == null) {
                if(nHContainerTimeOut != null) {
                    clearTimeout(nHContainerTimeOut);
                    nHContainerTimeOut = null;
                }
                if(normalizerTimeout != null) {
                    clearTimeout(normalizerTimeout);
                    normalizerTimeout = null;
                }
//                  nGSContainerTimeOut = setTimeout("console.log(\""+"show nGS"+"\")", 1750);
                nGSContainerTimeOut = setTimeout(SW.local.CommunityArea.showNewStories, 500);
            }
            yuiEvent.stopPropagation(e);
        },
        showNewStories: function () {
//                console.log("show nGS");
            nGSContainer.toggler.show();
        },
        normalize: function(e) {
            if(normalizerTimeout == null) {
                if(nHContainerTimeOut) {
                    clearTimeout(nHContainerTimeOut);
                    nHContainerTimeOut = null;
                }
                if(nGSContainerTimeOut) {
                    clearTimeout(nGSContainerTimeOut);
                    nGSContainerTimeOut = null;
                }

                normalizerTimeout = setTimeout(SW.local.CommunityArea.doNormalize, 500);
            }
            yuiEvent.stopPropagation(e);
        },
        doNormalize: function() {
            if(nHContainer.toggler.isShowing() || nGSContainer.toggler.isShowing()) {
                nGSContainer.normalizer.show();
                nHContainer.normalizer.show();
                nHContainer.toggler.setIsShowing(false);
                nGSContainer.toggler.setIsShowing(false);
            }
        }
    };

    yuiEvent.onDOMReady(function() {
        SW.local.CommunityArea.init();
    });
})();


