/**
 * Copyright (c) 2008 Google Inc.
 *
 * You are free to copy and use this sample.
 * License can be found here: http://code.google.com/apis/ajaxsearch/faq/#license
*/

window._uds_msw_donotrepair = true;
google.load("jqueryui", "1.7.2");

function tailAddToCart ( ajaxCallTail ){
	if (ajaxCallTail.length > 0) {
		var data = ajaxCallTail.pop()
		$.ajax({
			url: "/ajax-add-carrito.php",
			type: "POST",
			data: (data),
			dataType: "xml",
			success: function(){
				tailAddToCart(ajaxCallTail);
			}
		});
	}else{
		$('#availableDomains').empty();
		$('#addToCartButton').hide();
		$('.TitDomDisp').remove();
		$('#availableDomains').append('<div class="addToCartFinish">Los dominios seleccionados fueron agregados a su carro de compras. Siga con el paso 3 para registrarlos.</div>')
		$("#detalle_compras").show();
		$('#RegistrarBT').show();
		misComprasHandler.refresh();
		SacarTapa('md_app_container');
	}
}


$(document).ready(function(){
						   
						   
	$('#md_carro_compras_list').append($("#detalle_compras"));
	//$("#detalle_compras").hide();
	
	$('.md_help_step').tooltip({ 
		track: true,
		delay: 0,
		bodyHandler: function() {
			return $('.'+$(this).attr("id")).html();
		},
		showURL: false
	});
	
	$('#md_help_video').click(function(event){	
		firstTracker._trackEvent('UA_MapaDominios', 'overlay', 'Video "Como usar el mapa de dominios" pagina: Mapa de Dominios');
		$("#modal_video_tutorial").modal();
		event.preventDefault();
	});
	
	$('#RegistrarBT').click(function(){	
		firstTracker._trackEvent('UA_MapaDominios', 'click_int', 'Registrar - Paso 3');
	});
	
	$('.md_compartir_link').click(function(){	
		firstTracker._trackEvent('UA_MapaDominios', 'click_ext', 'Link Compartir Mapa Dominios ('+$(this).attr('title')+')');
	});
	
	var options = {
		zoomControl : GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL,
		showResultList : true,
		title : "dattatec.com",
		url : "http://datattec.com",
		idleMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM,
		activeMapZoom : GSmapSearchControl.ACTIVE_MAP_ZOOM
	}
	$("#traskBox").droppable({
		tolerance: 'pointer',
		hoverClass: 'ui-state-hover',
		activeClass: 'ui-state-active',
		drop: function(event, ui) {
			$(ui.draggable[0]).remove();
			if (jQuery.browser.msie) {
				ui.draggable[0].id = 'deleteME';
				var value = $(ui.draggable[0]).val();
				setTimeout("$('#deleteME').remove();", 10);
			}
			return true;
		}
	});
	
	switch(codPaisActual){
		case'ar':
			firstSearch = 'Buenos Aires Argentina';
		break;
		case'bo':
			firstSearch = 'La Paz Bolivia';
		break;
		case'br':
			firstSearch = 'Sao Paulo Brasil';
		break;
		case'cl':
			firstSearch = 'Santiago Chile';
		break;
		case'co':
			firstSearch = 'Bogota Colombia';
		break;
		case'es':
			firstSearch = 'Madrit España';
		break;
		case'us':
			firstSearch = 'New York USA';
		break;
		case'int':
			firstSearch = 'New York USA';
		break;
		case'mx':
			firstSearch = 'Distrito Federal Mexico';
		break;
		case'pe':
			firstSearch = 'Quito Peru';
		break;
		case'uy':
			firstSearch = 'Montevideo Uruguay';
		break;
		case've':
			firstSearch = 'Caracas Venezuela';
		break;
		default:
			firstSearch = 'Buenos Aires Argentina';
		break;
	}
	new GSmapSearchControl(
		document.getElementById("mapsearch"),
		firstSearch,
		options
	);

	$('#changeDomain').click(function(){
		var str = '';
		var ulWords = document.getElementById('sortableKey');
		$('input:[checked]',ulWords).each(function(){str+=this.value;});
		if(str!=''){
			//nodeForm.src= 'http://dattatec.com/fireDomain/forms/'+str.toLowerCase()+'.com.html';
			$('#availableDomains').empty();
			$('#addToCartButton').hide();
			$('.TitDomDisp').remove();
			$('#preload').show();
			$.ajax({
				type: "GET",
				url: 'http://dattatec.com/fireDomain/xml/'+str.toLowerCase()+'.com.xml',
				dataType:'xml',
				success: function(xmlResponse){
					$('#preload').hide();
					if($('error',xmlResponse).size() == 1){
						$('#availableDomains').append('<div class="error">'+$('error',xmlResponse).text()+'</div>');
					}else{
						$('#availableDomains').append('<div id="ListDomDisp" style="display:none;"></div>');
						var domainCount = 0;
						$('domain',xmlResponse).each(function(){
							if($(this).attr('available')==1){
								domainCount++;
								var tld = $(this).attr('tld');
								var domain = $(this).attr('idna');
								var producto = $(this).attr('plan');
								$('#ListDomDisp').append('<label class="ItemAvailableLabel"><input class="ItemAvailableChk" type="checkbox" domain="'+domain+'" tld="'+tld+'" producto="'+producto+'" />'+$(this).text()+'</label>');
							}
						});
						if(domainCount > 0){
							$('#availableDomains').before('<h3 class="TitDomDisp">Dominios Disponibles</h3>');
							$('#ListDomDisp').after('<a href="#" id="SelectAllLink">Seleccionar todos</a>');
							$('#ListDomDisp').show();
							$('#SelectAllLink').click(function(event){
								$('.ItemAvailableChk').attr("checked","true");
								event.preventDefault();
							});
							$('#addToCartButton').show();
						}
						if(domainCount == 0){
							$('#availableDomains').append('<div class="error">No hay dominios disponibles con el término </strong>&quot;'+str.toLowerCase()+'&quot;</strong>. Seleccione otro de los puntos del mapa o vuelva al paso 2 e intente con otras combinaciones.</div>');
						}
					}
				}
			});
		}
	});
	
						   

	$('#newKeyWordButton').click(function(){
		var newKeyWord = $('#newKeyWord').val();
		if(newKeyWord !=''){
			$('#traskBox').show();			
			newKeyWord = newKeyWord.replace(/[\s]+/,'').toUpperCase();
			var liWord = document.createElement('li');
			liWord.className = 'ui-state-default';
			liWord.innerHTML = '<input class ="domainKey" type="checkbox" checked="true" value="'+newKeyWord+'"/>'+newKeyWord;
			$('input.domainKey',liWord	).change(function(){
				var str = '';
				$('input.domainKey:[checked]').each(function(){str+=this.value;});
				$('#newDomain').text(str);
			});
			ulWords.appendChild(liWord);
			$('#newKeyWord').val('');
			var str = '';
			$('input.domainKey:[checked]').each(function(){str+=this.value;});
			$('#newDomain').text(str);
		}
	});
	$('#newKeyWord').keyup(function(event){
		if (event.keyCode == 13) {
			$('#newKeyWordButton').trigger('click');

		}
	});
});



