/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var mapTool = new Object();
mapTool.i18n = {};
//mapTool.orig = {};
//mapTool.origFlag = true;

//this.prototype.clone = function () {var o = new Object(); for (var property in this) {o[property] = typeof (this[property]) == 'object' ? this[property].clone() : this[property]} return o}

mapTool.saveMapObject = function(){
    //console.info("mapTool.savemapObject... ");
   mapTool.mapObject = $('mapFlash');
}

/*
mapTool.setOrig = function(){
	mapTool.origFlag = false;
	mapTool.orig = window.mapTool.clone();
}

mapTool.getOrig = function(){
	return mapTool.origFlag;
}
*/
mapTool.onInit = function(text){
    //console.info("mapTool.onInit: "+text);
    mapTool.mapObject.getDescriptionTo('mapTool.setMapAreasDescription');
}

mapTool.setAPIKey = function(key){
    //console.info("mapTool.setAPIKey: "+key);
    mapTool.apiKey=key;
}

mapTool.configMapRecovery = function(hidden_field,text_div){
    //console.info("mapTool.configMapRecovery: "+hidden_field+" "+text_div);
    mapTool.hiddenField = hidden_field;
    mapTool.textDiv = text_div;
}

mapTool.setSvgShapes = function(svg_shapes){
    //console.info("mapTool.setSvgShapes: "+svg_shapes);
    mapTool.svg_shapes = svg_shapes;
}

mapTool.recoverMapAreas = function(){
    //console.info("mapTool.recoverMapAreas...");
    try{
    mapTool.mapObject.getSVGDataTo('mapTool.setMapAreas');
    }catch(e){/*console.info(e);*/}
    try{
    mapTool.mapObject.getDescriptionTo('mapTool.setMapAreasDescription');
    }catch(e){/*console.info(e);*/}
    return true;
}

mapTool.setMapAreas = function(svg){
  //console.info("mapTool.setMapAreas: "+svg);
  createMapCookie("map_search_svg", svg, 1);
  mapTool.hiddenField.value = svg;
}

mapTool.setMapAreasDescription = function(description){
  //console.info("mapTool.setMapAreasDescription: "+description);
  try{
      cookie = readMapCookie("map_search_svg");
      if (cookie != ''){
        $("search_svg").value = cookie;
      }
    
  }catch(e){
    //console.info("Excp: "+e);
  }
  var description_array = description.split(';');
  mapTool.textDiv.innerHTML = description_array.join('<br/>');
}

mapTool.insertI18NText = function(i18n_key,i18n_text)
{
    mapTool.i18n["i18n_" + i18n_key]= i18n_text
}

mapTool.insertI18NImg = function(i18n_key,i18n_location)
{
    mapTool.i18n["i18n_img_" + i18n_key]= i18n_location
}

mapTool.local = function()
{
    mapTool.local_url = true;
}

mapTool.search_external = function()
{
    //console.info('search_external');
    ClearAllDefault();
    eraseMapCookie("map_search_svg");
    mapTool.recoverMapAreas();
    $("properties_form").submit();
}

