var photos;

function destsearch(x1, x2, x3, y1, y2, y3){
	var latc = x1 + (x2/60) + (x3 / 60 / 60);
	var longc = y1 + (y2/60) + (y3 / 60 / 60);
	latc = (Math.round(latc * 1000000)/1000000);
	longc = (Math.round(longc * 1000000)/1000000);
	//var point = new GLatLng(latc, longc);
	var x = longc; //point.x;
	var y = latc; //point.y;
	var url = "http://www.panoramio.com/map/get_panoramas.php"
	  + "?order=popularity"
	  + "&set=public"
	  + "&from=0&to=21"
	  + "&minx=" + (x - 0.001)
	  + "&miny=" + (y - 0.001)
	  + "&maxx=" + (x + 0.001)
	  + "&maxy=" + (y + 0.001)
	  + "&callback=callback"
	  + "&size=small";

	var target = document.createElement('script');
	target.charset = 'ISO-8859-1'; //'utf-8';
	target.type = 'text/javascript';
	target.src = url;
	document.body.appendChild(target);
	//window.alert('search final');
}


function callback(panoramio) {

	photos = panoramio.photos;
	var divGalerie  = document.getElementById("galerie_foto_destinatie");
	var htmlGalerie = "<div>"; //class='picGalerieStatiune'
	
	var style = "position: relative; width: 104px; height: 104px; border: 0; align: left; ";
	var top = 0;
	var left = 0;
	for (var i = 0; i < photos.length; i++) {
		p = photos[i];
		if (parseInt(p.height) < 150) {
			continue;
		}
		
		if (i > 3 && (i % 4) == 0 ) {
			top =  0;
			left = 0;
			style = style + "top: " + top + "px; left: " + left + "px; "
		}
		
		htmlGalerie = htmlGalerie 
		+ "<a target='_blank' href='" + p.photo_url + "'>" 
		+ "<img style='" + style + "' src='" + p.photo_file_url 
			+ "' alt='" + p.photo_title + "' title='" + p.photo_title 
			+ "' > " 
		+ "<\/a>";
		
		
	}
	divGalerie.innerHTML = htmlGalerie + "<\/div>";
}//callback(panoramio)


function loadXMLDoc(fname)
{
  var xmlDoc;
  // code for IE
  if (window.ActiveXObject)
    {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
  // code for Mozilla, Firefox, Opera, etc.
  else if (document.implementation
  && document.implementation.createDocument)
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  else
    {
    alert('Your browser cannot handle this script');
    }
  xmlDoc.async=false;
  xmlDoc.load(fname);
  return(xmlDoc);
}










var photos;
var map = null;
var geocoder = null;


var iconBlue = new GIcon(); 
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);


function createMarker(point, name, address) {
    var marker = new GMarker(point, iconBlue);
    var html = "<b>" + name + "</b> <br/>" + address;
    GEvent.addListener(marker, 'click', function() {
      marker.openInfoWindowHtml(html);
    });
    return marker;
  }


function BackToCenterControl() {
}

BackToCenterControl.prototype = new GControl();

  // Creates a one DIV for each of the buttons and places them in a container
  // DIV which is returned as our control element. We add the control to
  // to the map container and return the element for the map class to
  // position properly.
  BackToCenterControl.prototype.initialize = function(map) {
      var container = document.createElement("div");

      var zoomInDiv = document.createElement("div");
      this.setButtonStyle_(zoomInDiv);
      container.appendChild(zoomInDiv);
      zoomInDiv.appendChild(document.createTextNode("Reseteaza"));
      GEvent.addDomListener(zoomInDiv, "click", function() {
        map.returnToSavedPosition();
      });

      map.getContainer().appendChild(container);
      return container;
}

// By default, the control will appear in the top left corner of the
// map with 7 pixels of padding.
BackToCenterControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 27));
}

// Sets the proper CSS for the given button element.
BackToCenterControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "none";
  button.style.color = "#000000";
  button.style.backgroundColor = "#BBBBBB";
  button.style.font = "small Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "6em";
  button.style.cursor = "pointer";
}



function search(x, y){
  var url = "http://www.panoramio.com/map/get_panoramas.php"
        + "?order=popularity"
        + "&set=public"
        + "&from=0&to=20"
        + "&minx=" + (x - 0.05)
        + "&miny=" + (y - 0.05)
        + "&maxx=" + (x + 0.05)
        + "&maxy=" + (y + 0.05)
        + "&callback=callback"
        + "&size=small";

  var target = document.createElement('script');
  target.charset = 'utf-8';
  target.type = 'text/javascript';
  target.src = url;
  document.body.appendChild(target);
  //window.alert('search final');
}


function myload(lat, m1, s1, long, m2, s2, labelText){
  var latc = lat + (m1/60) + (s1 / 60 / 60);
  var longc = long + (m2/60) + (s2 / 60 / 60);

  if ( latc <= 90 && longc <= 180 && latc >=0 && longc >= 0 )
  {
    // Rounding off
    latc = (Math.round(latc * 1000000)/1000000);
    longc = (Math.round(longc * 1000000)/1000000);

    if (GBrowserIsCompatible()) {
       map = new GMap2(document.getElementById("map")); // ?? var
      map.setCenter(new GLatLng(latc, longc), 15);
      map.setMapType(G_HYBRID_MAP);
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.addControl(new GOverviewMapControl());
      map.addControl(new BackToCenterControl());
      var point = new GLatLng(latc, longc);
      var marker = createMarker(point, labelText, labelText)
      map.addOverlay(marker);
      map.savePosition();
      search(longc, latc);
      /*
      geocoder = new GClientGeocoder();
      if (geocoder) {
          geocoder.getLatLng(
            address,
            function(point) {
              map.clearOverlays();
              map.setCenter(point, 14);


              search(point.x, point.y);
            }
          );
      }//if geocoder
      */

    }//if GBrowserIsCompatible

  }// if latc <= 90 && longc <= 180 ...

}//function myload