GSmapSearchControl.ACTIVE_MAP_ZOOM = 14;
GSmapSearchControl.IDLE_MAP_ZOOM = 11;
GSmapSearchControl.MAP_TYPE_ENABLE_ALL = 1;
GSmapSearchControl.MAP_TYPE_ENABLE_ACTIVE = 2;
GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL = 1;
GSmapSearchControl.DEFAULT_RESULT_LIST = 1;
GSmapSearchControl.DRIVING_DIRECTIONS_FROM_CENTER = 1;
GSmapSearchControl.DRIVING_DIRECTIONS_FROM_USER = 2;
function GSmapSearchControl(container, mapCenter, opt_options) {
  this.letteredIconMode = true;
  this.userSelectedDirectionsMode = false;
  this.onBootCompleteHandler = null;

  this.enableResultsContainer = false;
  this.enableIdleZoomControl = false;
  this.enableActiveZoomControl = true;
  this.enableIdleMapType = false;
  this.enableActiveMapType = false;
  this.root = container;
  this.linkTarget = GSearch.LINK_TARGET_BLANK;
  this.ads = null;

  this.buildContainerGuts();

  this.activeMapZoom = GSmapSearchControl.ACTIVE_MAP_ZOOM;
  this.idleMapZoom = GSmapSearchControl.IDLE_MAP_ZOOM;

  // result scroller
  this.currentResultIndex = 0;
  this.markers = new Array();
  this.idle = true;

  // bind up the controls
  this.searchForm.input.onclick = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.onPreActive, []);
  this.searchForm.input.onfocus = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.onPreActive, []);
  $(this.searchForm.input).tooltip({ 
		track: true,
		delay: 0,
		bodyHandler: function() {
			return $('.md_help_step_4').html();
		},
		showURL: false
	});
  this.searchForm.setOnSubmitCallback(this,GSmapSearchControl.prototype.formSubmit);

  this.cancel.onclick = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.goIdle, []);
  this.next.onclick = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.onNext, []);
  this.prev.onclick = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.onPrev, []);

  // build the icons
  this.selectedIcon = new GIcon();
  this.selectedIcon.image = "http://www.google.com/mapfiles/gadget/markerSmall80.png";
  this.selectedIcon.shadow = "http://www.google.com/mapfiles/gadget/shadow50Small80.png";
  this.selectedIcon.iconSize = new GSize(16, 27);
  this.selectedIcon.shadowSize = new GSize(30, 28);
  this.selectedIcon.iconAnchor = new GPoint(8, 27);
  this.selectedIcon.infoWindowAnchor = new GPoint(5, 1);

  this.unselectedIcon = new GIcon(this.selectedIcon);
  this.unselectedIcon.image = "http://www.google.com/mapfiles/gadget/markerSmall80.png";

  this.centerIcon = new GIcon(this.selectedIcon);
  this.centerIcon.image = "http://www.google.com/mapfiles/gadget/arrowSmall80.png";
  this.centerIcon.shadow = "http://www.google.com/mapfiles/gadget/arrowshadowSmall80.png";;
  this.centerIcon.iconSize = new GSize(31, 27);
  this.centerIcon.shadowSize = new GSize(31, 27);
  this.centerIcon.iconAnchor = new GPoint(8, 27);
  this.centerIcon.infoWindowAnchor = new GPoint(5, 1);

  this.letteredIcons = new Array();
  for ( var i=0; i < 16; i++ ) {
    var icon = new GIcon(this.selectedIcon);
    var iconImageKey =
    icon.image = "http://www.google.com/mapfiles/gadget/letters/marker" +
                      String.fromCharCode(65+i) + ".png";
    this.letteredIcons[i] = icon;
  }

  this.mapCenterString = null;
  this.titleOverride = null;

  // handle title and url
  if (opt_options) {
    var title = null;
    var url = null;

    if (opt_options.linkTarget) {
      this.linkTarget = opt_options.linkTarget;
    }

    if (opt_options.title) {
      title = opt_options.title;
    }
    if (opt_options.url) {
      url = opt_options.url;
    }
    if (url && title) {
      var div = GSmapsc_createDiv(null,"gsmsc-user-title");
      var link = GSmapsc_createLink(url, title, this.linkTarget, "gsmsc-user-title");
      div.appendChild(link);
      this.titleOverride = div;
    }

    if (opt_options.hotspots) {
      // whip through the hotspots and bind away...
      for (var i=0; i < opt_options.hotspots.length; i++) {
        var hs = opt_options.hotspots[i];
        hs.element.onclick = GSmapsc_methodClosure(this, GSmapSearchControl.prototype.newSearch, [hs.query]);
      }
    }

    // allow custom center marker
    if (opt_options.centerIcon) {
      this.centerIcon = opt_options.centerIcon;
    }

    // allow custom selected marker
    if (opt_options.selectedIcon) {
      this.selectedIcon = opt_options.selectedIcon;
      this.letteredIconMode = false;
    }

    // allow custom unselected marker
    if (opt_options.unselectedIcon) {
      this.unselectedIcon = opt_options.unselectedIcon;
      this.letteredIconMode = false;
    }

    // allow zoom level specification
    if (opt_options.idleMapZoom) {
      this.idleMapZoom = opt_options.idleMapZoom;
    }

    if (opt_options.activeMapZoom) {
      this.activeMapZoom = opt_options.activeMapZoom;
    }

    if (opt_options.mapTypeControl) {
      if (opt_options.mapTypeControl == GSmapSearchControl.MAP_TYPE_ENABLE_ALL) {
        this.enableIdleMapType = true;
        this.enableActiveMapType = true;
      } else if (opt_options.mapTypeControl == GSmapSearchControl.MAP_TYPE_ENABLE_ACTIVE) {
        this.enableIdleMapType = false;
        this.enableActiveMapType = true;
      } else {
        this.enableIdleMapType = false;
        this.enableActiveMapType = false;
      }
    }

    if (opt_options.zoomControl) {
      if (opt_options.zoomControl == GSmapSearchControl.ZOOM_CONTROL_ENABLE_ALL) {
        this.enableIdleZoomControl = true;
        this.enableActiveZoomControl = true;
      }
    }

    // ads array
    if (opt_options.ads) {
      this.ads = opt_options.ads;
    }

    // results list
    if (opt_options.showResultList) {
      this.resultsList = GSmapsc_createDiv(null, "gsmsc-resultsBox");
      if (opt_options.showResultList == GSmapSearchControl.DEFAULT_RESULT_LIST ) {
        this.appContainer.appendChild(this.resultsList);
      } else {
        GSmapsc_removeChildren(opt_options.showResultList);
        opt_options.showResultList.appendChild(this.resultsList);
      }
    }

    // driving directions
    if (opt_options.drivingDirections) {
      if (opt_options.drivingDirections ==
          GSmapSearchControl.DRIVING_DIRECTIONS_FROM_USER) {
        this.userSelectedDirectionsMode = true;
      } else if (opt_options.drivingDirections ==
                 GSmapSearchControl.DRIVING_DIRECTIONS_FROM_CENTER) {
        this.userSelectedDirectionsMode = false;
      } else {
        this.userSelectedDirectionsMode = false;
      }
    }

  }

  // onBootComplete
  if (opt_options.onBootComplete) {
    this.onBootCompleteHandler = opt_options.onBootComplete;
  }
  // be kind, since typically a lot is going on at load...
  this.startupTimer = setTimeout(GSmapsc_methodClosure(this, GSmapSearchControl.prototype.deferLoad, [mapCenter]), 100);
}

