﻿$(function () {
    // print mods
    if (IgniteSite && IgniteSite.QueryString && IgniteSite.QueryString["print"]) {
        $("#header-container .logo").prependTo("#content-container");
        setTimeout('window.print();', 1000);
    }

    // TEXT SIZE MEMORY
    setTextSize($.cookie('text-size'));

    // UNORDERED LIST BULLET COLORING
    $('#content-container ul')
        .find('li').wrapInner("<span class='blueBullets-text'></span>").end()
        .addClass('blueBullets');

    // IN-FIELD LABELS
    $('.useInFieldLabels label').inFieldLabels();

    // MAIN NAV AUTO-HIGHLIGHTING
    $('#main-nav').find('.rollover').each(function () {
        if ($(this).attr('href') && window.location.href.match(new RegExp('/' + $(this).attr('href').replace('.', '\.') + "($|#)"))) {
            $(this).addClass('active');
            $('#main-nav').find('.nav-item[channel=' + $(this).attr('channel') + ']').addClass('active');
            $('#main-nav').find('.nav-subitem[channel=' + $(this).attr('channel') + ']').addClass('show');
        }
    });

    //  GLOSSARY TERMS
    var GlossaryData = $.get(IgniteSite.AppPath + '/dtc/glossary.aspx', function (data, textStatus, jqXHR) {
        var target = $('.glossary-term');
        if (IgniteSite.VersionIE > 0 && IgniteSite.VersionIE < 9) {
            target
                .hover(
                    function () { $(this).css('z-index', '500').prev(".glossary-popup").stop(true, true).show(); },
                    function () { $(this).css('z-index', '0').prev(".glossary-popup").stop(true, true).hide(); })
                .click(
                    function () { $(this).css('z-index', '0').prev(".glossary-popup").stop(true, true).hide(); })

        } else {
            target
                .hover(
                    function () { $(this).css('z-index', '500').prev(".glossary-popup").stop(true, true).fadeIn('fast'); },
                    function () { $(this).css('z-index', '0').prev(".glossary-popup").stop(true, true).fadeOut('fast'); })
                .click(
                    function () { $(this).css('z-index', '0').prev(".glossary-popup").stop(true, true).fadeOut('fast'); })
        }
        target.each(function () {
            var target = $(this);
            var targetString = target.attr('term');
            if (targetString == undefined) targetString = target.text().replace(/ /g, '').toLowerCase();
            target
                .attr('href', IgniteSite.AppPath + '/dtc/glossary.aspx#' + targetString)
                .wrap("<span class='glossary-container' />")
                .before(
                        "<span class='glossary-popup'>" +
                            "<span class='head'>" + $("#" + targetString, data).text() + "</span>" +
                            "<span class='body'>" + $("#" + targetString, data).next("p").text() + "</span>" +
                            "<span class='tail'></span>" +
                        "</span>");
        });
        if (IgniteSite.VersionIE > 0 && IgniteSite.VersionIE < 7)
            $('.glossary-popup').supersleight({ shim: IgniteSite.AppPath + "/images/shim.gif" });
    });

    // TOUT HIGHLIGHTING
    $('#side-tout-container a, #wide-tout-container a').hover(
        function () { $(this).parents('.toutBrick').addClass('active'); },
        function () { $(this).parents('.toutBrick').removeClass('active'); });

    // INTERSTITIALS
    var inTargets;
    inTargets = jQuery("[href^='" + IgniteSite.AppPath + "/']");
    //inTargets.filter("[href^='" + IgniteSite.AppPath + "/hcp']").addClass('outbound-hcp');
    //    if (!$.cookie('skip-hcp-interstitial'))
    //        $('#interstitial-to-hcp').jqm({ trigger: 'a.outbound-hcp', onShow: outboundHcpClicked });
    //    $('#interstitial-to-hcp .jqmContinue').click(function () { $('#interstitial-to-hcp').jqmHide(); $.cookie('skip-hcp-interstitial', true); });

    var currHost = window.location.hostname;
    var outTargets = $("[href^='http://'], [href^='https://']").not("[href*='://" + currHost + "'], [href$='.pdf']"); // should grab every outbound link on the page
    var outTargetsFilter = '[href*="celgene.com"], [href*="celgenepatientsupport.com"]';

    outTargets.filter(outTargetsFilter).addClass('outbound-allied');
    $('#interstitial-leaving-allied').jqm({ trigger: '.outbound-allied', onShow: outboundAlliedClicked });
    $('#interstitial-leaving-allied .interstitial-continue').click(function () {
        $('#interstitial-leaving-allied').jqmHide();
    });
    $('#interstitial-leaving-allied .interstitial-return, #interstitial-leaving-allied .interstitial-close').click(function (e) {
        $('#interstitial-leaving-allied').jqmHide();
        e.preventDefault();
    });

    outTargets.not(outTargetsFilter).addClass('outbound-generic');
    $('#interstitial-leaving').jqm({ trigger: '.outbound-generic', onShow: outboundGenericClicked });
    $('#interstitial-leaving .interstitial-continue').click(function () {
        $('#interstitial-leaving').jqmHide();
    });
    $('#interstitial-leaving .interstitial-return, #interstitial-leaving .interstitial-close').click(function (e) {
        $('#interstitial-leaving').jqmHide();
        e.preventDefault();
    });

    // EXIT LINK TRACKING
    $('#interstitial-leaving .interstitial-continue, #interstitial-leaving-allied .interstitial-continue').click(function () {
        trackLink(this, '', 'e');
    });

    // DOWNLOAD LINK TRACKING
    $('[href$=".pdf"]').click(function () {
        var filename_parts = $(this).attr('href').split('/');

        trackLink(this, '', 'd', { event: 'Event4', evars: [{ name: 'eVar3', value: filename_parts[filename_parts.length - 1]}] });
    });

    // PRINT PAGE LINK TRACKING
    $('#printPageLink').click(function () {
        trackLink(this, '/Print', 'o', { event: 'Event3' });
    });

});

