/*

 * Copyright (c) 2007 DataGis GmbH

 *

 * @author Achim Gerhardt

 *

 */

var map;

var mapItemListGeocode;

var mapItemListMain;



var highlightedMarker = null;

var myShow = null;



var geocoder;

var geocoderCache;

var initialBounds;



function load() {

  if (GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("map"));

    map.addControl(new GLargeMapControl());

    map.addControl(new GMapTypeControl());

    map.addControl(new GOverviewMapControl());

    map.enableScrollWheelZoom();




 

            // Setup the options for our map layer

            var GMapTypeOptions = new Object();

            GMapTypeOptions.minResolution = 8;

            GMapTypeOptions.maxResolution = 14;

            GMapTypeOptions.errorMessage = "No map data available";



 

    map.setCenter(new GLatLng(39.9600, 4.1190),10);

    initialBounds = map.getBounds();



    mapItemListGeocode = new MapItemManager("mapItemListGeocode", map);

    mapItemListMain = new MapItemManager("mapItemListMain", map);



    loadMenu(0);

    initGeocoder();

  }

};



    
function initGeocoder() {

	geocoderCache = new GFactualGeocodeCache();

	geocoder = new GClientGeocoder(geocoderCache);

	geocoder.setBaseCountryCode("ES");

};



function geocode() {

  geocoder.getLocations(document.forms[0].address.value, addAddressToMap);

};



function addAddressToMap(response) {

  mapItemListGeocode.clearMarkers();



  var geocoderesults = document.getElementById("geocoderesults");

  removeChildren(geocoderesults);

  if (!response || response.Status.code != 200) {

    alert("Sorry, we were unable to geocode that address");

  } else {

    var first = true;

  	for(i = 0; i < response.Placemark.length; i++) {

      place = response.Placemark[i];

      point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

  		if(initialBounds.containsLatLng(point)) {



        var mapItem = mapItemListGeocode.createItem();

        mapItem.heading = place.address;

        mapItem.point = point;



        var newLi = createListElementGeocode(mapItem);

      	geocoderesults.appendChild(newLi);



      	var marker = mapItemListGeocode.createMarker(mapItem, mapItem.heading, "marker1");

      	

      	if(first == true) {

      		marker.openInfoWindowHtml(place.address);

      	}

      	first = false;

  		}

  	}

  }

};



