﻿$(function () {

    //  PRE-SET FONT SIZE
    if ($.cookie('textsize') != undefined)
        setTextSize($.cookie('textsize'));
    else
        setTextSize("11pt");

    //  STRIP CONTAINERS FROM PAGE FOR PRINTING
    if (IgniteSite != undefined && IgniteSite.QueryString != undefined && IgniteSite.QueryString["print"] != undefined) {
        $('head').append('<link type="text/css" rel="stylesheet" href="../style/base-print.css" />');
        $('#container-header-outer, #container-mainnav, #container-subnav, #container-right-touts, #container-footer-outer').remove();
        setTimeout('window.print();', 1000);
    }

    //  SET UP AUTOMATIC NAV LINK HIGHLIGHTING
    $('#container-mainnav, #container-subnav').find('.rollover').each(function () {
        if ($(this).attr('href') && window.location.href.match(new RegExp($(this).attr('href') + "$"))) {
            var jThis = $(this);
            jThis.addClass('active');
            if (jThis.hasClass('mainnav-list-link'))
                jThis.parents('.mainnav-item').children('.mainnav-item-link').addClass('active');
            if (jThis.parents('.sublistContainer').size())
                jThis.parents('.sublistContainer').addClass('active');
            if (jThis.parent('.sublist').size()) {
                jThis.parent('.sublist').prev().addClass('active');
                var curSubUrl = $('.sublistContainer .active').attr('href');
                $('#container-mainnav .mainnav-item').find('.rollover').each(function () {
                    var mainNavUrl = $(this).attr('href');
                    if (curSubUrl == mainNavUrl) {
                        $(this).addClass('active');
                        $(this).parents('.mainnav-item').children('.mainnav-item-link').addClass('active');
                    }
                });

                //alert(curNavUrl);
            }
        }
    });

    //  SET UP MAIN NAV ANIMATION
    $('#container-mainnav').find('.mainnav-list')
        .wrap("<div class='mainnav-list-wrapper' />")
        .end()
    .find('.mainnav-item')
        .mouseenter(function () {
            clearTimeout(this.hideTimer);
            var target = $(this).find('.mainnav-list')
                .stop(true)
                .animate({ bottom: 0 }, 300)
                .css('height');
            $(this).find('.mainnav-list-wrapper')
                .stop(true)
                .animate({ height: target }, 300)
        })
        .mouseleave(function () {
            var target = this;
            this.hideTimer = setTimeout(function () {
                $(target).find('.mainnav-list')
                    .stop(true)
                    .animate({ bottom: -250 }, 300);
                $(target).find('.mainnav-list-wrapper')
                    .stop(true)
                    .animate({ height: 10 }, 300);
            }, 100);
        })
        .end()
    .find('.mainnav-list')
        .css('visibility', 'visible');

    //  SET UP SUB-SUB-NAV SLIDER
    $('.sublistContainer').not('.active').hover(
        function (e) { $(this).find('.sublist').slideDown('slow'); },
        function (e) { $(this).find('.sublist').slideUp('slow'); });

    //  SET UP INTERSTITIALS
    var inTargets;
    if (IgniteSite.AppPath) inTargets = jQuery("a[href^='" + IgniteSite.AppPath + "']");
    else inTargets = jQuery("a[href^='/']");
    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 = $("a[href^='http://'], a[href^='https://']").not("[href*='://" + currHost + "'], [href$='.pdf']"); // should grab every outbound link on the page

    outTargets.filter('[href*="livingpah.com"], [href*="unither.com"]').addClass('outbound-allied');
    $('#interstitial-leaving-site').jqm({ trigger: 'a.outbound-allied', onShow: outboundAlliedClicked });
    $('#interstitial-leaving-site .jqmContinue').click(function () {
        $('#interstitial-leaving-site').jqmHide();
    });

    outTargets.not('[href*="livingpah.com"], [href*="unither.com"]').addClass('outbound-generic');
    $('#interstitial-to-third-party').jqm({ trigger: 'a.outbound-generic', onShow: outboundGenericClicked });
    $('#interstitial-to-third-party .jqmContinue').click(function () {
        $('#interstitial-to-third-party').jqmHide();
    });

    // $('#popup-ComingSoon').jqm({ trigger: 'a.coming-soon, area.coming-soon' }); 
    $('#popup-ComingSoon').jqm({ trigger: 'a.coming-soon, area.coming-soon', onShow: comingSoonClicked });

    $("form.uses-validation").validate({
        submitHandler: function (form) {
            tracklinkFromFormAttribute(form);
            form.submit();
        }
    });
});

//---------------------------------------------------------------------------------------------------------------------------------------------------

function getQuerystringByName(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

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=750, height=700, resizable=yes, scrollbars=yes, toolbar=no, location=no, directories=no, status=no, menubar=yes, copyhistory=no");
}

function setTextSize(size) {
    $('body').css('font-size', size);
    $('#container-tools .rollover-text-size').removeClass('active');
    $('#container-tools .rollover-text-size[setTextSize=' + size + ']').addClass('active');
    $.cookie('textsize', size);
}

// Check if ipad/ipod or not
var isiPad = navigator.userAgent.match(/iPad/i) != null;
var isiPod = navigator.userAgent.match(/iPod/i) != null;
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;

function comingSoonClicked() {
    if (isiPod == true || isiPad == true || isiPhone == true) {
        window.scrollTo(0, 0);
    }   
    jQuery('#popup-ComingSoon').show();
}

function outboundAlliedClicked(hash) {
    if (isiPod == true || isiPad == true || isiPhone == true) {
        window.scrollTo(0, 0);
    }
    jQuery('#interstitial-leaving-site').show();
    jQuery('#interstitial-leaving-site .jqmContinue')
        .attr('href', jQuery(hash.t).attr('href'))
        .attr('tracklink', jQuery(hash.t).attr('tracklink'));
}

function outboundGenericClicked(hash) {
    if (isiPod == true || isiPad == true || isiPhone == true) {
    window.scrollTo(0, 0);
    }
    jQuery('#interstitial-to-third-party').show();
    jQuery('#interstitial-to-third-party .jqmContinue')
        .attr('href', jQuery(hash.t).attr('href'))
        .attr('tracklink', jQuery(hash.t).attr('tracklink'));
}

function outboundHcpClicked(hash) {
    if (isiPod == true || isiPad == true || isiPhone == true) {
        window.scrollTo(0, 0);
    }
    jQuery('#interstitial-to-hcp').show();
    jQuery('#interstitial-to-hcp .jqmContinue').attr('href', jQuery(hash.t).attr('href'));
}
