/* Author:

*/

$(function(){

	// Recalls
	// number of recalled items
	var recallItems = $('#recalls').find('article').length;
	
	// Buttons
	function recallToggle(msg) {
		return '<a href="#" id="toggle-recalls" class="btn btn-show corners"><span class="msg">'+msg+'</span><span class="btn-arrow"></span></a>';
	}
	
	var moreMsg = "Show More / Less items";
	var lessMsg = "Show More / Less items";
	
	// more than 2 recalls
	if(recallItems > 2) {
		// hide recalls greater than 3
		$('#recalls').find('article:gt(1)').hide();

		// add button to toggle display of recall items greater than 3
		$('#recalls').append(recallToggle(moreMsg));
	}	

	// displaying recalls
	$('#toggle-recalls').click(function(e){
		$('#recalls').find('article:gt(1)').slideToggle();

		var testMsg = $('#toggle-recalls').find('.msg').text();

		// Toggle more/less msg
		if(testMsg === moreMsg) {
			$('#toggle-recalls').find('.msg').text(lessMsg);
		} else {
			$('#toggle-recalls').find('.msg').text(moreMsg);
		}
		e.preventDefault();
	});

	// Collapsible content
	if($('.collapse').length){

		var $this = $('.collapse');
		$this.find('.expand').hide();
		
		// arrows
		$this.find('h1').append('<span class="arrow"></span>');
		
		// transition
		$this.find('header').click(function(){
			$(this).find('h1').toggleClass('arrow-up');
			$(this).next('.expand').slideToggle("slow");
		});
	}

	// arrows
	$('nav').find('a').append('<span class="nav-arrow"></span>');
	
	// Stripe affected models table rows
	if($('#affected-models').length){
		// hook for styling model number
		$(this).find('tr').each(function(){
			$(this).find('td:first').addClass('model');
		});
		// table row stripe
		$(this).find('tr:even').addClass('t-row');
	}
	
	/*
	// Button interaction
	$('.btn').mouseenter(function(){
		$(this).animate({
			'opacity' : 0.85
		}, 150);
	}).mouseleave(function(){
		$(this).animate({
			'opacity' : 1
		}, 150);
	});
	*/
	
	// Scroll top
	$('.top').click(function(){
		$('html, body').animate({scrollTop:0}, 500);
	});
	
	$(window).load(function () {

		if ($('.expand').attr('style') == undefined) {
			$(this).hide();
		}

		//$('.lg-response').find('.expand').css({'display': 'none'});

	/*
		// more than 2 recalls
		if(recallItems > 2) {

			// hide recalls greater than 3
			$('#recalls').find('article:gt(1)').hide();
			// add button to toggle display of recall items greater than 3
			$('#recalls').append(recallToggle(moreMsg));
		}
		
		if($('.collapse').length){
			$(this).find('.expand').each(function(){
				$(this).hide();
			});
		}
	*/
	});
});

