﻿$(document).ready(function () {
    var fileName = window.location.href.substr(window.location.href.lastIndexOf("/") + 1);

    if (fileName.indexOf("#") != -1) {
        fileName = fileName.substr(0, fileName.indexOf("#"));
    }

    $("#subNav").find("a[href=" + fileName + "]").parent().css("color", "#850057")

    $("#subNav").find("a[href=" + fileName + "]").replaceWith(
        $("#subNav").find("a[href=" + fileName + "]").html()
    );

    $("#searchBox").append($("#SRSB div .ms-sbtable .ms-sbgo a"));
    $("#searchBox a img").remove();
    $("#searchBox").append($("#SRSB div .ms-sbtable .ms-sbcell input"));
    $("#searchBox input").css("width", "206px");

    fixInputKeypressEnter();

    applyGlossary(document);

    accordianInit();

    //REMOVE THIS FOR BPV3
    //applyFunction(document);

    $(".jqmWindow").jqm({ toTop: true });
    $(".jqmWindow").jqmAddClose(".interClose");
    $(".jqmWindow").jqmAddClose(".interReturn");

    $(".jqmWindowHCP").jqm({ toTop: true });
    $(".jqmWindowHCP").jqmAddClose(".interClose");
    $(".jqmWindowHCP").jqmAddClose(".interReturn");

    $(".jqmWindowType").jqm({ toTop: true });
    $(".jqmWindowType").jqmAddClose(".interClose-arrowed");
    $(".jqmWindowType").jqmAddClose(".interClose");

    $(".jqmWindowLinks").jqm({ toTop: true });
    $(".jqmWindowLinks").jqmAddClose(".interClose");

    $(".jqmManuals").jqm({ toTop: true });
    $(".jqmManuals a").click(function () { $(".jqmManuals").jqmHide(); });
    $(".popManuals").click(function () { $(".jqmManuals").jqmShow(); });

    $(".jqmManualsMix").jqm({ toTop: true });
    $(".jqmManualsMix a").click(function () { $(".jqmManualsMix").jqmHide(); });
    $(".popManualsMix").click(function () { $(".jqmManualsMix").jqmShow(); });

    $("#fixedExpandingFooter").appendTo("body");

    //initialize all size links with setSizeHighlight function, then call
    //it to set the initial size highlight
    $("#size-print-email > a").click(function () { setSizeHighlight(); });
    setSizeHighlight();

    //disabled at request (globally)
    //setISIautoScrolling();


    /* myd leaving site dialog*/
    $('#myd-leavingSite').jqm({
        modal: true
    });


    $('.myd-leavingsite').bind('click', function (event) {
        event.preventDefault();
        var innerText = $(this).text();
        $('.btn_continue').attr("href", this.href);
        $('.btn_continue').click(function () {
            var spliced = this.href.split('/');
            var remainder = "";

            for (var i = 3; i < spliced.length; i++) {
                remainder += "/" + spliced[i];
            }

            dcsMultiTrack('DCS.dcssip', spliced[2], 'DCS.dcsuri', remainder, 'WT.ti', innerText);
        });
        $('.btn_continue').attr("target", "_blank");
        if ($(this).hasClass("hcpPop")) {
            $("#hcpCopy").css("display", "inline");
            $("#leavingCopy").css("display", "none");
        }
        else {
            $("#hcpCopy").css("display", "none");
            $("#leavingCopy").css("display", "inline");
        }
        $('#myd-leavingSite').jqmShow();
        $('body').prepend($('#myd-leavingSite')); //fix IE layer problem
    });

    $('.btn_continue').bind('click', function () {
        parent.$("#myd-leavingSite").jqmHide();
    })

    $('.btn_return').bind('click', function (event) {
        event.preventDefault();
        $("#myd-leavingSite").jqmHide();
    })


});

function popManuals() { $(".jqmManuals").jqmShow(); }
function popManualsMix() { $(".jqmManualsMix").jqmShow(); }

function fixInputKeypressEnter() {
    //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;
        }
    });
}

//functionality for scrolling ISI, basically iterates downward the scroll
//until the scrollbar stops changing position, then ends and returns scroll
//back to top. Also has interupts via "killScroll" so clicking on certain
//items stops the scrolling
var currentScroll = -1;
var killScroll = "no";

function setISIautoScrolling() {
    $("#fixedFootISI").mousedown(function () { killScroll = "yes"; });
    $("#fixedFootExpandBtn").mousedown(function () { killScroll = "yes"; });

    scrollMe();
}

