﻿$(function () {
    // get outer table height to match window size
    RefreshHeights();
    $(window).resize(RefreshHeights);

    initializeRollovers();
    isiAnchor();
    initializePopupInterstitials();
    showActiveNav();
    interactivePI();
    ts("#body .middle", 0);

    $('.external').click(function (e) {
        $('#leaving-interstitial').jqmShow();
        e.preventDefault();
        var value = $(this).attr('href');
        createCookie("url", value);
    });

    $('.internal').click(function (e) {
        $('#internal-interstitial').jqmShow();
        e.preventDefault();
        var value = $(this).attr('href');
        createCookie("url", value);
    });

    $('.go-hcp').click(function (e) {
        $('#hcp-interstitial').jqmShow();
        e.preventDefault();
        var value = $(this).attr('href');
        createCookie("url", value);       
    });

    $('#pi-popup').click(function () {
        $('#pi-contents').jqmShow();
    });

    $('#edu-resources').click(function () {
        $('#mds-edu-resources').jqmShow();
    });
});

function RefreshHeights() {
    var bodyTargetHeight = $(window).height() - $('#BottomPane').height();
    $('#TopPane').css('height', bodyTargetHeight.toString() + "px");

    var resourcepopupTargetHeight = $(window).height() - 40;
    resourcepopupTargetHeight = (resourcepopupTargetHeight > 835) ? 835 : resourcepopupTargetHeight;
    $('.resources-wrapper').css('height', resourcepopupTargetHeight.toString() + "px");
    $('.resources-wrapper-inner').css('height', (resourcepopupTargetHeight - 33).toString() + "px");

    var PIpopupTargetHeight = $(window).height() - 40;
    resourcepopupTargetHeight = (resourcepopupTargetHeight > 1370) ? 1370 : resourcepopupTargetHeight;
    $('.pi-wrapper').css('height', PIpopupTargetHeight.toString() + "px");
    $('.pi-wrapper-inner ').css('height', (PIpopupTargetHeight - 33).toString() + "px");
}

function interactivePI() {
    $("table#interactive td a").mouseover(function () {
        $(this).css("background", "#ffc423");
    }).mouseout(function () {
        $(this).css("background", "#fff");
    });

    var activePI = $(".active").attr("href");   
    $(activePI).show("fast");

    $("table#interactive td a").click(function () {
        var currentPI = $(".active").attr("href");
        $(currentPI).hide();
        $("#interactive td").find(".active").removeClass("active");
        $(this).addClass("active");
        showActivePI();
        return false;
    });
    
}

function showActivePI() {
    var activePI = $(".active").attr("href");
    createCookie("piId", activePI);
    $(activePI).show("fast");
}

function nextPI() {   
        var currentPI = $(".active").attr("href");
        $(currentPI).hide();

        switch (currentPI) {
            case "#contraindications":
                $("#interactive #column2 a:first").addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
                break;
            case "#clinical-pharmacology":
                $("#interactive #column3 a:first").addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
                break;
            case "#patient-counseling":
                $(currentPI).show();
                break;
            default:
                $(".active").next().addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
        }

}

function previousPI() {
   
        var currentPI = $(".active").attr("href");
        $(currentPI).hide();

        switch (currentPI) {
            case "#nonclinical-toxicology":
                $("#interactive #column2 a:last").addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
                break;
            case "#precautions":
                $("#interactive #column1 a:last").addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
                break;
            case "#highlights-pi":
                $(currentPI).show();
                break;
            default:
                $(".active").prev().addClass("active");
                $("#interactive td").find("a[href^='" + currentPI + "']").removeClass("active");
                showActivePI();
        }

      
}

function initializeRollovers() {
    $(".rollover").hover(
        function () { $(this).attr("src", $(this).attr("src").replace("-off", "-on")); },
        function () { $(this).attr("src", $(this).attr("src").replace("-on", "-off")); }
        );
}

function initializePopupInterstitials() {
    $('#leaving-interstitial').jqm({ modal: true });
    $('#hcp-interstitial').jqm({ modal: true });
    $('#internal-interstitial').jqm({ modal: true });
    $('#pi-contents').jqm({ modal: true });
    $('#mds-edu-resources').jqm({ modal: true });

    $('#mds-edu-resources, #leaving-interstitial, #hcp-interstitial, #internal-interstitial, #pi-contents').prependTo('body');
}

