var xmlHTTP;

function initialize_location_map() 
{
	var prime_location = locations[0];
	var prime_latitude = prime_location[2];
	var prime_longitude = prime_location[3];
	
	if ( prime_latitude == 0 )
	{
		prime_latitude = 39.805256;
		prime_longitude = -83.889713;
	}
	
	var myOptions = {
		scrollwheel: false,
		zoom: 10,
		center: new google.maps.LatLng(prime_latitude, prime_longitude),
		mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("location_map_canvas"), myOptions);

  var bounds = setMarkers(map, locations);
  
  map.fitBounds(bounds);
}

function setMarkers(map, locations)
{
	var markerBounds = new google.maps.LatLngBounds();

	function createMarker( map, location )
	{
		
		var mainContent = null;
		if ( location[5] == 'PICT' )
			mainContent = '<a href="' + location[7] + '" target="_blank"><img src="' + location[8] + '" border="0" height="150" width="150"></a><br />';
		else
			mainContent = '<div style="font-size: 12pt; font-weight: bold;">' + location[6] + "</div><br />" ;
			
		var infoWindowContent = '<div style="text-align: center;">' + 
									'<div style="font-size: 11pt; font-weight: bold;">' + location[1] + "</div><br />" +
									mainContent +
									location[4] + "<br />" + 
								"</div>";
		var infowindow = new google.maps.InfoWindow();

		var myLatLng = new google.maps.LatLng(location[2], location[3]);
		var marker = new google.maps.Marker({
			position: myLatLng,
			map: map,
			title: location[1] + " " + location[4],
			zIndex: location[0]
		});

		markerBounds.extend(myLatLng);
		
		google.maps.event.addListener( marker, 'click', function() {
			infowindow.set_content(infoWindowContent);
			infowindow.open(map, marker);
		});
	
		return marker;
	}

	for (var i = 0; i < locations.length; i++) {
		var location = locations[i];
		var marker = createMarker( map, location );
		
	}

	return markerBounds;
}

function openInfoWindow( map, marker, infowindow, contents )
{
	infowindow.set_content({content:contents});
	infowindow.open(map, marker);
}

function thumbsUp(postID)
{
	var buttonUpImage = document.getElementById("thumbs_up_"+postID);
	var state = buttonUpImage.getAttribute("state");
	
	if ( state == 0 )
	{
		buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up_hover.gif');
		buttonUpImage.setAttribute('state', 1 );
		
		var buttonDownImage = document.getElementById("thumbs_dn_"+postID);
		buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down.gif');
		buttonDownImage.setAttribute('state', 0 );

		postRating( postID, 1 );
	}
	else
	{
		buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up.gif');
		buttonUpImage.setAttribute('state', 0 );

		postRating( postID, 0 );
	}
}

function thumbsDown(postID)
{
	var buttonDownImage = document.getElementById("thumbs_dn_"+postID);
	var state = buttonDownImage.getAttribute("state");

	if ( state == 0 )
	{
		buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down_hover.gif');
		buttonDownImage.setAttribute('state', 1 );

		var buttonUpImage = document.getElementById("thumbs_up_"+postID);
		buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up.gif');
		buttonUpImage.setAttribute('state', 0 );

		postRating( postID, -1 );
	}
	else
	{
		buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down.gif');
		buttonDownImage.setAttribute('state', 0 );

		postRating( postID, 0 );
	}

}

function postRating(postID, rating)
{ 
	xmlHTTP = getXMLHTTPObject();
	
	if ( xmlHTTP != null )
	{
		var url="viewtopic_ratings.php";
		url=url+"?p="+postID;
		url=url+"&r="+rating;
		url=url+"&salt="+Math.random();
	
		xmlHTTP.onreadystatechange = stateChanged;
		xmlHTTP.open("GET",url,true);
		xmlHTTP.send(null);
	}
}

function stateChanged() 
{ 
	if ( xmlHTTP.readyState == 4 || xmlHTTP.readyState=="complete" )
	{ 
		xmlDoc = xmlHTTP.responseXML;
		
		var error = xmlDoc.getElementsByTagName("error")[0];
		if ( error )
		{
			var error_message = error.childNodes[0].nodeValue;
			var postID = xmlDoc.getElementsByTagName("post_id")[0].childNodes[0].nodeValue;
			
			if ( postID > 0 )
			{
				var buttonUpImage = document.getElementById("thumbs_up_"+postID);
				var buttonDownImage = document.getElementById("thumbs_dn_"+postID);
			
				buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up.gif');
				buttonUpImage.setAttribute('state', 0 );
			
				buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down.gif');
				buttonDownImage.setAttribute('state', 0 );
			}
			
			alert( error_message );
		}
		else
		{
			var postID = xmlDoc.getElementsByTagName("post_id")[0].childNodes[0].nodeValue;
			var rating = xmlDoc.getElementsByTagName("rating")[0].childNodes[0].nodeValue;
			var buttonUpImage = document.getElementById("thumbs_up_"+postID);
			var buttonDownImage = document.getElementById("thumbs_dn_"+postID);
			
			if ( rating < 0 )
			{
				buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up.gif');
				buttonUpImage.setAttribute('state', 0 );
	
				buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down_selected.gif');
				buttonDownImage.setAttribute('state', 1 );
			}
			else if ( rating > 0 )
			{
				buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up_selected.gif');
				buttonUpImage.setAttribute('state', 1 );
	
				buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down.gif');
				buttonDownImage.setAttribute('state', 0 );
			}
			else
			{
				buttonUpImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_up.gif');
				buttonUpImage.setAttribute('state', 0 );
	
				buttonDownImage.setAttribute('src', 'templates/zencss/images/icon_thumbs_down.gif');
				buttonDownImage.setAttribute('state', 0 );
			}
		}
	} 
}

function getXMLHTTPObject()
{
	var obj = null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		obj = new XMLHttpRequest();
	}
	catch (e)
	{
		//Internet Explorer
		try
		{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			obj= new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return obj;
}

