// Topnav equal height columns ---------------------------------------------------------------------------------
$(function () {
    $('.dropdown_2columns, .dropdown_4columns, .dropdown_5columns').each(function () {
        var highestBox = 0;
        $(this).find('.col_1, .col_2').each(function () {
            if ($(this).height() > highestBox) {
                highestBox = $(this).height();
            }
        })
        $(this).find('.col_1, .col_2').height(highestBox);
    });
});

// Homepage Featured Content Rotation --------------------------------------------------------------------------
$(function () {
    // Set starting slide to 1
    var startSlide = 1;
    // Get slide number if it exists
    if (window.location.hash) {
        startSlide = window.location.hash.replace('#', '');
    }
    // Initialize Slides
    $('#slides').slides({
        preload: true,
        //preloadImage: 'images/loading.gif',
        generatePagination: true,
        generateNextPrev: true,
        play: 7000,  //slow rotation speed - goal is 5 seconds and this seems to be a good buffer.
        pause: 2500,
        hoverPause: true,

        // Get the starting slide
        start: startSlide,
        animationComplete: function (current) {
            // Set the slide number as a hash
            window.location.hash = '#' + current;
            /*
            //var div_img_bg = $('div.img-bg-rotate');
            //set all invisible. 
            $("div.img-bg-rotate > img").each(function (i) {
            if ($(this).hasClass('actv')) {
            $(this).removeClass('actv');
            $(this).addClass('hide');
            }
            });
            //set current visible. 
            var imgCurrent = $("div.img-bg-rotate > img-bg-rotate-" + current );
            if (img != null)
            {
            $(this).removeClass('hide');
            $(this).addClass('actv');
            }
            */
        }
    });

    /*
    // Initialize image slides
    $('#slides2').slides({
    preload: true,
    //preloadImage: 'images/loading.gif',
    //generatePagination: true,
    //generateNextPrev: true,
    play: 5000,
    pause: 2500,
    hoverPause: true,
    });
    */
});

// ---- Left Nav ------------------------------------------------------------------
//2013-02-21 - SC - added this function to handle the expand/collapse of the menu via a click on the div
// rather than hover on the <a>. Note you can still click on the <a> and go to lower level page. 
//If this works, we can remove the next function down
$(function () {

    var menu_ul = $('.menu > li > ul'),
         menu_a = $('.menu > li > div > a'),
         menu_div = $('.menu > li > div');

    menu_div.click(function () {
        if (!$(this).hasClass('actv')) {
            menu_a.removeClass('actv');
            menu_div.removeClass('actv');
            menu_ul.filter(':visible').slideUp('normal');
            $(this).addClass('actv').next().stop(true, true).slideDown('normal');
        } else {
            $(this).removeClass('actv');
            $(this).next().stop(true, true).slideUp('normal');
        }
    });

    //expand the active on page load
    $(document).ready(function () {
        $('#divLeftNav-Active').siblings('ul').show();
    });
});

// Mobile Navigation - Reveal / Hide 
$(document).ready(function () {

    var menu_ul = $('ul.primary_nav'),
        btnToggle = $('img#mobile-menu-btn');


    btnToggle.click(function () {
        menu_ul.stop(true, true).slideToggle('slow');
        if (!$(this).hasClass('actv')) {
            btnToggle.addClass('actv');
        } else {
            btnToggle.removeClass('actv');
        }
    });
});

//
// ---- Mobile Nav Expand / Collapse - Primary  ------------------------------------------------------------------
//
$(document).ready(function () {
    var menu_ul = $('ul.primary_nav'),
    //         menu_a = $('ul.primary_nav > li > a'),
         menu_div = $('ul.primary_nav > li > div');

    menu_div.bind('click', function () {
        if (!$(this).hasClass('actv')) {
            $('ul.secondary_nav').stop(true, true).slideUp();
            menu_div.removeClass('actv');
            //                menu_a.removeClass('actv');
            $(this).addClass('actv');
            //                $(this).find('a').addClass('actv');
            $(this).parent().find('ul.secondary_nav').slideDown();
        } else {
            $('ul.secondary_nav').stop(true, true).slideUp();
            $(this).removeClass('actv');
            //                menu_a.addClass('actv');
        }
    });

});

