// Initialize and set Superfish dropdown options
$(document).ready(function () {
	$("a[href$='.pdf']").addClass("pdf");
	$("a[href$='.doc'], a[href$='.docx']").addClass("doc");
	
	//Set the _target attribute to blank
	$('a').filter(function() {
		//Compare the anchor tag's host name with location's host name
		return this.hostname && this.hostname !== location.hostname;
	}).addClass("external").attr("target", "_blank");

    $("ul.sf-menu").superfish({
        delay: 5000,
        disableHI: false,
        pathClass: 'current',
        autoArrows: false,
        dropShadows: false
    });

    $("a.leavingSite").click(function (objEvent) {
        leavingSiteConfirmation(this, objEvent);
    });

    // Initialize homepage accordion
    $("#hp_accordion").accordion({
        header: '.section_head',
        clearStyle: true
    });

    // Initialize homepage slider
    $('#coda-slider-1').codaSlider({
        autoSlide: true,
        autoHeight: false,
        autoSlideInterval: 10000,
        dynamicArrows: false,
        dynamicTabs: false
    });

    var addthis_config = {
        "data_track_clickback": true,
        "ui_508_compliant": true
    };

    var strURL = document.location.toString();
    if (strURL.match('#')) {
        strURL = strURL.split('#')[1];
        $('a[name="' + strURL + '"]').parent().addClass('openslide').addClass('close').removeClass('open');
    }

    $('div.slide').click(function () {
        if ($(this).hasClass('open')) {
            $(this).removeClass('open');
            $(this).addClass('close');
            $(this).next().slideDown(100);
            return false;
        } else {
            $(this).removeClass('close');
            $(this).addClass('open');
            $(this).next().slideUp(100);
            return false;
        }
    });

    $('div.openslide').next().slideDown(100);
    $("#divLoginSelector").dialog({
        "autoOpen": false,
        "modal": true,
        "buttons": {
            "Personal": function () {
                $(this).dialog("close");
                loginLinkRedirects("Personal");
            },
            "Business": function () {
                $(this).dialog("close");
                loginLinkRedirects("Business");
            }
        }
    });


    login_account_type_change();

    $(".login_account_type").change(function () {
        login_account_type_change();
    });

    $(".ctrlAgree_radContinue").click(function (objEvent) {
        if (!$("#radAgree").is(":checked") && !$("#radDisagree").is(":checked")) {
            objEvent.preventDefault();
            $("<" + "div" + ">" + "<" + "/div" + ">")
                .attr("id", "ctrlAgree_Notice")
                .attr("title", "Error!")
                .html("<" + "span class='ui-icon ui-icon-alert' style='float: left;'" + ">" + "&nbsp;" + "<" + "/span" + ">" + "You must indicate if you agree or disagree.")
                .dialog({
                    modal: true,
                    buttons: {
                        "Okay": function () {
                            $(this).dialog("close");
                        }
                    }
                });
        }
    });
});

var strLogin_Link = "";

function loginLinkClick($strLink) {
    strLogin_Link = $strLink
    $("#divLoginSelector").dialog("open");
}

function loginLinkRedirects($strType) {
    switch ($strType) {
        case "Personal":
            loginLinkRedirects_Personal();
            break;
        case "Business":
            loginLinkRedirects_Business();
            break;
    }
}

function loginLinkRedirects_Personal() {
    switch (strLogin_Link) {
        case "Enroll":
            document.location = "/resources/personal-online-banking.aspx";
            break;
        case "Demo":
            document.location = "http://onlinebanktours.com/oec/index.php?BID=38&CL=7&category=Personal";
            break;
        case "Learn":
            document.location = "/personal/eservices/online-banking-and-bill-pay.aspx";
            break;
    }
}
function loginLinkRedirects_Business() {
    switch (strLogin_Link) {
        case "Enroll":
            document.location = "/business/eservices/online-banking.aspx";
            break;
        case "Demo":
            document.location = "http://onlinebanktours.com/oec/index.php?BID=38&CL=36&category=Business";
            break;
        case "Learn":
            document.location = "/business/eservices/online-banking.aspx";
            break;
    }
}

var $s_arrHelpMeDecide_Personal = {
    "M": "Are you looking for a deposit account?",
    "YB": "Yes",
    "NB": "Other",
    "Y": {
        "M": "Are you planning to use your deposit account for Day to Day Transactions?",
        "YB": "Yes",
        "NB": "No",
        "Y": "/personal/checking/",
        "N": {
            "M": "Are you looking to save money with your deposit account or to create a Health Savings account?",
            "YB": "Save Money",
            "NB": "Health Savings",
            "Y": {
                "M": "Will you need access to your account more than once every few months?",
                "YB": "Yes",
                "NB": "No",
                "Y": "/personal/cds-iras-health-savings/",
                "N": "/personal/savings-mms/"
            }
        }
    },
    "N": {
        "M": "Are you looking for Online Services?",
        "YB": "Yes",
        "NB": "No",
        "Y": "/personal/eservices/",
        "N": {
            "M" : "Are you looking for a Loan or Trust & Advisory Investments?",
            "YB" : "Loan",
            "NB" : "Trust & Advisory",
            "Y": "/personal/loans-mortgages/",
            "N" : "/personal/trust-advisory/"
        }
    }
}

