var Map = new Class({
	
	initialize: function(element) {
		this.element = $(element);
		this.bubble = new Bubble();
		this.bubble.element.inject(this.element);
	},
	
	addPin: function(loc, title, plant, client) {
		var html = '<h5>' + title + '</h5>';
		html += '<table cellpadding="5" border="0" width="100%" cellspacing="0">';
		html += '<tr><th>Plant:</th><td>' + plant + '</td></tr>';
		html += '<tr><th>Client:</th><td>' + client + '</td></tr>';
		html += '</table>';
		
		var p = new Pin(loc, html);
		p.element.inject(this.element);
		p.element.addEvent('mouseenter', function(e) {
			e.stop();
			e.target.setStyle('background-position', 'bottom center');
			this.bubble.show(p);
		}.bind(this));
		p.element.addEvent('mouseleave', function(e) {
			e.stop();
			e.target.setStyle('background-position', '');
			this.bubble.hide();
		}.bind(this));
		p.element.addEvent('click', function(e) {
			e.stop();
		});
	}
	
});


var Pin = new Class({
	
	initialize: function(loc, html) {
		this.loc = loc;
		this.html = html;
		
		this.element = new Element('span', {
			'class': 'pin',
			'html': '',
			'styles': {
				'left': loc.x - 6,
				'top': loc.y - 6
			}
		});
	}
	
});


var Bubble = new Class({
	
	curPin: null,
	
	initialize: function() {
		this.element = new Element('div', {
			'class': 'bubble',
			'styles': {
				'opacity': 0
			}
		});
		
		this.element.addEvent('click', function(e) {
			e.stop();
		});
	},
	
	show: function(pin) {
		this.curPin = pin;
		this.element.setStyles({
			'display': 'block',
			'visibility': 'hidden'
		});
		this.element.set('html', pin.html);
		
		var x = (pin.loc.x + pin.element.getSize().x / 2 - this.element.getSize().x / 2).limit(0, 800 - this.element.getSize().x);
		var y = pin.loc.y - this.element.getSize().y - 10;
		
		this.element.setStyles({
			'top': y,
			'left': x,
			'visibility': 'visible'
		});
		this.element.fade('in');
		
		window.addEvent('click', function() {
			this.hide();
		}.bind(this));
	},
	
	hide: function() {
		this.element.fade('out');
	}
	
});


