﻿var userBrowser;
var currentElevation = "";
var initialRun = false;
var divPreviewImageLightBox;
var divVirtualTourLightBox;
var divOfferLightBox;
var divFloorPlanLightBox;
var interiorPhotosCount = -1;
var exteriorPhotosCount = -1;
var isVTLoaded = false;
var isFPLoaded = false;
var pageTrackingCode = "UT_Plan";
var vTourDialogHeight = "510px";
//Preload images
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
    }
}

var mainTopBorder = imagePath + "communityplan_main_bordertop.jpg";
var mainBorder = imagePath + "communityplan_main_border.jpg";
var summary = imagePath + "communityplan_summary_bg.jpg";
var firstTab = imagePath + "plan_firsttab.jpg";
var firstActiveTab = imagePath + "plan_firsttab_active.jpg";
var otherTab = imagePath + "plan_tab.jpg";
var otherActiveTab = imagePath + "plan_tab_active.jpg";
var tabTopBorder = imagePath + "communityplan_tab_bordertop.gif";
var tabBorder = imagePath + "communityplan_tab_border.jpg";
var vtButton = imagePath + "vt_btn.jpg";
var tabBottomBorder = imagePath + "communityplan_tab_borderbottom.jpg";
var mainBottomBorder = imagePath + "communityplan_main_borderbottom.jpg";
var rightDarkTop = imagePath + "comm_header_bg.gif";
var rightDarkBorder = imagePath + "communities_border_dark.gif";
var rightDarkBottom = imagePath + "communities_bottom_dark.gif";
//Add other images here
var deleteImage = imagePath + "delete.gif";
var sdLogo = imagePath + "sdTitle.gif";
var offerGraphic = imagePath + "communities_offer.gif";

MM_preloadImages(mainTopBorder, mainBorder, mainBottomBorder, firstActiveTab, otherTab, firstTab, otherActiveTab, summary, tabTopBorder, offerGraphic, tabBorder, tabBottomBorder, vtButton, rightDarkTop, rightDarkBorder, rightDarkBottom, deleteImage, sdLogo);