// ---- Mobile Nav Expand / Collapse - Second Level  ------------------------------------------------------------------
$(document).ready(function () {

    var menu_ul = $('ul.secondary_nav'),
    //         menu_a = $('ul.primary_nav > li > a'),
         menu_div = $('ul.secondary_nav > li > div');

    menu_div.click(function () {
        if (!$(this).hasClass('actv')) {
            $('ul.secondary_sub').slideUp();
            menu_div.removeClass('actv');
            $(this).parent().find('ul.secondary_sub').slideDown();
            $(this).addClass('actv');
        } else {
            $('ul.secondary_sub').slideUp();
            menu_div.removeClass('actv');
        }
    });

    //$('ul.sub1 li:nth-child(2), ul.sub2 li:nth-child(2)').css('border-top', 'none');
    //$('ul.sub1 li:last-child, ul.sub2 li:last-child').css('border-bottom', 'none');

});

// ---- Mobile Nav Close ------------------------------------------------------------------
$(function () {

    var menu_ul = $('ul.primary_nav'),
        btnToggle = $('img#mobile-menu-btn'),
        btnClose = $('div#div-mobile-close');

    btnClose.click(function () {
        //alert("clicking");
        if (!btnToggle.hasClass('actv')) {
            btnToggle.removeClass('actv');
            btnToggle.addClass('actv');
        } else {
            btnToggle.removeClass('actv');
        }
        menu_ul.slideToggle("slow");

        //collapse everything and don't set mobile close as active div
        var menu_ul_secondary = $('ul.secondary_nav'),
            menu_ul_secondary_sub = $('ul.secondary_sub'),
            menu_div = $('ul.primary_nav > li > div'),
            menu_div_secondary = $('ul.secondary_nav > li > div');

        menu_div.removeClass('actv');
        menu_div_secondary.removeClass('actv');
        menu_ul_secondary.slideUp();
        menu_ul_secondary_sub.slideUp();
    });
});

/* Mobile nav - proto code for reveal/hide button click
$(function () {
var menu_ul = $('.menu > li > ul'),
menu_a = $('.menu > li > a');

menu_ul.hide();

menu_a.mouseover(function (e) {
//e.preventDefault();
if (!$(this).hasClass('actv')) {
menu_a.removeClass('actv');
menu_ul.filter(':visible').slideUp('normal');
$(this).addClass('actv').next().stop(true, true).slideDown('normal');
} else {
$(this).removeClass('actv');
$(this).next().stop(true, true).slideUp('normal');
}
});
});
*/

// Video Carousel -------------------------------------------------------------------------------------
$(function () {
    $('.video-slides').slidesjs({
        width: 390,
        height: 120,
        pagination: false,
        navigation: false
    });
});

// Tools Carousel -------------------------------------------------------------------------------------
$(function () {
    $('.tool-slides').slidesjs({
        width: 390,
        height: 350,
        pagination: false,
        navigation: false
    });
});

// gallery Carousel -------------------------------------------------------------------------------------
$(function () {
    $('.gallery-slides').slidesjs({
        width: 390,
        height: 135,
        pagination: false,
        navigation: false
    });
});

// ---- Ghost text field ----------------------------------------------------------
$(function () {
    $('.txt-field').each(function (index) {// Adds the Ghost effect on textbox
        if ($(this).val() === "" || $(this).val() === $(this).attr("title")) {
            $(this).attr("value", $(this).attr("title")).css({ "color": "#999" });
        }
    });
    $('.txt-field').on("focusin", function () {
        if ($(this).val() === $(this).attr("title")) {
            $(this).val("").css({ "color": "#000000" }); //Clear text and return normal color
        }
    }).focusout(function () {
        if ($(this).val() === "") {//Nothing has changed
            $(this).val($(this).attr("title")).css({ "color": "#999" }); //return back to ghost state
        }
    });
});

// ---- Toggle article lists -------------------------------------------------------
$(function () {
    var dropdwn = $('.article-list-container'),
    accord_a = $('a.article-list-btn');

    dropdwn.hide();

    accord_a.click(function (e) {
        e.preventDefault();
        if (!$(this).hasClass('list-open')) {
            accord_a.removeClass('list-open');
            dropdwn.filter(':visible').slideUp('normal');
            //accord_a.html("Show Articles <span class='down'></span>");
            //$(this).html("Hide Articles <span class='up'></span>");
            $(this).addClass('list-open').next().stop(true, true).slideDown('normal');
        } else {
            $(this).removeClass('list-open');
            //$(this).html("Show Articles <span class='down'></span>");
            $(this).next().stop(true, true).slideUp('normal');
        }
    });
});