function toVideoGallery(videoXML, cssAdjustment) {

    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "hidden";
    mapDiv.innerHTML = "" 

    
    var multimediaDiv = document.getElementById("galerie_foto_destinatie");
    multimediaDiv.innerHTML = "" ;

    

    xml=loadXMLDoc(videoXML);
    xsl=loadXMLDoc("/test-video.xsl");  //http://www.miculparis.ro
    // code for IE
    if (window.ActiveXObject)
      {
	      if (cssAdjustment) {
		multimediaDiv.style.top = "-390px";
	      }
	      ex=xml.transformNode(xsl);
	      //document.getElementById("galerie_foto_destinatie").innerHTML=ex;
	      multimediaDiv.innerHTML=ex;
	      
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation
    && document.implementation.createDocument)
      {
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      resultDocument = xsltProcessor.transformToFragment(xml,document);
      multimediaDiv.appendChild(resultDocument);
      if (cssAdjustment) {
	multimediaDiv.style.top = "-510px";
      }
      }
    multimediaDiv.style.height = "700px";
    multimediaDiv.style.visibility = "visible";
}





function toMap(g, m, s, g1, m1, s1, nume) {

    var multimediaDiv = document.getElementById("galerie_foto_destinatie");
    multimediaDiv.innerHTML = "" ;    
    multimediaDiv.style.height = "1px";
    multimediaDiv.style.visibility = "hidden";

    var mapDiv = document.getElementById("map");
    if (window.ActiveXObject) {
    	mapDiv.style.top = "-550px";
    }
    mapDiv.style.visibility = "visible";

    myload(g, m, s, g1, m1, s1, nume);
}

function toPhotoGallery(g, m, s, g1, m1, s1) {

    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "hidden";

    var multimediaDiv = document.getElementById("galerie_foto_destinatie");

    multimediaDiv.style.visibility = "visible";
    multimediaDiv.innerHTML = "" ;

    destsearch(g, m, s, g1, m1, s1);
}




function afiseazaHartaYahoo(locatie, numeLocatie) {
     
    var multimediaDiv = document.getElementById("galerie_foto_destinatie");
    multimediaDiv.innerHTML = "" ;    
    multimediaDiv.style.height = "1px";
    multimediaDiv.style.visibility = "hidden";

    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "visible";
    
    startMap(locatie, numeLocatie);

}



function afiseazaComentarii(comentariiUrl) {

    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "hidden";
    mapDiv.innerHTML = "" 
    
    var multimediaDiv = document.getElementById("galerie_foto_destinatie");

    multimediaDiv.style.visibility = "visible";
    multimediaDiv.style.height = "700px";
    multimediaDiv.innerHTML = "" ;

    xml=loadXMLDoc(comentariiUrl);
    xsl=loadXMLDoc("/xsl/comentarii.xsl");  //http://www.miculparis.ro
    // code for IE
    if (window.ActiveXObject)
      {
      ex=xml.transformNode(xsl);
      document.getElementById("galerie_foto_destinatie").innerHTML=ex;
      }
    // code for Mozilla, Firefox, Opera, etc.
    else if (document.implementation
    && document.implementation.createDocument)
      {
      xsltProcessor=new XSLTProcessor();
      xsltProcessor.importStylesheet(xsl);
      resultDocument = xsltProcessor.transformToFragment(xml,document);
      multimediaDiv.appendChild(resultDocument);
      }

}




function startMap(locatie, numeLocatie){
     	 // Create a Map that will be placed in the "map" div.
     	 var map = new YMap(document.getElementById('map'));
    	 // Create an array to contain the points of our polyline
	 // Add the ability to change between Sat, Hybrid, and Regular Maps
	 map.addTypeControl();
	 // Add the zoom control. Long specifies a Slider versus a "+" and "-" zoom control
	 map.addZoomLong();
	 // Add the Pan control to have North, South, East and West directional control
	 map.addPanControl();
	 // Specifying the Map starting location and zoom level
	 map.drawZoomAndCenter(locatie, 3);
	 // Add an event to report to our Logger
	 var geoPointMap =  map.convertLatLonXY(map.getCenterLatLon()); //map.getCenterLatLon();
	 var mrk = new YMarker(geoPointMap);
	 mrk.addLabel(numeLocatie);
	 map.addMarker(mrk);
	 map.addOverlay(mrk);

	function showMyMarker(geocode_result){
		var myGeoPoint = geocode_result.GeoPoint;
		var myZoomLevel = 3;
		map.drawZoomAndCenter(myGeoPoint, myZoomLevel);
		map.addMarker(myGeoPoint);
	}
	
	YEvent.Capture(map, EventsList.onEndGeoCode, showMyMarker);

	var myAddress = locatie;
	map.geoCodeAddress(myAddress);
}