function loadMenu(id) {

  var navigationContainer = document.getElementById("Navigation1");

  var navigation = document.getElementById("naviList");

  removeChildren(navigationContainer);

  removeChildren(navigation);

  mapItemListMain.clearMarkers();



  var request = GXmlHttp.create();



  var requestString = "data.php?catid=" + id;

  if(pageLanguage != null) {

  	requestString += "&lang=" + pageLanguage;

  }

  

  request.open("GET", requestString, true);

  request.onreadystatechange = function() {

    if (request.readyState == 4) {

      var xmlDoc = request.responseXML;

      var categories = xmlDoc.documentElement.getElementsByTagName("category");



      for (var i = 0; i < categories.length; i++) {

      	var category = categories[i];

      	var newLi = document.createElement("li");

      	var newLink = document.createElement("a");

      	

      	var descriptionText = category.getElementsByTagName("description")[0].firstChild.nodeValue;

      	var LiText = document.createTextNode(descriptionText);

      	newLink.appendChild(LiText);

      	

      	var attHref = document.createAttribute("href");

      	attHref.nodeValue = "javascript:loadMenu(" + categories[i].getAttribute("id") + ");menuTrail.addMenuTrail(" + categories[i].getAttribute("id") + ",'" + descriptionText + "');menuTrail.renderMenuTrail();";

      	newLink.setAttributeNode(attHref);

      	

      	newLi.appendChild(newLink);

      	navigation.appendChild(newLi);

      }



      categories = xmlDoc.documentElement.getElementsByTagName("feature");

      for (var i = 0; i < categories.length; i++) {

      	var category = categories[i];



        var mapItem = mapItemListMain.createItem();

        mapItem.heading = category.getElementsByTagName("description")[0].firstChild.nodeValue;

        mapItem.text = category.getElementsByTagName("info1")[0].firstChild.nodeValue;

        mapItem.shortDescription = shortDescriptionText = category.getElementsByTagName("shortdescription")[0].firstChild.nodeValue;

        

      	var pointNode = category.getElementsByTagName("point")[0];

      	var point = new GLatLng(parseFloat(pointNode.getAttribute("y")), parseFloat(pointNode.getAttribute("x")));

        mapItem.point = point;



      	var newLi = createListElementMapItem(mapItem);

      	navigation.appendChild(newLi);



      	var imageHtml = "";

      	var markerHtml;

      	var imageName = null;

      	for(x = 0; x < category.getElementsByTagName("image").length; x++) {

      		imageName = category.getElementsByTagName("image")[x].firstChild.nodeValue;

      	  mapItem.images.push(imageName);

      	  if(x == 0) {

      	    mapItem.thumbImage = "pic/thumbs/" + imageName;

      	    imageHtml = "<a href=\"javascript:mapItemListMain.showImage(" + mapItem.id + ");\" style=\"margin-bottom:3px;border-width:0px;\"><img src='" + mapItem.thumbImage + "' height=\"60\" style=\"float:left;margin-right:5px;border-width:0px;\"/></a>";

      	  }

      	}

        var shortDescriptionText = 'NULL';

      	if(category.getElementsByTagName("shortdescription")[0].firstChild != null) {

      	  shortDescriptionText = category.getElementsByTagName("shortdescription")[0].firstChild.nodeValue;

        }

      	var descText = "";

      	if(mapItem.shortDescription == 'NULL') {

      		descText = mapItem.text;

      	} else {

      		descText = mapItem.shortDescription;

      	}

      	if(category.getElementsByTagName("info2")[0].firstChild != null) {

      	  mapItem.openingHours = category.getElementsByTagName("info2")[0].firstChild.nodeValue;

        }

      	if(category.getElementsByTagName("info3")[0].firstChild != null) {

      	  mapItem.admission = category.getElementsByTagName("info3")[0].firstChild.nodeValue;

        }

      	if(category.getElementsByTagName("web")[0].firstChild != null) {

      	  mapItem.webUrl = category.getElementsByTagName("web")[0].firstChild.nodeValue;

        }

      	var markerHtml = "<div style=\"width:250px;padding:5px;font-size:0.8em;overflow:hidden;\"><div style=\"margin-bottom:3px;padding:0px;\"><b>" + mapItem.heading + "</b></div>" + imageHtml + descText + " <nobr><a href=\"javascript:mapItemListMain.showImage(" + mapItem.id + ");\" style=\"margin-bottom:3px;\">" + moreText + "</a></nobr>" + "</div>";



      	var imageType = null;

      	if(id == 11 ||id == 12 || id == 13 || id == 39 || id == 48 || id == 50 || id == 51 || id == 53) {

      		imageType = id + "";

      	}

      	mapItemListMain.createMarker(mapItem, markerHtml, imageType);

      }

      navigationContainer.appendChild(navigation);

    }

  }

  request.send(null);

};



function createListElementMapItem(mapItem) {

	

	var htmlId = mapItem.htmlId;

	var newLi = document.createElement("li");

	

	var newLink = document.createElement("a");

  var LiText = document.createTextNode(mapItem.heading);

  newLink.appendChild(LiText);

  var attHref = document.createAttribute("href");

  attHref.nodeValue = "javascript:highlightMarker('" + mapItem.htmlId + "');mapItemListMain.openMarker(" + mapItem.id + ")";

  newLink.setAttributeNode(attHref);

  var attHref = document.createAttribute("id");

  attHref.nodeValue = htmlId;

  newLink.setAttributeNode(attHref);

  newLi.appendChild(newLink);



	return newLi;

};



function createListElementGeocode(mapItem) {

	

	var htmlId = mapItem.htmlId;

	var newLi = document.createElement("li");

	var newLink = document.createElement("a");

  var LiText = document.createTextNode(mapItem.heading);

  newLink.appendChild(LiText);

  var attHref = document.createAttribute("href");

  attHref.nodeValue = "javascript:highlightMarker('" + mapItem.htmlId + "');mapItemListGeocode.openMarker(" + mapItem.id + ")";

  newLink.setAttributeNode(attHref);

  var attHref = document.createAttribute("id");

  attHref.nodeValue = htmlId;

  newLink.setAttributeNode(attHref);

  newLi.appendChild(newLink);



	return newLi;

};



function removeChildren(elem) {

  var childNode = elem.firstChild;

  while(childNode != null) {

    var tempChild = childNode;

    childNode = tempChild.nextSibling;

    elem.removeChild(tempChild);

  }

};



function MapItem() {

	this.id;

	this.htmlId;

	this.heading;

	this.shortDescription;

	this.text;

	this.openingHours;

	this.admission;

	this.webUrl;

	this.images = new Array();

	this.thumbImage;

	this.marker;

};



function MapItemManager(mapItemManagerId, map) {

	this.id = mapItemManagerId;

	this.map = map;

	this.mapItems = new Array();

};



