$(document).ready(function() {	
  
  /* LIGHTBOX */
  
  $('.fancybox').fancybox();
  
  
  /* FOOTER */
  
  /*
  $('div#awards .next', 'div#awards .prev').each(function(){
    $(this).click(function(evt){
      evt.preventDefault();
    });
  });
  */
  
  var awards = $("div#awards").scrollable({
    circular: true,
    speed: 500,
    easing: 'easeOutExpo',
    next: '.next-award',
    prev: '.prev-award'
  })
  .autoscroll({ 
		autoplay: true,
		interval: 2000,
		autopause: false
	})
  
  
  /*
  function updateScrollerWidth(evt) {
    if (evt) {
      evt.preventDefault();
    }
    
    var scrollable = awards.data('scrollable');
    var index = $(awards).find('img')[scrollable.getIndex()];
    var width = $(index).css('width').replace('px', '');
    if ($(awards).find('img')[scrollable.getIndex() + 1]) {
      width = Number(width) + Number($($(awards).find('img')[scrollable.getIndex() + 1]).css('width').replace('px', ''));
      // console.log('img to the right exists');
    }
        
    // console.log(width);
    $('div#awards').css('width', width + 'px');
    
  }
  
  updateScrollerWidth();
  $('div#awards .next').click(updateScrollerWidth);
  $('div#awards .prev').click(updateScrollerWidth);
  */
  
  
  /*  =Nav
  ------------------------------------*/
  
  $('nav[role="navigation"] li:not("li li") > a').each(function(){
    var that = this;
    var arrow = $('<div class="nav-arrow" />');
    var arrowCurrent = $('<div class="nav-arrow current" />');
    // arrow.css('width', $(that).width());
    
    if($(that).parent().is('.current-menu-item, .current-menu-parent, .current-menu-ancestor')){

			$(that).parent().append(arrowCurrent);
    }
    else {
			$(that).parent().append(arrow);    
    }
    
    window.sliderColours();

  });
		
		function mouseOver(e){ 
		  
		  // console.log(e)
			
			if( $(this).children('a').text() == 'Solutions'){
				$(this).children('ul').addClass('solutions');
				// Set menu size
				var menuW = $('#menu-top-menu').width();
				$(this).children('ul').width(menuW - 60);		
				
				// console.log(this);
					
			}
						
			// Show menu
			var submenu = $(this).children('.sub-menu'),
					submenuH = submenu.height();			
					
					// console.log(submenu);
					// console.log(submenuH);
					
			//submenu.css('visibility','visible').hide().fadeIn();
			/*
			submenu.css('visibility','visible').hide().height(0).animate({
				'height' : submenuH,
		    easing: 'easeOutExpo'
			}, 300)
			*/
			
			submenu.css('visibility','visible').hide().fadeIn();
			
			/*
			submenu.css('visibility','visible').height(0).animate({
				'height' : submenuH,
		    easing: 'easeOutExpo'
			}, 300)
			*/
				
		};
	 
		function mouseOut(){ 					
			var submenu = $(this).children('.sub-menu');			
			submenu.fadeOut();
		};
		
		
		var config = {
			over: mouseOver, // function = onMouseOver callback (REQUIRED)    
			timeout: 200, 	// number = milliseconds delay before onMouseOut    
			out: mouseOut 	// function = onMouseOut callback (REQUIRED)    			
		}
				
		$('#menu-top-menu>li').hoverIntent(config);
		
	
	  // Convert certain menus to dropdowns:	
	if (!($.browser.msie && $.browser.version < 9)) {
	
	  $('#menu-fleet-complete-dropdown, #menu-es400-dropdown').selectify();
	  
	    // jQuery SelectBox
  	$('.sidebar select').selectBox({
  	    menuTransition: 'slide'
  	  , menuSpeed: 'fast'
  	});

      // Add Navigation functionality to Selectified Menus:
    $('.navigator').bind('change', function(){
      window.location = $(this).selectBox('value');
    });
	}
	
  

  /*	Executive Bios show/hide
  ------------------------------------*/
  
  $('#executive_bios .read_more .button').toggle(
  	function(e){  	
  		e.preventDefault();
  		var item = $(this).closest('li'),
  				bioH = $(this).closest('.bio').height(),
  				that = $(this);
  		
  		item.stop().animate({
  			'height' : bioH + 'px'
  		},
  		500, 'easeInOutQuad', 
  		function(){
  		  
  		  $(that).parent().addClass('read-less');
  		  $(that).text('Read Less');
  			
  		});
  		
			if( item.find('img.bio_image_alt').length > 0 ){
				item.find('img.bio_image').fadeOut(800);
				item.find('img.bio_image_alt').fadeIn(800);
			}
		},
  	function(e){
  		e.preventDefault();  	
  		var item = $(this).closest('li'),
  				that = $(this);
  				
	    $(that).parent().removeClass('read-less');
					
  		item.stop().animate({
  			'height' : '160px'
  		},
  		500, 'easeInOutQuad', 
  		function(){
  			$(that).text('Read More');
  		});
  		
			if( item.find('img.bio_image').length > 0 ){
				item.find('img.bio_image_alt').fadeOut(800);
				item.find('img.bio_image').fadeIn(800);
			}
  		
  	}
	  	
	);
  
  if ( $.browser.msie == true && $.browser.version < 8 && $('#scroll').hasClass('no-scroll') ) {
    $('#hero_fg').css('height', '300px');
  }
  
  $('.post_content img').not('.post_content header img, .page-id-43 .post_content img, .home .post_content img').each(function(){
  	if( $(this).attr('width') && $(this).attr('height')){
			$(this).wrap(function() {
			  var style = "";
			  if ($(this).attr('style')) {
			    style = $(this).attr('style');
			    if (style.replace(' ', '') == "float:left") {
			      style += "; margin-right: 20px; margin-bottom: 20px; ";
			    } else if (style.replace(' ', '') == "float:right") {
			      style += "; margin-left: 20px; margin-bottom: 20px; ";
			    }
			  }
				return '<span style="background-image:url(' + $(this).attr('src') + '); height: '+ $(this).attr('height') + 'px; width: '+ $(this).attr('width') + 'px; ' + style + '" class="rounded" />';				
			});  		  	
  	}
  	else {
  		$(this).after('<!-- This img needs width and height attributes for rounded corners to work -->');
  	}
  });
  
  
  // Industries page - remove <a> for links with href="#"
  
  var title = $('<ul><li class="side-heading"><h3>Other Industries We Serve</h3></li></ul>');
  // $('.parent-pageid-25 #menu-industries a[href="#"]:first').parent().append(title);
  
  $('.parent-pageid-25 #menu-industries a[href="#"]').each(function(i){
  	var text = $(this).text(),
  			that = this;
  	
  	if (i == 0) {  	  
  	  // $(that).parent().append(title);
  	  $(that).parent().prev().append(title);
  	  $(that).parent('li').text(text).addClass('empty');
  	  
  	} else {
  	  $(that).parent('li').text(text).addClass('empty');
  	}
  	
  	// $(that).remove();
  })
  
  /*
  $('.parent-pageid-25 #menu-industries a[href="#"]').each(function(){
  	var text = $(this).text(),
  			that = this;
  	$(that).parent('li').text(text).addClass('empty');
  	$(that).remove();
  })
  */
  
 });
 
 $('.business-solution-content p:empty').remove();
 
 
 
 /*	=Comments form 
 ------------------------------------*/
	
 $('#author[placeholder], #email[placeholder], #url[placeholder], #comment[placeholder]').placeholder();

 
 
 
