$(document).ready(ramahStart);

function ramahStart() 
{
	//Create debug box.  It will display debug information used for finding map coordinates

	var debugText = $('<div></div>')
										.attr('id', 'debug')
										.css({
											position : 'absolute',
											padding: '.5em',
											background: '#9cffa2',
											border: '3px solid #090'
										});

	$(debugText).append($('<p id="location-name"></p>'));
	//$(debugText).append($('<p id="coordinates"></p>'));
	//Attach debug box to body
	$('body').append(debugText);
	//Store US Image
	var topImg = getTopImage();
	//Store Canada Image
	var bottomImg = getBottomImage();
	//Initialize variables that keep track of when one of the two images is hovered over.
	var top = false;
	var bottom = false;

	//Activates functions for when the top image is moused over, moused out, or clicked on
	$(topImg).bind('mouseenter', function(eventObject){
		top = true;
	});
	$(topImg).mouseout(function(eventObject){
		top = false;
	});
	$(topImg).bind('click', topMapClick);
	
	//Activates functions for when the bottom image is moused over, moused out, or clicked on
	$(bottomImg).bind('mouseenter', function(eventObject){
		bottom = true;
	});
	$(bottomImg).mouseout(function(eventObject){
		bottom = false;
	});
	$(bottomImg).bind('click', bottomMapClick);
		
	//Set listener for whenever the mouse moves
	$('body').mousemove(function(eventObject) {
		//Store coordinates for debug box
		var coords = {
			top : eventObject.pageY + 5,
			left : eventObject.pageX + 5
		}
		
		var imageCoords = ''; //Variable that stores coordinate of mouse relative to top left corner of image
		var locationName = ''; //Variable that stores the mapped area's name.

		//If mouse is currently over the top image, calculate image coordinates and location name based on top image
		if (top == true) {
			var xCoordinate = (eventObject.pageX - $(topImg).offset().left);
			var yCoordinate = (eventObject.pageY - $(topImg).offset().top);
			//imageCoords = '('+xCoordinate+', '+yCoordinate+')';
			locationName = getLocationName('top', {xCoord : xCoordinate, yCoord : yCoordinate});
		}

		//If mouse is currently over the bottom image, calculate image coordinates and location name based on bottom image
		if (bottom == true) {
			var xCoordinate = (eventObject.pageX - $(bottomImg).offset().left);
			var yCoordinate = (eventObject.pageY - $(bottomImg).offset().top);
			//imageCoords = '('+xCoordinate+', '+yCoordinate+')';
			locationName = getLocationName('bottom', {xCoord : xCoordinate, yCoord : yCoordinate});
		}
		
		//Set image coordinates, location name of mouse location. Then set debug box coordinates
		//$('#coordinates').html(imageCoords);
		if ((locationName).length > 0) 
		{
			$('#debug').show();
		}
		else {
			$('#debug').hide();
		}
		$('#location-name').html(locationName);
		$('#debug').css(coords);
	});
}

//Returns a jQuery object for the top Map
function getTopImage()
{
	return $('.map img').filter(function(index) { return index == 0; });
}

//Returns a jQuery object for the bottom map
function getBottomImage()
{
	return $('.map img').filter(function(index) { return index == 1; });
}

//Event Handler for when the top map is clicked on
function topMapClick(eventObject)
{
	var xCoordinate = (eventObject.pageX - $(getTopImage()).offset().left);
	var yCoordinate = (eventObject.pageY - $(getTopImage()).offset().top);
	//imageCoords = '('+xCoordinate+', '+yCoordinate+')';
	locationName = getLocationName('top', {xCoord : xCoordinate, yCoord : yCoordinate});
	var url = getLocationUrl(locationName);
	window.open(url);
}

//Event Handler for when the bottom map is clicked on
function bottomMapClick(eventObject)
{
	var xCoordinate = (eventObject.pageX - $(getBottomImage()).offset().left);
	var yCoordinate = (eventObject.pageY - $(getBottomImage()).offset().top);
	//imageCoords = '('+xCoordinate+', '+yCoordinate+')';
	locationName = getLocationName('bottom', {xCoord : xCoordinate, yCoord : yCoordinate});
	var url = getLocationUrl(locationName);
	window.open(url);
}

