// detectBrowser
// Checks the browser and adds classes to the body to reflect it.
function detectBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase()); 
    
    // Is this a version of IE?
    if($.browser.msie){
        $('body').addClass('browserIE');
        
        // Add the version number
        $('body').addClass('browserIE' + $.browser.version.substring(0,1));
    }

    // Is this a version of Chrome?
    if($.browser.chrome){
    
        $('body').addClass('browserChrome');
        
        //Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('chrome/') +7);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserChrome' + userAgent);
        
        // If it is chrome then jQuery thinks it's safari so we have to tell it it isn't
        $.browser.safari = false;
    }
    
    // Is this a version of Safari?
    if($.browser.safari){
        $('body').addClass('browserSafari');
        
        // Add the version number
        userAgent = userAgent.substring(userAgent.indexOf('version/') +8);
        userAgent = userAgent.substring(0,1);
        $('body').addClass('browserSafari' + userAgent);
    }
    
    // Is this a version of Mozilla?
    if($.browser.mozilla){
        
        //Is it Firefox?
        if(navigator.userAgent.toLowerCase().indexOf('firefox') != -1){
            $('body').addClass('browserFirefox');
            
            // Add the version number
            userAgent = userAgent.substring(userAgent.indexOf('firefox/') +8);
            userAgent = userAgent.substring(0,1);
            $('body').addClass('browserFirefox' + userAgent);
        }
        // If not then it must be another Mozilla
        else{
            $('body').addClass('browserMozilla');
        }
    }
    
    // Is this a version of Opera?
    if($.browser.opera){
        $('body').addClass('browserOpera');
    }
}

// make columns equal height
function equalHeight(group) {
  tallest = 0;
  group.each(function() {
    thisHeight = $(this).height();
    if(thisHeight > tallest) {
      tallest = thisHeight;
    }
  });
  group.height(tallest);
}

// insert link icons
function insert_link_icons() {
	// Prepend Doc-Type icons before links
	$('body.docs #inner a[href$=pdf]').addClass('icon pdf');
	$('body.docs #inner a[href$=doc]').addClass('icon word');
	$('body.docs #inner a[href$=docx]').addClass('icon word');
	$('body.docs #inner a[href$=xls]').addClass('icon excel');
	$('body.docs #inner a[href$=xlsx]').addClass('icon excel');
	$('body.docs #inner a[href$=csv]').addClass('icon excel');
	$('body.docs #inner a[href$=txt]').addClass('icon txt');
	$('body.docs #inner a[href$=rtf]').addClass('icon txt');
	$('body.docs #inner a[href$=ppt]').addClass('icon ppt');
	$('body.docs #inner a[href$=pptx]').addClass('icon ppt');
	$('body.docs #inner a[href$=mp3]').addClass('icon ipod_sound');
	$('body.docs #inner a[href$=flv]').addClass('icon movie');
}


// Let's get it cracking...
$(function() {
  // Roger Glenn 2010-04-12
  // insert link icons
  insert_link_icons();
  
  // home page slideshow
  $('#slideshow .tabs').tabs('#slides li', { 
      effect: 'fade', 
      fadeOutSpeed: 'slow', 
      rotate: true 
    }).slideshow({
     autoplay: true,
     interval: 7000
  });
  
  // make each column the same height
  equalHeight($('#supporting, #middle, #quicklinks'));
  
  // fetch the height of the supporting column
  supportingHeight = ($('#supporting').height());
  
  // since margin-top is equal to -70, grab the value and add 70
  $('#supporting').css('height', supportingHeight + 70);

	// expandable sidebar menu
	$('#nav-sub ul > li > ul').hide();
	
	// return false on all links with nested ul's
	$('#nav-sub ul > li > a').each(function(){
		if ($(this).parent().find('ul').size()) {
			$(this).click(function(){
				return false;
			});
		}
	});
	// show nested ul's
	$('#nav-sub ul > li').each(function(){
		// if there's a ul in there...
		if ($(this).find('ul').size()) {
			// return false on a
			$(this).find(':first-child').bind('click',function(){
				// add current class
				$(this).parent().addClass('current');
				// show the ul
				$(this).parent().find('ul').slideDown();
			});
		}
	});


  $('#nav-sub').find('a[href="'+window.location.href+'"]').each(function(){
    $(this).parent().addClass('current');
  });

  $('#nav-sub ul ul').find('a[href="'+window.location.href+'"]').each(function(){
    $(this).parents().eq(2).addClass('current');
    $(this).parents().eq(1).css('display', 'block');
    $(this).addClass('on')
  });
  
    
  // grab the value attribute from the option element and send the user there  		
  $('#county-dropdown').change(function(){
    window.location.href = $(this).val();
  }); 

  // table striping
    $('#inner tr:even').addClass('stripe'); 

	// toggle benefits descriptions
	// Roger Glenn 2010-04-06
  $(".benefit_trigger").toggle(
			function(){ 
				// get benefit_id from rel attribute
				var benefit_id = $(this).attr("rel");
				$("#benefit_" + benefit_id).show(); 
				return false;
			},
			function(){ 
				// get benefit_id from rel attribute
				var benefit_id = $(this).attr("rel");
				$("#benefit_" + benefit_id).hide(); 
				return false;
			}
		);

  // newsletter overlay
  $('.button.subscribe a[rel]').overlay();


  // Commission Meetings table first cell width hack from hell
  $('.about.commission-meetings td:nth-child(1)').css('width', '35%');
  

});
