﻿/*
TopZIndex 1.1 (September 23, 2009) plugin for jQuery
http://topzindex.googlecode.com/
Copyright (c) 2009 Todd Northrop
http://www.speednet.biz/
Licensed under GPL 3, see  <http://www.gnu.org/licenses/>
*/
(function (a) { a.topZIndex = function (b) { return Math.max(0, Math.max.apply(null, a.map(a(b || "body *"), function (d) { return parseInt(a(d).css("z-index")) || null }))) }; a.fn.topZIndex = function (b) { if (this.length === 0) { return this } b = a.extend({ increment: 1, selector: "body *" }, b); var d = a.topZIndex(b.selector), c = b.increment; return this.each(function () { a(this).css("z-index", d += c) }) } })(jQuery);

var itemCounter = 0;

$(document).ready(function () {
    jQuery(".CMSSiteMapList li:has(ul)").addClass('Folder');
    jQuery(".CMSSiteMapList li:first").addClass('Home');

    if ($(".BlogPager").length != 0) {
        var PreviousButton;
        if ($(".SelectedPrev").length != 0) PreviousButton = $(".SelectedPrev");
        if ($(".UnselectedPrev").length != 0) PreviousButton = $(".UnselectedPrev");
        PreviousButton.addClass('PagerButton');
        PreviousButton.addClass('PreviousPagerButton');
        PreviousButton.html('Newer Posts');
        var NextButton;
        if ($(".SelectedNext").length != 0) NextButton = $(".SelectedNext");
        if ($(".UnselectedNext").length != 0) NextButton = $(".UnselectedNext");
        NextButton.addClass('PagerButton');
        NextButton.addClass('NextPagerButton');
        NextButton.html('Older Posts');
    };

    // Search text box value switching
    var fieldSearchBox = jQuery('#SiteSearch input.SearchText');
    var fieldSearchBoxDefault = 'search';
    fieldSearchBox.val(fieldSearchBoxDefault);
    fieldSearchBox.focus(function () {
        if (fieldSearchBox.attr('value') == fieldSearchBoxDefault) fieldSearchBox.attr('value', '');
    });
    fieldSearchBox.blur(function () {
        if (fieldSearchBox.attr('value') == '') fieldSearchBox.attr('value', fieldSearchBoxDefault);
    });

    // Search text box value switching
    var fieldNewsletterSubscriptionBox = jQuery('.NewsletterSubscription input.SubscriptionTextbox');
    var fieldNewsletterSubscriptionDefault = jQuery('.NewsletterSubscription label').html();
    fieldNewsletterSubscriptionBox.val(fieldNewsletterSubscriptionDefault);
    fieldNewsletterSubscriptionBox.focus(function () {
        if (fieldNewsletterSubscriptionBox.attr('value') == fieldNewsletterSubscriptionDefault) fieldNewsletterSubscriptionBox.attr('value', '');
    });
    fieldNewsletterSubscriptionBox.blur(function () {
        if (fieldNewsletterSubscriptionBox.attr('value') == '') fieldNewsletterSubscriptionBox.attr('value', fieldNewsletterSubscriptionDefault);
    });

    if (jQuery('#PoemOverlay').length != 0) {
        var PoemOverlay = jQuery('#PoemOverlay');
        var PoemContainer = jQuery('#PoemContainer');
        var PoemClose = jQuery('#PoemClose');

        jQuery("#PoemReadMore a").click(function () {
            PoemOverlay.fadeOut(1000).css('display', 'none');
            PoemContainer.fadeIn(2000).css('display', 'block');
            PoemClose.fadeIn(2000).css('display', 'block');

            $("#PoemContainer").css('overflow', 'hidden');
            var difference = $('#Poem').height() - $('#PoemContainer').height();

            if (difference > 0)//if the scrollbar is needed, set it up...
            {
                var proportion = difference / $('#Poem').height(); //eg 200px/500px
                var handleHeight = 34;

                $("#PoemContainer").after('<\div id="slider-wrap"><\div id="slider-vertical"><\/div><\/div>'); //append the necessary divs so they're only there if needed
                $("#slider-wrap").height($("#scroll-pane").height()); //set the height of the slider bar to that of the scroll pane


                //set up the slider 
                $('#slider-vertical').slider({
                    orientation: 'vertical',
                    range: 'max',
                    min: 0,
                    max: 100,
                    value: 100,
                    slide: function (event, ui) {
                        var topValue = -((100 - ui.value) * difference / 100);
                        $('#Poem').css({ top: topValue }); //move the top up (negative value) by the percentage the slider has been moved times the difference in height
                    }
                });

                //set the handle height and bottom margin so the middle of the handle is in line with the slider
                $(".ui-slider-handle").css({ height: handleHeight, 'margin-bottom': -0.5 * handleHeight });

                var origSliderHeight = $("#slider-vertical").height(); //read the original slider height
                var sliderHeight = origSliderHeight - handleHeight; //the height through which the handle can move needs to be the original height minus the handle height
                var sliderMargin = (origSliderHeight - sliderHeight) * 0.5; //so the slider needs to have both top and bottom margins equal to half the difference
                $(".ui-slider").css({ height: sliderHeight, 'margin-top': sliderMargin }); //set the slider height and margins
                $(".ui-slider-range").css({ top: -sliderMargin }); //position the slider-range div at the top of the slider container
            } //end if


            $("#PoemContainer,#slider-wrap").mousewheel(function (event, delta) {
                var speed = 5;
                var sliderVal = $("#slider-vertical").slider("value");
                sliderVal += (delta * speed);
                $("#slider-vertical").slider("value", sliderVal);
                var topValue = -((100 - sliderVal) * difference / 100);
                if (topValue > 0) topValue = 0;
                if (Math.abs(topValue) > difference) topValue = (-1) * difference;
                $("#Poem").css({ top: topValue });
                event.preventDefault();
            });

        });

        jQuery("#PoemClose").bind('click', function () {
            jQuery('#slider-wrap').fadeOut(1000).css('display', 'none');
            PoemContainer.fadeOut(1000).css('display', 'none');
            PoemClose.fadeOut(1000).css('display', 'none');
            PoemOverlay.css('display', 'block');
        });
    }

    // Modal windows
    if (jQuery(".ModalLink").length != 0) {
        $("a.ModalLink").click(function () {
            var ModalId = '#' + jQuery(this).attr("rel");
            var Modal = jQuery(ModalId);
            Modal.fadeIn(1000).css('display', 'block');
            return false;
        });
        $(".ModalClose").bind('click', function () {
            var ModalId = '#' + jQuery(this).attr("rel");
            var Modal = jQuery(ModalId);
            Modal.fadeOut(1000).css('display', 'none');
            return false;
        });
    }


    // Animated background hovers
    var argAllowAnimateBkg = false;
    if (!jQuery.browser.msie) {
        argAllowAnimateBkg = true;
    } else {
        if (jQuery.browser.version > 8) argAllowAnimateBkg = true;
    }

    if (argAllowAnimateBkg == true) {
        if (jQuery('a.DateLozenge').length != 0) {
            jQuery("a.DateLozenge").hover(function () {
                jQuery(this).animate({ backgroundColor: "#faffea" }, 500);
            }, function () {
                jQuery(this).animate({ backgroundColor: "#e6ead8" }, 300);
            });
        }

        if (jQuery('.BlogLozenge').length != 0) {
            jQuery(".BlogLozenge").hover(function () {
                jQuery(this).animate({ backgroundColor: "#faffea" }, 500);
            }, function () {
                jQuery(this).animate({ backgroundColor: "#e6ead8" }, 300);
            });
        }

        if (jQuery('.PatientStoryNavigation a').length != 0) {
            jQuery(".PatientStoryNavigation a").hover(function () {
                jQuery(this).animate({ backgroundColor: "#f2f6e6" }, 500);
            }, function () {
                jQuery(this).animate({ backgroundColor: "#FFFFFF" }, 300);
            });
        }

        if (jQuery('.ReturnToNews').length != 0) {
            jQuery(".ReturnToNews").hover(function () {
                jQuery(this).animate({ backgroundColor: "#f2f6e6" }, 500);
            }, function () {
                jQuery(this).animate({ backgroundColor: "#FFFFFF" }, 300);
            });
        }

        if (jQuery('a.NamedAnchorListing').length != 0) {
            jQuery("a.NamedAnchorListing").hover(function () {
                jQuery(this).animate({ backgroundColor: "#f2f6e6" }, 500);
            }, function () {
                jQuery(this).animate({ backgroundColor: "#FFFFFF" }, 300);
            });
        }

    }
    // End animated background hovers



    if (jQuery('.Calendar').length != 0) {
        jQuery(".Calendar table tbody tr td[colspan='7']").css('background-color', '#CBD3B0');
        jQuery("table.Title tbody tr td:eq(0) a").addClass('PreviousButton').html('Previous');
        jQuery("table.Title tbody tr td:eq(2) a").addClass('NextButton').html('Next');

        jQuery('.CalendarEvent').bind('mouseenter mouseleave', function () {
            jQuery(this).topZIndex();
            jQuery(this).find('div.ChiariEvent').toggleClass('ChiariEventPopup');
        });
    }

    //Smooth scrolling of local links
    jQuery.localScroll();

    // Increase Font Size
    var currentFontSize = 0;
    var currentFontSizeNum = 0;
    var IncreaseRatio = 1.05;
    var DecreaseRatio = 0.95;
    if (jQuery.browser.msie && jQuery.browser.version <= 8) {
        IncreaseRatio = 1.1;
        DecreaseRatio = 0.95;
    }
    var IncreaseInt = 0;
    var DecreaseInt = 0;

    jQuery(".FontSizerLarger").click(function () {
        if (IncreaseInt <= 2) {
            IncreaseInt++;
            DecreaseInt--;

            currentFontSize = jQuery('#ContentContainer').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#ContentContainer').css('font-size', currentFontSizeNum * IncreaseRatio);

            currentFontSize = jQuery('#MainNavigation li').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#MainNavigation li').css('font-size', currentFontSizeNum * IncreaseRatio);

            currentFontSize = jQuery('#FooterLinks li').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#FooterLinks li').css('font-size', currentFontSizeNum * IncreaseRatio);
        }

        return false;
    });

    // Decrease Font Size
    jQuery(".FontSizerSmaller").click(function () {
        if (DecreaseInt <= 2) {
            DecreaseInt++;
            IncreaseInt--;

            currentFontSize = jQuery('#ContentContainer').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#ContentContainer').css('font-size', currentFontSizeNum * DecreaseRatio);

            currentFontSize = jQuery('#MainNavigation li').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#MainNavigation li').css('font-size', currentFontSizeNum * DecreaseRatio);

            currentFontSize = jQuery('#FooterLinks li').css('font-size');
            currentFontSizeNum = parseFloat(currentFontSize, 10);
            jQuery('#FooterLinks li').css('font-size', currentFontSizeNum * DecreaseRatio);
        }

        return false;
    });

    jQuery(".PrintPage").click(function () {
        window.print();
        return false;
    });

    //    If IE, add a div for dropshadows
    jQuery('#MainNavigation ul li ul').each(function () {
        itemCounter++;
        jQuery(this).topZIndex();
        //    Help from http://placenamehere.com/article/384/CSS3BoxShadowinInternetExplorerBlurShadow
        if (jQuery.browser.msie && jQuery.browser.version <= 8) {
            var itemPos = jQuery(this).position();
            jQuery(this).after('<div class="IEShadow IEShadow' + itemCounter + '"></div>');
            jQuery('.IEShadow' + itemCounter).height(jQuery(this).height());
            jQuery('.IEShadow' + itemCounter).width(jQuery(this).width());
            jQuery('.IEShadow' + itemCounter).css('left', (itemPos.left) + 'px');
            jQuery('.IEShadow' + itemCounter).css('top', (itemPos.top) + 'px');
        }
    });

    if (jQuery("#Breadcrumbs").length != 0) {
        jQuery("#Breadcrumbs a:first").attr('href', '').addClass('NullBreadcrumb');
        if (jQuery.browser.msie && jQuery.browser.version <= 8) {
            jQuery("#Breadcrumbs").after('<div class="IEShadow" id="IEShadowBreadcrumbs"></div>');
            jQuery("#IEShadowBreadcrumbs").css('left', (jQuery("#Breadcrumbs").position().left) + 'px');
            jQuery("#IEShadowBreadcrumbs").css('top', (jQuery("#Breadcrumbs").position().top) + 'px');
        }
    }

    var deviceIphone = "iphone";
    var deviceIpod = "ipod";
    var deviceIpad = "ipad";

    //Initialize our user agent string to lower case.
    var uagent = navigator.userAgent.toLowerCase();

    //**************************
    // Detects if the current device is an iPad.
    function DetectIpad() {
        if (uagent.search(deviceIpad) > -1)
            return true;
        else
            return false;
    }

    //**************************
    // Detects if the current device is an iPhone.
    function DetectIphone() {
        if (uagent.search(deviceIphone) > -1)
            return true;
        else
            return false;
    }

    //**************************
    // Detects if the current device is an iPod Touch.
    function DetectIpod() {
        if (uagent.search(deviceIpod) > -1)
            return true;
        else
            return false;
    }

    //**************************
    // Detects if the current device is an iPhone or iPod Touch or iPad.
    function DetectIphoneOrIpodOrIpad() {
        if (DetectIphone())
            return true;
        else if (DetectIpod())
            return true;
        else if (DetectIpad())
            return true;
        else
            return false;
    }


    if (DetectIphoneOrIpodOrIpad() == true) {
        $("#VideoPlayer a").attr('href', 'media/DrOro.mov');
        $("#VideoPlayPause").attr('href', 'media/DrOro.mov');
    }
});


// Remotely play/pause video with text link
var player = null;
function onJavaScriptBridgeCreated(playerId) {
  if (player == null) {
    player = document.getElementById("VideoPlayer");
//    $(player).css({ 'position': 'relative', 'z-index': '99' });

    // Pause/Resume the playback when we click the Play/Pause link
    $("#VideoPlayPause").click(function () {
      var state = player.getState();
      if (state == "ready" || state == "paused") {
        player.play2();
        $(this).html('pause video');
      } else if (state == "playing") {
        player.pause();
        $(this).html('play video');
      }
      return false;
    });
  }
}