function InitPage() {
    //Initialize inline form
    InitializeInlineForm();
    
    divPreviewImageLightBox = new YAHOO.widget.Overlay("divPreviewImageLightBox", { visible: false,
        width: "0px", height: "0px"
    });
    divPreviewImageLightBox.render();
    
    divVirtualTourLightBox = new YAHOO.widget.Overlay("divVirtualTourLightBox", { visible: false,
        width: "0px", height: "0px"
    });
    divVirtualTourLightBox.render();

    divFloorPlanLightBox = new YAHOO.widget.Overlay("divFloorPlanLightBox", { visible: false,
        width: "0px", height: "0px"
    });
    divFloorPlanLightBox.render();

    divOfferLightBox = new YAHOO.widget.Overlay("divOfferLightBox", { visible: false, width: "0px", height: "0px" });
    divOfferLightBox.render();
    //Retrieve Browser Type
    userBrowser = BrowserType();

    //check if there is an offer
    //show banner if there is one
    if ($("div.mainTopBorderOffer div").children().length != 0) {
        $("div.mainTopBorder").hide();
        $("div.mainTopBorderOffer").show();


    }
    $("a#lnkOffer,a#lnkMetroOffer").click(function() {
        var offerKey = $(this).attr("rel");
        OpenOfferOverlay(offerKey);
    });

    //Browser Fixes
    $("#ifrVTour").height(485);
    vTourDialogHeight = "464px";
    switch (userBrowser) {
        case "msie6":
            $("div#divInventory div.imagesBorder").css({ marginTop: "-1px" });
            $("#ifrVTour").width(678);
            break;
    }


    $("div.planAttributes").find("a").click(function() {
        window.open("https://www0.bankofamerica.com/home-loans/calculators-tools/mlo/monthly-payment-calculator.go?request_locale=&isMLO=y&originationCode=00000&subCampCode=00000&branchCode=&branchSatelliteCode=&ReferralSourceNum=", "remoteWin", "width=800,height=500,screenX=10,left=10,screenY=10,top=10,status=no,scrollbars=yes,resize=no,menubar=no");
        return false;
    });

    //resize summary values if not existing
    $.each($("div.ranges div.values"), function() {
        var display = $(this);
        if (display.text().trim() == "") display.hide();
    });

    //Initialize Floor Plan Tabs - Hide if none are defined
    if ($("td.FPTab").length > 0) InitMainTabs();
    else {
        $("div.floorPlanWrapper").css({ display: "none" });
        $("div.tabTopBorder").css(
            {
                backgroundImage: "url(" + noFPTopBorder + ")",
                marginTop: "10px"
            });
        $("div.noFPImage").empty();
        $("div.noFPImage").append(
            "<div style='text-align:center'><img src=\"" + comingSoonFP + "\" /></div>"
        );
        $("div.noFPImage").show();
    }

    //Hide virtual tour button if no virtual tour is associated with the plan
    if (hasVT == 0)
        $("div.vtText").css({ display: "none" });
    else {
        $("div.vtText").css({ display: "block" });
        if (vtURL != "") {
            $("div.vtText")
                .find("a")
                .attr("href", "javascript:openExtVT('" + vtURL + "');")
        }
    }



    //Hide Interactive floorplan button if no legacyurl is provided
    if (hasIFP == 0)
        $("div.fpText").css({ display: "none" });
    else
        $("div.fpText").css({ display: "block" });

    var readyHomeCount = $("div.readyHomeItem").length;
    var futureHomeCount = $("div.futureHomeItem").length;
    //Hide quick move in display if there are no inventory available
    if (readyHomeCount > 0 || futureHomeCount > 0)
        $("div#divInventory").show();
    //Hide divider in quick move in display if either futureHomeItem or readyHomeItem is displayed

    if (readyHomeCount == 0 || futureHomeCount == 0)
        $("div.availableHomesDivider").hide();

    //Hide wrappers of HomeItem if there are none displayed
    if (readyHomeCount == 0)
        $("div.readyHomesWrapper").hide();

    if (futureHomeCount == 0)
        $("div.futureHomesWrapper").hide();


    //Hide other communities display if there are no other communities offering the same variation of the community plan
    if ($("div.otherCommunities").length > 0) $("div#divOfferedIn").show();

    //Initialize Slide Show Content
    InitSlideShow("#PLTabInfo_ElevationContent", false);

    //Initialize Slide Show Content
    InitSlideShow("#PLTabInfo_InteriorContent", false);


    $("#PLTabInfo_Elevation").click(function() {
        $("#PLTabInfo_InteriorContent").hide();
        $("#PLTabInfo_ElevationContent").show();
        $("#PLTabInfo_Interior").removeClass("selected");
        $("#PLTabInfo_Elevation").addClass("selected");
    });
    $("#PLTabInfo_Interior").click(function() {
        $("#PLTabInfo_ElevationContent").hide();
        $("#PLTabInfo_InteriorContent").show();
        $("#PLTabInfo_Interior").addClass("selected");
        $("#PLTabInfo_Elevation").removeClass("selected");
    });



    //Initialize ToolTips
    InitToolTips();

    //check spanish
    if (langCode == "") {
        $(".rightTopImage").css("backgroundImage", "url('" + rightUpperBack + "')");
        $(".fpText div").css("paddingTop", "3px");
        $(".fpText div").css("fontSize", "7pt");
        $(".vtText div").css("paddingTop", "7px");
        $(".vtText div").css("fontSize", "7pt");
    }
    else {
        $(".rightTopImage").css("height", "");
        $(".rightTopImage").css("backgroundImage", "url('" + rightUpperBackEn + "')");
        $(".fpText div").css("paddingTop", "");
        $(".fpText div").css("fontSize", "");
        $(".vtText div").css("paddingTop", "");
        $(".vtText div").css("fontSize", "");
    }
    var test = "";
    var preloadList = largeImages.split(',');

    $.each(preloadList, function(index) {
        MM_preloadImages(this);
    });

    $("#lnkFlipImage").click(function() {
        flipImage($(this));
    });

    //Open overlay if action is set 10/12/2010 case 7750
    var action = $.urlParam("action");
    switch (action) {
        case "open_tour":
            if (hasVT == 0) { }
            else {
                OpenVirtualTourOverlay();
            }
            break;
        case "open_ifp":
            if (hasIFP == 0) { }
            else {
                OpenFloorPlanOverlay();
            }
            break;
        default:
            break;
    }
}