function showInterstitial(site) {
    $('#leaving-interstitial').jqmShow();
    createCookie("url", site);
}


function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');

    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ')
            c = c.substring(1, c.length);

        if (c.indexOf(nameEQ) == 0)
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

function goHCP() {
    
    if (readCookie("url") != null) {
        window.location = readCookie("url");
        eraseCookie("url");
    }
    else {
        var url = $(ddlID).val();
        if (ddlID === ".ddlHCPMM") {                
                window.location = url;
            }else if (ddlID === ".ddlHCPMDS") {
                window.location = url;
            }
        }    
}

function leaveSite() {
    window.open(readCookie("url"), '_blank');
}

function showActiveNav() {

    var curPath = window.location.pathname;
    var curPage = curPath.substring(curPath.lastIndexOf("/") + 1);

    $(".nav").find("a[href^='" + curPage + "']").each(function () {
        $(this).addClass("current");
        if ($(this).hasClass("hasSubnav")) {
            $(this).next().slideDown();
        }

        $(".current img").attr("src", $(".current img").attr("src").replace("-off", "-active"));

        var subNav = $(this).parents("ul");
        if (subNav.hasClass("subnav")) {            
            subNav.prev().addClass("selected").next().show();
            $(".selected img").attr("src", $(".selected img").attr("src").replace("-off", "-active"));
        }
    });
}

//function isiAnchor() {
//    $("#isi-anchor").click(function () {
//        $(".expand-isi").toggleClass("expand-si", 1000);
//        $("#isi-anchor").toggleClass("btn-collapse");
//        return false;
//    });
//}

function isiAnchor() {
    $('.isi-anchor').toggle(function () {
        $('#BottomPane').css("height", expandHeight() + "px");
        $('#BottomContent').css("height", expandHeight() - 19 + "px");
        $('#BottomPane .isi-anchor').removeClass("btn-expand");
        $('#BottomPane .isi-anchor').addClass("btn-collapse");
        //hide Select boxes because IE6 ignores z-index for Select boxes
        $('.hcp-mm select').css('display', 'none');
        $('.hcp-mds select').css('display', 'none');
        $('.dtc-mm select').css('display', 'none');
        $('.dtc-mds select').css('display', 'none');
        $(window).resize();
    }, function () {
        $('#BottomPane').css("height", "");
        $('#BottomContent').css("height", "");
        $('#BottomPane .isi-anchor').addClass("btn-expand");
        $('#BottomPane .isi-anchor').removeClass("btn-collapse");
        //show Select boxes because IE6 ignores z-index for Select boxes
        $('.hcp-mm select').css('display', 'inline-block');
        $('.hcp-mds select').css('display', 'inline-block');
        $('.dtc-mm select').css('display', 'inline-block');
        $('.dtc-mds select').css('display', 'inline-block');
        $(window).resize();
    });
}

function expandHeight() {
    var topHeight = 300;
    var windowHeight = $(window).height();
    
    return windowHeight - topHeight;
}


function toggleAnchor() {
    $(".collapsible").toggle(function () {
        $(this).addClass("expansible");
        $(this).removeClass("collapsible");
        $(this).next().show("slow");
    }, function () {
        $(this).removeClass("expansible");
        $(this).addClass("collapsible");
        $(this).next().hide("slow");
    });
}


function printISI() {
    var piId = readCookie("piId")
    if (piId) {
        var currentPI = $('.active').attr("href");
        $(currentPI).hide();
        $("#interactive td").find(".active").removeClass("active");
        $("#interactive td").find("a[href^='" + piId + "']").addClass("active");
        showActivePI();
        eraseCookie("piId");
        return false;
    }
}

function referencesBeforeFooter() {
    $('div.references').insertBefore('#footer');
}

function removejscssfile(filename, filetype) {
    var targetEle = (filetype == "js") ? "script" : (filetype == "css") ? "link" : "none";
    var targetAttr = (filetype == "js") ? "src" : (filetype == "css") ? "href" : "none";
    var allFiles = document.getElementsByTagName(targetEle);

    for (var i = allFiles.length; i >= 0; i--) {
        if (allFiles[i] && allFiles[i].getAttribute(targetAttr) != null && allFiles[i].getAttribute(targetAttr).indexOf(filename) != -1)
            allFiles[i].parentNode.removeChild(allFiles[i]);
    }
}