//Returns url of location
function getLocationUrl(location)
{
	switch(location)
	{
		case 'Camp Ramah in California':
			return 'http://www.ramah.org/';
		case 'Camp Ramah in Wisconsin':
			return 'http://www.ramahwisconsin.com/';
		case 'Camp Ramah in Canada':
			return 'http://www.campramah.com/';
		case 'Camp Ramah in the Berkshires':
			return 'http://www.ramahberkshires.org/';
		case 'Camp Ramah New England':
			return 'http://www.campramahne.org/';
		case 'Camp Ramah in the Poconos':
			return 'http://www.ramahpoconos.org/intro.asp';
		case 'Camp Ramah Darom':
			return 'http://www.ramahdarom.org/';
	}
	return false;
}

//Returns Mapped location name
function getLocationName(image, point)
{
	switch(image)
	{
		case 'top':
			if (pointInPolygon(locations.us.alaska, point) == true) {
				return 'Camp Ramah in California';
			}
			if (pointInPolygon(locations.us.west, point) == true) {
				return 'Camp Ramah in California';
			}
			if (pointInPolygon(locations.us.north, point) == true) {
				return 'Camp Ramah in Wisconsin';
			}
			if (pointInPolygon(locations.us.greatlakes, point) == true) {
				return 'Camp Ramah in Canada';
			}
			if (pointInPolygon(locations.us.manhattan, point) == true) {
				return 'Camp Ramah in the Berkshires';
			}
			if (pointInPolygon(locations.us.northeast, point) == true) {
				return 'Camp Ramah New England';
			}
			if (pointInPolygon(locations.us.pennsylvania, point) == true) {
				return 'Camp Ramah in the Poconos';
			}
			if (pointInPolygon(locations.us.virginia, point) == true) {
				return 'Camp Ramah New England';
			}
			if (pointInPolygon(locations.us.southeast, point) == true) {
				return 'Camp Ramah Darom';
			}
			break;
		case 'bottom':
			if (pointInPolygon(locations.canada.southwest, point) == true) {
				return 'Camp Ramah in California';
			}
			if (pointInPolygon(locations.canada.central, point) == true) {
				return 'Camp Ramah in Wisconsin';
			}
			if (pointInPolygon(locations.canada.outlying, point) == true) {
				return 'Camp Ramah in Canada';
			}
			break;
	}
	return '';
}

//Returns true if a point(p) is within a polygon built from an array(polygon) of n points(x, y)
function pointInPolygon(polygon, p)
{
  var counter = 0;
  var i;
  var xinters;
  var p1;
	var p2;
	var pcoords = '';
	var n = polygon.length;

	  p1 = polygon[0];
	  for (i=1;i<=n;i++) {
	    p2 = polygon[i % n];
	    if (p.yCoord > min(p1.yCoord,p2.yCoord)) {
	      if (p.yCoord <= max(p1.yCoord,p2.yCoord)) {
	        if (p.xCoord <= min(p1.xCoord,p2.xCoord)) {
	          if (p1.yCoord != p2.yCoord) {
	            xinters = (p.yCoord-p1.yCoord)*(p2.xCoord-p1.xCoord)/(p2.yCoord-p1.yCoord)+p1.xCoord;
	            if (p1.xCoord == p2.xCoord || p.xCoord <= xinters)
	              counter++;
	          }
	        }
	      }
	    }
	    p1 = p2;
	  }
	  if (counter % 2 == 0)
		{ 
	    return false;
		}
	  else
		{ 
	    return true;
		}
}

//Returns smaller of two: x or y
function min(x, y)
{
	if (x < y)
	{
		return x;
	}
	return y;
}

//Returns larger of two: x or y
function max(x, y)
{
	if (x > y)
	{
		return x;
	}
	return y;
}

