
baseurl= 'http://www.feuerwehr-kayhude.de/';

map = null;
marker = null;
GM = null;
geocoder = null;
map_clickCounter = 0;

$(function () {
	
	
	$('.time').keypress(function(e){
		if ($(this).val().length == 2 && e.which != 58) {
			$(this).val($(this).val()+':');
		}
	});
	
	$('.service.del').live('click', function(){
		$.ajax({
		   type: "POST",
		   url: baseurl+"intern/dienstplan/delete",
		   data: ({id : $(this).attr('sid')}),
		   success: function(msg){
				$('#services').replaceWith(msg);
			}
		});
	});
	
	$('.service.add').live('click', function(){
		$.ajax({
		   type: "POST",
		   url: baseurl+"intern/dienstplan/add",
		   data: ({day : $('#day').val(), month: $('#month').val(), year: $('#year').val(), description: $('#description').val(), time: $('#time').val()}),
		   success: function(msg){
				$('#services').replaceWith(msg);
				$('#day,#month').val('');
			}
		});
		return false;
	});
	
	
	$(".next").keyup(function() {
			if ($(this).val().length >= $(this).attr('maxlength')) {
				var fields = $(this).parents('form:eq(0),body').find('input[tabindex]');
				var index = fields.index( this );
				if ( index > -1 && ( index + 1 ) < fields.length ) {
					fields.eq( index + 1 ).focus();
				}
			}
	});
	
	$('.word_count').each(function(){   
		var length = $(this).val().length;  
		$(this).parent().find('label span.counter').html( (121 - length) );  
		$(this).keyup(function(){  
			var new_length = $(this).val().length;
			if ( new_length > 121 ) {
				$(this).val($(this).val().substring(0,121));
				new_length = $(this).val().length;
				$(this).parent().find('label span.counter, label').css({'color': '#F2562C'});
			} else {
				$(this).parent().find('label span.counter, label').css({'color' : '#444444'});
			}
			$(this).parent().find('label span.counter').html( ( 121 - new_length) ); 
		});  
	});  
	$("#sidebar .element").click(function () {
		window.location.href = baseurl + 'Einsaetze/' +  $(this).attr('name');
	});
	$('#search_map').click(function () {
		load_address($('input[name=location]').val() + ", Germany", true);
	});
	
	if(typeof document.address != 'undefined')
		load_address(address,false);
	
	function load_address(_address, holdZoom) {
		if (holdZoom && map)
			zoom = map.getZoom();
		if (typeof GBrowserIsCompatible == 'function' && GBrowserIsCompatible()) {
			if (!map)
			map = new GMap2(document.getElementById("map_canvas"));
			map.setMapType(G_NORMAL_MAP);
			geocoder = new GClientGeocoder();
			if (geocoder) {
				geocoder.getLatLng(
				  _address,
				  function(point) {
					if (!point) {
						$('.location_error').html('Ort nicht gefunden!');
						$('#map_canvas').hide();
					} else {
						$('.location_error').html('');
						$('#map_canvas').show();
						marker = new GIcon(G_DEFAULT_ICON);
						marker.image = baseurl + "images/marker.png";
						markerOptions = { icon:marker };
						if (holdZoom) map.setCenter(point, zoom);
						else map.setCenter(point, 11);
						if (GM) map.removeOverlay(GM);
						GM = new GMarker(point, markerOptions);
						map.addOverlay(GM);
						GEvent.addListener(map, "click", function() {
							if ( map_clickCounter == 0) {
								$("#blog h2").css({marginTop: '10px', clear: 'both', float: 'left'});
								$("#blog table").css({float:'left', clear: 'both', width: '100%'});
								$("#map_canvas").animate({ 
								width: "99%",
								height: "400px"
								}, 1500, 'swing', function () {
									map.setCenter(point, 10);
								});
								map.enableScrollWheelZoom();
								map_clickCounter++;
							}
						});
					}
				  }
				);
			}
		}
	}
});

function insert(aTag, eTag, sel, repl) {
  var input = $(sel)[0];
  input.focus();
  if(typeof document.selection != 'undefined') {
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', aTag.length + insText.length + eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
  }
  else if(typeof input.selectionStart != 'undefined')
  {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = (repl) ? '' : input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length + insText.length + eTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;
  }
  else
  {
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}