GSmapSearchControl.prototype.deferLoad = function(mapCenter) {
  // allow mapCenter to be a string or a GLatLng
  if ( mapCenter && mapCenter.lat && mapCenter.lng ) {
    var lat;
    var lng;
    if (typeof mapCenter.lat == "function") {
      lat = mapCenter.lat();
    } else {
      lat = mapCenter.lat;
    }
    if (typeof mapCenter.lng == "function" ) {
      lng = mapCenter.lng();
    } else {
      lng = mapCenter.lng;
    }
    this.mapCenterString = lat + ", " + lng;
    this.mapCenter = new GLatLng(lat, lng);
    this.bootComplete(null);
  } else {
    // use ajax search based geocode
    this.mapCenter = null;
    this.mapCenterString = mapCenter;
    var gsLookup = new GlocalSearch();
    gsLookup.setSearchCompleteCallback(this, GSmapSearchControl.prototype.bootComplete, [gsLookup]);
    gsLookup.execute(mapCenter);
  }
}

GSmapSearchControl.prototype.buildContainerGuts = function() {
  // build out the map divs and search form

  GSmapsc_removeChildren(this.root);
  this.appContainer = GSmapsc_createDiv(null, "gsmsc-appContainer");

  this.resultsList = null;

  // active map div
  this.mapDiv = GSmapsc_createDiv(null, "gsmsc-mapDiv");

  this.appContainer.appendChild(this.mapDiv);

  this.attributionDiv = GSmapsc_createDiv(null, "gsmsc-attributionDiv");
  this.appContainer.appendChild(this.attributionDiv);

  // idle map div
  this.idleMapDiv = GSmapsc_createDiv(null, "gsmsc-idleMapDiv");

  this.appContainer.appendChild(this.idleMapDiv);

  var div = GSmapsc_createDiv(null, "gsmsc-controls");
  this.searchForm = new GSearchForm(false, div);

  // controls
  this.prevNext = GSmapsc_createDiv(null, "gsmsc-prevNext");
  this.prev = GSmapsc_createDiv(null, "gsmsc-prev");
  this.cancel = GSmapsc_createDiv(null, "gsmsc-cancel");
  this.next = GSmapsc_createDiv(null, "gsmsc-next");
  this.tooltip = GSmapsc_createDiv(GSearch.strings["scroll-results"],
                                   "gsmsc-tooltip");
  this.prev.innerHTML = "&nbsp;";
  this.cancel.innerHTML = "&nbsp;";
  this.next.innerHTML = "&nbsp;";
  this.prev.title = GSearch.strings["previous"];
  this.cancel.title = GSearch.strings["clear-results"];
  this.next.title = GSearch.strings["next"];
  this.prevNext.appendChild(this.prev);
  this.prevNext.appendChild(this.cancel);
  this.prevNext.appendChild(this.next);
  this.prevNext.appendChild(this.tooltip);
  this.searchForm.userDefinedCell.appendChild(this.prevNext);

  this.appContainer.appendChild(div);

  this.root.appendChild(this.appContainer);
}

