function slideleft() {
    new Effect.MoveBy('slide', 0, -281);
}
function slideright() {
    var el = $('slide');
    var ell = el.offsetLeft;

    if (ell < 20) {
        new Effect.MoveBy('slide', 0, 281);
    }
}
function wellhover(e) {
    var el = Event.element(e);
    el.addClassName('hover');
}
function wellhoverout(e) {
    var el = Event.element(e);
    el.removeClassName('hover');
}

/* this is for the floorplan photos popup */
var currentIndex = 0;

function fpPop2(objArray) {
    var floorplans = objArray;
    var fpLength = objArray.length;
    var fpDetailsContainer;

    function showByIndex(index) {
        currentIndex = index;
        if (currentIndex > fpLength - 1) {
            currentIndex = 0;
        }
        if (currentIndex < 0) {
            currentIndex = fpLength - 1;
        }
        var obj = floorplans[currentIndex];
        if (obj) {
            show();
        }
    }

    function show() {
        /*create the contents for the box*/
        if (!fpDetailsContainer) {
            fpDetailsContainer = $E("div", {className:"fpdetails"});
        } else {
            fpDetailsContainer.innerHTML = "";
        }


        Mbox.show(fpDetailsContainer, {
            className: "abox fppop",
            closer: true,
            screen: {cancel: true, className: 'smoke'},
            afterFinish: setContent
        });

    }

    function setContent() {
        var self = this;
        var obj = floorplans[currentIndex];
        var el = fpDetailsContainer;
        var propKey = obj[0], siteKey = obj[1], floorPlanId = obj[2], propName = obj[3], elTargetId = obj[4], pc = obj[5];

        /* swap stylesheet for special floorplan printing stylesheet */
        //altStyle('dlp-fp', 'print');

        //$('alertWrapper').style.width = '763px';
        var elc = $E("div", {parentNode:el});

        var header = $E('div', {parentNode: elc, className: 'fphead'});


        var printLink = $E('a', {parentNode: header, text:'Printable Brochure',className: 'fpprint'});
        printLink.href =  obj[0] ;
        printLink.target = "_blank";
        /*   printLink.onclick = function(e){
                Event.stop(e);
                //window.print();
                return false;
              };
        */

        var title = $E('h2', {parentNode: header, text:propName,className: 'fptitle'});

        //clone the floorplan info html and append it to our new element
        var elinfo = $E('div', {className:'fpinfo',parentNode:elc});
        appendClonedChildren($(elTargetId), elinfo);

        //create photo container
        if (pc.photos.length != 0) {
            var floorplanPhotoId = "fppho_" + floorPlanId + "_" + rnd();

            Element.removeClassName(Mbox.box, "narrow");
            elinfo.className = "fpinfo narrow";

            var photoContainer = $E('div', {
                className: "photos_obj",
                parentNode: elc
            });
            photoContainer.style.width = pc.getMaxWidth() + 'px';

            var largePhotoWrapper = $E('div', {
                className: "fp_pho",
                parentNode: photoContainer
            });
            largePhotoWrapper.style.height = pc.getMaxHeight() + 'px';
            largePhotoWrapper.style.width = pc.getMaxWidth() + 'px';

            var imgPhoto = $E('div', {
                id: floorplanPhotoId,
                parentNode: largePhotoWrapper
            });
            /*
                    var imgPhotoB = $E('img', {
                      id: 'floorplanPhoto_b',
                      className: 'photoBucket',
                      parentNode: largePhotoWrapper
                    });
            */
            /*

                    var pCaption = $E('p', {
                      id: 'fpCaption',
                      parentNode: largePhotoWrapper
                    });

            */
            var thumbs = document.getElementsByClassName('thumbs', elc)[0];
            var thumbsLength = thumbs.getElementsByTagName('a').length;
            if (thumbsLength > 1) {
                var photoWell = $E('div', {
                    className: 'photowell',
                    parentNode: photoContainer
                });
                var slidecontainer = $E('div', {
                    className: 'slidecontainer',
                    parentNode: photoWell
                });
                var slideLeft = $E('div', {
                    className: 'left',
                    parentNode: photoWell
                });
                Event.observe(slideLeft, 'click', slideright);
                Event.observe(slideLeft, 'mouseover', wellhover);
                Event.observe(slideLeft, 'mouseout', wellhoverout);

                var slideRight = $E('div', {
                    className: 'right',
                    parentNode: photoWell
                });
                Event.observe(slideRight, 'click', slideleft);
                Event.observe(slideRight, 'mouseover', wellhover);
                Event.observe(slideRight, 'mouseout', wellhoverout);

                //move thumbnails into photo container
                slidecontainer.appendChild(thumbs);
                thumbs.className = "slide";
                thumbs.id = "slide";
                thumbs.style.display = "";
            }

            pc.init({imageId: floorplanPhotoId, photos:pc.photos}, {resize: false});
            pc.setup();
            pc.show(0, false);

        } else {
            Element.addClassName(Mbox.box, "narrow");
        }

        var navclass = "";
        if (currentIndex == fpLength - 1) {
            navclass = "end";
        }
        if (currentIndex == 0) {
            navclass = "start";
        }

        graft(el, [
                "div", {id:"fpnav", className:navclass},
                ["a", {href:"nav://prev", id:"fpprev"}, "Previous Floor Plan"],
                ["a", {href:"nav://next", id:"fpnext"}, "Next Floor Plan"]
                ]);
        Event.observe("fpnav", "click", navClick.bindAsEventListener(self));

    }

    function navClick(e) {
        Event.stop(e);
        var direction = 0;
        var el = Event.element(e);
        if (el && el.href) {
            var func = el.href.match(/\/\/(\w*)/)[1];
            if (func === "next") {
                direction = 1;
            } else {
                direction = -1;
            }
            showByIndex(currentIndex + direction);
        }

    }


    return {
        show:showByIndex
    };
}

