$(document).ready(function(){
	
	$('.needJs').hide();
	
	$('html').removeClass('no-js');
	
	$('input.hint').hint();
	
	$('#carousel').slideCarousel({
		init: function(){
			var $nav = $('#carouselNav'),
				navHeight = $nav.height();
			$nav.css('margin-top',-navHeight/2);
		}
	});
	
	if($('.multiple-media').length)
		$('.multiple-media').multipleMedia(); 	
	
	$('.toggleTopTen a').click(function(){
		var $this = $(this);
		$('.toggleTopTen a').removeClass('selected');
		$this.addClass('selected');
		if($this.attr('id') == 'shortHaul'){
			$('#topTen').addClass('shortHaul');
		} else{
			$('#topTen').removeClass('shortHaul');	
		}
		return false;
	});
	
});

//matchbox multiple media jquery carousel v1.0
(function($){
	
	$.fn.multipleMedia = function(settings) {
		
		settings = jQuery.extend({
				thumbClass: 'thumbnails'
			}, settings);
		
		var $this = $(this),
			$frames = $this.find('.media-item'),
			carouselHeight = 0,
			gallery = '<ul class="gallery clearfix">';
			
		//Loop through all frames. Find the height of the tallest frame, position the images inside each frame and produce the thumbnails for each frame.
		$frames.each(function(){
			var $this = $(this),
				frameHeight = parseFloat($this.height()),
				thumbNail = $this.find('input.thumb-source').val(),
				$mediaWrap = $this.children('.big-media-wrapper'),
				mediaWrapHeight = $mediaWrap.height();
			$mediaWrap.css('margin-top',- mediaWrapHeight / 2);
			gallery += '<li class="' + settings.thumbClass + '"><a href="#"><b class="shroud"></b><img src="' + thumbNail + '" alt="media image"/></a></li>'	
			if(frameHeight > carouselHeight)
				carouselHeight = frameHeight;
		});
		
		//set the height of all the frames and the media viewer itself, push first frame to the top.
		$frames.height(carouselHeight).first().css('z-index','10');
		gallery += '</ul>';
		$this.append(gallery).children('.multiple-media-inner').height(carouselHeight);
		$this.children('.gallery').find('li').first().addClass('selected');
		
		//push frame to top based on thumbnail index on click.
		$('.'+settings.thumbClass).click(function(){
			var $this = $(this),
				matchedImage = $this.index(),
				$galleryControls = $this.closest('.gallery').find('li');
			$galleryControls.removeClass('selected');
			$this.addClass('selected');
			$frames.css('z-index','1').eq(matchedImage).css('z-index','10');
			return false;
		});
		
	}	
	
})(jQuery);

//matchbox multiple media jquery carousel v1.0
(function($){
	
	$.fn.slideCarousel = function(settings) {
		var setAuto,
			carousel = {
				settings: jQuery.extend({
						frameSelector: 'li',
						init: null
						}, settings),
				$frame: $(this),
				slide: function(index){
					var carouselHeight = carousel.$frame.height(),
						nSlides = carousel.$frame.children(carousel.settings.frameSelector).length-1,
						distance = -carouselHeight;
					if(index > nSlides){
						index = 0;
					}	
				    if(index > carousel.currentIndex){
				   		distance = carouselHeight;
				    }	
					carousel.currentIndex = index;
					carousel.$nav.find('a').removeClass('selected');
					carousel.$nav.find('li:eq('+carousel.currentIndex+')').children('a').addClass('selected');
				    carousel.$frame.children('li:eq('+index+')').css({
				   		'z-index': 3,
				   		'margin-top': distance
				    }).animate({
				   		'margin-top': 0
				    }, 300, function(){
				   		carousel.$frame.children('li').css('z-index',1).eq(index).css('z-index',2);	
						clearTimeout(setAuto);
						setAuto = setTimeout(function(){
							carousel.auto();
						},7000);
				    });	
				},
				auto: function(){
					var index = carousel.currentIndex + 1;
					carousel.slide(index);
				},
				currentIndex: 0
			}
		
		//Build the navigation
		carousel.$nav = $('<ul id="'+carousel.$frame.attr('id')+'Nav"></ul>');
		var nFrames = carousel.$frame.children(carousel.settings.frameSelector).length,
			navItems = '';
		for(var i=0;i<nFrames;i++){
			var selected = '';
			if(i == 0) selected = ' class="selected"';
			navItems += '<li class="navItem"><a'+selected+' href="#">'+(i+1)+'</a></li>';
		}
		carousel.$nav.append(navItems);
		carousel.$frame.wrap('<div id="'+carousel.$frame.attr('id')+'Wrap"/>').wrap('<div id="'+carousel.$frame.attr('id')+'Crop"/>').closest('#'+carousel.$frame.attr('id')+'Wrap').append(carousel.$nav);
		carousel.$frame.children(carousel.settings.frameSelector).first().css('z-index',2);
		if(carousel.settings.init && $.isFunction(carousel.settings.init))carousel.settings.init();
		
		carousel.$nav.find('a').click(function(){
			var $this = $(this);
			if(!$this.hasClass('selected')){
				var index = $this.parents('li').index();
				carousel.slide(index);
			}
			return false;
		});
		
		setAuto = setTimeout(function(){
			carousel.auto();
		},7000);
		
		carousel.$frame.closest('#'+carousel.$frame.attr('id')+'Wrap').hover(function(){
			clearTimeout(setAuto);
		}, function(){
			setAuto = setTimeout(function(){
				carousel.auto();
			},7000);
		});
		
	}	
	
})(jQuery);

/**
 * hint - Extension to add titles as input prompts to selected inputs.
 * @author Remy Sharp
 */
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};