// finish boot process by creating and centering our map(s)
GSmapSearchControl.prototype.bootComplete = function(gs) {
  if ( gs && gs.results && gs.results.length > 0 ) {
    // extract map center from first search result
    this.mapCenter = new GLatLng(parseFloat(gs.results[0].lat),
                                 parseFloat(gs.results[0].lng));
    var res0 = gs.results[0];
    var resHtml = res0.html.cloneNode(true);
    var div;
    if (this.titleOverride) {
      div = GSmapsc_createDiv(null, "gsmsc-user-title gsmsc-result-wrapper-user-selected-directions");
      div.appendChild(this.titleOverride);
    } else {
      div = GSmapsc_createDiv(null, "gsmsc-result-wrapper-user-selected-directions");
    }
    div.appendChild(resHtml);
    this.mapCenterHtml = div;
  } else if (this.mapCenter == null) {
    this.mapCenter = new GLatLng(37.421947, -122.084391);
    this.mapCenterString = this.mapCenter.lat() + ", " + this.mapCenter.lng();
  }

  // if we have no html, make it
  if (!this.mapCenterHtml) {
    var div;
    if (this.titleOverride) {
      div = GSmapsc_createDiv(null, "gsmsc-user-title");
      div.appendChild(this.titleOverride);
    } else {
      div = GSmapsc_createDiv(this.mapCenterString, "gsmsc-user-title");
    }
    // from user-prompt To Here
    var toUrl = "http://www.google.com/maps?source=uds&daddr=" +
      this.mapCenterString + "&iwstate1=dir:to";
    var fromUrl = "http://www.google.com/maps?source=uds&saddr=" +
      this.mapCenterString + "&iwstate1=dir:from";
    var dwrap = GSmapsc_createDiv(null, "gsmsc-directions-wrapper");
    var ditem = GSmapsc_createDiv(GSearch.strings["get-directions"]+":",
                                  "gsmsc-directions-label");
    dwrap.appendChild(ditem);

    // to here
    var ditem = GSmapsc_createLink(toUrl, GSearch.strings["to-here"], this.linkTarget,
                                   "gsmsc-directions-link");
    dwrap.appendChild(ditem);

    // spacer
    var ditem = GSmapsc_createDiv("-", "gsmsc-directions-spacer");
    dwrap.appendChild(ditem);

    // from here
    var ditem = GSmapsc_createLink(fromUrl, GSearch.strings["from-here"], this.linkTarget,
                                   "gsmsc-directions-link");
    dwrap.appendChild(ditem);

    div.appendChild(dwrap);
    this.mapCenterHtml = div;
  }

  // create the maps and add conditional controls
  this.gmap = new GMap2(this.mapDiv);
  if (this.enableActiveZoomControl) {
    this.gmap.addControl(new GSmallMapControl());
  }
  if (this.enableActiveMapType) {
    this.gmap.addControl(new GMapTypeControl());
  }
  this.gmap.setCenter(this.mapCenter, this.activeMapZoom);

  this.idleGmap = new GMap2(this.idleMapDiv);
  if (this.enableIdleZoomControl) {
    this.idleGmap.addControl(new GSmallMapControl());
  }
  if (this.enableIdleMapType) {
    this.idleGmap.addControl(new GMapTypeControl());
  }
  this.idleGmap.setCenter(this.mapCenter, this.idleMapZoom);

  // create a searcher and bind to the map
  this.gs = new GlocalSearch();
  this.gs.setResultSetSize(GSearch.LARGE_RESULTSET);
  this.gs.setLinkTarget(this.linkTarget);
  this.gs.setCenterPoint(this.mapCenter);
  this.gs.setSearchCompleteCallback(this, GSmapSearchControl.prototype.searchComplete, [null]);
  if (this.ads) {
    for (var i=0; i<this.ads.length; i++) {
      this.gs.addRelatedSearcher(this.ads[i]);
    }
  }

  this.idleMarker = new GMarker(this.mapCenter, this.centerIcon);
  this.mapCenterMarker = new GMarker(this.mapCenter, this.centerIcon);
  GEvent.bind(this.mapCenterMarker, "click", this, this.onCenterClick);
  GEvent.bind(this.idleMarker, "click", this, this.onIdleCenterClick);
  this.gmap.addOverlay(this.mapCenterMarker);
  this.idleGmap.addOverlay(this.idleMarker);

  GEvent.bind(this.gmap, "click", this, this.onMapClick);

  this.goIdle();
  if (this.onBootCompleteHandler) {
    this.onBootCompleteHandler(this);
  }
}