// ---- Article Text Size -----------------------------------------------------------
$(document).ready(function () {

    //min font size
    var min = 12;
    //max font size
    var max = 18;
    //grab the default font size
    var reset = 14;
    //font resize these elements
//    var elm = $('div.article-content p, div.article-content a');
    var elm = $('div.article-content p, div.article-content a, div.article-content li, div.sjia-center p, sjia-center a, sjia-center li');
    //set the default font size and remove px from the value
    var size = 14;

    //Increase font size
    $('a.plus').click(function (e) {
        e.preventDefault();
        if (size <= max) {
            size++;
            elm.css({ 'fontSize': size });
        }
        return false;
    });

    //decrease font size
    $('a.minus').click(function (e) {
        e.preventDefault();
        if (size >= min) {
            size--;
            elm.css({ 'fontSize': size });
        }
        return false;
    });

    //Reset the font size
    //$('a.fontReset').click(function () {

    //set the default font size 
    //elm.css({'fontSize' : reset});     
    //});
});


// ---- Article page check form ---------------------------------------------------
function checkform(form) {
    if (form.disc_name.value == "") {
        alert("Name is required");
        form.disc_name.focus();
        return false;
    }
    if (form.disc_email.value == "") {
        alert("Email Address is required");
        form.disc_email.focus();
        return false;
    }
    if (form.disc_body.value == "") {
        alert("Comment is required");
        form.disc_body.focus();
        return false;
    }
    return true;
}


// ---- Weather zip form -------------------------------------------------------------
function isZip(s) {
    s = s.replace(" ", "");
    // Check for correct zip code
    reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

    if (!reZip.test(s)) {
        alert("Please enter a valid Zip Code");
        return false;
    }
    return true;
}

// ---- Where Does it Hurt - Body Diagram --------------------------------------------
$(document).ready(function () {
    var beingShown = false;
    var shown = false;
    var distance = 10;
    var time = 250;
    var hideDelay = 500;
    var hideDelayTimer = null
    var el = $('#pain-pop');

    //grab all the anchor tag with rel set to pain
    $('a[rel=pain]').mouseenter(function () {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        el.css('opacity', 0);

        // set css top and left values for popup
        var top = $(this).position().top + 50;
        var left = $(this).position().left + ($(this).width() / 2) - ($('#pain-pop').width() / 2);

        // get and assign content to popup
        var div = "#" + $(this).attr("class");
        if (div == "#neck" || div == "#back") {
            var heading = $("#spine").find("h2").html();
            var content = $("#spine").find("p").html();
            var link = $("#spine").find("a").attr('href');
        } else {
            var heading = $(div).find("h2").html();
            var content = $(div).find("p").html();
            var link = $(div).find("a").attr('href');
        }
        el.find("h2").html(heading);
        el.find("p").html(content);
        el.find("a").attr('href', link);

        // Show popup
        if (beingShown) {
            // don't trigger the animation again
            return;
        } else {
            //display the box
            beingShown = true;
            el.css({
                top: top,
                left: left,
                display: 'block'
            }).animate({
                top: '-=' + distance + 'px',
                opacity: 1
            }, time, 'easeInOutCubic', function () {
                beingShown = false;
                shown = true;
            });
        }
        return false;

    }).mouseout(function () {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
        hideDelayTimer = setTimeout(function () {
            hideDelayTimer = null;
            el.animate({
                top: '-=' + distance + 'px',
                opacity: 0
            }, time, 'easeInOutCubic', function () {
                shown = false;
                el.css('display', 'none');
            });
        }, hideDelay);
        return false;
    });

    el.mouseenter(function () {
        if (hideDelayTimer) clearTimeout(hideDelayTimer);
    });

    // onmouse out hide the popup
    el.mouseleave(function () {
        if (shown) {
            $(this).animate({
                top: '-=' + distance + 'px',
                opacity: 0
            }, time, 'easeInOutCubic', function () {
                shown = false;
                el.css('display', 'none');
            });
        }
    });
});

