var cypress = function () {
    function load() {
        var navStory = $('nav-story');
        if (navStory) navStory.href = "/home#story";
        var navSearch = $('nav-search');
        if (navSearch) navSearch.href = "/home#search";
    }
    return {
        load:load
    }
}();
var cypressHome = function () {
    var nav, panelName, activePanel, offset, viewEffect, unviewEffect, ready;
    var panels = {
        'search':{offset:-343,id:'search-panel',visible:0},
        'story':{offset:940,id:'story-panel',visible:0}
    };
    function load() {
        for (var panelName in panels) {
            var panel = panels[panelName];
            var el = $(panel.id);
            if (el) {
                panel.element = el;
            }
        }
        nav = $('nav');
        if (nav) {
            Event.observe(nav, "click", function(e) {
                var el = Event.findElement(e, "a");
                if (el.rel && el.rel != "") {
                    Event.stop(e);
                    if (el && el.rel && ready) {
                        swap(el.rel);
                    }
                }

            });
        }
        var hash = hashResolver(window.location.hash);
        offset = panels[hash].offset;
        panelName = panels[hash].id;
        unhide(panelName);
        var mask = $('mask');
        var maskOffset = -2065;
        Effect.MoveBy(mask, 0, maskOffset, {duration:2, afterFinish:function() {
            swap(hash);
            $('cntnt').removeChild(mask);
        }});
    }
    function view(panel, taglineSwitch) {
        if (viewEffect && viewEffect.state && viewEffect.state == "running") {
            return;
        }
        viewEffect = Effect.MoveBy(panel.id, 0, panel.offset, {duration:0.9, afterFinish:function() {
            panel.visible = 1;
            if (taglineSwitch == 1) {
                tagline();
            }
            if (!ready) {
                tagline();
            }
            ready = 1;
        }});
    }
    function unview(panel, after) {
        if (unviewEffect && unviewEffect.state && unviewEffect.state == "running") {
            return;
        }
        unviewEffect = Effect.MoveBy(panel.id, 0, panel.offset * -1, {duration:0.9, afterFinish:function(){
            panel.visible = 0;
        }});
    }
    function tagline() {
        var cachebuster = Math.floor(Math.random() * 101);
        $('tagline').style.backgroundImage = "url(/images/pl/alderwood/v3/bg_tagline.gif?cb=" + cachebuster + ")";
    }
    function hashResolver(hash) {
        if (!hash) hash = "#search";
        hash = hash.slice(1, hash.length);
        return hash;
    }
    function unhide(panel) {
        panel = $(panel);
        panel.style.display = "block";
    }
    function swap(panelName) {
        var panel = panels[panelName];
        if (activePanel && activePanel.visible && activePanel != panel) {
            unview(activePanel);
        }
        if (!panel.visible) {
            view(panel, 0);
            activePanel = panel;
        }
    }
    load();
};

var jazzedDlp = function () {
    var slider,
        toggle,
        loadOffset = 613,           //static, must not be overwritten by script
        offset = 300,               //static
        finalOffset,
        linksOffset = 287,          //static
        currentMode = 0,
        defaultIntendedMode = 1,    //static
        intendedMode = 1,
        running,
        maprunning,
        viewSection = "inquiry",
        previousViewSection = "listingtop",
        mapMode = 0,
        sections = {
            'inquiry':{mode:1},
            'listingtop':{mode:-1},
            'MapDirections':{mode:-1},
            'photos':{mode:-1},
            'propAmenities':{mode:-1},
            'floorplansection':{mode:-1}
        };
    function sectionAssign(section) {
        for (var sectionName in sections) {
            if (section == sectionName) {
                viewSection = section;
                intendedMode = sections[sectionName].mode;
            }
        }
    }
    function load(hash) {
        slider = $('slider');
        toggle = $('toggle');
        if (slider && toggle) {
            Event.observe($('section-nav'), "click", function(e) {
                Event.stop(e);
                var el = Event.findElement(e, "li");
                if (el && el.id) {
                    var id = el.id.substr(3, el.id.length);
                    sectionAssign(id);
                    moveSlider(viewSection, intendedMode);
                    if (viewSection != "inquiry") previousViewSection = viewSection;
                }
            });
            Event.observe($('addressLink'), "click", function(e) {
                sectionAssign('MapDirections');
                moveSlider(viewSection, intendedMode);
            });
            Event.observe($('slidercontrol'), "click", toggleSlider);
            Event.observe($('toggle'), "click", toggleMap);
        }
        // supporting hashes at a later time
        /*if (hash) {
            hash = hash.slice(1, hash.length);
            sectionAssign(hash);
        }*/
        $('dlpsub').removeChild($('dlpmask'));
        moveSlider(viewSection, intendedMode);
    }
    function hide() { //done
        var sectionsC = $C('tabbed-section');
        var invertedSectionsC = $C('inverted-tabbed-section');
        for (var i=0;i<sectionsC.length;i++) {
            sectionsC[i].style.display = "none";
        }
        for (var i=0;i<invertedSectionsC.length;i++) {
            invertedSectionsC[i].style.display = "none";
        }
    }
    function view(section) { //done
        viewSection = section;
        var sectionId = $(section);
        sectionId.style.display = "block";
        if (section == "MapDirections") {
            if (!map_map) map_map = new initPage_map("map");
        }
    }
    function sliderEffect(section, offset) {
        Effect.MoveBy(slider, 0, offset, {afterFinish:function() {
            running = 0;
            view(section);
        }});
    }
    function moveSlider(section, futureMode) { //replaces openSlider and closeSlider
        if (running) return;
        running = 1;
        hide();
        if (currentMode != 0 && currentMode != futureMode) {
            finalOffset = offset*futureMode;
            sliderEffect(section, finalOffset);
            toggleLinks(-1*futureMode);
            currentMode = futureMode;
        } else if (currentMode == 0){
            if (defaultIntendedMode != futureMode) {
                finalOffset = loadOffset - offset;
            } else {
                finalOffset = loadOffset;
            }
            setTimeout(function(){
                sliderEffect(section, finalOffset);
                currentMode = 1;
            },1000);
        } else {
            view(section);
            running = 0;
        }
    }
    function toggleLinks(multiplier) {
        var detlinks = $('detlinks');
        Effect.MoveBy(detlinks, 0, linksOffset*multiplier);
    }
    function toggleSlider(e) { //this is for the button only
        Event.stop(e);
        sectionAssign(viewSection);
        if (viewSection != "inquiry") {
            moveSlider("inquiry", sections["inquiry"].mode);
            previousViewSection = viewSection;
        } else {
            sectionAssign(previousViewSection);
            moveSlider(viewSection, intendedMode);
        }
    }
    function toggleMap(e) { //done
        if (maprunning) return;
        maprunning = 1;
        Event.stop(e);
        sectionAssign(viewSection);
        if (viewSection != "inquiry") {
            moveSlider("inquiry", sections["inquiry"].mode);
            previousViewSection = viewSection;
        }
        toggle = $('toggle');
        var dlpmap = $('dlpmap');
        if (!mapMode) {
            Effect.Appear(dlpmap, {duration:1, afterFinish:function() {
            maprunning = 0;
        }});
            toggle.className = "photo";
            mapMode = 1;
        } else {
            Effect.Fade(dlpmap, {duration:1, afterFinish:function() {
            maprunning = 0;
        }});
            toggle.className = "map";
            mapMode = 0;
        }
    }
    load(window.location.hash);
};