//var MapPhotoWorldRequest = false;
var AlbumId = 0;
var GroupId = 0;
var FirstCall = true;
var bounds;

function MapGooglePhotoWorldAlbumSetAlbumBounds()
{
	Map.setZoom(Map.getBoundsZoomLevel(bounds));
	Map.setCenter(bounds.getCenter());
}

function MapGooglePhotoWorldInit(Latitude, Longitude, ZoomLevel, MapType, Width, Height, UseCookies, GetAlbumId, GetGroupId)
{
	if(GetAlbumId != null)
		AlbumId = GetAlbumId;
	
	if(GetGroupId != null)
		GroupId = GetGroupId;
		
	if(document.cookie && UseCookies)
	{
		CookieData = document.cookie;
		
		Index = CookieData.search("world_map_latitude");
		
		if(Index >= 0)
		{
			TempData = CookieData.substring(Index, CookieData.length);
			
			if(TempData.search(";") < 0)
				Latitude = Number(TempData.substring(TempData.search("=") + 1, TempData.length));
			else
				Latitude = Number(TempData.substring(TempData.search("=") + 1, TempData.search(";")));
		}		
		
		Index = CookieData.search("world_map_longitude");
		
		if(Index >= 0)
		{
			TempData = CookieData.substring(Index, CookieData.length);

			if(TempData.search(";") < 0)
				Longitude = Number(TempData.substring(TempData.search("=") + 1, TempData.length));
			else
				Longitude = Number(TempData.substring(TempData.search("=") + 1, TempData.search(";")));
		}
		Index = CookieData.search("world_map_zoom_level");
		
		if(Index >= 0)
		{
			TempData = CookieData.substring(Index, CookieData.length);

			if(TempData.search(";") < 0)
				ZoomLevel = Number(TempData.substring(TempData.search("=") + 1, TempData.length));
			else
				ZoomLevel = Number(TempData.substring(TempData.search("=") + 1, TempData.search(";")));
		}

		Index = CookieData.search("world_map_photo_index");
		
		if(Index >= 0)
		{
			TempData = CookieData.substring(Index, CookieData.length);

			if(TempData.search(";") < 0)
				MapPhotoLastStartIndex = Number(TempData.substring(TempData.search("=") + 1, TempData.length));
			else
				MapPhotoLastStartIndex = Number(TempData.substring(TempData.search("=") + 1, TempData.search(";")));
		}

		Index = CookieData.search("world_map_photo_count");
		
		if(Index >= 0)
		{
			TempData = CookieData.substring(Index, CookieData.length);

			if(TempData.search(";") < 0)
				MapPhotoLastMaxCount = Number(TempData.substring(TempData.search("=") + 1, TempData.length));
			else
				MapPhotoLastMaxCount = Number(TempData.substring(TempData.search("=") + 1, TempData.search(";")));
		}
	}

	MapGoogleOpen(Latitude, Longitude, 0, 0, ZoomLevel, MapType, Width, Height);
	
	Bounds = Map.getBounds();
	SouthWestLocation = Bounds.getSouthWest(); 
	NorthEastLocation = Bounds.getNorthEast(); 

	if(MapPhotoLastStartIndex != 0 && MapPhotoMaxCount == MapPhotoLastMaxCount)
	{
		MapPhotoStartIndex = MapPhotoLastStartIndex;
		MapPhotoLastStartIndex = 0;
	}

	if(AlbumId == 0)
		AlbumParam = '';
	else
		AlbumParam = '&album_id='+AlbumId;

	if(GroupId == 0)
		GroupParam = '';
	else
		GroupParam = '&group_id='+GroupId;

	// Layer

	Layer = "?layer=";

	CurrentZoomLevel = Map.getZoom();
	CurrentCenterLat = Map.getCenter().lat();
	CurrentCenterLng = Map.getCenter().lng();

	CurrentTileLayer = GetLayerAdress(CurrentCenterLng, CurrentCenterLat);

	Layer+= CurrentTileLayer;

	// Layer Ends
	var Url = "/photo_world_xml.php" + Layer + "&latitudemin=" + SouthWestLocation.lat() + "&longitudemin=" + SouthWestLocation.lng() + "&latitudemax=" + NorthEastLocation.lat() + "&longitudemax=" + NorthEastLocation.lng() + "&start=" + MapPhotoStartIndex + "&count=" + MapPhotoMaxCount + AlbumParam + GroupParam;
	var Request = new AjaxRequest();
		Request.Send(Url, {
			ajax_loader: false,
			request_name: 'photo_world_xml',
			cancel_same_named_requests: true,
			callback_parameters: {
				result_is_xml: true
			},
			callback: MapGooglePhotoWorldProcessData
		});

	// MapGooglePhotoWorldStartRequest("flickr_xml.php?latitudemin=-90&longitudemin=-180&latitudemax=90&longitudemax=180");

	// window.setTimeout("Hinweis()", 10000);
	GEvent.addListener(Map, "moveend", MapGooglePhotoWorldMapViewChanged);
	//GEvent.addListener(Map, "zoomend", MapGooglePhotoWorldMapViewChanged);
	if(document.getElementById('AlbumDescription'))
		setAlbumDescription();

	if(document.getElementById('map'))
		SetMapOverlay();
}

