var fileName = window.location.href.substr(window.location.href.lastIndexOf("/"));

$(document).ready(function () {

    pageFixes();
    navInit();
    moveReferences();
    accordianInit();
    fixRolloversIE6(".rolloverTout");
    fixInputKeypressEnter();
    applyPrintStyles();
});

function applyPrintStyles() {
    if (getParameterByName("print")) {
        if (location.pathname.indexOf("index") == -1 && location.pathname.indexOf("patient-profiles") == -1) {
            $("p:first", "#containerBody").css("clear", "both");
        }
        $("#pageHeader").css("border", "none").prependTo("#containerBody");
        $(".logoCollection", "#containerHeader").prependTo("#containerBody");
        $(".headerLinkBox").children("div").css("display", "none");
        $(".noFlashHeader", "#pageHeader").css("background", "none").css("height", "100px").css("padding-top", "0px").css("margin-left", "-210px"); ;
        $("#containerHeader").hide();
        $("#topNav").hide();
        $(".home-isi a").hide();
        setTimeout("self.print()", 500);
    }
}


function setLeftTouts() {
    for (var i = 0; i < arguments.length; i++) {
        $("#" + arguments[i]).css("display", "block");
        if (!($("#" + arguments[i]).hasClass("stay-inside"))) $("#" + arguments[i]).appendTo("#sideBar");
    }
}


function fixRolloversIE6() {
    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < '7') {
        for (var i = 0; i < arguments.length; i++) {
            $(arguments[i]).hover(
            function () {
                $(this).children("img").addClass("activeRollover");
            },
            function () {
                $(this).children("img").removeClass("activeRollover");
            }
        );
        }
    }
}


function navInit() {
    //initializes rollover effect
    $(".navGroup").each(function () {
        $(this).hover(
            function () {
                $(this).find(".navMainLink img").css("bottom", "0");
                $(this).children(".navSub").css("display", "block");
                $(this).children(".navSub").css("z-index", "2");
            },
            function () {
                $(this).find(".navMainLink img").css("bottom", "auto");
                $(this).children(".navSub").css("display", "none");
                $(this).children(".navSub").css("z-index", "1");
            });
    });


    //hides subnav after alignment, since need to be displayed to pull heights
    $(".navSub").each(function () { $(this).css("display", "none"); });

    //apply active class to appropriate nav links in chain
    $("a[href$='" + fileName + "']").each(function () {

        $(this).addClass("active");
        $(this).parent().each(function () {
            if ($(this).hasClass("navTertiary")) {
                $("a[name=" + $(this).attr("name") + "]").each(function () {
                    $(this).addClass("active");
                    $(this).parent().addClass("active");
                    $(this).parent().siblings(".navMainLink").addClass("active");
                });
                $(this).appendTo($("#tertiaryNav"));
            }
            else if ($(this).hasClass("navSub")) {
                $(this).addClass("active");
                $(this).siblings(".navMainLink").addClass("active");

            }
            else {
                $(this).children(".navSub").addClass("active");
            }
        });
        $(this).siblings("div[name=" + $(this).attr("name") + "]").appendTo($("#tertiaryNav"));
    });
}

function pageFixes() {
    //fix structural errors in bpv3 master
    //$("#printContent table tbody tr td").css("padding", "0");
    $("#container table:last").css("display", "none");

    $("#containerBody *:first").css("margin-top","16px");
}

function moveReferences() {
    $("#containerISI").append($("#references"));
}

function accordianInit() {

    $("a.toggler").toggle(
        function () {
            $(this).siblings("*").fadeIn('slow');
            $(this).addClass("open");
        },
        function () {
            $(this).siblings("*").fadeOut('slow');
            $(this).removeClass("open");
        }
    );

}

function dlPDF(selectID) {
    var pdfLink = document.getElementById(selectID).value;
    var pdfName = document.getElementById(selectID).name;

    dcsMultiTrack('DCS.dcssip', location.host, 'DCS.dcsuri', pdfLink.substr(2), 'WT.ti', pdfName);
    window.open(pdfLink, "pdfWindow");
}

/*===================================
-- Begin Email a Friend function
====================================*/

function go_now() {
    var currentSite = window.location.href;
    currentSite = currentSite.toLowerCase();

    var strauthUrl = "-authoring.rf.lilly.com";
    var strstgUrl = "-staging.rf.lilly.com";
    var strpseUrl = "-pseudo.rf.lilly.com";
    var url = "";

    if (currentSite.indexOf(strauthUrl.toLowerCase()) >= 0 || currentSite.indexOf(strstgUrl.toLowerCase()) >= 0 || currentSite.indexOf(strpseUrl.toLowerCase()) >= 0) {
        url = "https://emailafriend-d6.xh1.lilly.com/servlet/EmailAFriendServlet?url=" + currentSite;
    }
    else {
        url = "https://emailafriend.lilly.com/servlet/EmailAFriendServlet?url=" + currentSite;
    }

    window.open(url, 'EmailAFriend', 'height=500, resizable=yes, scrollbars=yes, width=650');
}
/*===================================
-- End Email a Friend functions
====================================*/

function popSizedWindow(url) {
    window.open(url, "", "height=500, width=500");
}

function popPrivacy() {
    dcsMultiTrack('DCS.dcssip', 'www.lillyhub.com', 'DCS.dcsuri', '/legal/consumer/lillyusa/privacy.html', 'WT.ti', 'Privacy Statement');
    popupWin = window.open("http://www.lillyhub.com/legal/hcp/lillyusa/privacy.html", "ViewPop", "status=0,height=590,width=600,scrollbars=1,resizable=yes,toolbar=0")
    return false;
}
function popTerms() {
    dcsMultiTrack('DCS.dcssip', 'www.lillyhub.com', 'DCS.dcsuri', '/legal/consumer/lillyusa/termsofuse.html', 'WT.ti', 'Terms of Use');
    popupWin = window.open("http://www.lillyhub.com/legal/hcp/lillyusa/termsofuse.html", "ViewPop", "status=0,height=590,width=600,scrollbars=1,resizable=yes,toolbar=0")
    return false;
}
function popCopy() {
    dcsMultiTrack('DCS.dcssip', 'www.lillyhub.com', 'DCS.dcsuri', '/legal/consumer/lillyusa/copyright.html', 'WT.ti', 'Copyright');
    popupWin = window.open("http://www.lillyhub.com/legal/hcp/lillyusa/copyright.html", "ViewPop", "status=0,height=590,width=600,scrollbars=1,resizable=yes,toolbar=0")
    return false;
}

function fixInputKeypressEnter() {
    $("#searchBox input").css("display", "none");
    $("input[id$='InputKeywords']").prependTo("#searchBox");

    //replaces key binding on search input with a similar function, adding
    //return false to break the default habit of the enter key, allowing the
    //submit function to perform as intended
    $("input[id$='InputKeywords']").unbind();
    $("input[id$='InputKeywords']").bind("keypress", function replacement_enterDetect(event1) {
        var kCode = String.fromCharCode(event1.keyCode);
        if (kCode == "\n" || kCode == "\r") {
            SABFBB873_Submit();
            return false;
        }
    });
}

