﻿var userBrowser;
var initialRun = false;
var divPreviewImageLightBox;
var divVirtualTourLightBox;
var divOfferLightBox;
var vTourDialogHeight;
var divFloorPlanLightBox;
var isVTLoaded = false;
var isFPLoaded = false;
var pageTrackingCode = "UT_Spec";
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();

    //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);
    });

    // Open the bannerOfferOverlay
    $("a#lnkBannerOffer").click(function() {
        var bannerOfferKey = $(this).attr("rel");
        OpenBannerOfferOverlay(bannerOfferKey);
    });
    //    
    //Retrieve Browser Type
    userBrowser = BrowserType();


    //Browser Fixes
    //Browser Fixes
    $("#ifrVTour").height(485);
    vTourDialogHeight = "464px";
    switch (userBrowser) {
        case "msie6":
            $("div#divInventory div.imagesBorder").css({ marginTop: "-1px" });
            $("#ifrVTour").width(678);
            break;
    }


    //resize summary values if not existing
    $.each($("div.ranges div.values"), function() {
        var display = $(this);
        if (display.text().trim() == "") display.hide();
    });

    //redirect estimate mortgage url
    $("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;
    });

    //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
    InitSlideShows();



    //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", "");
    }

    $("#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;
        case "open_offer":
            if ($("#lnkOffer").length > 0)
                $("a#lnkOffer").trigger("click");
            break;
        default:
            break;
    }

    //Open overlay if offer is set 10/14/2010 case 7764
    var offer = $.urlParam("offerID");
    var offerSectionVisible = false;
    if ($("#lnkOffer").length > 0) {
        offerSectionVisible = true;
    }
    if ($("#lnkOffer").attr("rel") == offer && offerSectionVisible) {
        $("a#lnkOffer").trigger("click");
        //OpenOfferOverlay(offer);
    }
}

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();
}

//Initialize Slide Show
function InitSlideShows() {
    //Show first slide show
    var slideshows = $("div#slideShow div");
    var count = slideshows.length;
    if (count == 0) {
        $("#sidePanels").hide();
        return;
    }

    slideshows.removeClass("selected");
    slideshows.hide();

    $("div#sideTabs tr td.side").click(function() {
        $("div#sideTabs tr td.side").removeClass("selected");
        $(this).addClass("selected");
        $("div#sidePanels").find("div.imagesBorder").hide();
        var tabContent = $("#" + $(this).attr("id") + "Content");
        tabContent.show();
        if (tabContent.find("div#slideShow div.selected").length == 0) {
            tabContent.find("div#slideShow div:first").addClass("selected").fadeIn(500);
            tabContent.find("div.slideshowText").html($(tabContent).find("div#slideShow div:first").find("INPUT").val());
        }
    });


    InitExteriorSlideShow();
    InitInteriorSlideShow();
    InitSlideShowLinks();

    //show first image
    var selectedTab = $("div#sideTabs tr td:visible:first").addClass("selected");
    var selectedTabContent = $("#" + selectedTab.attr("id") + "Content");
    selectedTabContent.show();
    selectedTabContent.find("div.slideshowText").html($(selectedTabContent).find("div#slideShow div:first").find("INPUT").val());
    selectedTabContent.find("div#slideShow div:first").addClass("selected").fadeIn(10);

}

function InitExteriorSlideShow() {
    var count = $("div.ssExterior").length;
    var tab = $("#PLTabInfo_Exterior");
    var tabContent = $("#PLTabInfo_ExteriorContent");
    if (count == 0) {
        tab.hide();
        tab.next("td").hide();
        tabContent.hide();
        return;
    }
    else {
        tab.show();
    }
    //hide arrows if there is only one image
    if (count == 1) tabContent.find("a.linkSS").css({ display: "none" });
}

function InitInteriorSlideShow() {
    var count = $("div.ssInterior").length;
    var tab = $("#PLTabInfo_Interior");
    var tabContent = $("#PLTabInfo_InteriorContent");
    if (count == 0) {
        tab.hide();
        tab.next("td").hide();
        tabContent.hide();
        return;
    }
    else {
        tab.show();
    }
    //hide arrows if there is only one image
    if (count == 1) tabContent.find("a.linkSS").css({ display: "none" });
}

function InitSlideShowLinks() {
    $("a.linkSS").click(function() {
        var isFirst = false;
        var isLast = false;


        var ExteriorInterior = $("tr#commTabsRow").find("TD.selected");
        var currentElevation = "ssExterior";
        if (ExteriorInterior.attr("id") == "PLTabInfo_Interior") {
            currentElevation = "ssInterior";
        }
        var tab = $("#" + ExteriorInterior.attr("id") + "Content");


        if ($(tab).find("div#slideShow div.selected").html() == $(tab).find("div#slideShow div." + currentElevation + ":first").html()) isFirst = true;
        if ($(tab).find("div#slideShow div.selected").html() == $(tab).find("div#slideShow div." + currentElevation + ":last").html()) isLast = true;

        if (isFirst && isLast) {
            return;
        }
        var slideshow;
        if ($(this).attr("ID") == "lnkssLeft") {
            if (isFirst) {
                slideshow = $(tab).find("div#slideShow div." + currentElevation + ":last");
            }
            else {
                slideshow = $(tab).find("div#slideShow div.selected").prev();
            }
        }
        if ($(this).attr("ID") == "lnkssRight") {
            if (isLast) {
                slideshow = $(tab).find("div#slideShow div." + currentElevation + ":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(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;

        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();
}

//function for opening banner offer overlay
function OpenBannerOfferOverlay(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", lfbSource + offerKey);

    Reposition_OfferOverlay();
}
//end function for opening banner offer overlay 

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 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", "650px");

    divFloorPlanLightBox.show();
    if (!isFPLoaded) {
        $("#ifrFPlan").attr("src", fpSource);
        isFPLoaded = true;
    }
    Reposition_FloorPlanOverlay();
}

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_FloorPlanOverlay = function() {

    divFloorPlanLightBox.center();

}

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();
    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());

    var count = $(".PreviewImageBodyBorder div." + type).length;
    if (count == 1)
        $("a.linklSS").hide();
    else
        $("a.linklSS").show();
    $("#divTitle").show();

}
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 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();
}
var Reposition_PreviewImageOverlay = function() {

    divPreviewImageLightBox.center();

}
var Reposition_VirtualTourOverlay = function() {

    divVirtualTourLightBox.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;
} 


