$(document).ready(function() {
	
	// External links
	
	$('a').each(function() {
		if($(this).attr('rel') == 'external') {
			$(this).click(function() {
				window.open($(this).attr('href'));
				this.blur();
				return false;
			});
		}
	});
	
	// Print
	
	$('#print').click(function() {
		window.print();
		this.blur();
		return false;
	});
	
	// Navigation
	
	$('#navigation .nav').hover(function() {
		$(this).find('ul').show();
		$(this).find('.link:first').addClass('hover');
	}, function() {
		$(this).find('ul').hide();
		$(this).find('.link:first').removeClass('hover');
	});

	$('#navigation-dealers .nav').hover(function() {
		$(this).find('ul').show();
		$(this).find('.link:first').addClass('hover');
	}, function() {
		$(this).find('ul').hide();
		$(this).find('.link:first').removeClass('hover');
	});
	
	$('#dealer-nav .select').click(function() {
		$('#dealer-nav-menu').show();
		$(document.body).click(function() {
			$('#dealer-nav-menu').hide();
			$(document.body).unbind('click');
		});
		this.blur();
		return false;
	});
	
	
	// Dealer Locator

	$('#dealer-locator-select').click(function() {
		$('#dealer-locator-menu').show();
		$(document.body).click(function() {
			$('#dealer-locator-menu').hide();
			$(document.body).unbind('click');
		});
		this.blur();
		return false;
	});
	
	$('#dealer-locator-menu a').click(function() {
		$('#dealer-locator-select span').html($(this).html());
		var h = $(this).attr('href');
		var v = h.substring(h.indexOf('#') + 1, h.length);
		$('#dealer-locator-mfg').val(v);
		$('#dealer-locator-menu').hide();
		this.blur();
		return false;
	});
	
	$('#dealer-locator-submit').click(function() {
		$('#dealer-locator form:first').submit();
		this.blur();
		return false;
	});
	
	
	// Latest news feed
	
	if($('#latest-news-feed')) {
		getNews(4);
	}
	
	$('#latest-news-refresh').click(function() {
		$('#latest-news-feed').fadeOut();
		getNews(4, true);
		this.blur();
		return false;
	});

	
});

function getNews(limit, fade) {
	$.ajax({
		url: '/_ajax/news',
		dataType: 'json',
		success: function(data) {
			var o = '';
			limit = (limit > data.length) ? data.length : limit;
			for(var i = 0; i < limit; i ++) {
				o += '<li><a href="' + data[i].link + '" target="_blank">' + data[i].title + '</a></li>';
			}
			$('#latest-news-feed').html('<ul>' + o + '</ul>');
			if(fade) $('#latest-news-feed').fadeIn();
		}
	});		
}