// show the center of the map
GSmapSearchControl.prototype.onCenterClick = function() {
  this.mapCenterMarker.openInfoWindow(this.mapCenterHtml, {maxWidth:200});
}

// show the center of the map
GSmapSearchControl.prototype.onIdleCenterClick = function() {

  // transition to semi-active state and open the center info window
  this.clearMarkers();
  GSmapsc_cssSetClass(this.prevNext, "gsmc-prevNext gsmsc-prev-next-active");
  GSmapsc_cssSetClass(this.appContainer, "gsmsc-appContainer gsmsc-active");
  GSmapsc_cssSetClass(this.prev, "gsmsc-prev gsmsc-prev-idle");
  GSmapsc_cssSetClass(this.next, "gsmsc-next gsmsc-next-idle");
  this.searchForm.input.value = "";
  this.gmap.checkResize();
  this.idle = false;

  this.mapCenterMarker.openInfoWindow(this.mapCenterHtml, {maxWidth:200});

}

// clear the old markers off of the map
GSmapSearchControl.prototype.clearMarkers = function() {
  GSmapsc_cssSetClass(this.prevNext, "gsmc-prevNext gsmsc-prev-next-idle");

  this.gmap.closeInfoWindow();
  for (var i=0; i < this.markers.length; i++) {
    this.gmap.removeOverlay(this.markers[i].marker);
    this.markers[i].resultsListItem = null;
  }

  // clear results list if present
  if (this.resultsList) {
    GSmapsc_removeChildren(this.resultsList);
  }

  // result scroller
  this.currentResultIndex = 0;
  this.markers = new Array();
}