function setTextSize(size) {
    $('.tools-text-size a').removeClass('active');
    switch (parseInt(size)) {
        case 1:
            $('body').css('font-size', '11px');
            $('#font-size-small').addClass('active');
            $.cookie('text-size', size)
            break;
        case 2:
            $('body').css('font-size', '13px');
            $('#font-size-medium').addClass('active');
            $.cookie('text-size', size)
            break;
        case 3:
            $('body').css('font-size', '15px');
            $('#font-size-large').addClass('active');
            $.cookie('text-size', size)
            break;
        default:
            setTextSize(3);
    }
}

function printThisPage() {
    var modifiedHref = String(window.location) + ((String(window.location).indexOf("?") == -1) ? "?print=true" : "&print=true");
    window.open(modifiedHref, "printPage", "screenX=200, left=200, screenY=100, top=100, width=1020, height=740, resizable=yes, scrollbars=yes, toolbar=no, location=no, directories=no, status=no, menubar=yes, copyhistory=no");
}

function outboundAlliedClicked(hash) {
    target = jQuery('#interstitial-leaving-allied');
    if (IgniteSite.VersionIE < 7 && IgniteSite.VersionIE > 0) { target.css('top', (document.documentElement.scrollTop+250)+'px'); }
    target.show()
        .find('.interstitial-continue')
            .attr('href', jQuery(hash.t).attr('href'));
}

function outboundGenericClicked(hash) {
    target = jQuery('#interstitial-leaving');
    if (IgniteSite.VersionIE < 7 && IgniteSite.VersionIE > 0) { target.css('top', (document.documentElement.scrollTop+250)+'px'); }
    target.show()
        .find('.interstitial-continue')
            .attr('href', jQuery(hash.t).attr('href'));
}

function outboundHcpClicked(hash) {
    target = jQuery('#interstitial-to-hcp');
    if (IgniteSite.VersionIE < 7 && IgniteSite.VersionIE > 0) { target.css('top', (document.documentElement.scrollTop+250)+'px'); }
    target.show()
        .find('.interstitial-continue')
            .attr('href', jQuery(hash.t).attr('href'));
}
