var photos;


function Ajax (url, parms, method, callback) {

    this.url = url;
    this.parms = parms;
    this.method = method;
    this.callback = callback;
    this.async = true;

    this.create ();

    this.req.onreadystatechange = this.dispatch (this);
}

Ajax.prototype.dispatch = function (ajax) {

    function funcRef()
    {
        if (ajax.req.readyState == 4) {
            if (ajax.callback) {
                ajax.callback (ajax.req);
            }
        }
    }

    return funcRef;
}

Ajax.prototype.request = function () {

    if (this.method == "POST") {
        this.req.open("POST", this.url, this.async);
        this.req.send (this.parms);
    }
    else if (this.method == "GET") {
        this.req.open("GET", this.url + this.parms, this.async);
        this.req.send (null);
    }

}

Ajax.prototype.setAsync = function (async) {

    this.async = async;
}

Ajax.prototype.create = function () {

    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)

    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E) {
            xmlhttp = false;
        }
    }

    @else

    xmlhttp = false;

    @end @*/

    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }

    this.req = xmlhttp;
}

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)












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) {

    ascundeToateDiv();

    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 divOther = document.getElementById("comentarii");
    divOther.style.visibility = "hidden";
*/

	ascundeToateDiv();

    var mapDiv = document.getElementById("map");
    if (window.ActiveXObject) {
    	mapDiv.style.top = "-770";
    }
    mapDiv.style.top = "-700px";
    mapDiv.style.visibility = "visible";

    myload(g, m, s, g1, m1, s1, nume);
}


function toComments(comentariiUrl) {
/*
    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 = "hidden";
    mapDiv.innerHTML = "" 
*/

    ascundeToateDiv();


    var commDiv = document.getElementById("divComentMare");
    if (window.ActiveXObject) {
    	commDiv.style.top = "-1330px";
    }
    commDiv.style.top = "-1230px";
    commDiv.style.visibility = "visible";
    
    //document.getElementById("comentarii")
    afiseazaComentarii(comentariiUrl);
}


function toPhotoGallery(g, m, s, g1, m1, s1) {

/*
    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "hidden";
*/

	ascundeToateDiv();

    var multimediaDiv = document.getElementById("galerie_foto_destinatie");
    multimediaDiv.style.visibility = "visible";
    multimediaDiv.innerHTML = "" ;
    destsearch(g, m, s, g1, m1, s1);
}




function afiseazaHartaYahoo(locatie, numeLocatie) {
    ascundeToateDiv();
    var mapDiv = document.getElementById("map");
    mapDiv.style.visibility = "visible";
    startMap(locatie, numeLocatie);
}





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);
}


function salveazaComentariu (nume, email, site, comentarii, numeFisier) {


	if (!verifUserComent(nume, email, site, comentarii)) {
		return;
	}

	if (!jcap()) {
		return;
	}
	


	if (window.XMLHttpRequest)
	  {
	  // code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp =  new XMLHttpRequest();
	  }
	if (window.ActiveXObject)
	  {
	  // code for IE6, IE5
	  xmlhttp =  new ActiveXObject("Microsoft.XMLHTTP");
	  }

	if (xmlhttp==null)
	  {
		 alert ("Your browser does not support XMLHTTP!");
		  return;
	  }

	var url="/php/adaugaComentariu.php5";
	url = url + "?nume=" + nume;
	url = url + "&email=" + email;	
	url = url + "&site=" + comentarii;
	url = url + "&comentarii=" + comentarii;
	url = url + "&numeFisier=" + numeFisier;
	

	xmlhttp.onreadystatechange = commentStateChanged;
	xmlhttp.open("GET", url, true);
	xmlhttp.send(null);

}



function commentStateChanged()
{
	if (xmlhttp.readyState==4)
	  {
		 document.comentariu.reset();
		 document.getElementById("divComentRasp").innerHTML = xmlhttp.responseText;

	  }
}



function afiseazaComentarii (fisierXMLComentarii) {
    var divLista = document.getElementById('divListaComent');
    divLista.style.visibility = "visible";
    document.getElementById('divComentRasp').style.visibility = "visible";
    
    if (divLista.innerHTML == "") {
	    xml = loadXMLDoc(fisierXMLComentarii);

	    xsl = loadXMLDoc("/xslget/lista-comentarii.xsl");

	    if (window.ActiveXObject)
	      {
			var ex = xml.transformNode(xsl);
			divLista.innerHTML = ex;

	      } else if (document.implementation && document.implementation.createDocument) {
			xsltProcessor=new XSLTProcessor();
			xsltProcessor.importStylesheet(xsl);
			resultDocument = xsltProcessor.transformToFragment(xml, document);
			divLista.appendChild(resultDocument);
	    }
    }
}


function ascundeToateDiv () {
	var divGeneric = document.getElementById('divListaComent');
        if (divGeneric != null)
	divGeneric.style.visibility = "hidden";

	divGeneric = document.getElementById('galerie_foto_destinatie');
	if (divGeneric != null)
        divGeneric.style.visibility = "hidden";


	divGeneric = document.getElementById('map');
	if (divGeneric != null) {
            divGeneric.style.visibility = "hidden";
            divGeneric.innerHTML = "";
        }

	divGeneric = document.getElementById('divComentMare');
	if (divGeneric != null)
        divGeneric.style.visibility = "hidden";

	divGeneric = document.getElementById('divComentRasp');
	if (divGeneric != null)
        divGeneric.style.visibility = "hidden";

	divGeneric = document.getElementById('divListaComent');
	if (divGeneric != null)
        divGeneric.style.visibility = "hidden";
}


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);
}


/*
    window.alert(fisierXMLComentarii);
    window.alert(div_name);
    // Get the XML file from the server.

    var ajax = new Ajax (fisierXMLComentarii, "", "GET", null);
    ajax.setAsync (false);
    ajax.request ();
    var xml_doc = ajax.req.responseXML;

    // Get the XSLT from the server.

    ajax = new Ajax ("/xsl/lista-comentarii.xsl", "", "GET", null);
    ajax.setAsync (false);
    ajax.request ();
    var xsl_doc = ajax.req.responseXML;

    var div = document.getElementById (div_name);

    // Use object detection to find out if we have
    // Firefox/Mozilla/Opera or IE XSLT support.

    if (typeof XSLTProcessor != "undefined") {
        var xsl_proc = new XSLTProcessor ();
        xsl_proc.importStylesheet (xsl_doc);
        var node = xsl_proc.transformToFragment (xml_doc, document);

        div.innerHTML = "";
        div.appendChild (node);
    }
    else if (typeof xml_doc.transformNode != "undefined") {
        div.innerHTML = xsl_proc.transformNode (xsl_doc);
    }
    else {
        div.innerHTML = "XSLT not supported in browser.";
    }
    
 */

/*
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);
      }

}




*/