function MapGoogleCreateWorldMarker(Location, Name, FileName, TempIcon) 
{
    var Marker = new GMarker(Location, TempIcon);
    // GEvent.addListener(Marker, "click", function() {Marker.openInfoWindowHtml("<div style=\"height: 100px; width: 200px;\"><a href=\"/photo_detail.php?name=" + Name + "\"><img alt=\"Test\" src=\"" + FileName + "\"></a></div>");});
	// GEvent.addListener(Marker, "click", function() {Marker.openInfoWindowHtml("<img alt=\"Test\" src=\"http://static.flickr.com/" + FileName + "\">");});

    return Marker;
}

function MapGooglePhotoWorldProcessData(Params) 
{
	document.getElementById("photo_navigation").style.color = "#666666";

	var XmlData = Params.XmlResult;
	var PhotoXmlDataList;
	var StartIndex = 0;
	var LatitudeMin = -90;
	var LatitudeMax = 90;
	var LongitudeMin = -180;
	var LongitudeMax = 180;
	var Koordinaten = '';
	var PhotoIndex = 0;

	//var TempIcon = new GIcon();
	//var TempFocusIcon = new GIcon();

	PhotoXmlInfosList = XmlData.getElementsByTagName("info");
	// alert(PhotoXmlInfosList[0].childNodes[0].firstChild.data + " " + PhotoXmlInfosList[0].childNodes[1].firstChild.data);

	// Startindex
	if(PhotoXmlInfosList[0].getElementsByTagName("index")[0])
		StartIndex = PhotoXmlInfosList[0].getElementsByTagName("index")[0].firstChild.data;
	// LatitudeMin, LatitudeMax, LongitudeMin, LongitudeMax
	if(PhotoXmlInfosList[0].getElementsByTagName("latitude_min")[0])
		LatitudeMin = PhotoXmlInfosList[0].getElementsByTagName("latitude_min")[0].firstChild.data;
	if(PhotoXmlInfosList[0].getElementsByTagName("latitude_max")[0])
		LatitudeMax = PhotoXmlInfosList[0].getElementsByTagName("latitude_max")[0].firstChild.data;
	if(PhotoXmlInfosList[0].getElementsByTagName("longitude_min")[0])
		LongitudeMin = PhotoXmlInfosList[0].getElementsByTagName("longitude_min")[0].firstChild.data;
	if(PhotoXmlInfosList[0].getElementsByTagName("longitude_max")[0])
		LongitudeMax = PhotoXmlInfosList[0].getElementsByTagName("longitude_max")[0].firstChild.data;

	NumberOfChildren = document.getElementById("photo_navigation").childNodes.length;

	for(Index = 0; Index < NumberOfChildren; Index++)
		document.getElementById("photo_navigation").removeChild(document.getElementById("photo_navigation").firstChild);

	if(PhotoXmlInfosList[0].childNodes[0].firstChild.data > 0)
	{
		/*
		InfoText = MapInfoPhotosText.replace("%s", (MapPhotoStartIndex + 1) + " - " + Math.min(MapPhotoStartIndex + MapPhotoMaxCount, PhotoXmlInfosList[0].childNodes[0].firstChild.data));
		InfoText = InfoText.replace("%s", PhotoXmlInfosList[0].childNodes[0].firstChild.data);
		InfoText = InfoText.replace("%s", PhotoXmlInfosList[0].childNodes[1].firstChild.data);
		*/

		InfoText = (MapPhotoStartIndex + 1) + " - " + (Number(MapPhotoStartIndex) + Number(PhotoXmlInfosList[0].childNodes[0].firstChild.data));

		var inputIndex = document.createElement('input');
		var typeIndex = document.createAttribute('type');
		var idIndex = document.createAttribute('id');
		var valueIndex = document.createAttribute('value');
			typeIndex.nodeValue = 'hidden';
			idIndex.nodeValue = 'PhotoIndex';
			valueIndex.nodeValue = MapPhotoStartIndex;
			inputIndex.setAttributeNode(typeIndex);
			inputIndex.setAttributeNode(idIndex);
			inputIndex.setAttributeNode(valueIndex);
			document.getElementById("photo_navigation").appendChild(inputIndex);
	}
	else
	{
		InfoText = MapInfoNoPhotosText.replace("%s", PhotoXmlInfosList[0].childNodes[1].firstChild.data);
		InfoText = InfoText.replace(/\(.*\)/, "");
	}

	document.getElementById("photo_navigation").appendChild(document.createTextNode(MapInfoBrowseText));

	if(AlbumId || GroupId)
	{
		Element = document.getElementById("photo_navigation").appendChild(document.createElement('a'));
		Element.setAttribute('href', 'javascript:MapGooglePhotoWorldAlbumSetAlbumBounds();');

		WorldWide = " ["+PhotoXmlInfosList[0].childNodes[1].firstChild.data+"]";

		Element.appendChild(document.createTextNode(WorldWide));

		document.getElementById("photo_navigation").appendChild(Element);
	}

	document.getElementById("photo_navigation").appendChild(document.createTextNode(": "+InfoText));

	/*
	document.getElementById("photo_navigation").appendChild(document.createTextNode(InfoText + "."));

	if(PhotoXmlInfosList[0].childNodes[0].firstChild.data > MapPhotoMaxCount)
		document.getElementById("photo_navigation").appendChild(document.createTextNode(" " + MapInfoBrowseText + ":"));
	*/

	if(MapPhotoStartIndex > 0)
	{
		var PreviousLink = document.createElement("a");
			PreviousLink.setAttribute("href", "javascript:MapPhotoStartIndex -= MapPhotoMaxCount; MapGooglePhotoWorldMapRefresh();");
			PreviousLink.appendChild(document.createTextNode(" [" + MapInfoPreviousText + "] "));
		document.getElementById("photo_navigation").appendChild(PreviousLink);
	}

	if(MapPhotoMaxCount < PhotoXmlInfosList[0].childNodes[0].firstChild.data)
	//if(MapPhotoStartIndex + MapPhotoMaxCount < PhotoXmlInfosList[0].childNodes[0].firstChild.data)
	{
		var NextLink = document.createElement("a");
			NextLink.setAttribute("href", "javascript:MapPhotoStartIndex += MapPhotoMaxCount; MapGooglePhotoWorldMapRefresh();");
			NextLink.appendChild(document.createTextNode(" [" + MapInfoNextText + "] "));
		document.getElementById("photo_navigation").appendChild(NextLink);
		}

	PhotoXmlDataList = XmlData.getElementsByTagName("photo");

	/*
	TempIcon.image = "gfx/png/map_marker/pin.png";
	TempIcon.shadow = "gfx/png/map_marker/pin_shadow.png";
	TempIcon.iconSize = new GSize(14, 23);
	TempIcon.shadowSize = new GSize(14, 24);			
	TempIcon.iconAnchor = new GPoint(7, 23);	
	TempIcon.infoWindowAnchor = new GPoint(5, 1);

	TempFocusIcon.image = "gfx/png/map_marker/pin_large.png";
	TempFocusIcon.shadow = "gfx/png/map_marker/pin_large_shadow.png";
	TempFocusIcon.iconSize = new GSize(22, 37);
	TempFocusIcon.shadowSize = new GSize(22, 39);
	TempFocusIcon.iconAnchor = new GPoint(11, 37);	
	TempFocusIcon.infoWindowAnchor = new GPoint(9, 2);
	*/

	Map.clearOverlays();

	NumberOfChildren = document.getElementById("displayed_photos").childNodes.length;

	for(Index = 0; Index < NumberOfChildren; Index++)
		document.getElementById("displayed_photos").removeChild(document.getElementById("displayed_photos").firstChild);

	if(AlbumId || GroupId)
	{
		bounds = new GLatLngBounds();

		var points = XmlData.getElementsByTagName('coordinates').item(0);
		var pointsItems = points.getElementsByTagName('coordinate');

		for(var i = 0; i < pointsItems.length; i++)
		{	
			var pointsEntry = pointsItems.item(i);
			var pointsEntryLatitude = parseFloat(pointsEntry.getAttribute('lat').toString());
			var pointsEntryLongitude = parseFloat(pointsEntry.getAttribute('lng').toString());

			bounds.extend(new GLatLng(pointsEntryLatitude, pointsEntryLongitude));
		}
	}

	for(Index = 0; Index < PhotoXmlDataList.length; Index++)
	{
		PhotoIndex = parseInt(Index) + parseInt(StartIndex);
		Name = PhotoXmlDataList[Index].childNodes[0].firstChild.data;
		Id = PhotoXmlDataList[Index].childNodes[1].firstChild.data;
		FileName = PhotoXmlDataList[Index].childNodes[2].firstChild.data;
		Latitude = PhotoXmlDataList[Index].childNodes[3].firstChild.data;
		Longitude = PhotoXmlDataList[Index].childNodes[4].firstChild.data;
		UserName = PhotoXmlDataList[Index].childNodes[5].firstChild.data;
		Created = PhotoXmlDataList[Index].childNodes[6].firstChild.data;
		Submitted = PhotoXmlDataList[Index].childNodes[7].firstChild.data;
		UserId = PhotoXmlDataList[Index].childNodes[8].firstChild.data;
		//Caption = PhotoXmlDataList[Index].childNodes[9].firstChild.data;
		//City = PhotoXmlDataList[Index].childNodes[10].firstChild.data;
		//Country = PhotoXmlDataList[Index].childNodes[11].firstChild.data;

		ParentWidth = parseInt(document.getElementById("displayed_photos").style.width);
		Test = document.getElementById("square_frame");

		var PhotoImage = document.createElement("img");
			PhotoImage.myIndex = Index;
			PhotoImage.setAttribute("id", "photo_" + Index);
			PhotoImage.setAttribute("src", FileName);
			PhotoImage.style.left = ((Index * 102)+2) % ParentWidth+'px';
			PhotoImage.style.top = parseInt(Index / (ParentWidth / 102)) * 102+'px';
			PhotoImage.style.position = "absolute";
			PhotoImage.onmouseover = function() { if(typeof Map == 'object') {Map.removeOverlay(MapMarkerArray[this.myIndex]); Map.addOverlay(MapFocusMarkerArray[this.myIndex]);} };
			PhotoImage.onmouseout = function() { if(typeof Map == 'object') {Map.addOverlay(MapMarkerArray[this.myIndex]); Map.removeOverlay(MapFocusMarkerArray[this.myIndex]);} };

		var PhotoLink = document.createElement("a");
		if(AlbumId)
		{
			var AlbumIndex = parseInt(Index) + parseInt(document.getElementById('PhotoIndex').value);
			PhotoLink.setAttribute("href", "/photo_detail.php?id=" + Id + "&ref=11&index=" + AlbumIndex + "&album_id=" + AlbumId); // 11 = QUERY_ALBUMS
		} else
		if(GroupId)
		{	
			var GroupIndex = parseInt(Index) + parseInt(document.getElementById('PhotoIndex').value);
			PhotoLink.setAttribute("href", "/photo_detail.php?id=" + Id + "&ref=34&index=" + GroupIndex + "&group_id=" + GroupId); // 34 = QUERY_GROUP_PHOTOS
		} else
		{
			Koordinaten = "&latitude_min=" + LatitudeMin;
			Koordinaten+= "&latitude_max=" + LatitudeMax;
			Koordinaten+= "&longitude_min=" + LongitudeMin;
			Koordinaten+= "&longitude_max=" + LongitudeMax;
			PhotoLink.setAttribute("href", "/photo_detail.php?id=" + Id + "&ref=33&index=" + PhotoIndex + Koordinaten);
		}
		PhotoLink.setAttribute("title", "Submitted by " + UserName);
		PhotoLink.appendChild(PhotoImage);
		document.getElementById("displayed_photos").appendChild(PhotoLink);

		var FocusIcon = new MapIconObject();
		var Icon = new MapIconObject();
		MapFocusMarkerArray[Index] = new GMarker(new GLatLng(Latitude, Longitude), FocusIcon.GetMapIcon('photographer'));
		MapMarkerArray[Index] = MapGoogleCreateWorldMarker(new GLatLng(Latitude, Longitude), Name, FileName, Icon.GetMapIcon('PushpinSmall'));
		MapMarkerArray[Index].myIndex = Index;
		MapMarkerArray[Index].myLink = "/photo_detail.php?id=" + Id + "&ref=33&index=" + PhotoIndex + Koordinaten;
		GEvent.addDomListener(MapMarkerArray[Index], "mouseover", function() { document.getElementById("square_frame").style.left = document.getElementById("photo_" + this.myIndex).style.left; document.getElementById("square_frame").style.top = document.getElementById("photo_" + this.myIndex).style.top; document.getElementById("square_frame").style.visibility = "visible"; });
		GEvent.addDomListener(MapMarkerArray[Index], "mouseout", function() { document.getElementById("square_frame").style.visibility = "hidden"; });
		GEvent.addDomListener(MapMarkerArray[Index], "click", function() { window.location = this.myLink; });
		Map.addOverlay(MapMarkerArray[Index]);
	}

	if((AlbumId || GroupId ) && FirstCall)
	{
		FirstCall = false;
		if(Map.getBoundsZoomLevel(bounds) != 0)
			Map.setZoom(Map.getBoundsZoomLevel(bounds)-1);
		else
			Map.setZoom(Map.getBoundsZoomLevel(bounds));
		Map.setCenter(bounds.getCenter());
	}

	var PhotoFrame = document.createElement("img");
		PhotoFrame.myIndex = Index;
		PhotoFrame.setAttribute("id", "square_frame");
		PhotoFrame.setAttribute("src", "http://"+LOCR_DOMAIN+"/gfx/gif/transparent.gif");
		//PhotoFrame.setAttribute("src", "http://"+LOCR_DOMAIN+"/gfx/png/square_frame.png");
		PhotoFrame.style.left = 0;
		PhotoFrame.style.top = 0;
		PhotoFrame.style.position = "absolute";
		PhotoFrame.style.visibility = "hidden";
	document.getElementById("displayed_photos").appendChild(PhotoFrame);

	// alert(PhotoXmlDataList[0].childNodes[0].firstChild.data);
	//MapGooglePhotoWorldRequest = false;
}

