var PageURL = "";
var ContentLoad = false;
var LastDiv = "";
var fancyboxframesrc = "";
var fancyboxsubmitted = false;

function getLocation(ctrl) {
    var hdn = Sys.UI.DomElement.getElementById(ctrl.replace('lbtnEmail', 'hdnLocationXY'));
    var bounds = Sys.UI.DomElement.getBounds(Sys.UI.DomElement.getElementById(ctrl));
    hdn.value = (bounds.x + 9) + '|' + (bounds.y + bounds.height);
}

$(document).ready(function($) {
    var CommunityContentSelector = $("#CommunityContent");
    var rvobject = false;
    var field = 'qmi';
    var url = window.location.href;
    if (url.indexOf('?' + field + '=') !== -1) {
        rvobject = true;
    }
    else if (url.indexOf('&' + field + '=') !== -1) {
        rvobject = true;
    }

    field = 'rfi=show';
    if (url.indexOf('?' + field) !== -1) {
        rvobject = true;
    }
    else if (url.indexOf('&' + field) !== -1) {
        rvobject = true;
    }

    field = 'rfi=thankyou';
    if (url.indexOf('?' + field) !== -1) {
        rvobject = true;
    }
    else if (url.indexOf('&' + field) !== -1) {
        rvobject = true;
    }

    var found = CommunityContentSelector;

    if (found.length !== 0 && rvobject === true) 
    {
        CommunityContentSelector.hide();
    }
});

$(document).ready(function() {
    var hlkNLBOverview = $("a[id$=hlkNLBOverview]");
    var hlkMPNLBMetroMap = $("a[id$=hlkMPNLBMetroMap]");
    var LastDivSelector = $("#LastDiv");

    if (LastDivSelector.length !== 0) {
        LastDiv = LastDivSelector.val();
    }
    if (hlkNLBOverview.length !== 0) {
        if (LastDivSelector.length === 0) {
            LastDiv = hlkNLBOverview.closest("div").attr("id");
        }
        $("#" + LastDiv).addClass("CMSListMenuLIRightSelected");
    }

    if (hlkMPNLBMetroMap.length !== 0) {
        if (LastDivSelector.length === 0) {
            LastDiv = hlkMPNLBMetroMap.closest("div").attr("id");
        }
        $("#" + LastDiv).addClass("CMSListMenuLILeftSelected");
    }

});

// added by charles to enable flash to control removing fancyboxProgressDIv
function hideFBProgressDiv() {
    $("#FancyBoxProgressDiv").attr("style", "z-index:1001; position:absolute; display:none");
}

String.prototype.endsWith = function(str) {
    return (this.match(str + "$") === str);
};

function FancyBoxOnComplete(control, url, width, height, titleHeight) {

    var InitTitleSize = 30;
    var fancyboxframe = $("#fancybox-frame");
    var fancyboxtitle = $('#fancybox-title');
    var fancyboxouter = $('#fancybox-outer');
    var fancyboxcontent = $('#fancybox-content');

    if (InitTitleSize < titleHeight) {
        InitTitleSize = titleHeight;
    }

    fancyboxframesrc = fancyboxframe.attr('src');
    
    fancyboxframe.load(function() {
        if (fancyboxframe.attr('src') !== "") {
            if (!$("#fancybox-frame").attr('src').endsWith("Gallery")) {
                $("#FancyBoxProgressDiv").attr("style", "z-index:1001; position:absolute; display:none");
            }
        }        
    });

    if (jQuery.browser.msie) {
        fancyboxtitle.css({ 'filter': 'progid:DXImageTransform.Microsoft.gradient(startColorstr=#f9f9f8, endColorstr=#e1e5e8)' });
        fancyboxouter.css({ 'border': '1px solid #808080' });
        fancyboxouter.corner("10px cc:#888888");
        fancyboxtitle.corner("10px tl cc:#888888");
    }

    if (jQuery.browser.mozilla) {
        fancyboxouter.css({ '-moz-box-shadow': '#666 0px 2px 10px', 'border': '1px solid #808080' });
        fancyboxtitle.css({ '-moz-border-radius': '10px 0px 0px 0px', 'background': '-moz-linear-gradient(#f9f9f8, #e1e5e8)' });
    }

    if (jQuery.browser.webkit) {
        fancyboxouter.css({ 'webkit-box-shadow': '#666 0px 2px 10px', 'box-shadow': '#666 0px 2px 10px', 'border': '1px solid #808080' });
        fancyboxtitle.css({ '-webkit-border-radius': '10px 0px 0px 0px', 'background': '-webkit-gradient(linear, 0 0, 0 bottom, from(#f9f9f8), to(#e1e5e8))' });
    }


    $('#fancybox-content').css({
        '-moz-border-radius': '10px 10px 10px 10px'
    });
    
    
    fancyboxtitle.css({
        'top': '0px',
        'position': 'absolute',
        'bottom': 'auto',
        'vertical-align': 'top',
        'margin-left': 'auto',
        'margin-right': 'auto',
        'color': '#000000',
        'border-bottom': '2px #808080 solid',
        'background-color': '#E5E5E5',
        'font-size': '20px',
        'height': InitTitleSize + 'px'
    });

    fancyboxcontent.attr("style", "top: " + (InitTitleSize + 30) + "px; left: 10px; width: " + width + "px; height: " + height + "px; overflow: hidden;");

    $("#CommunityContent").hide();
}