function fpPop(propKey, siteKey, floorPlanId, propName, elTargetId, pc) {
    /*

  var oldCancel = Mbox.cancel;
  Mbox.cancel = function() {
    */
    /* restore stylesheet */
    /*
    altStyle('dlp', 'print');
    oldCancel();
    */
    /*restore old cancel method*/
    /*
        Mbox.cancel = oldCancel;
      }

    */

    /*create the contents for the box*/
    var el = $E("div", {anonymous:true,className:"fpdetails"});

    Mbox.show(el, {
        className: "box bigbox fppop",
        closer: true,
        draggable: false,
        width: '100%',autoSize: true,
        screen: {cancel: true, className: 'smoke'},
        afterFinish: function() {
            /* swap stylesheet for special floorplan printing stylesheet */
            //altStyle('dlp-fp', 'print');

            //$('alertWrapper').style.width = '763px';
            var elc = $E("div", {parentNode:el});

            var header = $E('div', {parentNode: elc, className: 'fphead'});


            var printLink = $E('a', {parentNode: header, text:'Printable Brochure',className: 'fpprint'});
            printLink.href = "/apartment/" + propKey + "/fp" + floorPlanId + "Brochure.pdf";
            printLink.target = "_blank";
            printLink.onclick = function(e) {
                stopLinkPropagation(e);
                //window.print();
                return false;
            };


            var title = $E('h2', {parentNode: header, text:propName,className: 'fptitle'});

            //clone the floorplan info html and append it to our new element
            var elinfo = $E('div', {className:'fpinfo',parentNode:elc});
            appendClonedChildren($(elTargetId), elinfo);

            //create photo container
            if (pc.photos.length != 0) {

                Element.removeClassName(Mbox.box, "narrow");
                elinfo.className = "fpinfo narrow";

                var photoContainer = $E('div', {
                    className: "photos_obj",
                    parentNode: elc
                });
                photoContainer.style.width = pc.getMaxWidth() + 'px';

                var largePhotoWrapper = $E('div', {
                    className: "fp_pho",
                    parentNode: photoContainer
                });
                largePhotoWrapper.style.height = pc.getMaxHeight() + 'px';
                largePhotoWrapper.style.width = pc.getMaxWidth() + 'px';

                var imgPhoto = $E('img', {
                    id: 'floorplanPhoto',
                    parentNode: largePhotoWrapper
                });
                /*
                        var imgPhotoB = $E('img', {
                          id: 'floorplanPhoto_b',
                          className: 'photoBucket',
                          parentNode: largePhotoWrapper
                        });
                */

                var pCaption = $E('p', {
                    id: 'caption',
                    parentNode: largePhotoWrapper
                });

                var thumbs = document.getElementsByClassName('thumbs', elc)[0];
                var thumbsLength = thumbs.getElementsByTagName('a').length;
                if (thumbsLength > 1) {
                    var photoWell = $E('div', {
                        className: 'photowell',
                        parentNode: photoContainer
                    });
                    var slidecontainer = $E('div', {
                        className: 'slidecontainer',
                        parentNode: photoWell
                    });
                    var slideLeft = $E('div', {
                        className: 'left',
                        parentNode: photoWell
                    });
                    Event.observe(slideLeft, 'click', slideright);
                    Event.observe(slideLeft, 'mouseover', wellhover);
                    Event.observe(slideLeft, 'mouseout', wellhoverout);

                    var slideRight = $E('div', {
                        className: 'right',
                        parentNode: photoWell
                    });
                    Event.observe(slideRight, 'click', slideleft);
                    Event.observe(slideRight, 'mouseover', wellhover);
                    Event.observe(slideRight, 'mouseout', wellhoverout);

                    //move thumbnails into photo container
                    slidecontainer.appendChild(thumbs);
                    thumbs.className = "slide";
                    thumbs.id = "slide";
                    thumbs.style.display = "";
                }

                pc.setup();
                pc.show(0, true);

            } else {
                Element.addClassName(Mbox.box, "narrow");
            }


        }
    });


}