function MapGooglePhotoWorldMapViewChanged()
{
	MapPhotoStartIndex = 0;
	MapGooglePhotoWorldMapRefresh();
}

function MapGooglePhotoWorldMapRefresh()
{
	Location = Map.getCenter();
	document.cookie = "world_map_latitude=" + Location.lat() + ";";
	document.cookie = "world_map_longitude=" + Location.lng() + ";";
	document.cookie = "world_map_zoom_level=" + Map.getZoom() + ";";
	document.cookie = "world_map_photo_index=" + MapPhotoStartIndex + ";";
	document.cookie = "world_map_photo_count=" + MapPhotoMaxCount + ";";

	Bounds = Map.getBounds();
	SouthWestLocation = Bounds.getSouthWest(); 
	NorthEastLocation = Bounds.getNorthEast(); 
	// MapGooglePhotoWorldStartRequest("/photo_world_xml.php?latitudemin=" + SouthWestLocation.lat() + "&longitudemin=" + SouthWestLocation.lng() + "&latitudemax=" + NorthEastLocation.lat() + "&longitudemax=" + NorthEastLocation.lng() + "&get_infos=1");

	if(AlbumId == 0)
		AlbumParam = '';
	else
		AlbumParam = '&album_id='+AlbumId;
	
	if(GroupId == 0)
		GroupParam = '';
	else
		GroupParam = '&group_id='+GroupId;

	// Layer

	Layer = "?layer=";

	CurrentZoomLevel = Map.getZoom();
	CurrentCenterLat = Map.getCenter().lat();
	CurrentCenterLng = Map.getCenter().lng();

	CurrentTileLayer = GetLayerAdress(CurrentCenterLng, CurrentCenterLat);

	Layer+= CurrentTileLayer;

	// Layer Ends
	var Url = "/photo_world_xml.php" + Layer + "&latitudemin=" + SouthWestLocation.lat() + "&longitudemin=" + SouthWestLocation.lng() + "&latitudemax=" + NorthEastLocation.lat() + "&longitudemax=" + NorthEastLocation.lng() + "&start=" + MapPhotoStartIndex + "&count=" + MapPhotoMaxCount + AlbumParam + GroupParam;
	var Request = new AjaxRequest();
		Request.Send(Url, {
			ajax_loader: false,
			request_name: 'photo_world_xml',
			cancel_same_named_requests: true,
			callback_parameters: {
				result_is_xml: true
			},
			callback: MapGooglePhotoWorldProcessData
		});

	// MapGooglePhotoWorldStartRequest("flickr_xml.php?latitudemin=" + SouthWestLocation.lat() + "&longitudemin=" + SouthWestLocation.lng() + "&latitudemax=" + NorthEastLocation.lat() + "&longitudemax=" + NorthEastLocation.lng());
	// alert("/photo_world_xml.php?latitudemin=" + SouthWestLocation.lat() + "&longitudemin=" + SouthWestLocation.lng() + "&latitudemax=" + NorthEastLocation.lat() + "&longitudemax=" + NorthEastLocation.lng() + "&start=0&count=16");
}