function scrollMe() {
    if (currentScroll != $("#fixedFootISI").scrollTop() && killScroll == "no") {
        currentScroll = $("#fixedFootISI").scrollTop();
        $("#fixedFootISI").animate({ scrollTop: "+=5" }, 1000, "linear", function () { scrollMe(); });
    }
    else if (killScroll == "no") $("#fixedFootISI").scrollTop("0");
    else return;
}

//uses a variable set in bpv3.js to check text size then highlight
//the appropriate text size link with a class
function setSizeHighlight() {
    var linkArray = $("#size-print-email").children();
    $(linkArray).removeClass("activeSize");

    var sizeLink = 0;

    if (textSize == "Large Text") sizeLink = 2;
    else if (textSize == "Medium Text") sizeLink = 1;
    else sizeLink = 0;

    $(linkArray[sizeLink]).addClass("activeSize");
    //$(thisObject).addClass("activeSize");
}


/*========================================
-- BEGIN Flash Video Modal
==========================================*/

function launchFlashVideo_a(video) {
    $(".jqmFlashVideo").jqmShow();

    var flashvars = {};
    flashvars.videoPath = video;
    var params = {};
    params.wmode = "transparent";
    var attributes = {};
    swfobject.embedSWF("../Multimedia/swf/player_a.swf", "flashModal", 400, 520, "9.0.0", false, flashvars, params, attributes)
};

function launchFlashVideo_b(video) {
    $(".jqmFlashVideo").jqmShow();

    var flashvars = {};
    flashvars.videoPath = video;
    var params = {};
    params.wmode = "transparent";
    var attributes = {};
    swfobject.embedSWF("../Multimedia/swf/player_b.swf", "flashModal", 400, 520, "9.0.0", false, flashvars, params, attributes)
};

/*========================================
-- END Flash Video Modal
==========================================*/

/*========================================
-- BEGIN popup window for Flash
==========================================*/

function launchFlashPopupVideo(video, width, height, flv) {
    var windowWidth = Number(width) + 25;
    var windowHeight = Number(height) + 20;
    window.open("../Pages/pop-video-player.aspx?" + video + "," + width + "," + height + "," + flv, "", "width=" + windowWidth + ",height=" + windowHeight + ",");
}

/*========================================
-- END popup window for Flash
==========================================*/

/*========================================
-- Glossary Callout
==========================================*/
function applyGlossary(node) {
    $("a[name^='glo']").each(function () {
        var nodeName = $(this).attr("name").substring(3);
        $(this).attr("id", nodeName);
        $(this).hover(
			function () { showtrail(nodeName); },
			function () { hidetrail(); }
			);
    });
}

var offsetfrommouse = [20, -10]; //image x,y offsets from cursor position in pixels. Enter 0,0 for no offset
var displayduration = 0; //duration in seconds image should remain visible. 0 for always.
var currentimageheight = 0; // maximum image size.

if (document.getElementById || document.all) {
    document.write('<div id="trailimageid">');
    document.write('</div>');
}

function gettrailobj() {
    if (document.getElementById)
        return document.getElementById("trailimageid").style
    else if (document.all)
        return document.all.trailimageid.style
}

function gettrailobjnostyle() {
    if (document.getElementById)
        return document.getElementById("trailimageid")
    else if (document.all)
        return document.all.trailimageid
}