function FancyBoxOnStart(control, url, width, height, titleHeight) {

    var InitTitleSize = 30;
    var fancyboxframe = $("#fancybox-frame");
    var fancyboxtitle = $('#fancybox-title');
    var fancyboxouter = $('#fancybox-outer');
    var fancyboxcontent = $('#fancybox-content');
    var fancyboxclose = $('#fancybox-close');
    var fancydiv = $("#FancyDiv");
        
    if (InitTitleSize < titleHeight) {
        InitTitleSize = titleHeight;
    }

    if (jQuery.browser.msie) {
        if (parseInt(jQuery.browser.version, 10) <= 8) {
            fancyboxouter.css({
                'behavior': ''
            });
        }
    }

    fancyboxcontent.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px',
        'background-color': 'transparent'
    });

    fancyboxouter.addClass("corner iradius10");

    fancyboxouter.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px',
        'background-color': 'transparent'
    });

    fancyboxouter.css({
        'position': 'absolute',
        'background': '#FFFFFF'
    });

    if ($('#FancyBoxProgressDiv').length === 0) {
        fancyboxouter.append('<div id="FancyBoxProgressDiv" style="z-index:1001; position:relative; display: none;"><div id="FancyBoxProgressGrayOverlay"><div id="FancyBoxProgressSpinnerOverlay"><b>Loading, please wait...</b><br /><img src="/App_Themes/DRHortonSite/images/wait/wait30trans.gif" /></div></div></div>');
    }

    var fancyboxprogressdiv = $('#FancyBoxProgressDiv');
    var fancyboxprogressgrayoverlay = $('#FancyBoxProgressGrayOverlay');
    var fancyboxprogressspinneroverlay = $('#FancyBoxProgressSpinnerOverlay');

    fancyboxprogressgrayoverlay.css({
        'margin': '0px',
        'padding': '0px',
        'top': '0px',
        'left': '0px',
        'position': 'absolute',
        'z-index': '1000',
        'background-color': 'Gray', 
        'filter': 'alpha(opacity=70)',
        'opacity': '0.7',
        'height': '100%',
        'width': '100%',
        'min-width': '0px'
    });

    fancyboxprogressspinneroverlay.css({
        'border': '1px solid black',
        'padding': '10px',
        'background-color': '#eee',
        'z-index': '1001',
        'position': 'absolute',
        'filter': 'alpha(opacity=100)',
        'opacity': '0.9',
        'margin-top': '10%',
        'margin-left': '40%',
        'min-width': '0px',
        'text-align': 'center',
        'vertical-align': 'middle'
    });

    if (!jQuery.browser.msie) {
        $('#fancy-bg-e').css({
            'background-image': 'none'
        });
        $('#fancy-bg-n').css({
            'background-image': 'none'
        });
        $('#fancy-bg-ne').css({
            'background-image': 'none'
        });
        $('#fancy-bg-se').css({
            'background-image': 'none'
        });
        $('#fancy-bg-s').css({
            'background-image': 'none'
        });
        $('#fancy-bg-sw').css({
            'background-image': 'none'
        });
        $('#fancy-bg-w').css({
            'background-image': 'none'
        });
        $('#fancy-bg-nw').css({
            'background-image': 'none'
        });
    }

    fancyboxouter.css({
        'border': '1px solid',
        'border-color': '#808080',
        'position': 'absolute',
        'left': '0px',
        'top': '0px',
        'width': (width + 20) + 'px',
        'height': (height + 33 + InitTitleSize) + 'px'
    });

    fancyboxclose.css({
        'background-image': 'url("/App_Themes/DRHortonSite/js/fancybox/close_icon.png")',
        'background-position': '-30px 0',
        'height': '30px',
        'width': '30px',
        'top': '-15px',
        'right': '-15px',
        'position': 'absolute',
        'z-index': '10001'
    });

    fancyboxprogressgrayoverlay.css({
        'position': 'absolute',
        'left': '0px',
        'top': '0px',
        'width': (width + 20) + 'px',
        'height': (height + 33 + InitTitleSize) + 'px'
    });

    fancyboxprogressdiv.attr("style", "z-index:10000; position:absolute; top: 0;");

    fancydiv.width(jQuery(document).width());
    fancydiv.height(jQuery(document).height());

    document.getElementById("FancyDiv").style.display = "block";
}