mapTool.safe_reset = function (place){

     //console.info("mapTool.safe_reset: "+place);

     var lang = (document.URL).match("pt");

     if(lang == "pt"){

         mapTool.orig["i18n_" + "top_bar_zoom"]= encodeURIComponent("ZOOM");
         mapTool.orig["i18n_" + "top_bar_go"]= encodeURIComponent("Ir directamente para");
         mapTool.orig["i18n_" + "top_bar_input"]= encodeURIComponent("Localidade ou Zona");
         mapTool.orig["i18n_" + "top_bar_pen"]= encodeURIComponent("CANETA");
         mapTool.orig["i18n_"+"top_bar_circle"]=encodeURIComponent("CÍRCULO");
         mapTool.orig["i18n_"+"top_bar_erase"]=encodeURIComponent("APAGAR");
         mapTool.orig["i18n_"+"tut_initial_01"]=encodeURIComponent("Faça Zoom de uma área utilizando");
         mapTool.orig["i18n_"+"tut_initial_02"]=encodeURIComponent("ou escrevendo a sua Morada, Cidade ou Região");
         mapTool.orig["i18n_"+"tut_initial_03"]=encodeURIComponent("Use a CANETA");
         mapTool.orig["i18n_"+"tut_initial_04"]=encodeURIComponent("ou o CÍRCULO");
         mapTool.orig["i18n_"+"tut_initial_05"]=encodeURIComponent("para desenhar a sua área de pesquisa");
         mapTool.orig["i18n_"+"tut_initial_06"]=encodeURIComponent("Quando terminar, clique em ");

         mapTool.orig["i18n_"+"tut_with_shapes_01"]=encodeURIComponent("Para criar mais formas, selecciona a caneta");
         mapTool.orig["i18n_"+"tut_with_shapes_02"]=encodeURIComponent("ou o círculo");
         mapTool.orig["i18n_"+"tut_with_shapes_03"]=encodeURIComponent("Utilize o apagador");
         mapTool.orig["i18n_"+"tut_with_shapes_04"]=encodeURIComponent("para remover uma forma.");

         mapTool.orig["i18n_"+"tut_pen_01"]=encodeURIComponent("CANETA");
         mapTool.orig["i18n_"+"tut_pen_02"]=encodeURIComponent("Clique no mapa para marcar pontos que delimitem a área de pesquisa.");
         mapTool.orig["i18n_"+"tut_pen_03"]=encodeURIComponent("É necessário definir pelo menos 3 pontos.");

         mapTool.orig["i18n_"+"tut_circle_01"]=encodeURIComponent("CÍRCULO");
         mapTool.orig["i18n_"+"tut_circle_02"]=encodeURIComponent("Clique e arraste o rato para criar um círculo em torno da área que deseja pesquisar.");

         mapTool.orig["i18n_"+"tut_erase_01"]=encodeURIComponent("APAGAR");
         mapTool.orig["i18n_"+"tut_erase_02"]=encodeURIComponent("Clique nas formas com o apagador para as remover");

         mapTool.orig["i18n_"+"map_type_road"]=encodeURIComponent("ESTRADAS");
         mapTool.orig["i18n_"+"map_type_aerial"]=encodeURIComponent("AÉREA");
         mapTool.orig["i18n_"+"map_type_hybrid"]=encodeURIComponent("HÍBRIDO");
         mapTool.orig["i18n_"+"map_type_terrain"]=encodeURIComponent("TERRENO");

         mapTool.orig["i18n_"+"map_address_error"]=encodeURIComponent("Formato de morada incompreensível. Por favor use o formato \"rua, cidade, região\" - certifique-se que usa uma vírgula para separar o endereço da cidade.");
         mapTool.orig["i18n_"+"map_zoom_warn"]=encodeURIComponent("Amplie mais para poder definir a sua área de pesquisa");

         mapTool.orig["i18n_img_"+"finish_shape"]=encodeURIComponent("/images/maptool/finish_shape_pt.gif");
         mapTool.orig["i18n_img_"+"cancel"]=encodeURIComponent("/images/maptool/cancel_pt.gif");
         mapTool.orig["i18n_img_"+"go"]=encodeURIComponent("/images/maptool/go_pt.gif");
         mapTool.orig["i18n_img_"+"search"]=encodeURIComponent("/images/search_button_pt.gif");

     }else{

	 mapTool.orig["i18n_" + "top_bar_zoom"]= encodeURIComponent("ZOOM");
	 mapTool.orig["i18n_" + "top_bar_go"]= encodeURIComponent("Go directly to");
	 mapTool.orig["i18n_" + "top_bar_input"]= encodeURIComponent("Address, City OR Region");
	 mapTool.orig["i18n_" + "top_bar_pen"]= encodeURIComponent("PEN");
	 mapTool.orig["i18n_"+"top_bar_circle"]=encodeURIComponent("CIRCLE");
	 mapTool.orig["i18n_"+"top_bar_erase"]=encodeURIComponent("ERASE");
	 mapTool.orig["i18n_"+"tut_initial_01"]=encodeURIComponent("Zoom in to an area using");
	 mapTool.orig["i18n_"+"tut_initial_02"]=encodeURIComponent("or typing in your Address, City or Region.");
	 mapTool.orig["i18n_"+"tut_initial_03"]=encodeURIComponent("Use the pen");
     mapTool.orig["i18n_"+"tut_initial_04"]=encodeURIComponent("or circle");
     mapTool.orig["i18n_"+"tut_initial_05"]=encodeURIComponent("to draw your search area");
     mapTool.orig["i18n_"+"tut_initial_06"]=encodeURIComponent("When you're done, click");

     mapTool.orig["i18n_"+"tut_with_shapes_01"]=encodeURIComponent("To create more shapes, select the pen");
     mapTool.orig["i18n_"+"tut_with_shapes_02"]=encodeURIComponent("or circle");
	 mapTool.orig["i18n_"+"tut_with_shapes_03"]=encodeURIComponent("Use the eraser");
     mapTool.orig["i18n_"+"tut_with_shapes_04"]=encodeURIComponent("to remove a shape.");

     mapTool.orig["i18n_"+"tut_pen_01"]=encodeURIComponent("PEN");
     mapTool.orig["i18n_"+"tut_pen_02"]=encodeURIComponent("Click on map to plot individual points that form a searchable shape.");
     mapTool.orig["i18n_"+"tut_pen_03"]=encodeURIComponent("You must click at least three times.");

     mapTool.orig["i18n_"+"tut_circle_01"]=encodeURIComponent("CIRCLE");
     mapTool.orig["i18n_"+"tut_circle_02"]=encodeURIComponent("Click and drag the mouse to create a circle around the area you like to seach.");

     mapTool.orig["i18n_"+"tut_erase_01"]=encodeURIComponent("ERASER");
     mapTool.orig["i18n_"+"tut_erase_02"]=encodeURIComponent("Click on shapes with eraser to remove a shape.");

     mapTool.orig["i18n_"+"map_type_road"]=encodeURIComponent("ROAD");
     mapTool.orig["i18n_"+"map_type_aerial"]=encodeURIComponent("AERIAL");
     mapTool.orig["i18n_"+"map_type_hybrid"]=encodeURIComponent("HYBRID");
     mapTool.orig["i18n_"+"map_type_terrain"]=encodeURIComponent("TERRAIN");

	 mapTool.orig["i18n_"+"map_address_error"]=encodeURIComponent("Could not understand address format. Please try using the \"street,city name, region\" format -- make sure to use commas to separate your address and city.");
     mapTool.orig["i18n_"+"map_zoom_warn"]=encodeURIComponent("Zoom in further to enable the drawing tools");

	 mapTool.orig["i18n_img_"+"finish_shape"]=encodeURIComponent("/images/maptool/finish_shape.gif");
	 mapTool.orig["i18n_img_"+"cancel"]=encodeURIComponent("/images/maptool/cancel.gif");
     mapTool.orig["i18n_img_"+"go"]=encodeURIComponent("/images/maptool/go.gif");
     mapTool.orig["i18n_img_"+"search"]=encodeURIComponent("/images/search_button.gif");
     }

	var flashvars = mapTool.orig;
	var params ={};
	var attributes = {};
	attributes.id = "mapFlash";
	attributes.style = "z-index: -1; z-order: 1;";
	attributes.name = "mapFlash";
	params.wmode =  "Transparent";//"Opaque";

	params.menu =  "false";
	flashvars.map_key=mapTool.apiKey;
	flashvars.on_init = "mapTool.onInit";
	flashvars.external_search = "mapTool.search_external";

	if(mapTool.svg_shapes)
	{
		flashvars.svg_shapes = mapTool.svg_shapes;
	}

	if(mapTool.local_url)
	{
		flashvars.local_url = mapTool.local_url;
	}
	swfobject.embedSWF("/swf/MapTool.swf", place, "100%", "100%", "9.0.0","/swf/expressInstall.swf", flashvars, params, attributes);
	swfobject.addDomLoadEvent(mapTool.saveMapObject);
}