function truebody() {
    return (!window.opera && document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body
}

function hEvnt(obj, evtName, fnCall) {
    evtName = evtName.toLowerCase(); doCapture = false;

    if (obj.addEventListener) {
        obj.addEventListener(evtName.replace('on', ''), function () { eval(fnCall); }, false);
    }
    else if (obj.attachEvent) {
        obj.attachEvent(evtName, function () { eval(fnCall); });
    }
    else {
        eval(obj + "." + evtName + "=" + fnCall);
    }
}

function uhEvt(obj, evtName, fnCall) {
    evtName = evtName.toLowerCase(); doCapture = false;
    if (obj.addEventListener) { obj.removeEventListener(evtName.replace('on', ''), function () { eval(fnCall); }, doCapture); }
    else if (obj.attachEvent) { obj.detachEvent(evtName, function () { eval(fnCall); }); } else { eval(obj + "." + evtName + "='';"); }
}


/* LOAD THE XML FILE */
var xmlHttp;
var xmlDoc;
var glossarykey = "";
var glossarytitle = "";
var glossarydef = "Sorry, your browser does not support the glossary feature on this website.";

/* Fix for Safari! */
if ((navigator.userAgent.indexOf('Safari') != -1)) {
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open("GET", "/Documents/include/glossary.xml", false);
    xmlHttp.send();
    xmlDoc = xmlHttp.responseXML;
}
else {
    if (window.ActiveXObject) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    else if (document.implementation.createDocument) {
        xmlDoc = document.implementation.createDocument("", "", null);
    }

    if (xmlDoc != null) {
        xmlDoc.async = false;
        xmlDoc.load("/Documents/include/glossary.xml");
    }
    else {
        glossarydef = "Sorry, your browser does not support the glossary feature on this website.";
    }
}


/* PULL THE DEFINITION WHEN CALLED */
function pullDefinition() {
    var xmlLength = xmlDoc.getElementsByTagName("term").length;

    for (var g = 0; g < xmlLength; g++) {

        if (xmlDoc.childNodes[0].getElementsByTagName("term")[g].attributes[0].nodeValue.toLowerCase() == glossarykey) {
            glossarytitle = xmlDoc.childNodes[0].getElementsByTagName("term")[g].attributes[1].nodeValue;
            glossarydef = xmlDoc.childNodes[0].getElementsByTagName("term")[g].attributes[2].nodeValue;
            break;
        }
        else {
            glossarytitle = "N/A";
            glossarydef = "There is no definition for this term yet.";
        }
    }
}

/* CONTROL THE GLOSSARY BOX */
function showtrail(term) {
    glossarykey = term.toLowerCase();
    if (xmlDoc != null) {
        pullDefinition();
    }
    document.onmousemove = followmouse;

    newHTML = '<div id="glossaryDef">';

    newHTML += '<div class="glossaryHeader">Glossary</div>';

    newHTML += '<div class="glossaryContent"><b>';
    newHTML += glossarytitle;
    newHTML += '</b><br>';

    newHTML += glossarydef;

    newHTML += '</div></div>';

    gettrailobjnostyle().innerHTML = newHTML;
    gettrailobj().visibility = "visible";
}


function hidetrail() {
    gettrailobj().visibility = "hidden"
    document.onmousemove = ""
    gettrailobj().left = "-500px"
}

function followmouse(e) {
    var xcoord = offsetfrommouse[0]
    var ycoord = offsetfrommouse[1]

    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    var docheight = document.all ? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

    if (typeof e != "undefined") {
        if (docwidth - e.pageX < 380) {
            xcoord = e.pageX - xcoord - 320; // Move to the left side of the cursor
        } else {
            xcoord += e.pageX;
        }
        if (docheight - e.pageY < (currentimageheight + 110)) {
            ycoord += e.pageY - Math.max(0, (110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
        } else {
            ycoord += e.pageY;
        }
    } else if (typeof window.event != "undefined") {
        if (docwidth - event.clientX < 380) {
            xcoord = event.clientX + truebody().scrollLeft - xcoord - 320; // Move to the left side of the cursor
        } else {
            xcoord += truebody().scrollLeft + event.clientX
        }
        if (docheight - event.clientY < (currentimageheight + 110)) {
            ycoord += event.clientY + truebody().scrollTop - Math.max(0, (110 + currentimageheight + event.clientY - docheight));
        } else {
            ycoord += truebody().scrollTop + event.clientY;
        }
    }

    var docwidth = document.all ? truebody().scrollLeft + truebody().clientWidth : pageXOffset + window.innerWidth - 15
    var docheight = document.all ? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
    if (ycoord < 0) { ycoord = ycoord * -1; }
    gettrailobj().left = xcoord + "px"
    gettrailobj().top = ycoord + "px"
}

/*========================================
-- END Glossary Callout
==========================================*/


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");
}


function closeMe() {
    $(".jqmFlashVideo").jqmHide();
}

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/consumer/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/consumer/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/consumer/lillyusa/copyright.html", "ViewPop", "status=0,height=590,width=600,scrollbars=1,resizable=yes,toolbar=0")
    return false;
}

function popIntLinks() {
    $(".jqmWindowLinks").jqmShow();
}

function accordianInit() {
    $("a.toggler").toggle(
        function () {
            $(this).siblings("*").fadeIn('slow');
            $(this).addClass("open");
        },
        function () {
            $(this).siblings("*").fadeOut('slow');
            $(this).removeClass("open");
        }
    );
}