window.addEvent('domready', function() {
	
	var map = new Map('map');
	
	map.addPin({x: 527, y: 311}, 'Gadsen, AL',				'Keystone Foods', 'The Stellar Group');
	
	map.addPin({x: 410, y: 292}, 'Clarksville, AR',			'Wal-Mart Distribution', 'Republic Refrigeration');
	map.addPin({x: 425, y: 287}, 'Russellville, AR',		'Con-Agra', 'The Stellar Group');
	
	map.addPin({x:  50, y: 250}, 'Bakersfield, CA',			'Dreyers Ice Cream', 'The Stellar Group');
	
	map.addPin({x: 242, y: 207}, 'Golden, CO',				'Hazen Research', 'Weitz');
	map.addPin({x: 258, y: 209}, 'Denver, CO',				'Pepsi Bottling Company', 'DSI Process Systems');
	
	map.addPin({x: 601, y: 360}, 'Jacksonville, FL',		'Star Fabricators', 'The Stellar Group');
	map.addPin({x: 608, y: 395}, 'Orlando, FL',				'Coca-Cola Bottling Group', 'DSI Process Systems');
	
	map.addPin({x: 553, y: 309}, 'Marietta, GA',			'Coca-Cola Bottling Group', 'DSI Process Systems');
	
	map.addPin({x: 440, y: 162}, 'Dubuque, IA',				'Western Dubuque Biodeisel', 'Weitz');
	map.addPin({x: 410, y: 161}, 'Eagle Grove, IA',			'Ag Processing AGP', 'M&amp;W Contractors');
	map.addPin({x: 408, y: 149}, 'Hanlontown, IA',			'Poet Bio-Hanlontown', 'Poet');
	map.addPin({x: 388, y: 155}, 'Hartley, IA',				'VeraSun', 'Fagen');
	map.addPin({x: 418, y: 180}, 'Newton, IA',				'Central Iowa Energy', 'Todd &amp; Sargent');
	map.addPin({x: 399, y: 168}, 'Ralston, IA',				'Renewable Energy Group', 'Todd &amp; Sargent');
	map.addPin({x: 380, y: 172}, 'Sergeant Bluff, IA',		'Ag Processing AGP', 'M&amp;W Contractors');
	map.addPin({x: 393, y: 179}, 'Wall Lake, IA',			'Wall Lake Biodiesel', 'Todd &amp; Sargent');
	
	map.addPin({x: 144, y: 140}, 'Heyburn, ID',				'Renova Energy', 'Dilling Mechanical');
	
	map.addPin({x: 479, y: 199}, 'Gibson City, IL',			'One Earth Ethanol', 'ICM');
	map.addPin({x: 450, y: 202}, 'Mt. Sterling, IL',		'Dot Foods', 'The Stellar Group');
	map.addPin({x: 467, y: 233}, 'Red Bud, IL',				'Roeslein &amp; Assoc.', 'Roeslein &amp; Assoc.');
	
	map.addPin({x: 516, y: 204}, 'Anderson, IN',			'Nestle', 'The Stellar Group');
	map.addPin({x: 501, y: 202}, 'Delphi, IN',				'Indiana Packers', 'The Stellar Group');
	map.addPin({x: 513, y: 182}, 'North Manchester, IN',	'Poet Biorefinery North Manchester', 'Poet');
	map.addPin({x: 526, y: 196}, 'Portland, IN',			'Coca-Cola Bottling Group', 'MB Installations');
	
	map.addPin({x: 522, y: 235}, 'Louisville, KY',			'Dr. Pepper', 'MB Installations');
	
	map.addPin({x: 622, y: 190}, 'Williamsport, MD',		'Dot Foods', 'The Stellar Group');
	
	map.addPin({x: 404, y: 133}, 'Lake Crystal, MN',		'Poet Bio Refinery LC', 'Poet');
	map.addPin({x: 378, y: 142}, 'Lavern, MN',				'Agri Ethanol', 'Nelson Engineering');
	map.addPin({x: 424, y: 140}, 'Preston, MN',				'Poet Bio Refinery', 'Poet');
	map.addPin({x: 391, y: 142}, 'Welcome, MN',				'VeraSun Ethanol', 'Fagen');
	
	map.addPin({x: 401, y: 228}, 'Kansas City, MO',			'Cargill', 'PSI Process Systems');
	map.addPin({x: 395, y: 210}, 'St. Joseph, MO',			'Ag Processing AGP', 'M&amp;W Contractors');
	map.addPin({x: 425, y: 219}, 'Moberly, MO',				'Producers Choice', 'Process Concepts');
	map.addPin({x: 414, y: 226}, 'Marshall, MO',			'Con-Agra Foods', 'The Stellar Group');
	
	map.addPin({x: 352, y: 200}, 'Hastings, NB',			'Ag Processing AGP', 'Haskell');
	map.addPin({x: 375, y: 188}, 'Omaha, NB',				'Omaha Water Treatment Plant', 'Alberici Constructors');
	
	map.addPin({x: 345, y:  91}, 'Jamestown, ND',			'Aviko Foods', 'Stanley Jones Company');
	
	map.addPin({x: 671, y: 152}, 'White Plains, NY',		'Pepsi', 'DSI Process Systems');
	
	map.addPin({x: 544, y: 175}, 'Fostoria, OH',			'Poet Bio Refinery', 'Poet');
	map.addPin({x: 535, y: 184}, 'Leipsic, OH',				'Summit Bio Refinery', 'Poet');
	map.addPin({x: 550, y: 186}, 'Marion, OH',				'Marion Bio Refinery', 'Poet');
	
	map.addPin({x: 335, y: 300}, 'Altus, OK',				'Barr S Foods', 'Toromont Process Systems');
	map.addPin({x: 365, y: 310}, 'Ardmore, OK',				'Dot Foods', 'The Stellar Group');
	map.addPin({x: 335, y: 285}, 'Clinton, OK',				'Barr S Foods', 'Toromont Process Systems');
	
	map.addPin({x: 365, y: 147}, 'Chancellor, SD',			'Poet Bio Refinery Chancellor', 'Poet');
	map.addPin({x: 354, y: 154}, 'Scottland, SD',			'Poet Bio Scottland', 'Poet');
	map.addPin({x: 347, y: 134}, 'St. Lawrence, SD',		'South Dakota Oil Processors', 'Nelson Engineering');
	map.addPin({x: 359, y: 126}, 'Watertown, SD',			'Glacial Lakes Ethanol', 'NewMech Company, Inc.');
	
	map.addPin({x: 480, y: 285}, 'Jackson, TN',				'Pinnacle Foods', 'Electromotion Refrigeration');
	map.addPin({x: 510, y: 270}, 'Nashville, TN',			'Opryland Chiller Plant', 'Stanley Jones Company');
	
	map.addPin({x: 380, y: 375}, 'College Station, TX',		'Texas A&amp;M Chiller Plant', 'Toromont Process Systems');
	
	map.addPin({x: 472, y: 135}, 'Friesland, WI',			'United Ethanol', 'Mechanical Systems, Inc.');
	
	map.addPin({x: 250, y: 181}, 'Cheyenne, WY',			'Frontier Refinery', 'Alliance Constructors');
	
});