function flipImage(lnk) {
    var image = $("#divPreviewImageLightBox").find("div.selected div.exterior").find("IMG");

    if (lnk.text().indexOf(reverseText) > -1) {
        image.rotateRight([angle = 180]);
        lnk.text(originalText);
    }
    else {
        flipToOriginal(lnk, image);
    }
}

function flipToOriginal(lnk, image) {
    if (lnk.text().indexOf(originalText) > -1) {
        if (userBrowser.search("msie") == -1) {
            var canvas = $("canvas");
            if (canvas.length > 0) {
                var newImage = "<IMG src=\"" + canvas[0].oImage.src + "\" />";
                var parentContainer = canvas.parent();
                canvas.remove();
                parentContainer.append(newImage);
            }
        }
        else {
            image.rotate(0, 'abs');
        }
        lnk.text(reverseText);
    }
}

function openExtVT(extURL) {
    window.open(extURL, "vtWindow", "status=no,scrollbars=yes,resizable=yes,menubar=no,toolbar=no");
}

function closeSDWindows() {
    //make sure no tooltip is displayed when changing tabs
    $("div.qtip-active").hide();
}

function invokeChangeElevation(elevation, eName) {
    if (!$("a#" + elevation).hasClass("selectedElevation")) changeElevation(elevation, eName);
}