function altStyle(title, media) {
    var i, a, main;
    for (i = 0; (a = $T("link")[i]); i++) {
        if (a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
            a.disabled = !(a.getAttribute("title") == title && a.getAttribute("media") == media);
        }
    }

}

function displayConfirmMessage(proprty)
{
    var form = document.forms["inquiryForm"];
    var msg = 'Your message to ' + proprty + ' has been sent.';
    var ok = function() {
        msgHide();
    }
    msgShow(msg, 'Confirmation', 'OK', ok, null, null);
}

var tabbedDlp = new Object();

tabbedDlp.load = function (hash) {
    var sectionStr = "listingtop";
    if (hash) {
        hash = hash.slice(1,hash.length);
        if (hash == "listingtop" ||
            hash == "MapDirections" ||
            hash == "photos" ||
            hash == "propAmenities" ||
            hash == "floorplansection") {
                sectionStr = hash;

        } else if (hash == "inquiry") {
                sectionStr = "listingtop";
        }
    }
    tabbedDlp.hide();
    tabbedDlp.view(sectionStr);

};

tabbedDlp.swtch = function (section) {
    var navItems = $C('tabbed-navActive');
    var j;
    for (j=0;j<navItems.length;j++) {
        navItems[j].className="";
    }
    tabbedDlp.hide();
    tabbedDlp.view(section);
    if (section == "MapDirections") {
        if (!map_map) map_map = new initPage_map("map");
    }
};

tabbedDlp.hide = function () {
    var sections = $C('tabbed-section');
    var i;
    for (i=0;i<sections.length;i++) {
        sections[i].style.display = "none";
    }    
};

tabbedDlp.view = function (section) {
    var navStr = "li-"
    navStr += section;
    var navId = $(navStr);
    var sectionId = $(section);
    sectionId.style.display = "block";
    navId.className = "tabbed-navActive";
    //window.location.hash = section;
};
