(function($) {
  Drupal.behaviors.mega = {
    attach: function(context, setings) {
    $("#header .block")
    .hoverIntent(
      function () {
        $(this).children('.content')
        .fadeIn(500);
      $(this).find('.fade-title a').fadeIn(500);
      },
      function() {
        $(this).children('.content')
        .fadeOut(500);
      $(this).find('.fade-title a').fadeOut(500);
      }
    )
    }}

  Drupal.behaviors.mostPopular = {
    attach: function(context, setings) {
    $("#mini-panel-home_popular_content .panel-pane")
    .hoverIntent(
      function () {
        $(this).find('.pane-content')
        .fadeIn(500);
      $(this).find('.pane-title').addClass('ui-state-active');
      },
      function() {
        $(this).find('.pane-content')
        .fadeOut(500);
      $(this).find('.pane-title').removeClass('ui-state-active');
      }
    )
    }}

$().ready(function() {

  // see http://stackoverflow.com/questions/206997/jquery-javascript-ie-hover-doesnt-cover-select-box-options
  // This keeps the select list open in IE8;
  $("#mini-panel-home_popular_content .panel-pane.pane-region-jump-panel-pane-3 select").mouseleave(function(event){
    event.stopPropagation();
  });

    // The most popular block on home page. This is a hack to apply the jquery
    // accordian theme but not use the functionality.
		$( "#mini-panel-home_popular_content" ).accordion({
			collapsible: true,
      header: 'h2.pane-title',
      event: 'none',
      autoHeight: false
		});
    $(this).find('.pane-title').removeClass('ui-state-active');
  });

})(jQuery);;
/*
 * JS for minisites.
 */