function FancyBoxOnCleanup(control, url) {
    if (fancyboxsubmitted == false) {
        if (fancyboxframesrc.toLowerCase().endsWith("metro-rfi.aspx?rfi=start")) {
            var geturl = fancyboxframesrc.replace("start", "cancel");
            jQuery.ajax({
                url: geturl,
                type: 'GET',
                dataType: 'html',
                complete: function(res, status) {
                    //
                }
            });
        }
        if (fancyboxframesrc.toLowerCase().endsWith("request-information.aspx?rfi=start")) {
            var geturl = fancyboxframesrc.replace("start", "cancel");
            jQuery.ajax({
                url: geturl,
                type: 'GET',
                dataType: 'html',
                complete: function(res, status) {
                    //
                }
            });
        }
    }
}

function FancyBoxOnClosed(control, url, width, height, titleHeight) {
    var menuElemDivDiv = $("#menuElem div div");
    var LastDivControl = $("#" + LastDiv);

    var fancyboxtmp = $('#fancybox-tmp');
    var fancyboxwrap = $('#fancybox-wrap');
    var fancyboxcontent = $('#fancybox-content');
    var fancyboxouter = $('#fancybox-outer');
    var fancyboxtitle = $('#fancybox-title');
     
    document.getElementById("FancyDiv").style.display = "none";

    fancyboxtmp.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px'
    });

    fancyboxwrap.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px'
    });

    fancyboxcontent.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px'
    });

    fancyboxouter.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px'
    });

    fancyboxtitle.css({
        'left': '0px',
        'top': '0px',
        'width': '0px',
        'height': '0px'
    });

    $("#CommunityContent").show();

    if ($("a[id$=hlkNLBOverview]").length !== 0) {
        if (LastDivControl.length !== 0) {
            menuElemDivDiv.ready(function() {
                menuElemDivDiv.each(function(index, element) {
                    $(this).removeClass("CMSListMenuLIRightSelected");
                });

                LastDivControl.addClass("CMSListMenuLIRightSelected");
            });
        }
    }

    ContentLoad = false;
}

function FancyBoxTitleFormat(title, currentArray, currentIndex, currentOpts) {
    var html = String.format("<div style='line-height: 30px; vertical-align: middle;font-weight:bolder;'>{0}</div>", title); // Center Title
    return html;
}

function ShowFancyBoxSelector(control, url, width, height, title, titleHeight) {
    $(document).ready(function() {
        $(control).fancybox({
            'href': url, 'overlayShow': false, 'centerOnScroll': false, 'scrolling': 'auto', 'autoScale': false, 'width': width, 'height': height, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': true, 'titlePosition': 'inside', 'title': unescape(title),
            'onComplete': function() {
                FancyBoxOnComplete(control, url, width, height, titleHeight);
            },
            'onStart': function() {
                FancyBoxOnStart(control, url, width, height, titleHeight);
            },
            'onClosed': function() {
                FancyBoxOnClosed(control, url, width, height, titleHeight);
            },
            'onCleanup': function() {
                FancyBoxOnCleanup(control, url);
            },
            'titleFormat': FancyBoxTitleFormat
        });
    });
}