function changeElevation(elevation, eName) {
    updateElevation(elevation, eName)
    InitSlideShow("#PLTabInfo_ElevationContent", true);
}
function updateElevation(elevation, eName) {
    currentElevation = elevation;
    $("a.linkElevation").removeClass("selectedElevation");
    $.each($("a.linkElevation"), function() {
        var link = $(this);
        if (link.attr("id") == elevation) {
            link.html("[&nbsp;" + eName + "&nbsp;]");
            link.addClass("selectedElevation");
        }
        else {
            link.text(link.text());
        }
    });

}
//Initialize Slide Show
function InitSlideShow(tab, switched) {
    //Show first slide show
    var slideshows = $(tab).find("div#slideShow div");
    var count = slideshows.length;
    if (count == 0) {
        $(tab).hide();
        $(tab.replace("Content", "")).hide();
        if (tab == "#PLTabInfo_InteriorContent") {
            interiorPhotosCount = 0;
        }
        else {
            exteriorPhotosCount = 0;
        }
    }
    else {
        $(tab.replace("Content", "")).show();
    }

    if (tab == "#PLTabInfo_InteriorContent") {
        if (interiorPhotosCount == 0 && exteriorPhotosCount == 0) {
            $("#sidePanels").hide();
            return;
        }
        if (exteriorPhotosCount == 0) {
            $(tab.replace("Content", "")).addClass("selected");
            $(tab).show();
            $("#PLTabInfo_Elevation").removeClass("selected");
            $("#PLTabInfo_Elevation").next("td").hide();
        }
    }

    //Hide slide show block if there are no images
    if (count == 0) {
        $(tab).find("div.mainBody").hide();
        $(tab).find("div.slideshowCaption").hide();
        return;
    }

    var page = $(tab).find("#page");
    slideshows.removeClass("selected");
    $(tab).find("div#slideShow div").hide();

    if (currentElevation == "") {
        currentElevation = $(tab).find("a.linkElevation:first").attr("id");
        $(tab).find("a.linkElevation:first").addClass("selectedElevation");
        //$(tab).find("a.linkElevation:first").text(currentElevation);
    }
    var elevation = currentElevation;

    var captionElevation;
    var largeImageClass = "";

    if (!switched) {
        $.each($(tab).find("div#slideShow div"), function(index) {
            captionElevation = $(this).find("INPUT").val().split("|");

            if (tab == "#PLTabInfo_ElevationContent") {

                $(this).addClass(captionElevation[1]);
                largeImageClass = "exterior";
            }
            else {
                elevation = "interior";
                largeImageClass = "interior";
            }
        });
    }

    if (elevation == "interior") $(tab).find("div.slideshowText").html($(tab).find("div#slideShow div." + elevation + ":first").find("INPUT").val());
    $(tab).find("div#slideShow div." + elevation + ":first").addClass("selected").fadeIn(10);

    //Count # of images and hide arrows if there is only one
    if ($(tab).find("div#slideShow div").length == 1) $(tab).find("a.linkSS").css({ display: "none" });
    if ($("#divPreviewImageLightBox").find(".divslideShow2 div." + largeImageClass).length == 1) $("#divPreviewImageLightBox").find("a.linklSS").css({ display: "none" });

    //function to move the image
    var inits = $(tab).find("a.linkSS").length;
    $(tab).find("a.linkSS").unbind("click");
    $(tab).find("a.linkSS").click(function() {
        var isFirst = false;
        var isLast = false;

        var ExteriorInterior = $("tr#commTabsRow").find("TD.selected")[0].id;
        if (ExteriorInterior == "PLTabInfo_Interior") {
            currentElevation = "interior";
        }
        else {
            currentElevation = elevation;
        }

        if ($(tab).find("div#slideShow div.selected").html() == $(tab).find("div#slideShow div:first").html()) isFirst = true;
        if ($(tab).find("div#slideShow div.selected").html() == $(tab).find("div#slideShow div:last").html()) isLast = true;

        //WT1.2
        /*meta = document.createElement('meta');
        meta.name = "WT.z_slideshow";
        meta.content = "1";
        document.getElementsByTagName('head').item(0).appendChild(meta);
        */
        dcsMultiTrack('DCS.dcssip', 'www.beazer.com', 'DCS.dcsuri', location.pathname, 'WT.z_slideshow', '1');


        if (isFirst && isLast) {
            return;
        }
        var slideshow;
        if ($(this).attr("ID") == "lnkssLeft") {
            if (isFirst) {
                slideshow = $(tab).find("div#slideShow div:last");
            }
            else {
                slideshow = $(tab).find("div#slideShow div.selected").prev();
            }
        }
        if ($(this).attr("ID") == "lnkssRight") {
            if (isLast) {
                slideshow = $(tab).find("div#slideShow div:first");
            }
            else {
                slideshow = $(tab).find("div#slideShow div.selected").next();
            }
        }
        $(tab).find("div#slideShow div").removeClass("selected").css({ display: "none" });
        slideshow.addClass("selected");
        slideshow.fadeIn(500);
        var caption = slideshow.find("INPUT").val().split("|");
        $(tab).find("div.slideshowText").html("");
        if (currentElevation == "interior") $(tab).find("div.slideshowText").html(caption[0]);
        updateElevation(caption[1], caption[0]);


    });

    //Function to navigate large preview images
    $("#divPreviewImageLightBox").find("a.linklSS").unbind("click");
    $("#divPreviewImageLightBox").find("a.linklSS").click(function() {
        var isFirst = false;
        var isLast = false;

        //WT1.2
        /*meta = document.createElement('meta');
        meta.name = "WT.z_slideshow";
        meta.content = "1";
        document.getElementsByTagName('head').item(0).appendChild(meta);
        */
        dcsMultiTrack('DCS.dcssip', 'www.beazer.com', 'DCS.dcsuri', location.pathname, 'WT.z_slideshow', '1');


        var ExteriorInterior = $("tr#commTabsRow").find("TD.selected")[0].id;
        if (ExteriorInterior == "PLTabInfo_Interior") {
            currentElevation = "interior";
        }
        else {
            currentElevation = "exterior";
            flipToOriginal($("#lnkFlipImage"), $("#divPreviewImageLightBox").find("div.selected div.exterior").find("IMG"));
        }

        if ($("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div.selected div." + currentElevation).html() == $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div." + currentElevation + ":first").html()) isFirst = true;
        if ($("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div.selected div." + currentElevation).html() == $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div." + currentElevation + ":last").html()) isLast = true;

        if (isFirst && isLast) {
            return;
        }
        var slideshow;
        var caption;
        if ($(this).attr("ID") == "lssLeft") {
            if (isFirst) {
                slideshow = $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div." + currentElevation + ":last").parent();
            }
            else {
                slideshow = $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div.selected div." + currentElevation).parent().prev();
            }
        }
        if ($(this).attr("ID") == "lssRight") {
            if (isLast) {
                slideshow = $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div." + currentElevation + ":first").parent();
            }
            else {
                slideshow = $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div.selected div." + currentElevation).parent().next();
            }
        }

        caption = slideshow.find("input").val();
        $("#divPreviewImageLightBox").find("div.PreviewImageBodyBorder div").removeClass("selected").css({ display: "none" });
        $("#divPreviewImageLightBox").find("div#divTitle div.slide").removeClass("selected").hide();
        $("#divPreviewImageLightBox").find("div." + currentElevation).show();
        slideshow.addClass("selected");
        //if (currentElevation == "interior") {
        $("#divTitle").html(caption);
        //}
        //else
        //    $("#divTitle").html("");

        slideshow.fadeIn(500);

    });
}

//overlay helper functions
function OpenOfferOverlay(offerKey) {
    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    $("#ifrOffer").show();

    divOfferLightBox.cfg.setProperty("height", "580px");
    divOfferLightBox.cfg.setProperty("width", "655px");

    divOfferLightBox.show();

    $("#ifrOffer").attr("src", lfSource + offerKey);

    Reposition_OfferOverlay();
}

var Reposition_OfferOverlay = function() {
    divOfferLightBox.center();
}

function CloseOfferOverlay() {
    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "none");

    divOfferLightBox.cfg.setProperty("height", "0px");
    divOfferLightBox.cfg.setProperty("width", "0px");

    divOfferLightBox.hide();
}

function OpenPreviewImageOverlay(src, caption, title, type) {
    closeSDWindows();
    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    divPreviewImageLightBox.cfg.setProperty("height", "580px");
    divPreviewImageLightBox.cfg.setProperty("width", "850px");

    divPreviewImageLightBox.show();
    Reposition_PreviewImageOverlay();

    $(".PreviewImageBodyBorder div").removeClass("selected").hide();
    $(".PreviewImageBodyBorder div." + type).show();
    //$(".PreviewImageBodyBorder div img[src=" + src + "]:first").parent().parent().addClass("selected").show();
    var currentImageDisplayed =
    $(".PreviewImageBodyBorder div img").filter(function(index) {
        var selectedImage = $(this);
        return (selectedImage.attr("src").toLowerCase() == src.toLowerCase())
    }).filter(":first");

    currentImageDisplayed.parent().parent().addClass("selected").show();

    var imgCaption = "";
    if (type == "interior")
        $("#lnkFlipImage").hide();
    else $("#lnkFlipImage").show();

    imgCaption = currentImageDisplayed.parent().find("input").val();

    if (imgCaption != undefined)
        $("#divTitle").html(imgCaption);

    $(".lightboxTitle").text($("div.PlanName").text());

    $("#divTitle").show();
    if ($(".PreviewImageBodyBorder div." + type).length > 1)
        $("a.linklSS").show();
    else
        $("a.linklSS").hide();

}
function OpenVirtualTourOverlay() {

    closeSDWindows();

    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    $("#ifrVTour").show();

    divVirtualTourLightBox.cfg.setProperty("width", "673px");
    divVirtualTourLightBox.cfg.setProperty("height", vTourDialogHeight);

    divVirtualTourLightBox.show();

    if (!isVTLoaded) {
        $("#ifrVTour").attr("src", vtSource);
        isVTLoaded = true;
    }

    Reposition_VirtualTourOverlay();
}

function OpenVirtualTourOverlay2(cavKey) {

    closeSDWindows();

    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    $("#ifrVTour").show();

    divVirtualTourLightBox.cfg.setProperty("width", "673px");
    divVirtualTourLightBox.cfg.setProperty("height", vTourDialogHeight);

    divVirtualTourLightBox.show();

    $("#ifrVTour").attr("src", vtSource + "&CAV=" + cavKey);

    Reposition_VirtualTourOverlay();
}

function OpenFloorPlanOverlay() {
    closeSDWindows();

    Reposition_Curtain();

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "block");

    $("#ifrFPlan").show();

    divFloorPlanLightBox.cfg.setProperty("width", "830px");
    divFloorPlanLightBox.cfg.setProperty("height", "670px");

    divFloorPlanLightBox.show();
    if (!isFPLoaded) {
        $("#ifrFPlan").attr("src", fpSource);
        isFPLoaded = true;
    }
    Reposition_FloorPlanOverlay();
}