// drop the new markers on the map
GSmapSearchControl.prototype.setMarkers = function() {

  // result scroller
  this.currentResultIndex = 0;
  this.markers = new Array();
  var bestResultUrl = null;

  if ( this.gs.results && this.gs.results.length > 0) {
    for (var i = 0; i < this.gs.results.length && i < 16; i++) {
      var result = this.gs.results[i];
      var icon = this.unselectedIcon;
      if (this.letteredIconMode) {
        icon = this.letteredIcons[i];
      }
      this.markers.push(new GSmapscLocalResult(this, result, icon, i));

      // find the first, non-address result and hold on to it for the more-link
      if (bestResultUrl == null && !result.addressLookupResult ) {
        bestResultUrl = result.url;
      }
    }
    GSmapsc_cssSetClass(this.prevNext, "gsmc-prevNext gsmsc-prev-next-active");
    GSmapsc_cssSetClass(this.appContainer, "gsmsc-appContainer gsmsc-active");
    this.gmap.checkResize();
    this.idle = false;
    this.selectMarker(0);

    // stuff in a more results link
    if (this.resultsList) {
      if (bestResultUrl) {
        // NOW, take the URL and nuke from &latlnt.*&near ->&near
        var newUrl = bestResultUrl.replace(/&latlng=.*&near/,"&near");
        var moreDiv = GSmapsc_createDiv(null, "gsmsc-result-list-more-results");
        var alink = GSmapsc_createLink(newUrl, GSearch.strings["more-results"],
                                       this.linkTarget, "gsmsc-result-list-more-results");
        //moreDiv.appendChild(alink);
		if(this.gs.cursor.pages.length > 1){
		for(i =0; i < this.gs.cursor.pages.length; i++ ){
			var pageButton = document.createElement('span');
			pageButton.innerHTML = ' '+this.gs.cursor.pages[i].label +' ';
			
			if(this.gs.cursor.currentPageIndex != i){
				pageButton.setAttribute("style","color:blue;cursor:pointer;");
				pageButton.title = i;
				var control = this;
				$(pageButton).click(function(){
					control.clearMarkers();
					control.gs.gotoPage(this.title);
				});
			}
			moreDiv.appendChild(pageButton);
		}
		}
        var clearDiv = GSmapsc_createDiv(GSearch.strings["clear-results-uc"],
                                "gsmsc-result-list-clear-results");
        clearDiv.onclick = GSmapsc_methodClosure(this,
                                            GSmapSearchControl.prototype.goIdle,
                                            []);
        // create a table for these to sit within
        var table = GSmapsc_createTable("gsmsc-result-controls");
        var row = GSmapsc_createTableRow(table);
        var moreTd = GSmapsc_createTableCell(
                        row, "gsmsc-result-list-more-results");
        var clearTd = GSmapsc_createTableCell(
                        row, "gsmsc-result-list-clear-results");
        moreTd.appendChild(moreDiv);
        clearTd.appendChild(clearDiv);
        this.resultsList.appendChild(table);
      }
    }
  }
}

// light up the selected marker
GSmapSearchControl.prototype.selectMarker = function(index) {

  // clear info window and reset icon on current marker
  this.gmap.closeInfoWindow();

  var icon = this.unselectedIcon;
  if (this.letteredIconMode) {
    icon = this.letteredIcons[this.currentResultIndex];
  }
  this.markers[this.currentResultIndex].setMarker(icon);

  // if we have a results list, clear selected
  if (this.markers[this.currentResultIndex].resultsListItem) {
    GSmapsc_cssSetClass(this.markers[this.currentResultIndex].resultsListItem,
                        "gsmsc-result-list-item");
  }

  // snap to current
  this.currentResultIndex = index;

  // light up current
  var result = this.markers[this.currentResultIndex];

  if (result.resultsListItem) {
    GSmapsc_cssSetClass(result.resultsListItem,
                        "gsmsc-result-list-item gsmsc-selected");
  }
  var icon = this.selectedIcon;
  if (this.letteredIconMode) {
    icon = this.letteredIcons[this.currentResultIndex];
  }
  result.setMarker(icon);
  result.marker.openInfoWindow(result.getHtml(), {maxWidth:200});

  // set scroller
  if (index == 0) {
    GSmapsc_cssSetClass(this.prev, "gsmsc-prev gsmsc-prev-idle");
  } else {
    GSmapsc_cssSetClass(this.prev, "gsmsc-prev gsmsc-prev-active");
  }

  if (index == this.markers.length - 1) {
    GSmapsc_cssSetClass(this.next, "gsmsc-next gsmsc-next-idle");
  } else {
    GSmapsc_cssSetClass(this.next, "gsmsc-next gsmsc-next-active");
  }
}

// clear current markers and start a new search
GSmapSearchControl.prototype.formSubmit = function(form) {
  if (form.input.value) {
    this.newSearch(form.input.value);
  }
  return false;
}

// clear current markers and start a new search
GSmapSearchControl.prototype.execute = function(opt_query) {
  // hyperlink friendly...
  this.newSearch(opt_query);
}