MapItemManager.prototype.createItem = function() {

	var mapItem = new MapItem();

	mapItem.id = this.mapItems.length;

	mapItem.htmlId = this.id + "_" + this.mapItems.length;

	return mapItem;

};



MapItemManager.prototype.showImage = function(textindex) {



  slideShow(this.mapItems[textindex].images);



  document.getElementById("imageHeading").innerHTML = this.mapItems[textindex].heading;

  var text = this.mapItems[textindex].text;

  if(this.mapItems[textindex].openingHours != null) {

  	text += this.mapItems[textindex].openingHours;

  }

  if(this.mapItems[textindex].admission != null) {

  	text += this.mapItems[textindex].admission;

  }

  if(this.mapItems[textindex].webUrl != null) {

  	text += "<p><a target=\"_blank\" href=\"" + this.mapItems[textindex].webUrl + "\">" + this.mapItems[textindex].webUrl + "</a></p>" ;

  }

  document.getElementById("desctext").innerHTML = text;

  document.getElementById("imageViewerDiv").style.display = "";

  document.getElementById("darkBackgroundLayer").style.display = "";

};



MapItemManager.prototype.hideImage = function() {

  document.getElementById("darkBackgroundLayer").style.display = "none";

  document.getElementById("imageViewerDiv").style.display = "none";

  removeChildren(document.getElementById("desctext"));

  document.getElementById("photo").src = "images/placeholder.gif";

};



MapItemManager.prototype.createMarker = function(mapItem, label, iconType) {

	var marker = null;

	if(iconType != null) {

	  marker = new GMarker(mapItem.point, { icon: this.getIcon(iconType) });

  } else {

	  marker = new GMarker(mapItem.point);

  }

  mapItem.marker = marker;

	var text = "";

	if(mapItem.text == 'undefined') {

	  text = label;

	} else {

	  text =  "<p>" + mapItem.shortDescription + "</p><p>" + mapItem.text + "</p>";

	}

	mapItem.text = text;

	GEvent.addListener(marker, "click", function() {

    marker.openInfoWindowHtml(label, {maxHeight:150,autoScroll:true});

  });

  this.mapItems.push(mapItem);

  this.map.addOverlay(marker);



	return marker;

};



MapItemManager.prototype.getIcon = function(type) {

  

  var icon = new GIcon();

  icon.image = "images/" + type + ".png";

  icon.iconAnchor = new GPoint(4, 30);

  icon.infoWindowAnchor = new GPoint(16, 10);

  icon.iconSize = new GSize(32, 32);

  icon.shadow = "images/shadow.png";

    //  + IMAGES[i] + "-shadow.png";

  icon.shadowSize = new GSize(32, 32);



  return icon;

};



MapItemManager.prototype.clearMarkers = function() {

	for(i = 0; i < this.mapItems.length; i++) {

		this.map.removeOverlay(this.mapItems[i].marker);

	}

  this.mapItems.splice(0, this.mapItems.length -1);

  highlightedMarker = null;

};



MapItemManager.prototype.openMarker = function(i) {

	this.map.panTo(this.mapItems[i].marker.getPoint());

	GEvent.trigger(this.mapItems[i].marker, "click");

};



function highlightMarker(id) {

	if(highlightedMarker != null) {

    highlightedMarker.setAttribute("style", "");

	}

  highlightedMarker = document.getElementById(id);

  highlightedMarker.setAttribute("style", "font-weight:bold;background-color:#eee;color:black;");

};



var imageArray2;

var currentImageIndex = 0;



function slideShow(imagePathArray) {

	imageArray2 = new Array(imagePathArray.length);

	for(i = 0;i < imagePathArray.length; i++) {

	 imageArray2[i] = new Image();

	 imageArray2[i].onload = function() {

	 };

	 imageArray2[i].src = "pic/" + imagePathArray[i];

	}

  currentImageIndex = 0;

  document.getElementById("photo").onload = function() {

    if(imageArray2[currentImageIndex] != null) {

      document.getElementById("photo").width = imageArray2[currentImageIndex].width;

      document.getElementById("photo").height = imageArray2[currentImageIndex].height;

    }

  };

  if(imageArray2.length == 0) {

    document.getElementById("my_slideshow").style.display = "none";

    document.getElementById("imageNavigation").style.display = "none";

  } else if(imageArray2.length == 1) {

    document.getElementById("my_slideshow").style.display = "";

    document.getElementById("imageNavigation").style.display = "none";

    document.getElementById("photo").src = imageArray2[0].src;

  } else {

    document.getElementById("my_slideshow").style.display = "";

    document.getElementById("imageNavigation").style.display = "";

    document.getElementById("photo").src = imageArray2[0].src;

  }

}



