var toggleMapPanel = function() {
	var panel = $('.map-panel');
	if (panel.width()==0) {
		$('.map-container').css('padding-left', '240px' );
		panel.css('margin-left', '-240px');
		panel.css('width', '240px' );
	 } else {
		$('.map-container').css('padding-left', '0');
		panel.css('margin-left', '0');
		panel.css('width', '0px');
	 }
}

var showDirections = function() {
	var panel = $('.map-panel');
		
	var inputFrom = $('.input-directions-from', panel);
	var from = inputFrom.get(0).value;
	if (from == '') {
		alert( 'Введите адрес пункта отправления.\n\nНапример:   метро Университет' );
		return;
	}
	
	var inputBy = $('.input-directions-by', panel).get(0);
	var by = inputBy.selectedIndex;
	
	var inputTo = $('.input-directions-to', panel).get(0);
	var toNumber = inputTo.selectedIndex;
	
	if (by==0) {
		var travelMode = G_TRAVEL_MODE_WALKING;
	} else {
		var travelMode = G_TRAVEL_MODE_DRIVING;
	}
	
	if (additionalMarkers>0) {
		var toText = inputTo.options[toNumber].text;
		var toMarker = markers[toNumber].marker;
	
		gdir.load('from: Украина, Киев, ' + from + ' to: Украина, Киев, ' + toText + '@' + toMarker.getLatLng().toString(), {locale: 'ru_RU', travelMode: travelMode});
	} else {
		gdir.load('from: Украина, Киев, ' + from + ' to: @' + addressMarker.getLatLng().toString(), {locale: 'ru_RU', travelMode: travelMode});
	}
	return false;
}

function TextualControl() {
}

TextualControl.prototype = new GControl();

TextualControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var button = document.createElement("div");
  container.appendChild(button);
  var dirs = document.createElement("div");
  this.setButtonStyle_(button);
  button.appendChild(dirs); 
  dirs.appendChild(document.createTextNode("Как добираться?"));
  dirs._toggledOn = 1;
  this.setButtonOnStyle_(dirs);
  
  var control = this;
  
  GEvent.addDomListener(dirs, "click", function() {
		toggleMapPanel();
		dirs._toggledOn = 1 - dirs._toggledOn;
		if (dirs._toggledOn) {
			control.setButtonOnStyle_(dirs);
		} else {
			control.setButtonOffStyle_(dirs);
		}
	});
  map.getContainer().appendChild(container);
  return container;
}

TextualControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(7, 50));
}

TextualControl.prototype.setButtonOnStyle_ = function(button) {
	button.style.border = "1px solid rgb(52, 86, 132)";
	button.style.borderBottom = "1px solid rgb(108, 157, 223)";
	button.style.borderRight = button.style.borderBottom;
	button.style.fontWeight = 'bold';
}
TextualControl.prototype.setButtonOffStyle_ = function(button) {
	button.style.border = "1px solid white";
	button.style.borderBottom = "1px solid rgb(176,176,176)";
	button.style.borderRight = button.style.borderBottom;
	button.style.fontWeight = 'normal';
}

TextualControl.prototype.setButtonStyle_ = function(button) {
  button.style.textDecoration = "none";
  button.style.color = "black";
  button.style.backgroundColor = "white";
  button.style.font = "12px Arial";
  button.style.border = "1px solid black";
  button.style.padding = "2px";
  button.style.marginBottom = "3px";
  button.style.textAlign = "center";
  button.style.width = "9em";
  button.style.cursor = "pointer";
  button.style.padding = 0;
}

var appendOptionLast = function(target, text)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = text;
  var elSel = target

  try {
	elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
	elSel.add(elOptNew); // IE only
  }
}