function ShowFancyBox(control, url, width, height, title, titleHeight) {
    ShowFancyBoxSelector("#" + control, url, width, height, unescape(title), titleHeight);
}

function ShowFancyBoxLoad(control, url, width, height, title, titleHeight) {
    $(document).ready(function() {
        $("#" + control).fancybox({
            'href': url, 'overlayShow': false, 'centerOnScroll': false, 'scrolling': 'auto', 'autoScale': false, 'width': width, 'height': height, 'transitionIn': 'none', 'transitionOut': 'none', 'type': 'iframe', 'titleShow': true, 'titlePosition': 'inside', 'title': title,
            'onComplete': function() {
                FancyBoxOnComplete(control, url, width, height, titleHeight);
            },
            'onStart': function() {
                FancyBoxOnStart(control, url, width, height, titleHeight);
            },
            'onClosed': function() {
                FancyBoxOnClosed(control, url, width, height, titleHeight);
            },
            'onCleanup': function() {
                FancyBoxOnCleanup(control, url);
            },
            'titleFormat': FancyBoxTitleFormat
        }).trigger('click');
    });
}

function shadowPage() {
    var shadowdiv = $("#ShadowDiv");
    shadowdiv.width(jQuery(document).width());
    shadowdiv.height(jQuery(document).height());
    document.getElementById("ShadowDiv").style.display = "block";
}

function showSpinnerONLY() {
    scroll(0, 0);
    shadowPage();
    document.getElementById("PageSpinnerDiv").style.display = "block";
}

function showSpinner(redirectURL) {
    showSpinnerONLY();
    document.location = redirectURL;
}

function ShowContent(control) {
    var shadowdiv = $("#ShadowDiv");
    var controlselector = $("#" + control);
    var communitycontent = $("#CommunityContent");
    var pagespinner = $("#PageSpinnerDiv");

    ContentLoad = true;

    controlselector.click(function(e) {
        e.preventDefault();

        PageURL = $(this).attr('href');

        communitycontent.hide();
        shadowdiv.hide();
        pagespinner.hide();

        loadWholePage($(this).attr('id'), $(this).attr('href'), true);

        return true;
    });

    controlselector.load(function() {
    }).trigger('click');
}

$(function() {
    $("a[id^=ctl00_ctl00_ctl00_cphContent_NavigationColumn_hlkNLB]").click(function(e) { e.preventDefault(); loadWholePage($(this).attr('id'), $(this).attr('href'), false); });
});

