$(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);
    
    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');
    });
    $('.tabs li').each(function() {
        $(this).hover(function(){
            $(this).addClass('tab_hover'); 
        }, function() {
            $(this).removeClass('tab_hover');             
        });
        
        $(this).click(function(){
            if (!$(this).hasClass('tab_active')) {
                $('.tabs li, .tabs li a').toggleClass('tab_active');
                $('div.tab').css('display', 'none');
                $('div.' + this.id).css('display', 'block');
            }
            return false;
        });
    })
    
    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');
    $('.l_alphabet li').hover(function(){
        if (!$(this).hasClass('active') && !$(this).hasClass('empty'))
            $(this).addClass('hover'); 
    }, function(){
        $(this).removeClass('hover');
    });
 
    $('.l_alphabet li').click(function(){
        if (!$(this).hasClass('empty')) {
            $('.l_alphabet li.active').removeClass('active');
            $(this).addClass('active');
            $('.alpha_panel').removeClass('alpha_panel_active');
            $('#alpha_' + this.id).addClass('alpha_panel_active');
        }
        return false; 
    });
    
    $('.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;
    });
    
    $('.expandy_list .expand_all a').click(function(){
       if ($(this).hasClass('expand'))
            $('.expandy_list li').removeClass('closed').addClass('opened');
       else 
            $('.expandy_list li').removeClass('opened').addClass('closed');
       $(this).toggleClass('collapse').toggleClass('expand');
       return false; 
    }); 
    
    $("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);
        }
    });    
    
    if ($('.primary').height() > $('.side_nav').height())
        $('.side_nav').height($('.primary').height());
    
    
    $('.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
    $('.direction_search input.form_submit').click(function(){
      var directionSearchCenterID = $(this).attr('id').substr(20); //direction_search_go_NNNN
      var saddr = $("div.direction_search #direction_search_saddr_"+directionSearchCenterID).val();
      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 {
        url = "http://maps.google.com/maps?saddr=" + saddr + "&daddr=" + daddr
        win = window.open(url, "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;
    });
    
}); 
/* Brandon Aaron (http://brandonaaron.net)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 */
(function($){$.fn.bgIframe=$.fn.bgiframe=function(s){if($.browser.msie&&/6.0/.test(navigator.userAgent)){s=$.extend({top:'auto',left:'auto',width:'auto',height:'auto',opacity:true,src:'javascript:false;'},s||{});var prop=function(n){return n&&n.constructor==Number?n+'px':n;},html='<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+'style="display:block;position:absolute;z-index:-1;'+(s.opacity!==false?'filter:Alpha(Opacity=\'0\');':'')+'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+'"/>';return this.each(function(){if($('> iframe.bgiframe',this).length==0)this.insertBefore(document.createElement(html),this.firstChild);});}return this;};})(jQuery);

window.onload = function() { // Added 4/25/09 Matt
    var winH = 0;
    if (window.innerHeight && window.scrollMaxY) { winH = window.innerHeight + window.scrollMaxY; }
    else if( document.body.scrollHeight > document.body.offsetHeight ) { winH = document.body.scrollHeight; }
    else { winH = document.body.offsetHeight + document.body.offsetTop; }
    if (document.getElementById) {
        if (document.getElementById("overlay")) { document.getElementById("overlay").style.height = winH; }
    }
    if (document.all) {
        if (document.all["overlay"]) { document.all["overlay"].style.height = winH; }
    }
} // Enda added 4/25/09 Matt