/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
var statusText=null;
var latlng = null;
var europe = new google.maps.LatLng(51.151786,10.415039);
var counter = 0; 
var warnColor = 1;
var warnMarker = null;
var map = null;
var geocoder = new google.maps.Geocoder();
var markLocation = "";
var arSplit;
var arResult = new Array();
var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
var start = null;
var contentString ="";
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function initialize(portal, lang, zoomView, arMarker) { 
	document.getElementById("start").value="";
	try {
		var temp = document.getElementById("directionsPanel").firstChild;
		document.getElementById("directionsPanel").removeChild(temp);
		window.clearInterval(warnMarker);
	} catch(e){
		//do nothing
	}	
	if(portal='Europe') {
		query = europe 
	}
	else {
		query = europe;
	}
    var myOptions = {
      zoom: zoomView,
      center: query,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      disableDefaultUI: true,
      navigationControl: true,
      navigationControlOptions: {
      	  style: google.maps.NavigationControlStyle.SMALL
      }
    };
    
    map = new google.maps.Map(document.getElementById("map"), myOptions);

	/*
	0:<xsl:value-of select="$gm_country" />
	1:<xsl:value-of select="$gm_national_code" />
	2:<xsl:value-of select="$gm_town" />
	3:<xsl:value-of select="$gm_street" />
	4:<xsl:value-of select="$gm_company" />
	5:<xsl:value-of select="$gm_latlang" />
	6:<xsl:value-of select="$gm_homepage" />
	7:3
	8:<xsl:value-of select="$gm_street" />
	*/
    for(i = 1; i < arMarker.length; i++) {
    	//alert(arMarker[i]);
		var strStreet2 ="";
		var strStreet2Formatted = "";
    	arSplit = String(arMarker[i]).split("##");
		if(arSplit[8] != "") {
			strStreet2 = ", "+arSplit[8];
			strStreet2Formatted = "<br />"+arSplit[8];
		}
		else {
			strStreet2="";
			strStreet2Formatted = "";
		}
    	strLocation = arSplit[4]+", "+arSplit[0]+", "+arSplit[1]+" "+arSplit[2]+", "+arSplit[3]+strStreet2;
    	strFormattedLocation = "<h2>"+arSplit[4]+"</h2><br />"+arSplit[3]+strStreet2Formatted+"<br />"+arSplit[1]+" "+arSplit[2]+"<br />"+arSplit[0]+"<br /><a href=\""+arSplit[6]+"\" target=\"_blank\" >"+arSplit[6]+"</a>";
    	latlang = doLatLang(arSplit[5]);
		//alert(arSplit);
    	setMarker(latlang, strLocation, strFormattedLocation, i);
    	
    }// endfor
  }
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function setMarker(coords, titles, infos, pos) {
	var marker = new google.maps.Marker({
		map: map, 
		position: coords,
		title: unescapeEntities(titles),
		icon: 'http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/marker'+i+'.png'
	}); 
	google.maps.event.addListener(marker, 'click', function() {
			setInfoWindow(marker, infos);
			infoWindow.open(map,marker);
		});		
	//setInfoWindow(marker, infos);
	
	
	google.maps.event.addListener(marker, 'click', function() {
		jQuery(function($) {
		var p = $("#address");
		var t = pos * 108 - 108;
				//$(p).stop().scrollTo({top:'+=100px', left:'0'}, {duration: 800, axis:"y"});
				$(p).stop().scrollTo( { top:t, left:0}, 800 );
			});
	});
	
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function setInfoWindow(marker, infos) {
	contentString = '<div class="infoBubble">'+
		'<div class="bubbleNotice"></div>'+
		'<div class="bubbleContent">'+
			infos
		'</div>'+
		'</div>';
		if(!infoWindow) {
			infoWindow = new google.maps.InfoWindow({
					content: contentString
			});
		}
		else {
			infoWindow.setContent(contentString);
		}
		
		/*
		google.maps.event.addListener(marker, 'click', function() {
			infoWindow.open(map,marker);
		});
		*/
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function findRoute(strCoords, portal, lang, zoomView, arMarker) {
	objStart = document.getElementById("start");
	objStatus = document.getElementById("status");
	start = objStart.value;
	directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById("directionsPanel"));
    clearStatus();
	if(start.length > 0) {
		var end = doLatLang(strCoords);
		//set the destination to a Address-String, not to Coords, because calculation might be incorrect by using Coords
		var request = {
			origin:start, 
			destination:unescapeEntities(portal),
			travelMode: google.maps.DirectionsTravelMode.DRIVING
		  };
		  directionsService.route(request, function(response, status) {
			if (status == google.maps.DirectionsStatus.OK) {
			  directionsDisplay.setDirections(response);
			}
			else{
				//statusText = document.createTextNode(start+" "+status);
				//objStatus.appendChild(statusText);
				checkInput(start, "hint3");
				//alert("not found "+status);
			}
		  });
	}
	else {
		//warnMarker = window.setInterval("alterColor()", 150);
		
	}
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function checkInput(strTxt, numb) {
	clearStatus();
	/*
	if(document.getElementById("start").value == "" || numb == 13) {
		statusText = document.createTextNode(strTxt);
		document.getElementById("status").appendChild(statusText);
		document.getElementById("routing").style.color="#cd1543";
		document.getElementById("start").style.borderColor="#cd1543";
	}
	*/
	//alert(strTxt);
	
	if(document.getElementById("start").value == "" && strTxt == "hint1") {
		var hint = document.getElementById("hint1");
		hint.style.display="inline";
		hint.style.visibility="visible";
	}
	if(document.getElementById("start").value != "" && numb == 13) {
		var hint = document.getElementById("hint2");
		hint.style.display="inline";
		hint.style.visibility="visible";
	}
	if(numb == "hint3") {
		var hint = document.getElementById("hint3");
		hint.style.display="inline";
		hint.style.visibility="visible";
	}	
	if(document.getElementById("start").value == "" && numb == 13) {
		var hint = document.getElementById("hint4");
		hint.style.display="inline";
		hint.style.visibility="visible";	
	}	
	
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function clearStatus() {
	try {
		document.getElementById("status").removeChild(statusText);
		document.getElementById("routing").style.color="#000000";
		document.getElementById("start").style.borderColor="#959595";
	} catch(e) {
		//do nothing
	}
	document.getElementById("hint1").style.display="none";
	document.getElementById("hint1").style.visibility="hidden";
	document.getElementById("hint2").style.display="none";
	document.getElementById("hint2").style.visibility="hidden";
	document.getElementById("hint3").style.display="none";
	document.getElementById("hint3").style.visibility="hidden";
	document.getElementById("hint4").style.display="none";
	document.getElementById("hint4").style.visibility="hidden";		
	
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function alterColor () {
	if (warnColor == 1) {
		document.getElementById("routing").style.color="#ffffff";
		document.getElementById("start").style.borderColor="#ffffff";
		warnColor = 2;
	} else {
		document.getElementById("routing").style.color="#cd1543";
		document.getElementById("start").style.borderColor="#cd1543";
		warnColor = 1;
	}
	counter++;
	if (counter >= 10) {
		window.clearInterval(warnMarker);

		counter = 0;
	}	
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function alterColorReset() {
	document.getElementById("routing").style.color="#000000";
	document.getElementById("start").style.borderColor="#959595";
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function doLatLang(str) {
	lat=str.substring(0,str.indexOf(','));
	lang=str.substring(str.indexOf(',')+1);
	latlang = new google.maps.LatLng(Number(lat,10), Number(lang,10));
	return latlang;
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function zoomLocation(lang, zoomView, coords) {
	latlang = doLatLang(coords);
	map.setCenter(latlang);
	map.setZoom(zoomView);
  }
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function cssForPrint() {
	if(document.createStyleSheet) {
	  document.createStyleSheet('/systemstatic/_master/gm_printview.css');
	}
	else {
	  var styles = "/systemstatic/_master/gm_printview.css";
	  var newSS=document.createElement('link');
	  newSS.rel='stylesheet';
	  newSS.href=styles;
	  newSS.type='text/css'; 
	  newSS.id='forPrint';
	  document.getElementsByTagName("head")[0].appendChild(newSS);
	}
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function closeView() {
	try {
		location.reload(true);
		//var tmp = document.getElementById("forPrint");
		//alert(tmp.id);
		//document.getElementsByTagName("head")[0].removeChild(tmp);
	} catch(e) {
		//do nothing
		//alert("e: "+e);
	}
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function warMsg(numb) {
	if(numb == 13){
	
	}
}

/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function showCoords() {

  var geocoder1;
  var map1;
  
    geocoder1 = new google.maps.Geocoder();
    var latlng1 = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng1,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map1 = new google.maps.Map(document.getElementById("map"), myOptions);
  


    var address = document.getElementById("findCoords").value;
    if (geocoder) {
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map1.setCenter(results[0].geometry.location);
          var marker1 = new google.maps.Marker({
              map: map1, 
              position: results[0].geometry.location
          });
          
          alert(results[0].geometry.location);
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
}
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* */
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
function unescapeEntities(strStr) {
	var temp=document.createElement("pre");
	temp.innerHTML=strStr;
	return temp.firstChild.nodeValue;	
}