$(document).ready(function() {
    var height = $(window).height();
    var heightfromtop = height * .2;
    var margin = "margin-top: " + heightfromtop.toString() + "px";
    var width = $(window).width();
    var movewidth = width - 30;
    var marginleft = "margin-right: " + movewidth.toString() + "px";
    if (width < 1025) {
        $("#rfitab").attr("style", marginleft);
        $("#rfitab").attr("z-index", "0");
    }
    $("#rfitab").attr("style", margin);
    //$("#rfitab").attr("onmouseover", "ShowFancyBox('ctl00_ctl00_ctl00_rfitab', '/Where-We-Build/Texas/Dallas-Fort-Worth/Metro-RFI.aspx', 400, 400, 'Request Infomation', 0);");

    var Metro = $("a[id$=hlkMPNLBMetroMap]").length;
    var Community = $("a[id$=hlkNLBOverview]").length;

    if (Metro === 0 & Community === 0) {
        $("#menuElem ul li").children("a").each(function(index, element) {
            $(element).attr("title", $(element).text());
            $(element).closest("li").css({ "cursor": "pointer" });
            if ($(element).text().length >= 24) {
                $(element).html($(element).text().substring(0, 21) + "...");
            }
        });
        $("#menuElem ul li").children("span").each(function(index, element) {
            $(element).attr("title", $(element).text());
            $(element).closest("li").css({ "cursor": "pointer" });
            if ($(element).text().length >= 24) {
                $(element).html($(element).text().substring(0, 21) + "...");
            }
        });
        $("#menuElem div ul li").click(function(e) {
            $("#menuElem div ul").children("li").each(function(index, element) {
                if ($(this).hasClass("CMSListMenuHighlightedLI")) {
                    $(this).addClass("CMSListMenuLI").find("a").addClass("CMSListMenuLink");
                }
                if ($(this).hasClass("CMSListMenuHighlightedLI")) {
                    $(this).removeClass("CMSListMenuHighlightedLI").find("a").removeClass("CMSListMenuLinkHighlighted");
                }
            });
            $(this).children("a").each(function(index, element) {
                e.preventDefault();
                $(element).closest("li").removeClass("CMSListMenuLI");
                $(element).removeClass("CMSListMenuLink");
                $(element).closest("li").addClass("CMSListMenuHighlightedLI");
                $(element).addClass("CMSListMenuLinkHighlighted");
                $(element).blur();
                window.location = $(element).attr('href');
            });
        });

        $("#menuElem ul li").click(function(e) {
            $("#menuElem ul").children("li").each(function(index, element) {
                if ($(this).hasClass("CMSListMenuHighlightedLI")) {
                    $(this).addClass("CMSListMenuLI").find("a").addClass("CMSListMenuLink");
                }
                if ($(this).hasClass("CMSListMenuHighlightedLI")) {
                    $(this).removeClass("CMSListMenuHighlightedLI").find("a").removeClass("CMSListMenuLinkHighlighted");
                }
            });
            $(this).children("a").each(function(index, element) {
                e.preventDefault();
                $(element).closest("li").removeClass("CMSListMenuLI");
                $(element).removeClass("CMSListMenuLink");
                $(element).closest("li").addClass("CMSListMenuHighlightedLI");
                $(element).addClass("CMSListMenuLinkHighlighted");
                $(element).blur();
                window.location = $(element).attr('href');
            });
        });
    }
    if (Metro !== 0) {
        $("#menuElem div div ul li").click(function(e) {
            $(this).children("a").each(function(index, element) {
                $("#menuElem div div").removeClass("CMSListMenuLILeftSelected");
                $(this).closest("div").addClass("CMSListMenuLILeftSelected");
                $(this).blur();
                var NLB = $(element).closest("div").find("a[id^=ctl00_ctl00_ctl00_cphContent_NavigationColumn_hlkNLB]").length;
                var LB = $(element).closest("div").find("a[id^=ctl00_ctl00_ctl00_cphContent_NavigationColumn_hlkLB]").length;

                if (NLB !== 0) {
                    LastDiv = $(this).closest("div").attr("id");
                }
            });
        });
        $("#menuElem div div ul li a").click(function() {
            $("#menuElem div div").removeClass("CMSListMenuLILeftSelected");
            $(this).closest("div").addClass("CMSListMenuLILeftSelected");
            $(this).blur();
        });
        $("#menuElem div div ul li span").click(function() {
            $("#menuElem div div").removeClass("CMSListMenuLILeftSelected");
            $(this).closest("div").addClass("CMSListMenuLILeftSelected");
            $(this).blur();
        });
    }
    else if (Community !== 0) {
        $("#menuElem div div ul li").click(function(e) {
            $(this).children("a").each(function(index, element) {
                $("#menuElem div div").removeClass("CMSListMenuLIRightSelected");
                $(this).closest("div").addClass("CMSListMenuLIRightSelected");
                $(this).blur();
                var NLB = $(element).closest("div").find("a[id^=ctl00_ctl00_ctl00_cphContent_NavigationColumn_hlkNLB]").length;
                var LB = $(element).closest("div").find("a[id^=ctl00_ctl00_ctl00_cphContent_NavigationColumn_hlkLB]").length;

                if (NLB !== 0) {
                    LastDiv = $(this).closest("div").attr("id");
                }
            });
        });
        $("#menuElem div div ul li a").click(function() {
            $("#menuElem div div").removeClass("CMSListMenuLIRightSelected");
            $(this).closest("div").addClass("CMSListMenuLIRightSelected");
            $(this).blur();
        });
        $("#menuElem div div ul li span").click(function() {
            $("#menuElem div div").removeClass("CMSListMenuLIRightSelected");
            $(this).closest("div").addClass("CMSListMenuLIRightSelected");
            $(this).blur();
        });
    }
});
