/*-------------------------------------------------
 Document Ready
-------------------------------------------------*/
$(document).ready(function(){

// -----------------------------------
// Open External Links in a New Window
	$(function(){
		$('a[rel="external"]').click( function() {
	        	window.open( $(this).attr('href') );
	        	return false;
	    	});
		// $('a[rel="windowsize"]').click( function() {
		//         window.open( $(this).attr('href'),this,'height=870,width=600,scrollbars=yes,');
		//         return false;
		//     });
	});

// -----------------------------------
// Call overlabel
	$(function(){
		$("label.overlabel").overlabel();
	});
// -----------------------------------
// Dropdown Navigation
	$(function(){
		$("#Nav > ul > li").addClass("parent");
		$("#Nav li ul").addClass("slideDown");
		$("#Nav li ul").hide();
		$("#Nav li ul").parent("li").addClass("hasSub");
		$("#Nav li").hover(function () {
				$(this).children("ul")
				.show();
				}, function () {
				$(this).children("ul")
				.hide();
			});
		$("#Nav li ul").hover(function () {
				$(this).parent("li")
				.addClass("active");
				}, function () {
				$(this).parent("li")
				.removeClass("active");
			});
	});
// -----------------------------------
// Current Openings
	$(function(){
		$("div#openings > div:gt(0)").hide();
		$("div#openings div").addClass("content");
		$("div#openings > h4").click(function() {
			$(this).next("div:hidden").fadeIn()
			.siblings("div:visible").hide();
		});
		$("div#openings > h4").click(function() {
		    	$(this).addClass("current")
				.siblings("h4.current").removeClass("current");
		});
	});
// -----------------------------------
// Truncate text
	$(function(){
		$('div.whypnImgCol h5 + p').expander({
		    slicePoint:       80,  // default is 100
		    expandText:       '&nbsp;', // default is 'read more...'
			widow:            10,
			expandPrefix:     '&nbsp;', // text to come before the expand link
		    userCollapseText: '&nbsp',  // default is '[collapse expanded text]'
		 	expandEffect: 	  'fadeIn'
		});
	});
// -----------------------------------
// Alternating Table BG Color (Stripes)
	$(function(){
		$(".stripe tr").mouseover(function() {$(this).addClass("over");}).mouseout(function() {$(this).removeClass("over");});
		$("table.stripe tr:even").addClass("alt");
	});

}); // ------------------------------------  end of Document Ready

// temporary onClick for recruitment link
function openDateWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


/*-------------------------------------------------
 Random Image Script
-------------------------------------------------*/

// Set up the image files to be used.
var theImages = new Array() // do not change this

theImages[0] = 'images/landingpage/3rd.gif'
theImages[1] = 'images/landingpage/6th.gif'
theImages[2] = 'images/landingpage/10-assoc-man.gif'
theImages[3] = 'images/landingpage/18_yrs.gif'
theImages[4] = 'images/landingpage/28-direct.gif'
theImages[5] = 'images/landingpage/30-years.gif'
theImages[6] = 'images/landingpage/40-fin_cli.gif'
theImages[7] = 'images/landingpage/40-yrs.gif'
theImages[8] = 'images/landingpage/60-gov_pros.gif'
theImages[9] = 'images/landingpage/60-yrs.gif'
theImages[10] = 'images/landingpage/75-char.gif'
theImages[11] = 'images/landingpage/100-gov_cli.gif'
theImages[12] = 'images/landingpage/320-employees.gif'
theImages[13] = 'images/landingpage/1949_found.gif'
theImages[14] = 'images/landingpage/6392-tax_rtn.gif'

// ======================================
// do not change anything below this line
// ======================================

var j = 0
var p = theImages.length;

var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}

var whichImage = Math.round(Math.random()*(p-1));
function showImage(){
document.write('<img src="'+theImages[whichImage]+'">');
}



/*-------------------------------------------------
 Overlabel for Search Plugin
-------------------------------------------------*/
( function( $ ) {
    // plugin definition
    $.fn.overlabel = function( options ) {
         // build main options before element iteration
        var opts = $.extend( {}, $.fn.overlabel.defaults, options );
        var selection = this.filter( 'label[for]' ).map( function() {
            var label = $( this );
            var id = label.attr( 'for' );
            var field = document.getElementById( id );
 
           if ( !field ) return;
 
            // build element specific options
            var o = $.meta ? $.extend( {}, opts, label.data() ) : opts;
            label.addClass( o.label_class );
            var hide_label = function() { label.css( o.hide_css ) };
            var show_label = function() { this.value || label.css( o.show_css ) };
            $( field )
                 .parent().addClass( o.wrapper_class ).end()
                 .focus( hide_label ).blur( show_label ).each( hide_label ).each( show_label );
            return this;
        } );
        return opts.filter ? selection : selection.end();
    };
    // publicly accessible defaults
    $.fn.overlabel.defaults = {
        label_class:   'overlabel-apply',
        wrapper_class: 'overlabel-wrapper',
        hide_css:      { 'text-indent': '-10000px' },
        show_css:      { 'text-indent': '0px', 'cursor': 'text' },
        filter:        false
    };
} )( jQuery );