function nextImage() {

  var index = (currentImageIndex + 1) % imageArray2.length;

  currentImageIndex = index;

  document.getElementById("photo").src = imageArray2[index].src;

};



function previousImage() {

  var index = ((currentImageIndex - 1) + imageArray2.length) % imageArray2.length;

  currentImageIndex = index;

  document.getElementById("photo").src = imageArray2[index].src;

};



/*

 *  Manager for the menu trail

 */

function MenuTrailManager(menuTrailId) {

	this.id = menuTrailId;

	this.menuTrail = new Array();

};



MenuTrailManager.prototype.addMenuTrail = function(id, name) {

  var menuItem = new MenuTrail();

  menuItem.id = id;

  menuItem.name = name;

  this.menuTrail.push(menuItem);

};



MenuTrailManager.prototype.removeMenuTrail = function(id) {

	for(i = this.menuTrail.length - 1; i >= 0; i--) {

		if(this.menuTrail[i].id == id) {

  		this.menuTrail.splice(i + 1, (this.menuTrail.length - (i + 1)));

		}

	}

};



MenuTrailManager.prototype.renderMenuTrail = function() {

	var naviHeaderDiv = document.getElementById("naviHeader");

	removeChildren(naviHeaderDiv);

	for(i = 0; i < this.menuTrail.length; i++) {

		if(i < this.menuTrail.length - 1) {

		  naviHeaderDiv.appendChild(this.createLinkNode(i));

		  naviHeaderDiv.appendChild(document.createElement("br"));

		  naviHeaderDiv.appendChild(document.createTextNode(" > "));

		} else {

			var spanElem = document.createElement("span");

      var classHref = document.createAttribute("class");

      classHref.nodeValue = "naviList";

      spanElem.setAttributeNode(classHref);

			spanElem.appendChild(document.createTextNode(this.menuTrail[i].name));

			naviHeaderDiv.appendChild(spanElem);

		}

	}

};



MenuTrailManager.prototype.createLinkNode = function(mapItemId) {

 	var newLink = document.createElement("a");

 	var LiText = document.createTextNode(this.menuTrail[mapItemId].name);

 	newLink.appendChild(LiText);

 	var attHref = document.createAttribute("href");

 	attHref.nodeValue = "javascript:loadMenu(" + this.menuTrail[mapItemId].id + ");" + this.id + ".removeMenuTrail(" + this.menuTrail[mapItemId].id + ");" + this.id + ".renderMenuTrail();";

 	newLink.setAttributeNode(attHref);



  var attClass = document.createAttribute("class");

 	attClass.nodeValue = "naviList";

 	newLink.setAttributeNode(attClass);

 	

 	return newLink;

};



function MenuTrail() {

	var id;

	var name;

};



/*

 *  TabManager

 */

function TabManager(tabManagerId) {

	this.tabManagerId = tabManagerId;

	this.tabs = new Array();

};



TabManager.prototype.addTab = function(tab) {

	this.tabs.push(tab);

};



TabManager.prototype.initTabs = function() {

      	

  for(i = 0; i < this.tabs.length; i++) {

		var header = document.getElementById(this.tabs[i].tabHeaderId);

		var tabDiv = document.getElementById(this.tabs[i].tabId);

		var tabHeaderClass = "";

		if(i == 0) {

			tabDiv.style.display = "";

			tabHeaderClass = "tab_active";

		} else {

			tabDiv.style.display = "none";

			tabHeaderClass = "tab";

		}

    header.innerHTML = "<div id=\"" + this.tabManagerId + "_" + this.tabs[i].tabHeaderId + "\" class=\"" + tabHeaderClass + "\"><a href=\"javascript:" + this.tabManagerId + ".activateTab('" + this.tabs[i].tabId + "');\" class=\"tab_link\">" + this.tabs[i].headerText + "</a></div>";

	}

};





TabManager.prototype.activateTab = function(tab) {

	for(i = 0; i < this.tabs.length; i++) {

		var header = document.getElementById(this.tabManagerId + "_" + this.tabs[i].tabHeaderId);

		var tabDiv = document.getElementById(this.tabs[i].tabId);

		if(this.tabs[i].tabId == tab) {

			tabDiv.style.display = "";

			header.className = "tab_active";

		} else {

			tabDiv.style.display = "none";

			header.className = "tab";

		}

	}

};



function Tab(headerId, tabId, headerText_) {

	this.tabHeaderId = headerId;

	this.tabId = tabId;

	this.headerText = headerText_;

}