GSmapSearchControl.prototype.newSearch = function(opt_query) {
  if (opt_query) {
    this.searchForm.input.value  = opt_query;
  }
  if (this.searchForm.input.value) {
	firstTracker._trackEvent('UA_MapaDominios', 'busqueda_mapadominio', 'Busqueda: '+this.searchForm.input.value);
    // clear markers, set prev/next
    this.clearMarkers();
    GSmapsc_removeChildren(this.attributionDiv);
    this.gs.execute(this.searchForm.input.value);
  }
  return false;
}

GSmapSearchControl.prototype.searchComplete = function() {
  var attribution = this.gs.getAttribution();
  if (attribution) {
    this.attributionDiv.appendChild(attribution);
  }
  this.setMarkers();
}

// forwards through the search results
GSmapSearchControl.prototype.onNext = function() {
  if (this.currentResultIndex < this.markers.length - 1) {
    this.selectMarker(this.currentResultIndex+1);
  }
}

// backwards through the search results
GSmapSearchControl.prototype.onPrev = function() {
  if (this.currentResultIndex > 0) {
    this.selectMarker(this.currentResultIndex-1);
  }
}

// called onboot complete, and on cancel click
GSmapSearchControl.prototype.goIdle = function() {
  this.searchForm.input.value = GSearch.strings["search-the-map"];
  this.gmap.setCenter(this.mapCenter, this.activeMapZoom);
  this.idleGmap.setCenter(this.gmap.getCenter(), this.idleMapZoom);
  GSmapsc_cssSetClass(this.appContainer, "gsmsc-appContainer gsmsc-idle");
  GSmapsc_cssSetClass(this.prevNext, "gsmc-prevNext gsmsc-prev-next-idle");
  this.idleGmap.checkResize();
  this.idle = true;
  $('#helpContainer').show();
  $('.domainKeywordsTool').hide();
  // if we are doing resultsList, clear...
  if (this.resultsList) {
    GSmapsc_removeChildren(this.resultsList);
  }
}

// call onfocus/onclick for search input cell
GSmapSearchControl.prototype.onPreActive = function() {
  if (this.idle) {
    this.searchForm.input.value = "";
  }
}

GSmapSearchControl.prototype.onMapClick = function(marker, point) {
  if (marker && marker.__ls__) {
    var localResult = marker.__ls__;
    localResult.onClick();
  }
}

// A class representing a single Local Search result returned by the
// Google AJAX Search API.
function GSmapscLocalResult(gsmsc, result, icon, index) {
  this.gsmsc = gsmsc;
  this.result = result;
  this.latLng = new GLatLng(parseFloat(result.lat), parseFloat(result.lng));
  this.index = index;
  this.setMarker(icon);

  if (gsmsc.resultsList) {
    var div = GSmapsc_createDiv(null, "gsmsc-result-list-item");
    if (gsmsc.letteredIconMode) {
      var key = GSmapsc_createDiv("(" + String.fromCharCode(65+index) + ")",
                                  "gsmsc-result-list-item-key");
      div.appendChild(key);
    }
    var tdiv = GSmapsc_createDiv(result.title, "gs-title");
    div.appendChild(tdiv);
    if (!result.addressLookupResult) {
/*      if (result.streetAddress && result.streetAddress != "") {
        var str = "&nbsp;-&nbsp;" + result.streetAddress;
        var tdiv = GSmapsc_createDiv(str, "gs-street");
        div.appendChild(tdiv);
      }
*/
    }
    div.onclick = GSmapsc_methodClosure(
                    gsmsc, GSmapSearchControl.prototype.selectMarker, [index]);
    this.resultsListItem = div;
    gsmsc.resultsList.appendChild(div)
  } else {
    this.resultsListItem = null;
  }
}

