$(document).ready(function() { 
    
    var ie55 = (navigator.appName == "Microsoft Internet Explorer" 
                    && parseInt(navigator.appVersion) == 4 
                    && navigator.appVersion.indexOf("MSIE 5.5") != -1);
                    
    var ie6 = (navigator.appName == "Microsoft Internet Explorer" 
                    && parseInt(navigator.appVersion) == 4 
                    && navigator.appVersion.indexOf("MSIE 6.0") != -1);
    var $url = "ppol_" + String(window.location.href).replace(/[\/\.:#\?=\+]/gi, "");
    var $expandy_url = $url+"_expand";
    var $alpha_url = $url + "_a_to_z";
    var $tab_url = $url + "_tab_browse";
  /*
    ie55/ie6 code below was commented-out for a fix, but this was cancelling-out the ie6 png js fix. adding it back. -alan, 5/1/09
  */
    if (jQuery.browser.msie && (ie55 || ie6)) {
      $(".footer").after("<div class='ie_footer'>&nbsp;</div>");
        $(document).pngFix( { blankgif: "/images/blank.gif" } ); 
    }
    
    jQuery("input[type='text']").each(function(){
        this.orig_value = jQuery(this).val();
        jQuery(this).focus(function(){ 
            if ( jQuery(this).val() == this.orig_value) 
                jQuery(this).val(''); 
        }).blur(function(){
            if ( (jQuery(this).val() == this.orig_value) || (!jQuery(this).val().length)) 
                jQuery(this).val(this.orig_value);
        });
    });    
    $filename = new RegExp("^(.*?)\.(gif|jpg|png)", "i");
    $filename_hover = new RegExp("^(.*?)_hover\.(gif|jpg|png)", "i");
    jQuery('input[type="image"].hover').hover(function(){
        $m = $filename.exec(jQuery(this).attr('src'));
        jQuery(this).attr('src', $m[1] + "_hover." + $m[2]);
    }, function(){
        $m = $filename_hover.exec(jQuery(this).attr('src'));
        jQuery(this).attr('src', $m[1] + "." + $m[2]);        
    });
    $('.items .item:last-child').addClass('last-item');
    $('ul > li:last-child, ol > li:last-child').addClass('last');
    $('ul > li:first-child, ol > li:first-child').addClass('first');
    $('.teens .section .section-teen:last-child').addClass('last-section');
    $('.center_list .center:first-child').addClass('first_center');
    if (jQuery.browser.msie && (ie55 || ie6)) {
        $('.main_nav .nav_menu').bgiframe();
    }
    $('.main_nav > li').hover(function(){
        $(this).children('a, .nav_menu').addClass('nav_menu_hover');
    }, function(){
        $(this).children('a, .nav_menu').removeClass('nav_menu_hover');
    });
    
    $('.billboard').each(function(){
        if ($(this).height() < 240)
            $(this).css('height', '240px');
    });
    
    if ($.cookie($tab_url) == 'active')
        tab_click($('.tabs li.tab_browse')[0]);
    $('.tabs li').each(function() {
        $(this).hover(function(){
            $(this).addClass('tab_hover'); 
        }, function() {
            $(this).removeClass('tab_hover');             
        });
        $(this).click(function(){
            tab_click(this);
            return false;
        });
    })
    
    function tab_click($tab) {
        var $is_az = $($tab).hasClass('tab_browse');
        if (!$($tab).hasClass('tab_active')) {
            if ($is_az)
                $.cookie($tab_url, 'active');
            else
                $.cookie($tab_url, null);
            $('.tabs li, .tabs li a').toggleClass('tab_active');
            $('div.tab').css('display', 'none');
            $('div.' + $tab.id).css('display', 'block');
        }
    }
    
    jQuery(".category_list ul").each(function(){
        $lis = jQuery(this).find("li");
        $inc = parseInt(($lis.length/2) + ($lis.length % 2 > 0 )); //Where N = 4
        jQuery.map([$inc, $inc*2], function(value) { //Where [$inc*1, $inc*2,...$inc*N]
            $lis.slice(value-$inc, value).wrapAll("<div class='wrap'></div>");
        });
    });
    $('.category_list ul div.wrap:last-child').addClass('wrap_last');
    jQuery(".l_sub_categories").each(function(){
        $lis = jQuery(this).find("li");
        $inc = parseInt(($lis.length/3) + ($lis.length % 3 > 0 )); //Where N = 4
        jQuery.map([$inc, $inc*2, $inc*3], function(value) { //Where [$inc*1, $inc*2,...$inc*N]
            $lis.slice(value-$inc, value).wrapAll("<div class='wrap'></div>");
        });
        $max = 0;
        $(this).find(".wrap").each(function(){
            if ($(this).height() > $max)
                $max = $(this).height();
        });
        $(this).find(".wrap").height($max);
    });
    $('.l_sub_categories div.wrap:last-child').addClass('wrap_last');
    jQuery(".services_offered ul").each(function(){
        $lis = jQuery(this).find("li");
        $inc = parseInt(($lis.length/2) + ($lis.length % 2 > 0 )); //Where N = 4
        jQuery.map([$inc, $inc*2], function(value) { //Where [$inc*1, $inc*2,...$inc*N]
            $lis.slice(value-$inc, value).wrapAll("<div class='wrap'></div>");
        });
    });
    $('.services_offered ul div.wrap:last-child').addClass('wrap_last');
    
    jQuery(".online_services").each(function(){
        $lis = jQuery(this).find("li");
        $inc = parseInt(($lis.length/2) + ($lis.length % 2 > 0 )); //Where N = 4
        jQuery.map([$inc, $inc*2], function(value) { //Where [$inc*1, $inc*2,...$inc*N]
            $lis.slice(value-$inc, value).wrapAll("<div class='wrap'></div>");
        });
    });
    $('.online_services div.wrap:last-child').addClass('wrap_last');
    
    // move the affiliates promos to the sidebar region    
    $('.affiliates ul.l_links').append($('.affiliates #affiliates_promos').html());
    $('.affiliates #affiliates_promos').remove();
    $('.affiliates ul.l_links').children().removeClass('last');
    $('.affiliates ul.l_links .did_you_know').wrap('<li class="promo"></li>')
    $('.affiliates ul.l_links > li:last-child').addClass('last');
    
    //move teen talk promos to the sidebar region
    $('.teens ul.l_links').append($('.teens #teens_promos').html());
    $('.teens #teens_promos').remove();
    $('.teens ul.l_links').children().removeClass('last');
    $('.teens ul.l_links > li:last-child').addClass('last');
    
    $('.l_alphabet li').hover(function(){
        if (!$(this).hasClass('active') && !$(this).hasClass('empty'))
            $(this).addClass('hover'); 
    }, function(){
        $(this).removeClass('hover');
    });
 
    if (!!$.cookie($alpha_url))
        alpha_switch($('#'+$.cookie($alpha_url))[0]);
 
    $('.l_alphabet li').click(function(){
        alpha_switch(this);
        return false; 
    });
    
    function alpha_switch($letter) {
        if (!$($letter).hasClass('empty')) {
            //set alpha cookie
            $.cookie($alpha_url, $letter.id);
            $('.l_alphabet li.active').removeClass('active');
            $($letter).addClass('active');
            $('.alpha_panel').removeClass('alpha_panel_active');
            $('#alpha_' + $letter.id).addClass('alpha_panel_active');
        }
    }
    
    $('.expandy_list .toggle a, .expandy_list h3.question').click(function(){
       $li = $(this).parent().parent();
       if ($li.hasClass('opened')) {
           $li.removeClass('opened');
           $li.addClass('closed');
       } else if ($li.hasClass('closed')) {
           $li.removeClass('closed');
           $li.addClass('opened');
       }
        return false;
    });
    
    // Give questions a hover style
    $('.expandy_list h3.question').mouseover(function() {
      $(this).addClass('question-hovered');
    }).mouseout(function(){
       $(this).removeClass('question-hovered');
    });
    
    $('.expandy_list .expand_all a').click(function(){
       expand_all(this);
       return false; 
    });
    
    if ($.cookie($expandy_url) == "expanded") {
        expand_all($('.expandy_list .expand_all a'));
    }
    
    function expand_all($elem) {
        if ($($elem).hasClass('expand')) {
            //expand
            $.cookie($expandy_url, "expanded")
            $('.expandy_list li').removeClass('closed').addClass('opened');
        } else { 
            //collapse
            $.cookie($expandy_url, null)
            $('.expandy_list li').removeClass('opened').addClass('closed');
        }
        $($elem).toggleClass('collapse').toggleClass('expand');
    }
    
    $("a.balloon").mouseover(function(){    
        if ($("#balloon").length < 1) {
            this.t = this.title;
            $left = $(this).offset().left;
            $top = $(this).offset().top;
            this.title = "";                                      
            $("body").append("<div id='balloon'><div id='balloon_body'>"+ "<p>" + this.t  + 
                "</p></div><img id='balloon_foot' src='/images/bg_balloon_bottom.png' alt='' /></div>");
            $h = $('#balloon').height();
            $("#balloon")
                .css("top",($top - 15 - $h) + "px")
                .css("left",($left - 235) + "px")
                .css('display', 'block');
        }
    }).mouseout(function(){
       if ($("#balloon").length > 0) {
            $("#balloon").remove();
            this.title = this.t;
        }
    });
    
    $('.center_list .center').each(function(){
        if (!$('.center_single').length) {
            $info = $(this).find('.info');
            $serv = $(this).find('.services_offered');
            $h = Math.max($info.height(),$serv.height());
            $info.height($h);
            $serv.height($h);
        }
    });    
    
    $('.expandy_list .closed .answer').css('display', 'none');
    if ($('.primary').height() > $('.side_nav').height()) {
        $('.side_nav').height($('.primary').height());
    }
    if (($('.side_nav').height() - $('.side_nav > ul').height()) < 100) {
        $('.side_nav').height($('.side_nav').height()+100);
    }
    $('.expandy_list .closed .answer').attr('style', '');
    
    
    $('.link_promo_magenta').append('<img class="link_promo_hover" style="margin-bottom: -1px; margin-left: 5px;" src="/images/promo_magenta.gif" alt="" />');
    $('.link_promo_blue').append('<img class="link_promo_hover" style="margin-bottom: -1px; margin-left: 5px;" src="/images/promo_blue.gif" alt="" />');
    
    $('.link_promo_magenta, .link_promo_blue').hover(function(){
        $img = $(this).find('.link_promo_hover');
       $img.original_src = $($img).attr('src');
       $($img).attr('src', '/images/promo_gray.gif'); 
    }, function() {
        $($img).attr('src', $img.original_src);
    });
    
    jQuery(".wysiwyg img").each(function(){
        $attr = $(this).attr('align');
        if($attr == 'left') {
            $(this).removeAttr('align');
            $(this).addClass('float_left');
        } else if ($attr == 'right') {
            $(this).removeAttr('align');
            $(this).addClass('float_right');
        }
    });
    
    
    $("table.hours_operation tr th:odd, table.hours_operation tr td:odd").addClass("even_col");
    
    $(".center_map_button").click(function(){
      $center = $(this).parents('.center'); 
      if ($(this).hasClass('center_button_closed')) {
        $center.addClass('center_opened');
        $(this).addClass('center_button_opened');
        $(this).removeClass('center_button_closed');
        /*
          code to load health center google maps on demand.
          requires google maps api and jmaps jquery plugin (/scripts/jquery.jmap.js)
          http://code.google.com/p/jmaps/
          -alan, 4/18/09
        */
        var mapButtonCenterID = $(this).attr('id').substr(11); //unique identifier for center (i.e. facilityID); div id named "map_button_NNNN"
        var centerMapDivID = "#gmap_"+mapButtonCenterID; //div to display map in
        var centerAddress = $(this).children('.center_address').text();
        var centerCity = $(this).children('.center_city').text();
        var centerStateAbbr = $(this).children('.center_state_abbr').text();
        var centerZip = $(this).children('.center_zip').text();
        var centerGoogleAddress = $(this).children('.center_google_address').text();
        /*
          lat/long stored in database for each center corrresponds to the center zip (not the address)
          using GClientGeocoder object to return geo coordinates for each center address
          http://code.google.com/apis/maps/documentation/services.html#Geocoding_Object
        */
        var geocoder = new GClientGeocoder();
        geocoder.getLatLng(
          centerGoogleAddress,
          function(point) {
            if (point) {
              //display map if geo coordinates returned
              $(centerMapDivID).jmap('Initialise', {
                'mapType':'map',
                'mapCenter':[point.y, point.x], //lat, long
                'mapZoom':15, //increase number to zoom in closer
                'mapEnableType':true,
                'mapShowjMapsIcon':false
              });
              $(centerMapDivID).jmap('AddMarker', {
                'pointLatLng': [point.y, point.x],
                'pointHTML': centerAddress+'<br />'+centerCity+', '+centerStateAbbr+'<br />'+centerZip
              });
            }
          }
        );
      } else {
        $center.removeClass('center_opened');
        $(this).addClass('center_button_closed');
        $(this).removeClass('center_button_opened');
      }
      return false;
    });
    
    //display google maps page when clicking "get directions" on health center pages. -alan, 4/20/09
    $('.center_map .enter form.center_map_form').submit(function(){
      var directionSearchCenterID = $('.form_submit', this).attr('id').substr(20); //direction_search_go_NNNN
      //get starting address, remove whitespace, concatenate.
      var saddr = $("div.direction_search #direction_search_saddr_"+directionSearchCenterID).val().replace(/\s/g,"+");
      //get destination address.
      var daddr = $("#map_button_"+directionSearchCenterID+" .center_google_address").text();
      if ((saddr == "")||(saddr.toLowerCase() == "enter starting address")||(saddr.toLowerCase() == "direcció")){
        alert("Please enter a Starting Address.");
      } else {
        window.open("http://maps.google.com/maps?saddr=" + saddr + "&daddr=" + daddr, "googleWin");
      }
      return false;
    });
    
    //toggle health center details service panels. -alan, 4/16/09
    $('.center_toggle a').click(function(){
      var centerServiceClass = $(this).attr('class'); 
      if (centerServiceClass == "center_home") {
        var targetOffset = $("div.center_list").offset().top;
      } else {
        var targetOffset = $("div."+centerServiceClass).offset().top;
      }
      $("div.info").removeClass('opened');
      $("div.info").addClass('closed');
      $("div."+centerServiceClass).removeClass('closed');
      $("div."+centerServiceClass).addClass('opened');
      $('html,body').animate({scrollTop: targetOffset}, 200);
      return false;
    });
    
    $(".js_degradation").remove();
    
});
  
function launchPPFADisclaimer(theLink){
  url = escape(theLink.href);
  windowURL = "/scripts/disclaimerPopPPFA.asp?url=" + url;
  win = window.open(windowURL,'disclaimerWindow','scrollbars=yes,status=yes,toolbar=no,menubar=no,width=400,height=350');
}
function launchExternalDisclaimer(theLink){
  window.open(theLink.href)
}
  
// Begin added 6/17/09 Matt - last update 2/22/2010
var oisID = 0;
var oisAll = 0;
if (document.getElementById) { oisID = 1; }
else if (document.all) { oisAll = 1; }
function findMyDOM(objectID, withStyle) {
    if (withStyle == 1) {
        if (oisID) {
            if (document.getElementById(objectID)) { return (document.getElementById(objectID).style); }
        }
        else if (oisAll) {
            if (document.all[objectID]) { return (document.all[objectID].style); }
        }
    }
    else {
        if (oisID) {
            if (document.getElementById(objectID)) { return (document.getElementById(objectID)); }
        }
        else if (oisAll) {
            if (document.all[objectID]) { return (document.all[objectID]); }
        }
    }
}
window.onload = function() {
    var owinH = 0;
    var ovisibleWinH = 0;
    if (window.innerHeight && window.scrollMaxY) { owinH = window.innerHeight + window.scrollMaxY; }
    else if( document.body.scrollHeight > document.body.offsetHeight ) { owinH = document.body.scrollHeight; }
    else { owinH = document.body.offsetHeight + document.body.offsetTop; }
    if (findMyDOM("overlay",1)) {
        findMyDOM("overlay",1).height = owinH+"px";
        ieOwinH = owinH + 54;
        if (navigator.userAgent.toLowerCase().indexOf("windows") != -1 && navigator.userAgent.toLowerCase().indexOf("msie") != -1) { findMyDOM("overlay",1).height = ieOwinH+"px"; }
    }
    if (window.innerHeight) { ovisibleWinH = window.innerHeight; }
    else if (document.documentElement && document.documentElement.clientHeight) { ovisibleWinH = document.documentElement.clientHeight; }
    else if (document.body) { ovisibleWinH = document.body.clientHeight; }
    if (findMyDOM("outerOverlayTable",1)) { findMyDOM("outerOverlayTable",1).height = ovisibleWinH+"px"; }
    if (findMyDOM("overlayTable",1)) { findMyDOM("overlayTable",1).height = ovisibleWinH+"px"; }
}
function viewNow(oWidth,oHeight,oURL) {
    scrOfY = 0;
    if ( typeof(window.pageYOffset ) == 'number') {//Netscape compliant
        scrOfY = window.pageYOffset; }
    else if (document.body && document.body.scrollTop) {//DOM compliant
        scrOfY = document.body.scrollTop; }
    else if (document.documentElement && document.documentElement.scrollTop) {//IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop; }
    if (findMyDOM("innerCell",1)) {
        if (oWidth != "" && !isNaN(oWidth)) {
        oAdjustedWidth = oWidth+40+"px";
        findMyDOM("innerCell",1).width = oAdjustedWidth; }
        else { findMyDOM("innerCell",1).width = "800px"; }
        if (oHeight != "" && !isNaN(oHeight)) {
        oAdjustedHeight = oHeight+40+"px";
        findMyDOM("innerCell",1).height = oAdjustedHeight; }
        else { findMyDOM("innerCell",1).height = "600px"; }
    }
    if (findMyDOM("overlay",1)) { findMyDOM("overlay",1).visibility = "visible"; }
    var RDoverlayAdj = oHeight+300+scrOfY;
    
    if (findMyDOM("outerOverlayTable",1)) {
        findMyDOM("outerOverlayTable",1).visibility = "visible";
        innerCellStuff = '<iframe src="" width="'+oWidth+'" height="'+oAdjustedHeight+'" frameborder="0" scrolling="no" name="overlayiframe" id="overlayiframe"></iframe>';
        if (findMyDOM("innerCellContent",0)) { findMyDOM("innerCellContent",0).innerHTML = innerCellStuff; }
        if (findMyDOM("overlayiframe",0)) { findMyDOM("overlayiframe",0).src = oURL; }
        if (scrOfY > 0) { findMyDOM("outerOverlayTable",1).top = scrOfY+"px"; }
        else { findMyDOM("outerOverlayTable",1).top = "0px"; }
    }
}
function hideNowRD() {
    if (findMyDOM("overlay",1)) { findMyDOM("overlay",1).visibility = "hidden"; }
    if (findMyDOM("outerOverlayTable",1)) { findMyDOM("outerOverlayTable",1).visibility = "hidden"; }
    location.reload(true); }
function hideNow() {
    if (findMyDOM("innerCellContent",0)) { findMyDOM("innerCellContent",0).innerHTML = ""; }
    if (findMyDOM("overlay",1)) { findMyDOM("overlay",1).visibility = "hidden"; }
    if (findMyDOM("outerOverlayTable",1)) { findMyDOM("outerOverlayTable",1).visibility = "hidden"; }
}
// End added 6/17/09 Matt - last update 2/22/2010