var $s_arrHelpMeDecide_Business = {
    "M": "Are you looking for a deposit account?",
    "YB": "Yes",
    "NB": "No",
    "Y": {
        "M" : "Are you going to user your deposit account for Day-To-Day Transactions or to Save Money?",
        "YB" : "Day-To-Day",
        "NB" : "Save Money",
        "Y" : "/business/checking/",
        "N" : "/business/savings/"
    },
    "N": {
        "M" : "Are you looking for a Loan or other services?",
        "YB" : "Loan",
        "NB" : "Other",
        "Y" : "/business/loans/",
        "N" : {
            "M" : "Are you looking for Online Services or Other Services?",
            "YB" : "Online",
            "NB" : "Other",
            "Y" : "/business/eservices/",
            "N" : "/business/other-services/"
        }
    }
}

var $s_arrHelpMeDecide_PC = {
    "M" : "Are you a student and 22 or under?",
    "YB" : "Yes",
    "NB" : "No",
    "Y" : "/personal/checking/student-checking.aspx",
    "N": {
        "M" : "What is your average daily balance?",
        "YB" : "Under $1,000",
        "NB" : "Over $1,000",
        "Y" : {
            "M" : "Are you over 55 and seeking an interest-bearing account?",
            "YB" : "Yes",
            "NB" : "No",
            "Y": "/personal/checking/golden-premier-checking.aspx",
            "N": "/personal/checking/relationship-checking.aspx"
        },
        "N" : {
            "M" : "What is your average daily balance?",
            "YB" : "$10,000 and under",
            "NB" : "Over $10,000",
            "Y" : {
                "M" : "Are you over 55 years of age?",
                "YB" : "Yes",
                "NB" : "No",
                "Y" : "/personal/checking/golden-premier-checking.aspx",
                "N" : "/personal/checking/premier-checking.aspx"
            },
            "N" : "/personal/checking/investment-account.aspx"
        }
    }
};

var $s_arrHelpMeDecide_PS = {
    "M": "Are you a student and 22 or under?",
    "YB" : "Yes",
    "NB" : "No",
    "Y" : "/personal/savings-mms/student-savings.aspx",
    "N" : {
        "M" : "What is your average daily balance",
        "YB" : "Under $1,500",
        "NB" : "Over $1,500",
        "Y" : "/personal/savings-mms/savings-account.aspx",
        "N" : "/personal/savings-mms/money-market.aspx"
    }
}

var $s_arrHelpMeDecide_BC = {
    "M" : "What do you consider your business?",
    "YB" : "Non-Profit or Government Unit",
    "NB" : "Other",
    "Y": "/business/checking/non-profit-checking.aspx",
    "N" : {
        "M" : "Are you a small business?",
        "YB" : "Yes",
        "NB" : "No",
        "Y" : {
            "M": "What is your average daily balance??",
            "YB" : "Under $1,000",
            "NB" : "Over $1,000",
            "Y"  : "/business/checking/business-value-checking.aspx",
            "N"  : "/business/checking/business-value-checking.aspx"
        },
        "N" : {
            "M": "Are you a mid-size business?",
            "YB" : "Yes",
            "NB" : "No",
            "Y" : "/business/checking/business-checking.aspx",
            "N" : "/business/checking/prestige-business-checking.aspx"
        }
    }
}

/*
function HelpMeDecide() {
    $("<" + "div" + ">" + "<" + "/" + "div" + ">")
        .attr("id", "diaHelpMeDecide")
        .attr("title", "Help Me Decide")
        .html("This wizard will ask a series of questions to help guide you to the account that will best suit your needs. First up, are you looking for a Personal account or a Business account?")
        .dialog({
            modal: true,
            buttons: {
                "Personal" : function() {
                    $(this).dialog("close");
                        $("<" + "div" + ">" + "<" + "/" + "div" + ">")
                            .attr("id", "diaHelpMeDecide")
                            .attr("title", "Help Me Decide")
                            .html("Are you looking for a Checking account or a Savings/Money Market account?")
                            .dialog({
                                modal: true,
                                buttons: {
                                    "Checking" : function() {
                                        $(this).dialog("close");
                                        runHelpMeDecide($s_arrHelpMeDecide_PC);
                                    },
                                    "Savings/Money Market" : function() {
                                        $(this).dialog("close");
                                        runHelpMeDecide($s_arrHelpMeDecide_PS);
                                    }
                                }
                            });
                },
                "Business" : function() {
                    $(this).dialog("close");
                    runHelpMeDecide($s_arrHelpMeDecide_BC);
                }
            }
        });
}*/