GSmapscLocalResult.prototype.getHtml = function() {

	var wrapperClass = "gsmsc-result-wrapper";
	if (this.gsmsc.userSelectedDirectionsMode) {
		wrapperClass += " gsmsc-result-wrapper-user-selected-directions";
	}
	var dom = this.result.titleNoFormatting.replace(/[`´!'&\.,;()*-]/ig,' ').toUpperCase();
	var str = this.result.titleNoFormatting.replace(/[`´!'&\.,;\()*-]/ig,' ').toUpperCase();
	str += ' ';
	str = str.replace( /(^|\s+)((y|de|por|para|el|la|al|lo|del|[a-z]{1}|\d+|en)(\s+))+/ig, ' ');
	var arr = str.split(/[\s-,]+/);
	var allWord = dom.split(/[\s]+/);
	newStr = '';
	wordIndex = 0;
	while(newStr.length < 16 && wordIndex < arr.length){
		newStr += ' '+arr[wordIndex];
		wordIndex++;
	}
	str = newStr;

	ulWords = document.getElementById('sortableKey');
	ulWords.innerHTML = '';
	for( i = 0 ; i < allWord.length ; i++){
		if(allWord[i]!=''){
			var liWord = document.createElement('li');
			liWord.className = 'ui-state-default';
			checked = '';
			if(str.indexOf(' '+allWord[i])>=0){
				checked = 'checked ="true"';
			}
			liWord.innerHTML = '<input class ="domainKey" type="checkbox"'+checked+'" value="'+allWord[i]+'"/>'+allWord[i];
			ulWords.appendChild(liWord);
		}
	}
	$('#traskBox').show();
	$("#sortableKey").sortable({
			placeholder: 'ui-state-highlight',
			update: function(event, ui) {
				str = '';
				$('input.domainKey:[checked]').each(function(){str+=this.value;});
				$('#newDomain').text(str);
			}
		}
	);
	$("#sortableKey").disableSelection();

	dom =  str.replace(/\s+/g,'');
	var result = GSmapsc_createDiv('<h3 class="TitReturn">'+this.result.titleNoFormatting+'</h3>');

	$(result).append('<div id="preload"><img src="http://images.dattatec.com/sp/imagenes/spinner.gif" align="absmiddle" /><strong>Cargando...</strong></div>');
	var nodeForm = document.createElement('div');
	nodeForm.id = 'availableDomains';
	//domainTool = document.getElementById('domainTool');
	//domainTool.innerHTML = '';

	$('input.domainKey',ulWords	).change(function(){
		var str = '';
		$('input.domainKey:[checked]').each(function(){str+=this.value;});
		$('#newDomain').text(str);
	});
	$(result).append(nodeForm);
	$(result).append('<button id="addToCartButton"></button>');
	$('#addToCartButton',result).click(function(){
		if($('#availableDomains input:[checked]').size() > 0){
			var ajaxCallTail = []
			$('#availableDomains input:[checked]').each(function(){
				ajaxCallTail.push({dom: this.getAttribute('domain'), tld: this.getAttribute('tld'), plan: this.getAttribute('producto'), periodo: 12, cantidad:1})
			});
			PonerTapa('md_app_container');
			tailAddToCart(ajaxCallTail);
		}else{
			alert('Por favor, seleccione los dominios que desea registrar y luego presione el botón.');
		}
	});
	nodeForm.setAttribute("style","border:none; height:145px;");

	$('#newDomain').text(dom.toUpperCase());
	$("#sortableKey",result).sortable();
	$("#sortableKey",result).disableSelection();

	$('.domainKeywordsTool').show();
	misComprasHandler.refresh();
	$('#RegistrarBT').show();
	$('#helpContainer').hide();
	
	setTimeout("$('#changeDomain').trigger('click');",500);
	return result;
}


GSmapscLocalResult.prototype.setMarker = function(icon) {
  if (this.marker) {
    this.gsmsc.gmap.removeOverlay(this.marker);
    this.marker.__ls__ = null;
    var marker = this.marker;
    this.marker = null;
    delete(marker);
  }
  this.marker = new GMarker(this.latLng, icon);
  this.marker.__ls__ = this;
  this.gsmsc.gmap.addOverlay(this.marker);
}

GSmapscLocalResult.prototype.onClick = function() {
  this.gsmsc.selectMarker(this.index);
}


/**
 * Various Static DOM Wrappers.
*/
function GSmapsc_methodClosure(object, method, opt_argArray) {
  return function() {
    return method.apply(object, opt_argArray);
  }
}

function GSmapsc_methodCallback(object, method) {
  return function() {
    return method.apply(object, arguments);
  }
}

function GSmapsc_createDiv(opt_text, opt_className) {
  var el = document.createElement("div");
  if (opt_text) {
    el.innerHTML = opt_text;
  }
  if (opt_className) { el.className = opt_className; }
  return el;
}

function GSmapsc_removeChildren(parent) {
  while (parent.firstChild) {
    parent.removeChild(parent.firstChild);
  }
}

function GSmapsc_cssSetClass(el, className) {
  el.className = className;
}


function GSmapsc_createForm(opt_className) {
  var el = document.createElement("form");
  if (opt_className) { el.className = opt_className; }
  return el;
}

function GSmapsc_createTable(opt_className) {
  var el = document.createElement("table");
  if (opt_className) { el.className = opt_className; }
  return el;
}

function GSmapsc_createTableRow(table) {
  var tr = table.insertRow(-1);
  return tr;
}

function GSmapsc_createTableCell(tr, opt_className) {
  var td = tr.insertCell(-1);
  if (opt_className) { td.className = opt_className; }
  return td;
}

function GSmapsc_createTextInput(opt_className) {
  var el = document.createElement("input");
  el.type = "text";
  if (opt_className) { el.className = opt_className; }
  return el;
}

function GSmapsc_createLink(href, text, opt_target, opt_className) {
  var el = document.createElement("a");
  el.href = href;
  el.appendChild(document.createTextNode(text));
  if (opt_className) {
    el.className = opt_className;
  }
  if (opt_target) {
    el.target = opt_target;
  }
  return el;
}
