jQuery.fn.extend({
	silGmap: function(options) {
		return this.each(function() {
			new jQuery.silGmap(this, options);
		});
	}
});

jQuery.silGmap = function(obj, opt) {
  //Opcje ogólne
  var opt = opt || {};
  //Opcje główne x,y,zoom, key,description, route
  if(!opt.main) opt.main = {};
  opt.main.x = opt.main.x || 50.317408;
  opt.main.y = opt.main.y || 18.676758;
  opt.main.route = opt.main.route || 0;
  opt.main.zoom = opt.main.zoom || 13;
  opt.main.description = opt.main.description || "";
  

  //Pozostałe punkty na mapie
  opt.otherPoints = opt.otherPoints || [];


  //Pobierz główny punkt
  var $main_div = $(obj);
  
  var gdir, map , control;
  var main_div_id = $main_div.attr("id");
  

  function main() {
    if(GBrowserIsCompatible()) {
      if(opt.main.route == 1) {
        $main_div.before("<div id=\"wrap_1_" + main_div_id + "\" align=\"center\"><div id=\"miasto_" + main_div_id + "\">Miasto</div><div id=\"ulica_" + main_div_id + "\">Ulica</div><div id=\"nr_domu_" + main_div_id + "\">Nr domu</div></div><div id=\"select_" + main_div_id + "\">Punkt docelowy</div></div>");
        $main_div.before("<div id=\"wrap_2_" + main_div_id + "\" align=\"center\"><div><input type=\"text\" size=\"20\" id=\"fromAddress_1_" + main_div_id + "\" class=\"form\"/></div><div><input type=\"text\" size=\"25\" id=\"fromAddress_2_" + main_div_id + "\" class=\"form\"/></div><div><input type=\"text\" size=\"15\" id=\"fromAddress_3_" + main_div_id + "\" class=\"form\"/></div><div><select id=\"fromAddress_4_" + main_div_id + "\" class=\"form\"/></select></div><input id=\"button_1" + main_div_id + "\" name=\"submit\" type=\"button\" value=\"Wyznacz trasę\"/></div>");
        $main_div.before("<div id=\"silGMap_Error_" + main_div_id + "\"></div>");
        $main_div.after("<div align=\"center\" id=\"silGmap_Route_" + main_div_id + "\" style=\"display:none\"></div>");
        document.getElementById("silGmap_Route_" + main_div_id).innerHTML = "";
 	     $("#button_1" + main_div_id).click(function() {
          setDirections(document.getElementById('fromAddress_1_' + main_div_id).value + ', ' + document.getElementById('fromAddress_2_' + main_div_id).value + ' ' + document.getElementById('fromAddress_3_' + main_div_id).value);
        });
      }//if(opt.main.route == 1) {
      
      map = new GMap2(document.getElementById(main_div_id));

      map.addControl(new GMapTypeControl());
      map.addControl(new GLargeMapControl());
      map.removeMapType(G_HYBRID_MAP);
      if(opt.main.route == 1) {
        gdir = new GDirections(map, document.getElementById("silGmap_Route_" + main_div_id));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
      }//if(opt.main.route == 1) {
      
      //Wycentruj i wstaw marker
      point = new GLatLng(opt.main.x, opt.main.y);
      map.setCenter(point, opt.main.zoom);
      var marker = new GMarker(point);
      map.addOverlay(marker);
      if(opt.main.description)
        if(opt.main.description!='') {
          marker.openInfoWindowHtml(opt.main.description);
          marker.bindInfoWindowHtml(opt.main.description);
      }//if(opt.main.description) if(opt.main.description!='') {

          
      //Wstaw pozostałe markery
      if(opt.otherPoints.length>=1) for(i=0;i<opt.otherPoints.length;i++) {
        var point = new GLatLng(opt.otherPoints[i].x , opt.otherPoints[i].y);
        marker = new GMarker(point);
        map.addOverlay(marker);
        
        if(opt.otherPoints[i].description)
          if(opt.otherPoints[i].description!='') {
            marker.bindInfoWindowHtml(opt.otherPoints[i].description);
        }//if(opt.otherPoints[i].description) if(opt.otherPoints[i].description!='') {
      }//if(opt.otherPoints.length) for(i=0;i<otherPoints.length;i++) {
    }//if(GBrowserIsCompatible()) {
  }//function main() {
  



  function setDirections(fromAddress) {
    //if(opt.main.route) {
    //  opt.googleLatLng = $("#fromAddress_4_" + main_div_id).val();
    //  gdir.load("from: " + fromAddress + " to: @" + opt.googleLatLng);
    //}
    //else {
      gdir.load("from: " + fromAddress + " to: @" + opt.main.x + ", " + opt.main.y);
    //}
    document.getElementById("silGmap_Route_" + main_div_id).style.display = 'block';
    map.setCenter(new GLatLng(opt.main.x, opt.main.y), opt.main.zoom);
  }//function setDirections(fromAddress) {

  function onGDirectionsLoad() {
    $("#silGMap_Error_" + main_div_id).hide(0);
  }

  function handleErrors() {
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS){
      $("#silGMap_Error_" + main_div_id).html("Nie znaleziono zadanej lokalizacji.<br/>Wskazówka: Szukając opisz dokładnie lokalizację, np: Warszawa, Mickiewicza 30.");
      $("#silGMap_Error_" + main_div_id).slideDown(1000);
    }
    else if(gdir.getStatus().code == G_GEO_SERVER_ERROR)
      alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if(gdir.getStatus().code == G_GEO_MISSING_QUERY)
      alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if(gdir.getStatus().code == G_GEO_BAD_KEY)
      alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if(gdir.getStatus().code == G_GEO_BAD_REQUEST)
      alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else {
      document.getElementById("silGMap_Error_" + main_div_id).innerHTML = "Zadany punkt wyjściowy znajduje poza zasięgiem systemu.";
      $("#silGMap_Error_" + main_div_id).slideDown(1000);
    }
  }


  //Funkcja główna
  main();
}//jQuery.silGmap = function(obj, opt) {