$(window).load(function(){
  
  /*	=Homepage Accordions
	------------------------------------*/
	
	
	if( $('.accordion').length > 0){
	
		Array.prototype.max = function() {
		  return Math.max.apply(null, this)
		}
		
		Array.prototype.min = function() {
		  return Math.min.apply(null, this)
		}	  
	  
	  var heights = [];
	  
	  $('.home .accordion').each(function(){
	  	heights.push( $(this).height() );
	  });
	  			
		var max = Math.max.apply(null, heights);
	
	  $('.home .accordion').each(function(){
	  	if( $(this).height() !== max ){
		  	var pane = $(this).find('.pane:first'),
		  			extraH = 0;
		  	
		  	// TODO: Webkit is calculating the height incorrectly, 
		  	// adding 44px to compensate. Need to figure out reason.
		  	if( $('body').is('.browser-chrome, .browser-safari') ){
		  		extraH = 0;
		  	}
				
				if($(this).hasClass('blue')){
					var currentPane = $(this).find('h2.current').next('.pane'),
							thisPaneH = currentPane.height(),
							thisH = $(this).height(),
							diff = thisPaneH + (max - thisH);
							
					//currentPane.height(thisPaneH + (max - thisH));
					currentPane.animate({
			  		'height': diff + 'px'
			  	}, 400);		
				} 
				
				if ( !$(this).hasClass('blue') ) {
				  
			  	$(this).animate({
			  		'height': max + extraH + 'px'
			  	}, 400);
			  	pane.animate({
			  		'height': max - 90 + extraH + 'px'
			  	}, 400);		
			  	
			  } else {
			    
			    var extraMargin = '19px'
			    if( $('body').is('.browser-chrome, .browser-safari') ){
  		  		extraMargin = '23px';
  		  	}
  		  	
			    $(this).find('.pane:first .button').css('margin-bottom', extraMargin);
			    
			    
			  }
			  	
	  	}	  	
	  });
  }
  
  
	
}); 
