/**
 *	Concertina JS
 */

var cur;

$(function() {
	var a = $('#banner-sections dt a');
	a.click(function (e) {
		e.preventDefault();
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');
		concertina(hash);
		$.historyLoad(hash);
	});
	$.historyInit(concertina);
});

function concertina(rel) {
	if (!rel) {
		rel = 'whats-new';
	}
	if (cur) {
		$('#detail-' + cur).hide();
		$('#link-' + cur).removeClass('active');
		$('#' + cur + '-content').hide();
	} else {
		$('#banner-content div.home-content-wrapper').hide();
	}
	$('#link-' + rel).addClass('active');
	$('#detail-' + rel).slideDown('slow');
	$('#' + rel + '-content').show();
	cur = rel;
  if (cur == 'find-a-dealership') {
    $('input[name="livesearch"]').liveSearch();
    $('div#map form').submit(function(){ return false; });
    initMap();
  }
}

/**
 *	Map JS
 */
var map;
var markers = [];
var clicked = false;

function loadDealer(code) {
	$.get('/dealers/ajax/' + code, { },_loadDealer);
}

function _loadDealer (d) {
	$('#dealership-results').html(d);
}

function loadDealers () {
	$.getJSON('/dealers/ajax', { }, _loadDealers);
}

function _loadDealers (data, t) {
  return true;
}

function initMap() {
	if (!clicked) {
		loadDealers();
		$('#dealer_id').change(
			function() {
				var code = this.options[this.selectedIndex].value;
				return loadDealer(code);
			}
		);
		clicked = true;
	}
}