function ClosePreviewImageOverlay() {
    if ($("a#lnkFlipImage:hidden").length == 0)
        flipToOriginal($("#lnkFlipImage"), $("#divPreviewImageLightBox").find("div.selected div.exterior").find("IMG"));

    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "none");

    divPreviewImageLightBox.cfg.setProperty("height", "0px");
    divPreviewImageLightBox.cfg.setProperty("width", "0px");

    divPreviewImageLightBox.hide();
}
function CloseVirtualTourOverlay() {
    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "none");

    divVirtualTourLightBox.cfg.setProperty("height", "0px");
    divVirtualTourLightBox.cfg.setProperty("width", "0px");

    divVirtualTourLightBox.hide();
}

function CloseFloorPlanOverlay() {
    var divCurtain = YAHOO.util.Dom.get("divCurtain");
    YAHOO.util.Dom.setStyle(divCurtain, "display", "none");

    divFloorPlanLightBox.cfg.setProperty("height", "0px");
    divFloorPlanLightBox.cfg.setProperty("width", "0px");

    divFloorPlanLightBox.hide();
}

var Reposition_PreviewImageOverlay = function() {

    divPreviewImageLightBox.center();

}
var Reposition_VirtualTourOverlay = function() {

    divVirtualTourLightBox.center();

}
var Reposition_FloorPlanOverlay = function() {

    divFloorPlanLightBox.center();

}

