/*
	content.js - Content Loader for JavaScript-based Catalogs.
	Author - Eric L .Truitte
	Conception Date - 01-13-2008
	Caveats -
		This script should not need editing.  To add more states or dealers to
		the catalog, follow format in ./dealers.js and add them where needed.
*/

function Dealer (dealer, address, phone, website, email) {
  return {
    dealer: dealer,
    address: address,
    phone: phone,
    website: website,
    email: email
  };
};

function State (initials, state, dealers) {
  return {
    initials: initials,
    state: state,
    dealers: dealers
  };
};

function formatdealer (s, d) {
	var html = "";
	html = '<div class="listing">\n';
	if (dealers[s].dealers[d].website != "") {
		html = html + '<div class="itemid"><a href="' + dealers[s].dealers[d].website + '" target="_blank">Website</a></div>';
	};
	html = html + '<div class="infocontainer">';
	html = html + '  <div class="infotitle">' + dealers[s].dealers[d].dealer + '</div>';
	html = html + '  <div class="info">' + dealers[s].dealers[d].address + '<br />' + dealers[s].dealers[d].phone + '</div>';
	html = html + '</div></div>';
	return html;
};

function listdealers(s) {
	var html = "";
	for (var d = 0; d < dealers[s].dealers.length; d++) { html = html + formatdealer(s, d); }
	return html;
};

function contentfinder(state) {
	var s = 0;
	for (; s < dealers.length; s++) {
		if (dealers[s].initials == state) {
			location.hash = '#' + dealers[s].initials;
			return listdealers(s);
		};
	};
	return '';
};

function hashload () {
	/*
		grabs the hash from reload or load if linked.  Very useful client-side
		mechanism to change the content by, but only works when mechanically
		clicked on something.  For some reason the internet people NEVER put
		changes to the hash into the re-load or created a event for the changing
		if the hash.  Bastards.  -etruitte 01-14-08
	*/
	if (location.hash.length = 3) {
		var hashstring = location.hash.substr(1,location.hash.length).toLowerCase();
		$('#content').html(contentfinder(hashstring));
	};
};

function initmenu() {
  var table = $('<table></table>').attr('id', 'menutable');
  var row = $('<tr></tr>').append($('<td></td>').attr('colspan', '3').text('USA:'));
	var rc = 2;
	for (var s = 0; s < dealers.length; s++) {
		if (dealers[s].dealers.length > 0) {
			if (rc++ == 2) {
        table.append(row);
        row = $('<tr></tr>');
        rc = 0;
      };
      row.append($('<td></td>').attr('id', dealers[s].state)
          .attr('initials', dealers[s].initials)
          .addClass('menustate')
          .text(dealers[s].initials.toUpperCase())
          .click(function () { $('#content').html(contentfinder($(this).attr('initials'))); })
      );
		};
    if (s == 50) { // 50 states,  start next country
      if (row.children.length) {
        table.append(row);
        row = $('<tr></tr>');
        rc = 0;
      };
      table.append($('<tr></tr>').append($('<td></td>').attr('colspan', '3').text('Canada:')));
    };
	};
  if (row.children.length) table.append(row);
	$('#menu').append(table);
};