;
/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);;
// wrap everything so tha $ is scoped to just this file for jQuery.
(function ($) {


// set some static variables (should be moved to settings at some point)
var itemWidth = 200;
var numItems = 3;
var numToScroll = 3;
var selector = '.issues-custom-jcarousel';
var clipWidth = itemWidth * numItems;
var totalWidth = clipWidth + 200;
var arrowWidth = 100;
var animationSpeed = 1000;
var wrap = 'back';

    // start document ready function
    $(document).ready(function(){
        // figure out how many items we're working with, and how wide they are
        var total = $(selector +' ul').children('li').length;
        var width = total * itemWidth;
        var LastRowStart = (width - (numItems * itemWidth));
        // css backward compat
        $(selector).addClass('jcarousel-skin-default');
        $(selector +' ul > li').addClass('jcarousel-item');

        // turn all of the valid links in top grid into non-valid links.
        $('.view-display-id-panel_pane_1 .views-field-field-short-title a').attr ('href', '#');

        // build and insert some HTML elements
        var wrapperTop = '<div class="issues-custom-jcarousel-container jcarousel-container-horizontal"></div>';
        var wrapperClip = '<div class="issues-custom-jcarousel-clip jcarousel-clip-horizontal"></div>';
        var prevDiv = '<a class="jcarousel-prev jcarousel-prev-horizontal jscarousel-custom-prev jscarousel-custom-nav" href="javascript:void(0)" style="display: block;"></a>';
        // var prevDiv = '<div class="jscarousel-custom-prev jscarousel-custom-nav jcarousel-prev-horizontal"><<</div>';
        var nextDiv = '<a class="jcarousel-next jcarousel-next-horizontal jscarousel-custom-next jscarousel-custom-nav" href="javascript:void(0)" style="display: block;"></a>';
        // var nextDiv = '<div class="jscarousel-custom-next jscarousel-custom-nav jcarousel-next-horizontal">>></div>';
        $(selector +' ul').wrap(wrapperTop);
        $(selector +' ul').wrap(wrapperClip);
        $(selector +'-container').append(nextDiv);
        $(selector +'-container').append(prevDiv);

        // add CSS values to affected elements
        $('.issues-custom-jcarousel-clip').css({
            overflow: 'hidden',
            float: 'left'
        });
        $(selector +' ul').css({
          position: 'relative',
          top: 0,
          left: 0,
          margin: 0,
          padding: 0,
          width: width +'px',
          display: 'inline-block'

        });
        $(selector +' ul > li').css({
            float: 'left',
            margin: '0 4px'
        });

        // start click listener for prev/next arrows
        $('.jscarousel-custom-nav').click(function() {
            // figure out where your are, and where your can go.
            var currentPos = parseFloat($(selector +' ul').css('left').replace('px', ''));
            var rowWidth = itemWidth * numItems;
            var endPos = total * itemWidth * -1;
            var endRow = endPos + rowWidth;

            // handle previous button
            if ($(this).hasClass('jscarousel-custom-prev')) {
                // depending on settings and position the change value
                // can be different
                if (wrap == 'back' && currentPos === 0) {
                    var change = endRow;
                }
                else {
                    var change = currentPos + (itemWidth * numToScroll);
                    if (change > 0) {
                        change = 0;
                    }
                }
                    $(selector +' ul').animate({
                        left: change
                    }, animationSpeed, function(){
                        // Animation complete.
                    });

            } // end previous button

            // handle next button
            if ($(this).hasClass('jscarousel-custom-next')){
                if (currentPos >= endPos && currentPos <= endRow) {
                    if (wrap == 'back') {
                        var change = 0;
                    }
                    else {
                        return;
                    }
                }
                else {
                    var change = currentPos - (itemWidth * numToScroll);
                    if (change >=  endPos && change <= endRow) {
                        change = endRow;
                    }
                }
                    $(selector +' ul').animate({
                        left: change
                    }, animationSpeed, function(){
                        // Animation complete.
                    });
            } // end next button
        }); // end prev/next click listener
        // start top grid click listener
        $('.view-display-id-panel_pane_1 .views-field-field-short-title').click(function() {
            // figure out the nid from the view field and create a
            // search string
            var topElement = $(this).children('.field-content').attr('class').split(' ');
            var nid = topElement[1].replace('issues-carousel-id-', '');
            var search = 'issues-carousel-item-' + nid;
            buttonScroll(search, LastRowStart, width);
            return false; // make sure the link does not send return data.
        }); // end top grid click listener
        // start top grid hover listener
        $('.view-display-id-panel_pane_1 .views-field-field-short-title').hoverIntent(function () {
            // figure out the nid from the view field and create a
            // search string
            var topElement = $(this).children('.field-content').attr('class').split(' ');
            var nid = topElement[1].replace('issues-carousel-id-', '');
            var search = 'issues-carousel-item-' + nid;
            buttonScroll(search, LastRowStart, width);
            $(selector +' ul').addClass('locked');
        },
        function() {
            $(selector +' ul').removeClass('locked');
        }); // end top grid hover listener
    }); // end Doc ready function

    function buttonScroll(search, LastRowStart, width) {
           // loop through list items looking for our matching li
            $(selector +' ul').children('li').each(function(i) {
                var idDiv = $(this).children('.views-field-nid').children('.field-content').children('div');
             //   console.log($(this).children('.views-field-nid').children('.field-content'));
                if (idDiv.hasClass(search)) {
                    $(selector +' ul li').removeClass('carousel-active');
                    $(this).addClass('carousel-active');
                    // now that we've found it figure out how much you need
                    // move the slider
                    var changeVal = i * ($(this).width());
                    var remainder = changeVal % itemWidth;
                    var change = (changeVal - remainder) * -1;

                    if (changeVal > LastRowStart && changeVal < width) {
                        var change = LastRowStart * -1;
                    }

                    // animate a change in position
                    $(this).parent().addClass('carousel-locked');
                    $(this).parent().animate({
                        left: change
                    }, animationSpeed, function(){
                    });


                }
            }); // end item loop

    }

}(jQuery)); // end compatability wrapper
;
// wrap everything so tha $ is scoped to just this file for jQuery.
(function ($) {
// start document ready function
    $(document).ready(function(){
        // grab the page title, and strip away state name
        var title = $('body.node-type-region h1#page-title').text();
        var state = title.replace('In Your State: ', '');

        // hide all of the open accordia tabs
//        $('body.node-type-region div#quickset-regional_offices_information .ui-accordion-content').hide();

        $('body.node-type-region div#quickset-regional_offices_information .views-field-field-region-ref').each(function(i) {
            if ($(this).text().search(state) != -1) {
//                var tabnum = $(this).parent().parent().parent().parent().prev().attr('tabindex');

                jQuery.bbq.pushState('quickset-regional_offices_information='+ i);
            }

        });

    });
}(jQuery)); // end compatability wrapper
;