//Function to retrieve Browser Type
function BrowserType() {
    var _browser;
    jQuery.each(jQuery.browser, function(i, val) {
        switch (i) {
            case "msie":
                if (val) _browser = "msie" + jQuery.browser.version.substr(0, 1);
                break;
            case "mozilla":
                if (val) _browser = i;
                break;
            case "safari":
                if (val) _browser = "safari" + jQuery.browser.version.substr(0, 3);
                break;
            case "opera":
                if (val) _browser = i;
                break;
            default:
                break;
        }
    });
    return _browser;
}

function InitMainTabs() {
    if ($.browser.msie) {
        $("#planTabs tr td").css({ display: "block" });
    }
    else {
        $("#planTabs tr td").css({ display: "table-cell" });
    }

    //calculate spacer for border on right of tabs
    var totalLen = $("#planTabs tr td.FPTab").length * 120;
    totalLen = $("div.floorPlanWrapper").width() - totalLen - 19;
    if (totalLen < 0) totalLen = 0;
    $("td.FPBlankTab").css({ width: totalLen + "px" });


    //init hovers on all metro tabs
    $("#planTabs tr td.FPTab").hover(function() {
        if (!$(this).hasClass("selected")) {
            $(this).addClass("hover");
        }
    }, function() {
        if (!$(this).hasClass("selected")) {
            $(this).removeClass("hover");
        }
    });

    //bind switching events
    $("#planTabs tr td.FPTab").click(function() {
        var tdItem = $(this);
        var tabId = $(this).find("INPUT").val();
        var tabIndex = $("td").index(this);
        //clicked on a selected tab should be ignore
        if (!tdItem.hasClass("selected")) {
            //unmark all

            $("#planTabs tr td").removeClass("selected");

            $("div.floorPlanContent div.FPContentItem").css({ display: "none" });

            $("div.FPContentItem").filter(function(index) {
                return $(this).find("INPUT").val() == tabId;
            }).fadeIn(500);


            //mark newly clicked one
            tdItem.addClass("selected");
            tdItem.removeClass("hover");
            var bgImageActive = imagePath + "plan_tab_active.jpg";
            var bgImage = imagePath + "plan_tab.jpg";

            if ($("#planTabs tr td.FPTab:first").find("INPUT").val() == tabId) {
                bgImageActive = imagePath + "plan_firsttab_active.jpg";
            }

            tdItem.css({ backgroundImage: "url(\"" + bgImageActive + "\")" });

            $.each($("#planTabs tr td.FPTab"), function() {
                if (!$(this).hasClass("selected")) {
                    if ($("#planTabs tr td:first").find("INPUT").val() == $(this).find("INPUT").val()) bgImage = imagePath + "plan_firsttab.jpg";
                    $(this).css({ backgroundImage: "url(\"" + bgImage + "\")" });
                }
            });

            //scroll to the top of the image
            //$("html,body").animate({ scrollTop: $("div.floorPlanWrapper").position().top }, "fast")

        }
    });
    //Mozilla fix for top border
    if (userBrowser == "mozilla") {
        $("div.tabTopBorder").css({ backgroundPosition: "0px -1px" });
    }


    //make first comm tab selected
    $("#planTabs td:first").addClass("selected")
            .css({ backgroundImage: "url(\"" + imagePath + "plan_firsttab_active.jpg" + "\")" });

    $("div.FPContentItem:first").fadeIn(10);

}