mapTool.insertMapTool = function (place)
{
  //console.info("mapTool.insertMapTool: "+place);
  var flashvars = mapTool.i18n;
  var params ={};
  var attributes = {};
  attributes.id = "mapFlash";
  attributes.style = "z-index: -1; z-order: 1;";
  attributes.name = "mapFlash";
  params.wmode =  "Transparent";//"Opaque";
  //params.scale="noborder";
  params.menu =  "false";

  flashvars.map_key=mapTool.apiKey;
  flashvars.on_init = "mapTool.onInit";
  flashvars.external_search = "mapTool.search_external";
  if(mapTool.svg_shapes)
  {
    flashvars.svg_shapes = mapTool.svg_shapes;
  }
  //console.info(mapTool.svg_shapes);
   if(mapTool.local_url)
  {
    flashvars.local_url = mapTool.local_url;
  }
  ////console.info(mapTool.local_url);
  ////console.info("/swf/MapTool.swf");
  ////console.info(place+"100%"+"100%"+"9.0.0"+"/swf/expressInstall.swf");
  //for (var v in flashvars) {
  //  //console.info(v);
  //}

  //for (var v in params) {
  //  //console.info(v);
  //}
  
  //for (var v in attributes) {
  //  //console.info(v);
  //}

  swfobject.embedSWF("/swf/MapTool.swf", place, "100%", "100%", "9.0.0","/swf/expressInstall.swf", flashvars, params, attributes);
  swfobject.addDomLoadEvent(mapTool.saveMapObject);
}