/*-------------------------------------------------
 Truncate/Expander jquery plugin
-------------------------------------------------*/
(function($) {

  $.fn.expander = function(options) {

    var opts = $.extend({}, $.fn.expander.defaults, options);
    var delayedCollapse;
    return this.each(function() {
      var $this = $(this);
      var o = $.meta ? $.extend({}, opts, $this.data()) : opts;
     	var cleanedTag, startTags, endTags;	
     	var allText = $this.html();
     	var startText = allText.slice(0, o.slicePoint).replace(/\w+$/,'');
     	startTags = startText.match(/<\w[^>]*>/g);
   	  if (startTags) {startText = allText.slice(0,o.slicePoint + startTags.join('').length).replace(/\w+$/,'');}
   	  
     	if (startText.lastIndexOf('<') > startText.lastIndexOf('>') ) {
     	  startText = startText.slice(0,startText.lastIndexOf('<'));
     	}
     	var endText = allText.slice(startText.length);    	  
     	// create necessary expand/collapse elements if they don't already exist
   	  if (!$('span.details', this).length) {
        // end script if text length isn't long enough.
       	if ( endText.replace(/\s+$/,'').split(' ').length < o.widow ) { return; }
       	// otherwise, continue...    
       	if (endText.indexOf('</') > -1) {
         	endTags = endText.match(/<(\/)?[^>]*>/g);
          for (var i=0; i < endTags.length; i++) {

            if (endTags[i].indexOf('</') > -1) {
              var startTag, startTagExists = false;
              for (var j=0; j < i; j++) {
                startTag = endTags[j].slice(0, endTags[j].indexOf(' ')).replace(/(\w)$/,'$1>');
                if (startTag == rSlash(endTags[i])) {
                  startTagExists = true;
                }
              }              
              if (!startTagExists) {
                startText = startText + endTags[i];
                var matched = false;
                for (var s=startTags.length - 1; s >= 0; s--) {
                  if (startTags[s].slice(0, startTags[s].indexOf(' ')).replace(/(\w)$/,'$1>') == rSlash(endTags[i]) 
                  && matched == false) {
                    cleanedTag = cleanedTag ? startTags[s] + cleanedTag : startTags[s];
                    matched = true;
                  }
                };
              }
            }
          }

          endText = cleanedTag && cleanedTag + endText || endText;
        }
     	  $this.html([
     		startText,
     		'<span class="more">',
     		o.expandPrefix,
       		'<a href="#">',
       		  o.expandText,
       		'</a>',
        '</span>',
     		'<span class="details">',
     		  endText,
     		'</span>'
     		].join('')
     	  );
      }
      var $thisDetails = $('span.details', this),
        $readMore = $('span.more', this);
   	  $thisDetails.hide();
 	    $readMore.find('a').click(function() {
 	      $readMore.hide();

 	      if (o.expandEffect === 'show' && !o.expandSpeed) {
          o.beforeExpand($this);
 	        $thisDetails.show();
          o.afterExpand($this);
          delayCollapse(o, $thisDetails);
 	      } else {
          o.beforeExpand($this);
 	        $thisDetails[o.expandEffect](o.expandSpeed, function() {
            $thisDetails.css({zoom: ''});
            o.afterExpand($this);
            delayCollapse(o, $thisDetails);
 	        });
 	      }
        return false;
 	    });
      if (o.userCollapse) {
        $this
        .find('span.details').append('<span class="collapse">' + o.userCollapsePrefix + '<a href="#">' + o.userCollapseText + '</a></span>');
        $this.find('span.collapse a').click(function() {

          clearTimeout(delayedCollapse);
          var $detailsCollapsed = $(this).parents('span.details');
          reCollapse($detailsCollapsed);
          o.onCollapse($this, true);
          return false;
        });
      }
    });
    function reCollapse(el) {
       el.hide()
        .prev('span.more').show();
    }
    function delayCollapse(option, $collapseEl) {
      if (option.collapseTimer) {
        delayedCollapse = setTimeout(function() {  
          reCollapse($collapseEl);
          option.onCollapse($collapseEl.parent(), false);
          },
          option.collapseTimer
        );
      }
    }
    function rSlash(rString) {
      return rString.replace(/\//,'');
    }    
  };
    // plugin defaults
  $.fn.expander.defaults = {
    slicePoint:       100,  // the number of characters at which the contents will be sliced into two parts. 
                            // Note: any tag names in the HTML that appear inside the sliced element before 
                            // the slicePoint will be counted along with the text characters.
    widow:            4,  // a threshold of sorts for whether to initially hide/collapse part of the element's contents. 
                          // If after slicing the contents in two there are fewer words in the second part than 
                          // the value set by widow, we won't bother hiding/collapsing anything.
    expandText:       'read more', // text displayed in a link instead of the hidden part of the element. 
                                      // clicking this will expand/show the hidden/collapsed text
    expandPrefix:     '&hellip; ',
    collapseTimer:    0, // number of milliseconds after text has been expanded at which to collapse the text again
    expandEffect:     'fadeIn',
    expandSpeed:      '',   // speed in milliseconds of the animation effect for expanding the text
    userCollapse:     true, // allow the user to collapse the expanded text.
    userCollapseText: '[collapse expanded text]',  // text to use for the link to collapse the text
    userCollapsePrefix: ' ',
    beforeExpand: function($thisEl) {},
    afterExpand: function($thisEl) {},
    onCollapse: function($thisEl, byUser) {}
  };
})(jQuery);