//function to retrieve the smartblock content of the clicked link
function getSDBlock(sdBlockId) {
    var result;
    result = $("div#smartBlock").find("#" + sdBlockId).html();
    return (result == "" ? "No details available" : result);
}


//Initialize tooltips for each link that has smart design block content
//Only links with rel attributes will be initialized
function InitToolTips() {
    $("div.PlanDescription a.SDBlock").each(
                function() {
                    if ($(this).attr("rel") == "") return;
                    var self = this;
                    var sdId = $(this).attr("rel").replace("balloon", "");
                    var sdContent = getSDBlock(sdId);
                    var strTitle = SDTitle;

                    if (userBrowser == "msie6") sdContent = "<div style=\"display:inline-block;\">" + sdContent + "</div>";

                    var areaX = "R";
                    var areaY = "U";
                    var area = areaY + areaX;

                    var positions = new Array(
                        {
                            "UpperLeft": { corner: "bottomRight", target: "topRight", tooltip: "rightBottom" },
                            "UpperRight": { corner: "bottomLeft", target: "topRight", tooltip: "leftBottom" },
                            "LowerLeft": { corner: "topRight", target: "topRight", tooltip: "rightTop" },
                            "LowerRight": { corner: "topLeft", target: "topRight", tooltip: "leftTop" }
                        }
                    );
                    var position = eval(positions);

                    switch (area) {
                        case "UL":
                            position = position[0].UpperLeft;
                            break;
                        case "UR":
                            position = position[0].UpperRight;
                            break;
                        case "LL":
                            position = position[0].LowerLeft;
                            break;
                        case "LR":
                            position = position[0].LowerRight;
                            break;
                    }

                    $(self).qtip(
                      {
                          content: {
                              text: sdContent,
                              title: {
                                  text: strTitle,
                                  button: "<img onclick=\"return false;\" style=\"border:solid 1px #E0E0E0;\" src=\"" + imagePath + "delete.gif\" alt=\"Close\" />"
                              }

                          },
                          hide: false,
                          show: {
                              solo: true,
                              when: { event: 'click' }
                          },
                          style: {
                              width: 250,
                              padding: 12,
                              background: '#FFFFFF',
                              color: '#3E3E3E',
                              border: {
                                  width: 1,
                                  radius: 12,
                                  color: '#F2F3ED'
                              },
                              tip: {
                                  corner: position.corner,
                                  color: '#F2F3ED',
                                  size: { x: 12, y: 40 }
                              },
                              name: 'dark'
                          },
                          position: {
                              corner: {
                                  target: position.target,
                                  tooltip: position.tooltip
                              },
                              adjust: {
                                  x: -6, y: 12
                              }
                          }
                      });
                });



}

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}


//For getting the querystring values
//usage $.urlParam(ParamName)
$.urlParam = function(name) {
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (!results) { return 0; }
    return results[1] || 0;
} 