//Set mapped location vertices
var locations = {
	us : {
		alaska : new Array(
				{xCoord : 0, yCoord : 0},
				{xCoord : 60, yCoord : 1},
				{xCoord : 61, yCoord : 81},
				{xCoord : 0, yCoord : 80}
			),
		west : new Array(
				{xCoord : 95, yCoord : 55},
				{xCoord : 259, yCoord : 80},
				{xCoord : 254, yCoord : 179},
				{xCoord : 272, yCoord : 180},
				{xCoord : 273, yCoord : 229},
				{xCoord : 264, yCoord : 230},
				{xCoord : 322, yCoord : 358},
				{xCoord : 298, yCoord : 352},
				{xCoord : 212, yCoord : 292},
				{xCoord : 120, yCoord : 269},
				{xCoord : 72, yCoord : 160}
			),
		north : new Array(
				{xCoord : 261, yCoord : 81},
				{xCoord : 331, yCoord : 68},
				{xCoord : 377, yCoord : 90},
				{xCoord : 365, yCoord : 109},
				{xCoord : 391, yCoord : 123},
				{xCoord : 400, yCoord : 161},
				{xCoord : 456, yCoord : 147},
				{xCoord : 462, yCoord : 169},
				{xCoord : 447, yCoord : 216},
				{xCoord : 384, yCoord : 230},
				{xCoord : 273, yCoord : 229},
				{xCoord : 272, yCoord : 180},
				{xCoord : 255, yCoord : 181}
			),
		greatlakes : new Array(
				{xCoord : 367, yCoord : 106},
				{xCoord : 377, yCoord : 96},
				{xCoord : 415, yCoord : 97},
				{xCoord : 485, yCoord : 113},
				{xCoord : 495, yCoord : 134},
				{xCoord : 472, yCoord : 144},
				{xCoord : 471, yCoord : 165},
				{xCoord : 462, yCoord : 167},
				{xCoord : 458, yCoord : 149},
				{xCoord : 398, yCoord : 159},
				{xCoord : 391, yCoord : 120}
			),
		manhattan	: new Array(
				{xCoord : 508, yCoord : 145},
				{xCoord : 533, yCoord : 132},
				{xCoord : 530, yCoord : 155},
				{xCoord : 506, yCoord : 155}
			),
		northeast : new Array(
				{xCoord : 479, yCoord : 100},
				{xCoord : 537, yCoord : 41},
				{xCoord : 555, yCoord : 74},
				{xCoord : 545, yCoord : 124},
				{xCoord : 515, yCoord : 145}
			),
		pennsylvania : new Array(
				{xCoord : 472, yCoord : 145},
				{xCoord : 503, yCoord : 136},
				{xCoord : 509, yCoord : 144},
				{xCoord : 508, yCoord : 154},
				{xCoord : 519, yCoord : 152},
				{xCoord : 517, yCoord : 176},
				{xCoord : 510, yCoord : 177},
				{xCoord : 502, yCoord : 167},
				{xCoord : 463, yCoord : 173},
				{xCoord : 461, yCoord : 166},
				{xCoord : 470, yCoord : 165}
			),
		virginia : new Array(
				{xCoord : 462, yCoord : 174},
				{xCoord : 503, yCoord : 165},
				{xCoord : 531, yCoord : 199},
				{xCoord : 446, yCoord : 218},
				{xCoord : 455, yCoord : 205},
				{xCoord : 446, yCoord : 195}
			),
		southeast : new Array(
				{xCoord : 264, yCoord : 231},
				{xCoord : 388, yCoord : 230},
				{xCoord : 527, yCoord : 201},
				{xCoord : 487, yCoord : 301},
				{xCoord : 505, yCoord : 332},
				{xCoord : 504, yCoord : 340},
				{xCoord : 493, yCoord : 355},
				{xCoord : 452, yCoord : 302},
				{xCoord : 390, yCoord : 318},
				{xCoord : 321, yCoord : 355}
			)
	},
	canada : {
		southwest : new Array(
				{xCoord : 56, yCoord : 157},
				{xCoord : 209, yCoord : 161},
				{xCoord : 188, yCoord : 262},
				{xCoord : 102, yCoord : 263}
			),
		central : new Array(
				{xCoord : 210, yCoord : 160},
				{xCoord : 293, yCoord : 159},
				{xCoord : 322, yCoord : 189},
				{xCoord : 290, yCoord : 224},
				{xCoord : 286, yCoord : 264},
				{xCoord : 189, yCoord : 265}
			),
		outlying : new Array(
				{xCoord : 38, yCoord : 159},
				{xCoord : 105, yCoord : 33},
				{xCoord : 602, yCoord : 1},
				{xCoord : 602, yCoord : 330},
				{xCoord : 277, yCoord : 349},
				{xCoord : 286, yCoord : 224},
				{xCoord : 321, yCoord : 189},
				{xCoord : 292, yCoord : 160}
			)
	}
}