function HelpMeDecide($strType) {
    switch ($strType) {
        case "Business_Checking":
            runHelpMeDecide($s_arrHelpMeDecide_BC);
            break;
        case "Personal_Checking":
            runHelpMeDecide($s_arrHelpMeDecide_PC);
            break;
        case "Personal_Savings":
            runHelpMeDecide($s_arrHelpMeDecide_PS);
            break;
        case "Business":
            runHelpMeDecide($s_arrHelpMeDecide_Business);
            break;
        case "Personal":
            runHelpMeDecide($s_arrHelpMeDecide_Personal);
            break;
    }
}

function runHelpMeDecide($arrHelp) {
    var $arrButtons = {};
    $arrButtons[$arrHelp["YB"]] = function () {
        $(this).dialog("close");
        processHelpMeDecide($arrHelp["Y"]);
    }

    $arrButtons[$arrHelp["NB"]] = function () {
        $(this).dialog("close");
        processHelpMeDecide($arrHelp["N"]);
    }

    $("<" + "div" + ">" + "<" + "/" + "div" + ">")
        .attr("id", "diaHelpMeDecide")
        .attr("title", "Help Me Decide")
        .html($arrHelp["M"])
        .dialog({
            modal: true,
            buttons: $arrButtons
        });
}

function processHelpMeDecide($arrHelp) {
    if (typeof($arrHelp) == "object") {
        runHelpMeDecide($arrHelp);
    } else {
        document.location = $arrHelp;
    }
}

function login_account_type_change() {
    $(".login_take_business").toggle(false);
    $(".login_take_personal").toggle(false);
    //$(".login_username").toggle(false);

    switch ($(".login_account_type").val()) {
        case "Personal":
            $(".login_take_personal").toggle(true);
            //$(".login_username").toggle(true);
            break;
        case "Business":
            $(".login_take_business").toggle(true);
            break;
        default:
            break;
    }
}

function searchFAQ($objEvent, $blnSkip) {
    var $strSearch = $("#faq_search").val();

    if ($objEvent == null) {
        $objEvent = window.event;
        if ($objEvent == null && !$blnSkip) {
            alert("It appears your browser doesn't support dynamic searching. Please use the search bar at the top of your screen.");
            return false;
        }
    }

    if ($objEvent && $objEvent.keyCode == '13') {
        $objEvent.returnValue = false;
        return false;
    } else if ($objEvent && $objEvent.keyCode == '8') {
        //$strSearch = $strSearch.substr(0, $strSearch.length - 1);
    }


    if ($strSearch == "" || $strSearch == "Enter Search Terms...") {
        $strSearch = ".*";
    } else {
        if ($strSearch.indexOf(" ") != -1) {
            $arrSearch = $strSearch.split(" ");
            $strSearch = "";
            for ($tString in $arrSearch) {
                if ($arrSearch[$tString] != "") {
                    $strSearch = $strSearch + "(?=.*(" + $arrSearch[$tString] + "))";
                }
            }
            //$strSearch = "^" + $strSearch;
        } else {
            $strSearch = "(" + $strSearch + ")";
        }
    }

    $(".channel").each(function () {
        $(this).toggle(true);
    });


    var tReg = new RegExp($strSearch, "i");

    $(".slide").each(function () {
        $blnGood = false;

        if ($(this).text().match(tReg) || $(this).next().text().match(tReg)) {
            $blnGood = true;
        }

        $(this).toggle($blnGood);
        if ($(this).hasClass("close")) {
            $(this).next().toggle(true);
        }
    });

    var $blnAllHidden = true;
    $(".channel").each(function () {
        blnVisible = false;

        $(this).children(".slide").each(function () {
            if ($(this).is(":visible")) {
                blnVisible = true;
                $blnAllHidden = false;
            }
        });

        $(this).toggle(blnVisible);
    });

    if ($blnAllHidden == true) {
        $("<" + "div" + ">" + "<" + "/div" + ">")
        .attr("id", "divLeaveSite")
        .attr("title", "No Results")
        .html("<" + "span class='ui-icon ui-icon-alert'" + ">" + "&nbsp;" + "<" + "/span" + ">" + "No search results were found. Try using less and/or more specific search terms.")
        .dialog({
            modal: true,
            buttons: {
                "Okay": function () {
                    $(this).dialog("close");
                }
            }
        });
    }
}

function clearSearchFAQ() {
    $("#faq_search").val("Enter Search Terms...");
    return searchFAQ(null, true);
}

function leavingSiteConfirmation(objLink, objEvent) {
    objEvent.preventDefault();

    $("<" + "div" + ">" + "<" + "/div" + ">")
        .attr("id", "divLeaveSite")
        .attr("title", "Are you sure?")
        .html("<" + "span class='ui-icon ui-icon-alert'" + ">" + "&nbsp;" + "<" + "/span" + ">" + "You are about to leave the UCBI website. Are you sure?")
        .dialog({
            modal: true,
            buttons: {
                "Yes": function () {
                    $(this).dialog("close");
                    document.location = $(objLink).attr("href");
                },
                "No": function () {
                    $(this).dialog("close");
                }
            }
        });
}