// ---- Mobile Nav Expand / Collapse ------------------------------------------------------------------
/*
$(function () {

var menu_ul = $('ul#mobile-menu'),
btnToggle = $('img#mobile-menu-btn');

btnToggle.click(function () {
//            alert("clicking");
if (!btnToggle.hasClass('actv')) {
btnToggle.removeClass('actv');
btnToggle.addClass('actv');
} else {
btnToggle.removeClass('actv');
}
menu_ul.slideToggle("slow");
});
});

// ---- Mobile Nav Close ------------------------------------------------------------------
$(function () {

var menu_ul = $('ul#mobile-menu'),
btnToggle = $('img#mobile-menu-btn'),
btnClose = $('div#div-mobile-close');
        
btnClose.click(function () {
//alert("clicking");
if (!btnToggle.hasClass('actv')) {
btnToggle.removeClass('actv');
btnToggle.addClass('actv');
} else {
btnToggle.removeClass('actv');
}
menu_ul.slideToggle("slow");
});
});
*/


/*2013-07-23 - SC - comment out - it messes up on screen re-size
//2013-07-17 SC - Set the widget height to the same height as the parent
//SC - move this from being coded inline, add logic to increase height to parent
//only if the parent height is greater. 
$(window).load(function () {

//    var parentHeight = parseInt($(".rt-rail").parent().css("height"));
//    var widgetHeight = parseInt($(".rt-rail").css("height"));
//    var widgetHeightMax = Math.max(parentHeight, widgetHeight);
//    $(".rt-rail").css("height", widgetHeightMax);

var parentHeight = $(".rt-rail").parent().outerHeight(true);
var widgetHeight = $(".rt-rail").outerHeight(true);
var widgetHeightMarginBottom = parseInt($(".rt-rail").css("margin-bottom"));
var widgetHeightMax = Math.max(parentHeight, widgetHeight);
//    var widgetHeightMax = Math.max(parentHeight, widgetHeight - widgetHeightMarginBottom);
$(".rt-rail").css("height", widgetHeightMax);
});
*/


//--------------------------------Analytics - Add Delay to some clicks ----------------------//
//2013-04-09 - SC
//length is milliseconds. This is done for b/c the analytics guys need
//the button click to register prior to redirecting to the new page. 
function DelayExecution(length) {
    //delay for X milliseconds
    //note setTimeout is asynch and won't wait till the delayed function completes.
    var dtStart = new Date();
    var n = dtStart.getTime();
    var target = n + length;
    var current = n;
    while (current < target) {
        var dtCurrent = new Date();
        current = dtCurrent.getTime();
    }
    //debug alert("Delay complete");
}

//----------------------Article Social Media Toggle ----------------------------------------//
/*
$(document).ready(function () {
var btn = $('img#btn-social-media');
btn.bind('click', function () {
if (!$(this).hasClass('actv')) {
$('div.sm-toggle').stop(true, true).slideUp();
$(this).removeClass('actv');
$(this).addClass('actv');
$('div.sm-toggle').slideDown();
} else {
$('div.sm-toggle').stop(true, true).slideUp();
$(this).removeClass('actv');
}
});
});
*/

/*
$(function () {
var btn = $('div#social-media');
btn.click(function () {
$('div.sm-toggle').slideToggle('slow');
});
});
*/

// Mobile Navigation - Reveal / Hide 
$(document).ready(function () {

    var btnToggle = $('div#social-media');

    btnToggle.click(function () {
        $('div.sm-toggle').stop(true, true).slideToggle('slow');
        if (!$(this).hasClass('actv')) {
            btnToggle.addClass('actv');
        } else {
            btnToggle.removeClass('actv');
        }
    });
});




//----------------------Bx Slider - Swipable Slider ----------------------------------------//
$(document).ready(function () {
    $('ul.bxslider').bxSlider({
        auto: true,
        mode: 'fade',   //2014-02-10 - SC - add this in temp to cure bug w/ horizontal slide jumping
        speed: 1000,
        pause: 4000,
        // autoStart: true,
        autoHover: true,
        infiniteLoop: true,
        hideControlOnEnd: true,
        touchEnabled: true,
        //       preventDefaultSwipeY:true,
        //       preventDefaultSwipeX:false,
        swipeThreshold: 30,
        oneToOneTouch: false,
        responsive: true
    });

    //$('ul.bxslider').bxSlider().startAuto();
});

//    $(document).ready(function () {
//        slider = $('.bxslider').bxSlider();
//        slider.startAuto();
//    });

//----------------------Bx Slider - Responsive Video Carousel ----------------------------------------//
$(document).ready(function () {
    $('ul.bx-video-carousel').bxSlider({
        minSlides: 2,
        maxSlides: 3,
        slideWidth: 110,
        slideMargin: 10
    });
});

