var ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var CONST_GOOGLE = 0;
var CONST_GOOGLE_CLIPPED = 1;
var CONST_GOOGLE_CLIPPED_LINKED = 2;
var CONST_PREMIUM = 6;
var CONST_PRODUCT = 8;
var CONST_USER = 3;

var ENTRY_TYPE_PREVIEW = 0;
var ENTRY_TYPE_PRODUCT_1 = 1;
var poweredByGoogle = "<div class=\"poweredByGoogle\">powered by <img style=\"position:relative;top:5px\" src=\"http://www.google.com/uds/css/small-logo.png\"></div>";

var wikiLayer = null;
var directions;
var geocoder;
var map;
var sr = new SR();
var mem = new MEM();
var imprint_marker = null;
var user_marker = null;
var homePoint = null;
var icon = new GIcon();

icon.image = 'images/marker/caro/image.png';
icon.shadow = 'images/marker/caro/shadow.png';
icon.iconSize = new GSize(22, 29);
icon.shadowSize = new GSize(37, 29);
icon.iconAnchor = new GPoint(11, 29);
icon.infoWindowAnchor = new GPoint(11, 0);
icon.printImage = 'images/marker/caro/printImage.gif';
icon.mozPrintImage = 'images/marker/caro/mozPrintImage.gif';
icon.printShadow = 'images/marker/caro/printShadow.gif';
icon.transparent = 'images/marker/caro/transparent.png';
icon.imageMap = [ 11, 2, 12, 3, 12, 4, 13, 5, 14, 6, 14, 7, 15, 8, 16, 9, 17,
		10, 17, 11, 18, 12, 19, 13, 19, 14, 20, 15, 19, 16, 18, 17, 17, 18, 17,
		19, 16, 20, 15, 21, 14, 22, 14, 23, 13, 24, 12, 25, 12, 26, 11, 27, 10,
		27, 9, 26, 9, 25, 8, 24, 7, 23, 6, 22, 6, 21, 5, 20, 4, 19, 4, 18, 3,
		17, 2, 16, 1, 15, 1, 14, 2, 13, 3, 12, 4, 11, 4, 10, 5, 9, 6, 8, 6, 7,
		7, 6, 8, 5, 9, 4, 9, 3, 10, 2 ];

var icongreen = new GIcon(icon, "images/icon_green.png");
var icongrey = new GIcon(icon, "images/icon_grey.png");
var iconblue = new GIcon(icon, "images/icon_blue.png");
var iconred = new GIcon(icon, "images/icon_red.png");
var iconwhite = new GIcon(icon, "images/icon_white.png");
var icongreen_saved = new GIcon(icon, "images/icon_green_saved.png");
var iconblue_saved = new GIcon(icon, "images/icon_blue_saved.png");
var iconred_saved = new GIcon(icon, "images/icon_red_saved.png");
var iconwhite_saved = new GIcon(icon, "images/icon_white_saved.png");

var GLOBAL_ROUTING = 0;
var CONST_PED_ROUTING = 1;
var CONST_CAR_ROUTING = 2;


var myWin=null;    
function newWin(Adresse) {
		myWin= window.open(Adresse, "WinNew", "width=1280,height=850,scrollbars=yes,location=yes");
		myWin.focus();
}
function selectAll(obj) {
	var text_val = eval(obj);
	text_val.focus();
	text_val.select();
	if (!document.all)
		return; // IE only
	r = text_val.createTextRange();
	// r.execCommand('copy');
}

function greenBut(obj) {
	obj.setStyle( {
		backgroundColor : '#b5d29c',
		color : '#FFFFFF'
	});
}
function normBut(obj) {
	obj.setStyle( {
		backgroundColor : '#f3f3ff',
		color : '#000000'
	});
}

function fill(txt, length) {
	txt = txt + "";
	while (txt.length < length)
		txt = "0" + txt;
	return txt;
}

function getNowFormatted(withTime, withSeconds) {
	var d = new Date();
	var rw = "";
	if (sr.user.preferredLang == "DE") {
		rw = fill(d.getDate(), 2) + "." + fill(d.getMonth() + 1, 2) + "."
				+ d.getFullYear();
	} else {
		rw = d.getFullYear() + "/" + fill(d.getMonth() + 1, 2) + "/"
				+ fill(d.getDate(), 2);
	}
	if (withTime) {
		rw += " " + fill(d.getHours(), 2) + ":" + fill(d.getMinutes(), 2)
				+ (withSeconds ? ":" + fill(d.getSeconds(), 2) : "");
	}
	return rw;
}

function goHome() {
	if (homePoint)
		map.setCenter(homePoint);
}

function createSpotMarker(type, point, titleText, isDraggable) {
	var baseIcon = new GIcon();
	var pushpin = null;
	switch (type) {
	case 0:// norm4
	{
		var myIcon = new GIcon();
		myIcon.image = 'images/marker/norm4/image.png';
		myIcon.shadow = 'images/marker/norm4/shadow.png';
		myIcon.iconSize = new GSize(62, 15);
		myIcon.shadowSize = new GSize(70, 15);
		myIcon.iconAnchor = new GPoint(31, 15);
		myIcon.infoWindowAnchor = new GPoint(31, 0);
		myIcon.printImage = 'images/marker/norm4/printImage.gif';
		myIcon.mozPrintImage = 'images/marker/norm4/mozPrintImage.gif';
		myIcon.printShadow = 'images/marker/norm4/printShadow.gif';
		myIcon.transparent = 'images/marker/norm4/transparent.png';
		myIcon.imageMap = [ 61, 0, 61, 1, 61, 2, 61, 3, 61, 4, 61, 5, 61, 6,
				61, 7, 61, 8, 61, 9, 61, 10, 61, 11, 61, 12, 31, 13, 31, 14,
				29, 14, 29, 13, 0, 12, 0, 11, 0, 10, 0, 9, 0, 8, 0, 7, 0, 6, 0,
				5, 0, 4, 0, 3, 0, 2, 0, 1, 0, 0 ];
		pushpin = myIcon;
	}
		break;
	case 1:// man
	{
		var myIcon = new GIcon();
		myIcon.image = 'images/marker/man/image.png';
		myIcon.shadow = 'images/marker/man/shadow.png';
		myIcon.iconSize = new GSize(35, 40);
		myIcon.shadowSize = new GSize(55, 40);
		myIcon.iconAnchor = new GPoint(18, 40);
		myIcon.infoWindowAnchor = new GPoint(18, 0);
		myIcon.printImage = 'images/marker/man/printImage.gif';
		myIcon.mozPrintImage = 'images/marker/man/mozPrintImage.gif';
		myIcon.printShadow = 'images/marker/man/printShadow.gif';
		myIcon.transparent = 'images/marker/man/transparent.png';
		myIcon.imageMap = [ 17, 0, 17, 1, 18, 2, 18, 3, 18, 4, 17, 5, 16, 6,
				20, 7, 21, 8, 22, 9, 23, 10, 24, 11, 24, 12, 25, 13, 26, 14,
				27, 15, 27, 16, 27, 17, 27, 18, 19, 19, 19, 20, 19, 21, 19, 22,
				19, 23, 19, 24, 19, 25, 19, 26, 19, 27, 19, 28, 19, 29, 19, 30,
				19, 31, 19, 32, 19, 33, 19, 34, 19, 35, 19, 36, 19, 37, 18, 38,
				18, 39, 11, 39, 10, 38, 10, 37, 10, 36, 10, 35, 10, 34, 10, 33,
				10, 32, 10, 31, 10, 30, 10, 29, 10, 28, 10, 27, 10, 26, 10, 25,
				10, 24, 10, 23, 10, 22, 10, 21, 10, 20, 10, 19, 1, 18, 1, 17,
				1, 16, 1, 15, 3, 14, 3, 13, 4, 12, 5, 11, 6, 10, 7, 9, 8, 8, 8,
				7, 13, 6, 12, 5, 11, 4, 11, 3, 11, 2, 11, 1, 12, 0 ];
		pushpin = myIcon;
	}
		break;
	case 2:// premium
		pushpin = iconred;
		break;
	case 3:// product
		pushpin = iconblue;
		break;
	case 4:// shoppingcart
	{
		var myIcon = new GIcon();
		myIcon.image = 'images/marker/shoppingcart/image.png';
		myIcon.shadow = 'images/marker/shoppingcart/shadow.png';
		myIcon.iconSize = new GSize(29, 18);
		myIcon.shadowSize = new GSize(38, 18);
		myIcon.iconAnchor = new GPoint(15, 18);
		myIcon.infoWindowAnchor = new GPoint(15, 0);
		myIcon.printImage = 'images/marker/shoppingcart/printImage.gif';
		myIcon.mozPrintImage = 'images/marker/shoppingcart/mozPrintImage.gif';
		myIcon.printShadow = 'images/marker/shoppingcart/printShadow.gif';
		myIcon.transparent = 'markers/transparent.png';
		myIcon.imageMap = [ 6, 0, 6, 1, 12, 2, 13, 3, 28, 4, 28, 5, 28, 6, 28,
				7, 28, 8, 26, 9, 26, 10, 14, 11, 14, 12, 13, 13, 27, 14, 25,
				15, 26, 16, 25, 17, 1, 17, 0, 16, 1, 15, 2, 14, 3, 13, 4, 12,
				4, 11, 0, 10, 0, 9, 0, 8, 1, 7, 2, 6, 3, 5, 3, 4, 11, 3, 4, 2,
				4, 1, 4, 0 ];
		pushpin = myIcon;
	}
		break;
	case 5:// car
	{
		var myIcon = new GIcon();
		myIcon.image = 'images/marker/car/image.png';
		myIcon.shadow = 'images/marker/car/shadow.png';
		myIcon.iconSize = new GSize(28, 18);
		myIcon.shadowSize = new GSize(37, 18);
		myIcon.iconAnchor = new GPoint(14, 18);
		myIcon.infoWindowAnchor = new GPoint(14, 0);
		myIcon.printImage = 'images/marker/car/printImage.gif';
		myIcon.mozPrintImage = 'images/marker/car/mozPrintImage.gif';
		myIcon.printShadow = 'images/marker/car/printShadow.gif';
		myIcon.transparent = 'images/marker/car/transparent.png';
		myIcon.imageMap = [ 22, 1, 23, 2, 24, 3, 24, 4, 25, 5, 26, 6, 26, 7,
				26, 8, 27, 9, 27, 10, 27, 11, 26, 12, 24, 13, 24, 14, 24, 15,
				24, 16, 23, 17, 3, 17, 2, 16, 2, 15, 2, 14, 2, 13, 0, 12, 0,
				11, 0, 10, 0, 9, 1, 8, 3, 7, 2, 6, 1, 5, 1, 4, 2, 3, 3, 2, 19,
				1 ];
		pushpin = myIcon;
	}
		break;
	default:
		alertERR("createSpotMarker" + type);
		pushpin = iconred;
		break;

	}

	var options = {
		title : titleText,
		draggable : isDraggable,
		bouncy : true,
		icon : pushpin
	};

	var marker = new GMarker(point, options);

	return marker;
}
var overviewMapControl = null;
function setMap() {
	if (GBrowserIsCompatible()) {
		// Create Google Map Object
		map = new GMap2(document.getElementById("map_canvas"), {
			mapTypes : [ G_NORMAL_MAP, G_HYBRID_MAP ]
		});

		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl(), new GControlPosition(
				G_ANCHOR_BOTTOM_LEFT, new GSize(1, 35)));
		map.addControl(new GScaleControl());

		overviewMapControl = new GOverviewMapControl();
		map.addControl(overviewMapControl);

		map.enableScrollWheelZoom();

		geocoder = new GClientGeocoder();

		// routing
		directionsPanel = $("directions_canvas");
		$('routing_button').hide();
		directions = new GDirections(map, directionsPanel);
		// GEvent.addListener(directions, "loadFromWaypoints",
		// onDirectionsLoadFromWaypoints);
		GEvent.addListener(directions, "load", onDirectionsLoadFromWaypoints);

		homePoint = new GLatLng(homeLat, homeLng);
		map.setCenter(homePoint, 13);
		secondLoader();
		// sr.restoreMem();
		sr.show();
		onLoadMessages();
	}
}

function doAdrSearch() {
	var nl = $F('adrsearch');
	if (nl != homesearch) {
		top.document.title = nl + " - mirutas";
		homesearch = nl;
		geocoder.getLatLng(nl, function(point) {
			if (point) {
				homeLat = point.y;
				homeLng = point.x;
				homePoint = point;
				sr.moveUser(homePoint);
				goHome();
				callBackUserMarker();
				saveTown(point);
			}
		});
		// top.location = APPLICATION + "/" + escape($F('adrsearch'));
	} else {
		homePoint = new GLatLng(homeLat, homeLng);
		sr.moveUser(homePoint);
		goHome();
	}
	return false;
}

function loadEntry(id) {
	sr.openHelpWindowPreloadId('PREMENTRY', 'entry', id);
}
function loadProduct(id, entryId) {
	sr.openHelpWindowPreloadId('PRODUCT', 'product', id, entryId);
}

function saveTown(point) {
	// reverse lookup
	geocoder
			.getLocations(
					point,
					function(addresses) {
						if (addresses.Status.code != 200) {
							alertERR("reverse geocoder failed ");
						} else {

							address = addresses.Placemark[0];
							// get city
							var city = "";
							try {
								city = address.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName;
								homesearch = city;
							} catch (err) {
								try {
									city = address.AddressDetails.Country.AdministrativeArea.Locality.LocalityName;
								} catch (err) {
									alertERR(err);
								}
							}
							// geocode & save city
							geocoder.getLatLng(city, function(pp) {
								if (pp) {
									new Ajax.Request('save?lat=' + pp.lat()
											+ '&lng=' + pp.lng() + '&c='
											+ escape(city) + "&rnd="
											+ Math.random(), {
										method : 'post',
										onSuccess : function(transport) {
											// alert(transport.responseText);
									}
									});
								}
							});

						}
					});
}
/**
 * Navigation on
 * 
 * @return
 */
function checkDirectionsCanvas() {
	$('directions_canvas').hide();
	// setMapFocus(true);
	var html = $("directions_canvas").innerHTML;
	if (html == "") {
		setTimeout("checkDirectionsCanvas()", 500);
	} else {

		var count = 0;
		var i = 0;

		elements = $('directions_canvas').getElementsByTagName('img');
		for (i = elements.length - 1; i >= 0; i--) {
			// imageSources.push(elements[i].src);
			elements[i].remove();
		}

		var elements = $('directions_canvas').getElementsByTagName('tr');
		for (i = 0; i < elements.length; i++) {
			if (elements[i].getAttributeNode) {
				if (elements[i].getAttributeNode('waypointId')) {
					if (elements[i].getAttributeNode('xonclick')) {
						elements[i].getAttributeNode('xonclick').value = '';
					}
					if (elements[i].getAttributeNode('jsaction')) {
						elements[i].getAttributeNode('jsaction').value = '';
					}
				}
			}
		}

		var imageCount = 0;
		var elements = $('directions_canvas').getElementsByTagName('td');
		for (i = 0; i < elements.length; i++) {
			if (elements[i].getAttributeNode
					&& elements[i].getAttributeNode('jscontent')) {
				if (elements[i].getAttributeNode('jscontent').value == 'address') {
					elements[i - 1].style.paddingRight = '0px';
					elements[i - 1].style.width = '100%';
					var html = sr.hits[sr.navigation[count]].getHTML(2);
					html = html.replace("images/marker_34_white.png",
							"images/marker/" + ABC.charAt(imageCount % 26)
									+ ".png");
					imageCount++;
					elements[i - 1].innerHTML = html;
					elements[i].remove();
					count++;
				}
			}
		}
		elements = $('directions_canvas').getElementsByTagName('div');
		for (i = 0; i < elements.length; i++) {
			if (elements[i].getAttributeNode
					&& elements[i].getAttributeNode('jsdisplay')) {
				if (elements[i].getAttributeNode('jsdisplay').value == 'walking') {
					elements[i].style.display = 'none';
				}
			}
		}

		elements = $('directions_canvas').getElementsByTagName('table');
		for (i = 0; i < elements.length; i++) {
			if (elements[i].style) {
				elements[i].style.backgroundColor = '#fff';
				elements[i].style.border = '0px';
			}
		}

		elements = $('directions_canvas').getElementsByTagName('td');
		for (i = 0; i < elements.length; i++) {
			if (elements[i].style) {
				elements[i].style.verticalAlign = 'top';
			}
		}

		$('directions_canvas').setStyle( {
			backgroundColor : '#FFF'
		});
		// $('debug').value=$('directions_canvas').innerHTML;
		$('directions_canvas').show();
		setMapFocus(false);
		// sr.drawUserMarker();
		sr.updatePrint();
	}
}

var routingBounds = null;
function onDirectionsLoadFromWaypoints() {
	checkDirectionsCanvas();
	// polyline = directions.getPolyline();
	routingBounds = directions.getBounds();
	// sr.zoom(2);
}

/**
 * User-Memory
 * 
 * @return
 */
function MEM() {
	this.mems = new Array(); // ergebnisse
}

// Suchergebnis hinzufügen, sofern noch nicht vorhanden
MEM.prototype.push = function(hit) {
	this.mems.push(hit);
	var drin = false;
	for ( var i = 0; i < this.mems.length; i++) {
		if (hit.name == this.mems[i].name) {
			drin = true;
		}
	}
	if (!drin) {
		this.mems.push(hit);
	}
};
MEM.prototype.pop = function(hit) {
	var newMems = new Array();
	for ( var i = 0; i < this.mems.length; i++) {
		if (this.mems[i].name != hit.name) {
			newMems.push(this.mems[i]);
		}
	}
	this.mems = newMems;
};

MEM.prototype.clearChecked = function(hit) {
	for ( var i = 0; i < this.mems.length; i++) {
		if (this.mems[i].checked) {
			this.mems[i].checked = false;
		}
		if (this.mems[i].isNavigation) {
			this.mems[i].isNavigation = false;
		}

	}
};

/**
 * User-Objekt
 */
function USER(firstName, lastName, email, nickname, preferredLang, sex, title,
		hlat, hlng, lat, lng, base) {
	this.firstName = firstName ? firstName : "";
	this.lastName = lastName ? lastName : "";
	this.email = email ? email : "";
	this.nickname = nickname ? nickname : "";
	this.preferredLang = preferredLang ? preferredLang : "";
	this.sex = sex ? sex : false;
	this.title = title ? title : "";
	this.latitude = lat ? lat : "";
	this.longitude = lng ? lng : "";
	this.base = base;
	this.loggedIn = false;
	this.result = null;
	this.homePoint = new GLatLng(hlat, hlng);
	this.basePoint = new GLatLng(this.latitude, this.longitude);
}

USER.prototype.goHome = function() {
	sr.moveUser(this.basePoint);
	goHome();
	if (sr.hits[0].marker)
		sr.hits[0].marker.openInfoWindowHtml(sr.hits[0].getHTML(0));
};

USER.prototype.setLatLng = function(lat, lng) {
	this.latitude = lat;
	this.longitude = lng;
	var p = new GLatLng(lat, lng);
	this.setPoint(p);
	sr.updateUser();
};

USER.prototype.setPoint = function(p) {
	this.homePoint = p;
	homePoint = p;
	if (this.result) {
		result.lat = this.latitude;
		result.lng = this.longitude;
		result.point = this.homePoint;
	}
};

USER.prototype.login = function(u) {
	this.firstName = u.firstName;
	this.lastName = u.lastName;
	this.email = u.email;
	this.nickname = u.nickname;
	this.preferredLang = u.preferredLang;
	this.sex = u.sex;
	this.title = u.title;
	this.latitude = u.latitude;
	this.longitude = u.longitude;
	this.base = u.base;
	this.loggedIn = true;
	this.homePoint = new GLatLng(this.latitude, this.longitude);
	this.setPoint(this.homePoint);
	this.basePoint = this.homePoint;
	$('user_buttons').show();

};

USER.prototype.logout = function(u) {
	this.firstName = "";
	this.lastName = "";
	this.email = "";
	this.nickname = "anoymous";
	this.preferredLang = "DE";
	this.sex = false;
	this.title = "";
	this.latitude = 0;
	this.longitude = 0;
	this.base = "";
	this.loggedIn = false;
	$('user_buttons').hide();
};

function USERRESULT(user) {
	this.lat = user.latitude;
	this.lng = user.longitude;
	this.title = user.nickname;
	this.titleNoFormatting = user.nickname;
	this.streetAddress = user.base;
	this.city = "";
	this.point = user.homePoint;
}

USER.prototype.getResult = function() {
	var result = new RES(new USERRESULT(this), 0, CONST_USER);
	result.marker = user_marker;
	return result;
};

/**
 * GlocalResult-Objekt
 */
function RES(res, name, type) {
	this.isOwner = false;
	this.type = type;
	this.name = name;
	this.result = res;
	this.checked = false;
	this.isNavigation = false;
	this.saved = false;
	this.marker = null;
	this.modified = null;
	this.setHTML();
	this.setModified();
	this.point = new GLatLng(this.result.lat, this.result.lng);
	if (type != CONST_USER)
		this.createMarker(this.result.titleNoFormatting);
	this.setOwner();
}

RES.prototype.setOwner = function() {
	if (this.result) {
		this.isOwner = (this.result.entry && this.result.entry.owner)
				|| (this.result.product && this.result.product.owner);
	}

};

RES.prototype.getId = function() {
	if (this.result) {
		if (this.type == CONST_GOOGLE_CLIPPED
				|| this.type == CONST_GOOGLE_CLIPPED_LINKED) {
			return (this.result.id);
		} else if (this.type == CONST_PREMIUM) {
			return (this.result.entry ? this.result.entry.pid : null);
		} else if (this.type == CONST_PRODUCT) {
			return (this.result.product ? this.result.product.id : null);
		}
	}
	return null;
};

RES.prototype.setModified = function() {
	if (this.result.modified) {
		this.modified = this.result.modified;
		/*
		 * if (this.modified.indexOf(" ")>-1){
		 * this.modified=this.modified.substring(0,this.modified.indexOf(" ")); }
		 */
	} else {
		this.modified = getNowFormatted(false, false);
	}

};

RES.prototype.setHTML = function() {
	if (this.type == CONST_GOOGLE) {
		this.html = "<a target=_blank href=\"" + this.result.url + "\">"
				+ this.result.titleNoFormatting + "</a><br/>"
				+ this.result.streetAddress + "<br/>" + this.result.city;
	} else if (this.type == CONST_USER) {
		this.html = this.result.titleNoFormatting;

	} else {
		this.html = this.result.title + "<br/>" + this.result.streetAddress
				+ "<br/>" + this.result.city;
	}

};

RES.prototype.update = function(res, type) {
	this.result = res;
	this.type = type;
	this.setHTML();
	this.setModified();
	if (type != CONST_USER)
		this.createMarker(this.result.titleNoFormatting);
	this.setOwner();
};

RES.prototype.smallUpdate = function(res) {
	this.result = res;
	this.setHTML();
	this.setModified();
	this.point = new GLatLng(this.result.lat, this.result.lng);
	this.createMarker(this.result.titleNoFormatting);
};

RES.prototype.isFromDb = function() {
	if (this.result && this.result.region)
		return this.result.region == "p";

};

RES.prototype.createMarker = function(titleText) {
	var that = this;
	this.marker = new GMarker(this.point, {
		title : titleText,
		draggable : false,
		bouncy : false,
		icon : that.getIcon(false)
	});
	this.marker.name = this.name;
	GEvent.addListener(this.marker, "click", function() {
		sr.click(this.name);
	});
	// return marker;
};

RES.prototype.updateImages = function(renewMarker) {
	$('MARKERRIGHT' + this.name).src = this.getIcon(true);
	if (renewMarker && (GLOBAL_ROUTING == 0)) {
		// change z-index on map:
		map.removeOverlay(this.marker);
		map.addOverlay(this.marker);
	}
	// new image
	if (GLOBAL_ROUTING == 0)
		this.marker.setImage(this.getIcon(true));
	var cont = "CRN";// possible contexts: content,right,navigation
	if (this.saved) {
		for ( var i = 0; i < 3; i++) {
			if ($('but' + cont.charAt(i) + this.name))
				greenBut($('but' + cont.charAt(i) + this.name));
		}
		drin = true;
	} else {
		for ( var i = 0; i < 3; i++) {
			if ($('but' + cont.charAt(i) + this.name))
				normBut($('but' + cont.charAt(i) + this.name));
		}
	}
};

RES.prototype.getPremiumEntry = function(detail, print,context) {
	var rw = "";
	if (this.result.entry.type == 0) {
		rw += "<div class=preview onclick=\"sr.openHelpWindowPreloadId('BUYIT','entry',"
				+ this.result.entry.id
				+ ");sr.stopEvent(event)\">"
				+ TEXT_PREVIEW + "</div>";
	}
	if (this.result.entry.logo) {
		var logo = "<img  src=\"" + APPLICATION + this.result.entry.logo
				+ "\" border=0>";
		if (this.result.entry.website) {
			logo = "<a title=\"" + this.result.entry.website
					+ "\" target=\"_blank\" href=\""
					+ this.result.entry.website + "\">" + logo + "</a>";
		}

		rw += logo + "<br/><br/>";
	}
	if (detail) {
		if (this.result.entry.description)
			rw += "<div class=\"description\">" + this.result.entry.description
					+ "</div>";
	}

	if (this.result.entry.website) {
		rw += "<a title=\"" + TEXT_WEBSITE + "\" target=\"_blank\" href=\""
				+ this.result.entry.website + "\">";
		rw += "<b>" + this.result.titleNoFormatting + "</b></a><br/>";
	} else {
		rw += "<b>" + this.result.titleNoFormatting + "</b><br/>";
	}

	rw += this.result.streetAddress + "<br/>";
	if (this.result.entry.zip) {
		rw += this.result.entry.zip + " ";
	}
	rw += this.result.city + "<br/><br/>";

	if (this.result.entry.tel)
		rw += "<i>Tel: " + this.result.entry.tel + "</i><br/>";
	if (this.result.entry.fax)
		rw += "<i>Fax: " + this.result.entry.fax + "</i><br/>";

	if (this.result.entry.email) {
		rw += "<br/><a title=\"" + this.result.entry.email
				+ "\" href=\"mailto:" + this.result.entry.email
				+ "\"><img src=\"images/mailS.gif\" border=0> "
				+ this.result.entry.email + "</a><br/>";
	}

	if (this.result.entry.website && print) {
		// if (print)
		rw += this.result.entry.website;
		/*
		 * else rw += "<br/><a title=\"" + TEXT_WEBSITE + "\"
		 * target=\"_blank\" href=\"" + this.result.entry.website + "\">" +
		 * WEBSITE + "</a><br/>";
		 */
	}

	if (!print && detail && this.result.entry.productList
			&& this.result.entry.productList.size() > 0) {
		rw += "<br/><br/><select onchange=\"if (this[this.selectedIndex].value!=0)sr.showElement(this[this.selectedIndex].value)\"><option value=\"0\">"
				+ TEXT_PRODUCT_LIST + "</option>";
		for ( var i = 0; i < this.result.entry.productList.size(); i++) {
			rw += "<option value=\""
					+ this.result.entry.productList[i].uniqueTitle + "\">"
					+ this.result.entry.productList[i].title + "</option>";
		}
		rw += "</select>";
	}
	
	rw += this.getLink2Go(context);
	
	
	return rw;
};

RES.prototype.getProductEntry = function(detail, print,context) {
	var rw = "";
	if (this.result.product.entry.type == 0) {
		rw += "<div class=preview onclick=\"sr.openHelpWindowPreloadId('BUYIT','entry',"
				+ this.result.product.entry.pid
				+ ");sr.stopEvent(event)\">"
				+ TEXT_PREVIEW + "</div>";
	}

	if (this.result.product.image) {
		var logo = "<img  src=\"" + APPLICATION + this.result.product.image
				+ "\" border=0>";
		if (this.result.product.website) {
			logo = "<a title=\"" + TEXT_WEBSITE
					+ "\" target=\"_blank\" href=\""
					+ this.result.product.website + "\">" + logo + "</a>";
		}
		rw += logo + "<br/><br/>";
	}
	if (detail) {
		if (this.result.product.description)
			rw += "<div class=\"description\">"
					+ this.result.product.description + "</div>";
	}
	if (this.result.product.website) {
		rw += "<a title=\"" + TEXT_WEBSITE + "\" target=\"_blank\" href=\""
				+ this.result.product.website + "\">";
		rw += "<b>" + this.result.titleNoFormatting + "</b></a><br/><br/>";
	} else {
		rw += "<b>" + this.result.titleNoFormatting + "</b><br/><br/>";
	}
	if (this.result.product.website && print) {
		// if (print)
		rw += "<a title=\"" + TEXT_WEBSITE + "\" target=\"_blank\" href=\""
				+ this.result.product.website + "\">"
				+ this.result.product.website + "</a><br/><br/>";
		/*
		 * else rw += "<a title=\"" + TEXT_WEBSITE + "\" target=\"_blank\"
		 * href=\"" + this.result.product.website + "\">" + WEBSITE + "</a><br/><br/>";
		 */
	}
	if (this.result.product.entry) {
		if (print) {
			rw += "<div class=\"description\">"
					+ this.result.product.entry.title + "</div>";
		} else {
			if (detail) {
				rw += "<div class=\"top_button_small\" title=\""
						+ TEXT_VIEW
						+ "\" onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"sr.showElement('"
						+ this.result.product.entry.uniqueTitle + "')\">"
						+ this.result.product.entry.title + "</div><br/>";
			} else {
				rw += "<a class=company href=\"#\" title=\"" + TEXT_VIEW
						+ "\" onclick=\"sr.showElement('"
						+ this.result.product.entry.uniqueTitle + "')\">"
						+ this.result.product.entry.title + "</a>";
			}
		}
	}
	
	rw += this.getLink2Go(context);
	
	return rw;
};

RES.prototype.getGoogleEntry = function(detail, print,context) {
	var rw = "";
	if (this.result) {
		if (this.result.entry && this.result.entry.website
				&& this.result.entry.title) {
			rw += "<a title=\"" + this.result.titleNoFormatting
					+ "\" target=\"_blank\" href=\""
					+ this.result.entry.website + "\">"
					+ this.result.entry.title + "</a><br/>";
		} else {
			rw += "<b>" + this.result.titleNoFormatting + "</b><br/>";
		}
		rw += this.result.streetAddress + ", " + this.result.city + "<br/>";
		if (this.result.numbers) {
			rw += "<i>" + this.result.numbers + "</i>";
		}

		if (this.result.entry && this.result.entry.website && print) {
			rw += TEXT_WEBSITE + " <a title=\"" + TEXT_WEBSITE
					+ "\" target=\"_blank\" href=\""
					+ this.result.entry.website + "\">"
					+ this.result.entry.website + "</a><br/><br/>";
		}
		rw += this.getLink2Go(context);
	}
	
	return rw;
};

RES.prototype.getMemoButton = function(context) {
	if (this.type == CONST_PREMIUM || this.type == CONST_PRODUCT
			|| this.isFromDb()) {
		return "<div id=\"but"
				+ context
				+ this.name
				+ "\" class=\"top_button_small\" "
				+ (this.saved ? "style=\"background-color:#b5d29c;color:#FFFFFF\""
						: "") + " title=\"" + TEXT_MEMORIZE
				+ "\"  onclick=\"sr.stopEvent(event);sr.save(" + this.name
				+ ")\">&loz;</div>";

	}
	return "";
};

RES.prototype.getMemoMarker = function() {
	return "<img title=\"" + TEXT_MEMORIZE + "\" onclick=\"sr.save("
			+ this.name + ");sr.stopEvent(event)\" id=\"MARKERRIGHT"
			+ this.name + "\" src=\"" + this.getIcon(true) + "\" >";
};

RES.prototype.getNaviIcon = function(context) {
	return "<img align=\"left\" height=20 id=\"CI" + context + this.name
			+ "\" title=\"" + TEXT_NAVI + "\" id=\"navi" + this.name
			+ "\" onclick=\"sr.stopEvent(event);return sr.naviclick("
			+ this.name
			+ ")\" src=\"images/marker_34_white.png\" class=\"smallmarker\" >";
};

RES.prototype.getMailButton = function() {
	if (this.type == CONST_PREMIUM || this.type == CONST_PRODUCT
			|| this.isFromDb()) {
		return "<div  class=\"top_button_small\" title=\""
				+ TEXT_SEND
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"sr.mailMemo("
				+ this.name
				+ ");sr.stopEvent(event)\"><img src=\"images/mailS.gif\" border=0></div>";
	}
	return "";
};
RES.prototype.getEditButton = function() {
	var rw = "";
	if (this.type == CONST_PREMIUM || this.type == CONST_PRODUCT
			|| this.isFromDb()) {
		rw += "<div class=\"top_button_small\" title=\""
				+ TEXT_EDIT
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" ";
		if (this.type == CONST_PREMIUM && this.result.entry.owner)
			rw += "onclick=\"loadEntry(" + this.result.entry.pid
					+ ");sr.stopEvent(event)\"";
		else if (this.type == CONST_PRODUCT && this.result.product.owner)
			rw += "onclick=\"loadProduct(" + this.result.product.id + ","
					+ this.result.product.entry.id + ");sr.stopEvent(event)\"";
		else if (this.isFromDb())
			rw += "onclick=\"sr.enter(" + this.name + ");sr.stopEvent(event)\"";
		rw += ">&rsaquo;&nbsp;&loz;</div>";
	}
	return rw;
};

RES.prototype.getHTMLContent = function() {
	var rw = "";
	if (this.type == CONST_USER) {
		rw = "<img src=\"" + this.getIcon(true) + "\" ><b>"
				+ this.result.titleNoFormatting + "</b><br/>";
		rw += this.getUserButtons("C");

	} else if (this.type == CONST_PREMIUM) {
		rw += this.getPremiumEntry(true, false,'C');
		rw += this
				.getButtons("C", true, true, this.isOwner, true, false, false);
	} else if (this.type == CONST_PRODUCT) {
		rw += this.getProductEntry(true, false,'C');
		rw += this
				.getButtons("C", true, true, this.isOwner, true, false, false);
	} else if (this.type == CONST_GOOGLE_CLIPPED || this.type == CONST_GOOGLE
			|| this.type == CONST_GOOGLE_CLIPPED_LINKED) {
		rw += this.getGoogleEntry(true, false,'C');
		rw += this.getButtons("C", true, true, true, true, true, true);

		// container für weiteren text
		rw += "<div id=\"more" + this.name + "\">";
	
	}

	return rw;

};

RES.prototype.getLink2Go = function(context) {
	
	if (!(this.result&&this.result.absoluteLink))return "";
	
	var linkclass = "";
	switch (context) {
	case 'C':
		linkclass = "link2go_content";
		break;
	case 'R':
		linkclass = "link2go_right";
		break;
	case 'N':
		linkclass = "link2go_navi";
		break;
	}
	return "<div class=\"" + linkclass+ "\">Link2go:<br/><input title=\"click2copy / doubleclick2open\" onclick=\"selectAll(this)\" ondblclick=\"newWin(this.value)\"  readonly type=text value=\"" + this.result.absoluteLink	+ "\"></div>";

};

RES.prototype.getHTMLRight = function() {
	var rw = "";
	if (this.type == CONST_USER) {
		rw = "<div class=\"content_user_right\" onclick=\"sr.click("
				+ this.name + ");sr.stopEvent(event)\"";
		rw += " id=\"adr" + this.name + "\">";

		rw += "<img id=\"MARKERRIGHT" + this.name + "\" src=\""
				+ this.getIcon(true) + "\" ><b>"
				+ this.result.titleNoFormatting + "</b><br/>";

		rw += this.getUserButtons("R");

	} else if (this.type == CONST_PREMIUM || this.type == CONST_PRODUCT) {
		rw = "<div onclick=\"return sr.click("
				+ this.name
				+ ")\"  class=\"content_right"
				+ (this.opened != null && this.opened == this.name ? "_saved"
						: "") + "\" style=\"clear:both\"  id=\"adr" + this.name
				+ "\">";
		rw += this.getMemoMarker();
		if (this.type == CONST_PREMIUM)
			rw += this.getPremiumEntry(false, false,'R');
		else if (this.type == CONST_PRODUCT)
			rw += this.getProductEntry(false, false,'R');

		rw += this
				.getButtons("R", true, true, this.isOwner, true, false, false);
	} else if (this.type == CONST_GOOGLE_CLIPPED || this.type == CONST_GOOGLE
			|| this.type == CONST_GOOGLE_CLIPPED_LINKED) {
		rw = "<div onclick=\"return sr.click("
				+ this.name
				+ ")\"  class=\"content_right"
				+ (this.opened != null && this.opened == this.name ? "_saved"
						: "") + "\" style=\"clear:both\"  id=\"adr" + this.name
				+ "\">";
		rw += this.getMemoMarker();
		rw += this.getGoogleEntry(false, false,'R');
		rw += this.getButtons("R", true, true, true, true, true, true);
	} else {
		rw += this.type + " undefinded";
	}

	rw += "</div>";
	return rw;
};

RES.prototype.getUserButtons = function(context) {
	var rw = "<div style=\"margin-top:10px;margin-bottom:20px;\">";
	rw += this.getNaviIcon(context);
	if (sr.user.loggedIn) {
		rw += "<div class=\"top_button_small\" title=\""
				+ TEXT_HOME
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"return sr.user.goHome();sr.stopEvent(event)\">&Delta;</div>";
		rw += "<div class=\"top_button_small\" title=\""
				+ TEXT_PERSONALAREA
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"sr.openHelpWindow('EDITPA');sr.stopEvent(event)\">&rsaquo;&nbsp;&Xi;</div>";
		rw += "<div class=\"top_button_small\" title=\""
				+ TEXT_PROFILE
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"sr.openHelpWindow('EDITUSER');sr.stopEvent(event)\">&rsaquo;&nbsp;<img src=\"images/littleman.gif\"></div>";
	} else {
		rw += "<div class=\"top_button_small\" title=\""
				+ TEXT_REGISTER
				+ "\"  onmousedown=\"greenBut(this);\" onmouseup=\"normBut(this);\" onclick=\"sr.openHelpWindow('REGISTER');sr.stopEvent(event)\"><b>*</b></div>";
	}
	rw += "</div><br/>";
	return rw;
};

RES.prototype.getButtons = function(context, isDiv, isMemo, isEdit, isMail,
		isGoogle, isGoogleClipped) {
	var rw = "";
	if (isDiv)
		rw += "<div  style=\"margin-top:10px;margin-bottom:20px;\">";

	rw += this.getNaviIcon(context);

	if (isMemo)
		rw += this.getMemoButton(context);
	if (isEdit)
		rw += this.getEditButton();
	if (isMail)
		rw += this.getMailButton();
	if (isGoogle) {
		var googleLinks = "<a class=\"extern\" title=\"Show result in Google\" target=_blank href=\""
				+ this.result.url + "\">Google</a>";
		googleLinks += " <a class=\"extern\" title=\"Google Route 'hierher'\" target=_blank href=\""
				+ this.result.ddUrlToHere
				+ "\">&rArr;</a> <span style=\"color:#979694\">&loz;</span> ";
		googleLinks += "<a class=\"extern\" title=\"Google Route 'von hier'\" target=_blank href=\""
				+ this.result.ddUrlFromHere + "\">&rArr;</a>";
		googleLinks = "<div style=\"margin-right:10px;float:right\">"
				+ googleLinks + "</div>";
		rw += googleLinks;
	}
	if (isDiv)
		rw += "</div>";
	rw += "<div style=\"clear:both\"></div>";
	if (isGoogleClipped && this.modified) {
		rw += "<div class=clippedByGoogle>Clipped by Google [" + this.modified
				+ "]</div>";
	}

	return rw;
};

RES.prototype.getHTMLNavigation = function() {
	var rw = "";
	if (this.type == CONST_USER) {
		rw = "<div class=\"content_right_navi\" onclick=\"return sr.click("
				+ this.name + ")\"  id=\"navi" + this.name
				+ "\"><img src=\"images/marker_34_white.png\" align=right>";
		rw += "<img  src=\"images/man.png\" >";
		rw += "<b>" + this.result.titleNoFormatting + "</b><br/>";
		rw += "</div>";
	} else if (this.type == CONST_PRODUCT || this.type == CONST_PREMIUM) {
		rw = "<div class=\"content_right_navi" + (this.saved ? "_saved" : "")
				+ "\"  onclick=\"return sr.click(" + this.name
				+ ")\"  id=\"navi" + this.name
				+ "\"><img src=\"images/marker_34_white.png\" align=right>";
		if (this.type == CONST_PREMIUM) {
			rw += this.getPremiumEntry(false, false,'N');
		} else if (this.type == CONST_PRODUCT) {
			rw += this.getProductEntry(false, false,'N');
		}
		rw += this.getButtons("N", true, false, true, true, false, false);
		rw += "</div>";
	} else {
		rw = "<div onclick=\"return sr.click(" + this.name
				+ ")\"  class=\"content_right_navi"
				+ (this.saved ? "_saved" : "") + "\"  id=\"navi" + this.name
				+ "\">";
		rw += "<img src=\"images/marker_34_white.png\" align=right>";

		rw += this.getGoogleEntry(false, false,'N');
		rw += this.getButtons("N", true, false, true, true, true, true);
		rw += "</div>";

	}
	return rw;
};

RES.prototype.getHTMLPrint = function() {
	var rw = "";
	if (this.type == CONST_USER) {
		rw += "<img id=\"MARKERRIGHT" + this.name + "\" src=\""
				+ this.getIcon(true) + "\" ><b>"
				+ this.result.titleNoFormatting + "</b>";
	} else if (this.type == CONST_PRODUCT || this.type == CONST_PREMIUM) {
		if (this.type == CONST_PREMIUM) {
			rw += this.getPremiumEntry(true, true,'P');
		} else if (this.type == CONST_PRODUCT) {
			rw += this.getProductEntry(true, true,'P');
		}
	} else {
		rw += this.getGoogleEntry(true, true,'P');
		this.getButtons("P", false, false, false, false, false, true);
	}
	return rw;
};

RES.prototype.getHTML = function(htmlType) {
	switch (htmlType) {
	case 0: /* window im content */
		return this.getHTMLContent();
	case 1: /* div rechts */
		return this.getHTMLRight();
	case 2: /* navigationsausgabe */
		return this.getHTMLNavigation();
	case 3: /* Print */
		return this.getHTMLPrint();
	default:
		return null;
	}

};

RES.prototype.getIcon = function(txt) {
	if (txt) {
		if (this.checked) {
			if (this.saved)
				return "images/icon_green_saved.png";
			else
				return "images/icon_green.png";
		}
		switch (this.type) {
		case CONST_GOOGLE_CLIPPED:
			if (this.saved)
				return "images/icon_white_saved.png";
			else
				return "images/icon_white.png";
		case CONST_GOOGLE_CLIPPED_LINKED:
			if (this.saved)
				return "images/icon_white_saved.png";
			else
				return "images/icon_white.png";
		case CONST_USER:
			return "images/man.png";
		case CONST_PREMIUM:
			if (this.saved)
				return "images/icon_white_saved.png";
			else
				return "images/icon_white.png";
		case CONST_PRODUCT:
			if (this.saved)
				return "images/icon_white_saved.png";
			else
				return "images/icon_white.png";
		default:
			return "images/icon_grey.png";
		}
	} else {
		if (this.checked) {
			if (this.saved)
				return icongreen_saved;
			else
				return icongreen;
		}
		switch (this.type) {
		case CONST_GOOGLE_CLIPPED:
			if (this.saved)
				return iconwhite_saved;
			else
				return iconwhite;
		case CONST_GOOGLE_CLIPPED_LINKED:
			if (this.saved)
				return iconwhite_saved;
			else
				return iconwhite;
		case CONST_PREMIUM:
			if (this.saved)
				return iconwhite_saved;
			else
				return iconwhite;
		case CONST_PRODUCT:
			if (this.saved)
				return iconwhite_saved;
			else
				return iconwhite;
		default:
			return icongrey;
		}
	}
};

function callBackUserMarker() {
	var newPoint = user_marker.getLatLng();
	sr.user.setPoint(newPoint);
	new Ajax.Request('transact?mode=homepoint&lat=' + newPoint.y + '&lng='
			+ newPoint.x + '&rnd=' + Math.random(), {
		method : 'post'
	});
	sr.updateUser();
	if (GLOBAL_ROUTING > 0) {
		directions.clear();
		directions
				.loadFromWaypoints(
						sr.getRoutingPoints(),
						{
							travelMode : GLOBAL_ROUTING == CONST_PED_ROUTING ? G_TRAVEL_MODE_WALKING
									: G_TRAVEL_MODE_DRIVING,
							getPolyline : true,
							getSteps : true,
							preserveViewport : true
						});
	}

}

function SR() {
	this.hits = new Array(); // ergebnisse
	this.navigation = new Array();// markierung
	this.tooltips = new Array();// markierung
	this.opened = null;
	this.user = null;
	this.entry = null;
	this.searchIsActive = false;
}

SR.prototype.getRouteMemo = function() {
	var memo = new Array();
	for ( var i = 0; i < this.navigation.length; i++) {
		if (this.hits[this.navigation[i]].type == CONST_PREMIUM) {
			memo.push("E" + this.hits[this.navigation[i]].result.entry.pid);
		} else if (this.hits[this.navigation[i]].type == CONST_PRODUCT) {
			memo.push("P" + this.hits[this.navigation[i]].result.product.id);
		} else if (this.hits[this.navigation[i]].type == CONST_USER) {
			memo.push(0);
		} else {
			memo.push("R" + this.hits[this.navigation[i]].result.id);
		}
	}
	return memo;
};

SR.prototype.getLocationMemo = function(name) {
	var memo = new Array();
	if (name) {
		if (this.hits[name].type == CONST_PREMIUM) {
			memo.push("E" + this.hits[name].result.entry.pid);
		} else if (this.hits[name].type == CONST_PRODUCT) {
			memo.push("P" + this.hits[name].result.product.id);
		}

		else {
			memo.push("R" + this.hits[name].result.id);
		}

	} else {
		for ( var i = 0; i < this.hits.length; i++) {
			if (this.hits[i].saved) {
				if (this.hits[i].type == CONST_PREMIUM) {
					memo.push("E" + this.hits[i].result.entry.pid);
				} else if (this.hits[i].type == CONST_PRODUCT) {
					memo.push("P" + this.hits[i].result.product.id);
				} else {
					memo.push("R" + this.hits[i].result.id);
				}

			}
		}
	}
	return memo;
};

SR.prototype.viewSavedRoute = function(entryList, lat, lng, info) {
	initRouting = info;
	this.viewSavedLocations(entryList, lat, lng);
};

SR.prototype.drawSaved = function(titleList) {
	initLocationArray = titleList.split(",");
	initLocations = initLocationArray.length;
	initLocationCounter = 0;
	this.getNextLocation();
};

var initLocationCounter = 0;
SR.prototype.getNextLocation = function() {
	if (initLocationCounter < initLocations) {
		this.updateByTitle(initLocationArray[initLocationCounter]);
	} else {
		if (initRouting != "") {
			/*
			 * if (routingUserPosition){ this.user.homePoint=homePoint;
			 * this.moveUser(homePoint); }
			 */
			this.doInitRouting();
		} else {
			this.show();
			if (openFirst && sr.hits.length > 1) {
				this.click(1);
				openFirst = false;
			}

		}
		initLocations = 0;
		initRouting = "";
		this.zoom(1);
	}
	initLocationCounter++;
};

SR.prototype.viewSavedLocations = function(entryList, lat, lng) {
	this.user.setLatLng(lat, lng);
	if (entryList && entryList.length > 0) {
		this.clearWiki();
		this.clear();
		if (!initRouting)
			this.restoreMem();
		sr.clearHelpWindow();
		alertOK(TEXT_WAIT);
		new Ajax.Request('transact?mode=getlocations&rnd=' + Math.random(), {
			method : 'post',
			postBody : JSON.stringify(entryList),
			onSuccess : function(transport) {
				if (transport.responseText) {
					result = transport.responseText.evalJSON(true);
					if (result.status == 0)
						alertERR(result.error);
					else {
						if (result.userpos && result.userpos != "null") {
							routingUserPosition = result.userpos;
						}
						sr.drawSaved(result.object);
					}

				} else
					alertERR(TEXT_NOSERVERRESPONSE);
			}// endfunction
		});
	}
};

SR.prototype.mailRoute = function() {
	this.openMailer(this.getRouteMemo(), "MAILROUTE");
};
SR.prototype.mailMemo = function(name) {
	this.openMailer(this.getLocationMemo(name), "MAILMEMO");
};

SR.prototype.saveRoute = function() {
	this.saveMemoNow(this.getRouteMemo(), "addmemroute",
			(GLOBAL_ROUTING == CONST_PED_ROUTING ? "P" : "C"));
};
SR.prototype.saveMemo = function(name) {
	this.saveMemoNow(this.getLocationMemo(name), "addmemlocations", "");
};

SR.prototype.saveMemoNow = function(memo, mode, info) {
	if (!this.user.loggedIn) {
		alertERR(LOGIN_OR_REGISTER);
		return;
	}
	if (memo.length > 0) {
		var d = new Date();
		title = getNowFormatted(true, true);

		new Ajax.Request('transact?lat=' + this.user.homePoint.y + '&lng='
				+ this.user.homePoint.x + '&mode=' + mode + '&title='
				+ escape(title) + '&i=' + info + '&rnd=' + Math.random(), {
			method : 'post',
			postBody : JSON.stringify(memo),
			onSuccess : function(transport) {
				if (transport.responseText) {
					result = transport.responseText.evalJSON(true);
					if (result.status == '0')
						alertERR(result.error);
					else if (result.status == '1') {
						alertOK(result.message);
						if ($('help_canvas').visible()) {
							sr.openHelpWindow('EDITPA');
						}
					}
				} else
					alertERR(TEXT_NOSERVERRESPONSE);

			}// endfunction
		});
	}
};

SR.prototype.openMailer = function(memo, mode) {
	if (memo.length > 0) {
		var url = 'transact?lat=' + this.user.homePoint.y + '&lng='
				+ this.user.homePoint.x + '&mode=' + mode + '&rnd='
				+ Math.random();
		new Ajax.Request(url, {
			method : 'post',
			postBody : JSON.stringify(memo),
			onSuccess : function(transport) {
				if (transport.responseText == "ok") {
					var url = 'help?mode=' + mode + '&rnd=' + Math.random();
					$('help_canvas').show();
					window.HELPFRAME.location = APPLICATION + "/" + url;
				} else
					alertERR(transport.responseText);

			}// endfunction
		});
	}
};

SR.prototype.stopEvent = function(e) {
	Event.stop(e);

};

SR.prototype.moveUser = function(point) {
	this.user.setPoint(point);
	this.updateUser();
	this.show();
};

var entry_marker = null;

SR.prototype.drawEntryMarker = function(entryObject) {
	if (entry_marker != null) {
		map.removeOverlay(entry_marker);
	}
	this.entry = entryObject;
	if (this.entry.point) {
		entry_marker = createSpotMarker(2, this.entry.point, this.entry.title,
				false);
		map.addOverlay(entry_marker);
		map.setCenter(this.entry.point);
	}
};

var product_marker = null;

SR.prototype.drawProductMarker = function(productObject) {
	if (product_marker != null) {
		map.removeOverlay(product_marker);
	}
	this.product = productObject;
	if (this.product.point) {
		product_marker = createSpotMarker(3, this.product.point,
				this.product.title, false);
		map.addOverlay(product_marker);
		map.setCenter(this.product.point);
	}
};

SR.prototype.drawUserMarker = function() {

	if (user_marker != null) {
		map.removeOverlay(user_marker);
	}

	var spottype = 1;
	switch (GLOBAL_ROUTING) {
	case CONST_PED_ROUTING:
		spottype = 4;
		break;
	case CONST_CAR_ROUTING:
		spottype = 5;
		break;
	}
	user_marker = createSpotMarker(spottype, this.user.homePoint,
			this.user.nickname + "", true);

	// anlegen wenn nicht da
	if (this.hits.length == 0) {
		this.hits[0] = this.user.getResult();
		this.hits[0].point = this.user.homePoint;
	} else if (this.hits.length > 0) {
		this.hits[0].marker = user_marker;
		this.hits[0].point = this.user.homePoint;
	}
	map.addOverlay(user_marker);
	GEvent.addListener(user_marker, "dragend", callBackUserMarker);

};

SR.prototype.login = function(email, pass) {
	new Ajax.Request('transact?email=' + email + '&pass=' + pass + '&rnd='
			+ Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText != "ko") {
				if (transport.responseText.startsWith("ERR")) {
					var errorTxt = transport.responseText.substring(4);
					alertERR(errorTxt);
					if (errorTxt.startsWith("Activation-Error"))
						sr.openHelpWindow('REACTIVATE');
					else
						sr.openHelpWindow('FORGETPASSWORD');
				} else {
					sr.user = new USER();
					sr.user.login(transport.responseText.evalJSON(true));
					$('user_content').update($('loggedin_source').innerHTML);
					sr.switchOffRouting();
					sr.navigation = new Array();
					sr.clearHelpWindow();
					sr.moveUser(sr.user.homePoint);
					goHome();
				}
			}

		}// endfunction
	});
};

SR.prototype.updateUser = function() {
	this.updateNick();
	if (this.hits.length > 0) {
		this.hits[0] = this.user.getResult();
		this.hits[0].point = this.user.homePoint;
	}

};

SR.prototype.updateNick = function() {
	$('nickname').update(
			"<a href=\"#\" title=\"" + TEXT_PERSONALAREA
					+ "\" onclick=\"return sr.openHelpWindow('EDITPA')\">"
					+ this.user.nickname + "</a>&nbsp;&nbsp;");

};

SR.prototype.logout = function() {
	this.clear();
	this.clearWiki();
	this.clearHelpWindow();
	this.restoreMem();
	new Ajax.Request('transact?logout=true&rnd=' + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText && transport.responseText != "ko") {
				sr.user = new USER();
				sr.user.logout();
				sr.switchOffRouting();
				sr.user.setPoint(homePoint);
				goHome();
				$('user_content').update($('login_source').innerHTML);
				if (sr.hits.length > 0) {
					sr.hits[0] = sr.user.getResult();
					sr.hits[0].point = homePoint;
				}
				sr.show();
			} else {
				alertERR(transport.responseText);
			}
		}// endfunction
	});

};

SR.prototype.showElement = function(title) {
	this.clear();
	this.updateByTitle(title);
	this.restoreMem();

};

SR.prototype.updateByTitle = function(title) {
	new Ajax.Request('search?title=' + title + "&rnd=" + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			var nr = -1;
			if (transport.responseText != "ko") {
				var results = transport.responseText.evalJSON(true);
				var nr = sr.push(results[0]); // push or update
		sr.show();
		if (initLocations > 0) {
			sr.getNextLocation();
		} else {
			if (nr > -1) {
				map.setCenter(sr.hits[nr].point);
				sr.click(nr);
			}
		}
	} else {
		alertERR(TEXT_NOT_FOUND + title);
	}
}// endfunction
	});
};

SR.prototype.update = function(id) {
	new Ajax.Request('search?id=' + id + "&rnd=" + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText != "ko") {
				var showIt = false;
				var results = transport.responseText.evalJSON(true);
				if (sr.hits.length == 0) {
					sr.push(results[0]);
					showIt = true;
				} else {
					for ( var i = 0; i < sr.hits.length; i++) {
						if (sr.hits[i].result.id == id) {
							// austauschen
							sr.hits[i] = new RES(results[0], sr.hits[i].name,
									sr.getResultType(results[0]));
							showIt = true;
						}
					}
					if (!showIt) {
						sr.push(results[0]);
						showIt = true;
					}
				}

				if (showIt) {
					sr.show();
				}

			} else {
				alertERR(TEXT_NOT_FOUND + id);
			}
		}// endfunction
	});
};

SR.prototype.restoreMem = function() {
	// map.clearOverlays();
	// this.hits.push(this.user.getResult());
	for ( var i = 0; i < mem.mems.length; i++) {
		mem.mems[i].name = this.hits.length;
		mem.mems[i].createMarker(mem.mems[i].result.titleNoFormatting);
		this.hits.push(mem.mems[i]);
	}
};

SR.prototype.toggleWiki = function() {
	if (wikiLayer) {
		this.clearWiki();
	} else {
		greenBut($('wikibutton'));
		// $('wikibutton').src = "images/wiki_pressed.gif";
		wikiLayer = new GLayer("org.wikipedia.de");
		map.addOverlay(wikiLayer);
	}
};

SR.prototype.clearWiki = function() {
	if (wikiLayer) {
		map.removeOverlay(wikiLayer);
		wikiLayer = null;
		normBut($('wikibutton'));
		// $('wikibutton').src = "images/wiki.gif";
	}
};

SR.prototype.doSearch = function() {
	if (this.searchIsActive)
		return false;
	this.searchIsActive = true;
	this.clearWiki();
	this.clear();
	this.restoreMem();
	sr.clearHelpWindow();
	this.show();
	var sss = $F('search');
	printSearch = sss;
	this.searchEntries(sss);
	// this.searchGoogle(sss);
	return false;
};

SR.prototype.click = function(name) {
	if (name == 0) {// user clicked
		if (this.user.loggedIn) {
			this.openHelpWindow('EDITPA');
		} else {
			this.openHelpWindow('REGISTER');
		}
		this.hits[name].marker.openInfoWindowHtml(this.hits[name].getHTML(0));
	} else {
		this.clearWiki();
		if (this.opened != null && this.opened != name)
			this.uncheck(this.opened);
		if (this.hits[name].checked) {
			this.uncheck(name);
		} else {
			this.hits[name].checked = true;
		}

		if (this.hits[name].checked) {
			this.hits[name].marker.openInfoWindowHtml(this.hits[name]
					.getHTML(0));
			this.opened = name;
		} else {
			this.opened = null;
			this.hits[name].marker.closeInfoWindow();
		}
		this.highLight(this.opened);
		// icons auf karte nur ändern, wenn routing ausgeschaltet ist
		this.hits[name].updateImages(false);
		this.updatePrint();
	}
	sr.updateNavigtionImages();

};

SR.prototype.naviclick = function(name) {
	if (this.hits[name].isNavigation) {
		this.hits[name].isNavigation = false;
		this.navigationPop(name);
	} else {
		this.hits[name].isNavigation = true;
		this.navigationPush(name);
	}
	if (this.countRouting() > 1) {
		$('routing_button').show();
	} else {
		$('routing_button').hide();
	}
	this.checkScreen();
	return false;
};

SR.prototype.checkScreen = function() {
	if ($('routing_button').visible() && $('mail_memo').visible()) {
		$('main_title').hide();
	} else {
		$('main_title').show();
	}
};

SR.prototype.uncheck = function(name) {

	this.hits[name].checked = false;
	this.hits[name].updateImages(GLOBAL_ROUTING == 0);

	$('adr' + this.opened).setStyle( {
		backgroundColor : '#FFFFFF'
	});
	if ($('navi' + this.opened)) {
		$('navi' + this.opened).setStyle( {
			backgroundColor : '#FFFFFF'
		});
	}
	sr.updateNavigtionImages();
};

SR.prototype.highLight = function(name) {
	if (name != null) {
		$('adr' + name).setStyle( {
			backgroundColor : '#FFFDBE'
		});
		if ($('navi' + name)) {
			$('navi' + name).setStyle( {
				backgroundColor : '#FFFDBE'
			});
		}
	}
};

SR.prototype.save = function(hitName) {
	this.hits[hitName].saved = !this.hits[hitName].saved;
	var drin = false;
	// var cont = "CRN";//possible contexts: content,right,navigation

	if (this.hits[hitName].saved) {
		mem.push(this.hits[hitName]);
		/*
		 * for ( var i = 0; i < 3; i++) { if ($('but' + cont.charAt(i) +
		 * hitName)) greenBut($('but' + cont.charAt(i) + hitName)); }
		 */
		drin = true;
	} else {
		mem.pop(this.hits[hitName]);
		/*
		 * for ( var i = 0; i < 3; i++) { if ($('but' + cont.charAt(i) +
		 * hitName)) normBut($('but' + cont.charAt(i) + hitName)); }
		 */
	}
	this.hits[hitName].updateImages(false);
	if (!drin) {
		for ( var i = 0; i < this.hits.length; i++) {
			if (this.hits[i].saved) {
				drin = true;
				$('mail_memo').show();
				break;
			}
		}
	} else {
		$('mail_memo').show();
	}
	if (!drin) {
		$('mail_memo').hide();
	}
	this.checkScreen();
	this.updatePrint();
	sr.updateNavigtionImages();
	return false;
};

SR.prototype.enter = function(hitName) {
	this.opened = hitName;
	if (this.hits[hitName].type == CONST_PREMIUM) {
		this.openHelpWindowPreloadId('PREMENTRY', 'entry',
				this.hits[hitName].result.entry.pid);
	} else if (this.hits[hitName].type == CONST_PRODUCT) {
		this.openHelpWindowPreloadId('PRODUCT', 'product',
				this.hits[hitName].result.product.id);
	} else {
		this.hits[hitName].marker.openInfoWindowHtml(this.hits[hitName]
				.getHTML(0));
		var html = "";
		// if (CONST_GOOGLE_CLIPPED_LINKED != this.hits[hitName].type) {
		html += $('txt_enter_website').innerHTML;
		// }
		html += $('txt_enter_premium').innerHTML;

		if ($('more' + hitName)) {
			$('more' + hitName).update(html);
		}
	}

};

SR.checkURL = function(url) {
	if (url.indexOf(".") < 0) {
		top.alertERR(ERR_noValidInetAddress);
		return "";
	}
	if (url.indexOf("://") < 0) {
		url = "http://" + url;
	}
	return url;
};

SR.prototype.doEnter = function() {
	var value = $F('enter');
	value = SR.checkURL(value);
	var id = this.hits[this.opened].result.id;
	var lat = this.hits[this.opened].result.lat;
	var lng = this.hits[this.opened].result.lng;
	if (lat && lng) {
		new Ajax.Request('save?lat=' + lat + '&lng=' + lng + '&id=' + id
				+ '&w=' + escape(value) + "&rnd=" + Math.random(), {
			method : 'post',
			onSuccess : function(transport) {
				if (transport.responseText == "ko") {
					top.alertERR(ERR_noValidInetAddress);
				} else if (transport.responseText == "nid") {
					top.alertERR(ERR_noValidResult);
				} else if (transport.responseText == "nor") {
					top.alertERR(ERR_noBaseEntry);
				} else if (transport.responseText == "nad") {
					top.alertERR(ERR_addressNotFoundOnServer);
					sr.openHelpWindow('BASIC');
				} else if (transport.responseText == "ok") {
					top.alertOK(OK_thankyouBaseEntry);
					sr.update(id);
				}
			}
		});
	} else {
		alertERR("Error on location!");
	}

	return false;
};

SR.prototype.doInitRouting = function() {
	if (routingUserPosition != "")
		routingUserPosition = routingUserPosition * 1;
	else
		routingUserPosition = -1;
	var cnt = 0;

	for ( var i = 1; i < this.hits.length; i++) {
		this.hits[i].isNavigation = true;
		if (cnt == routingUserPosition) {
			this.navigationPush(0);
		}
		cnt++;
		this.navigationPush(i);
		if (i == this.hits.length - 1 && cnt == routingUserPosition) {
			this.navigationPush(0);
		}
	}
	$('routing_button').show();
	var peddy = (initRouting == "P");
	initRouting = "";
	this.doRouting(peddy);
	this.checkScreen();

};

SR.prototype.makeTooltips = function() {
	$$("#info_canvas img.help").each(function(input) {
		sr.tooltips.push(new Tooltip(input, {
			mouseFollow : true,
			backgroundColor : "#FF0000",
			borderColor : "#333",
			textColor : "#FFF",
			textShadowColor : "#000"
		}));
	});
};

SR.prototype.searchEntries = function(sss) {
	if (!sss || sss == "")
		return;
	$('news_info').update("NEWS");
	SR.showStatus("START SEARCH ENTRIES");
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();

	new Ajax.Request('search?e=1&q=' + escape(sss) + "&lat="
			+ map.getCenter().lat() + "&lng=" + map.getCenter().lng()
			+ "&latspan=" + (latSpan / 2) + "&lngspan=" + (lngSpan / 2)
			+ "&rnd=" + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText != "ko") {
				var result = transport.responseText.evalJSON(true);
				if (result) {
					// array
		for ( var i = 0; i < result.length; i++) {
			sr.push(result[i]);
		}

		// nicht genug
		if (result.length < 50) {
			sr.searchProducts(sss);
		} else {
			SR.showStatus((result.length - 1) + " HITS");
			sr.searchIsActive = false;
		}
		sr.show();
		if (sr.hits.length == 1) {
			sr.click(sr.hits[0].name);
		}

	}
} else {// ko
	sr.searchProducts(sss);
}

}// endfunction
	});
};

SR.prototype.searchProducts = function(sss) {
	if (!sss || sss == "")
		return;
	SR.showStatus("SEARCH PRODUCTS");

	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();

	new Ajax.Request('search?p=1&q=' + escape(sss) + "&lat="
			+ map.getCenter().lat() + "&lng=" + map.getCenter().lng()
			+ "&latspan=" + (latSpan / 2) + "&lngspan=" + (lngSpan / 2)
			+ "&rnd=" + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText != "ko") {
				var result = transport.responseText.evalJSON(true);
				if (result) {
					// array
		for ( var i = 0; i < result.length; i++) {

			sr.push(result[i]);
		}

		// nicht genug
		if (result.length < 50) {
			sr.searchPortal(sss);
		} else {
			SR.showStatus((result.length - 1) + " Treffer");
			sr.searchIsActive = false;
		}
		sr.show();
		if (sr.hits.length == 1) {
			sr.click(sr.hits[0].name);
		}

	}
} else {// ko
	sr.searchPortal(sss);
}

}// endfunction
	});
};

SR.prototype.searchPortal = function(sss) {

	if (!sss || sss == "")
		return;
	SR.showStatus("SEARCH PORTAL");

	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();

	new Ajax.Request('search?q=' + escape(sss) + "&lat="
			+ map.getCenter().lat() + "&lng=" + map.getCenter().lng()
			+ "&latspan=" + (latSpan / 2) + "&lngspan=" + (lngSpan / 2)
			+ "&rnd=" + Math.random(), {
		method : 'post',
		onSuccess : function(transport) {
			if (transport.responseText != "ko") {
				var result = transport.responseText.evalJSON(true);
				if (result) {
					// array
		for ( var i = 0; i < result.length; i++) {
			sr.push(result[i]);
		}

		// nicht genug
		if (result.length < 50) {
			sr.searchGoogle(sss);
		} else {
			SR.showStatus((result.length - 1) + " HITS");
			sr.searchIsActive = false;
		}
		sr.show();
		if (sr.hits.length == 1) {
			sr.click(sr.hits[0].name);
		}

	}
} else {// ko
	sr.searchGoogle(sss);
}

}// endfunction
	});
};

SR.showStatus = function(txt) {
	$('status_info').update(txt);
};

SR.prototype.searchGoogle = function(sss) {

	if (!sss || sss == "")
		return;
	SR.showStatus("START SEARCH INTERNET");

	var localSearch = new GlocalSearch();
	var foundSome = false;
	localSearch.setCenterPoint(map);
	localSearch.setResultSetSize(GSearch.LARGE_RESULTSET);
	localSearch.setAddressLookupMode(GlocalSearch.ADDRESS_LOOKUP_DISABLED);

	localSearch.setSearchCompleteCallback(null, function() {
		SR.showStatus("START SEARCH INTERNET +");
		$A(localSearch.results).each(function(result) {
			// alert(JSON.stringify(result));
				sr.push(result);
				foundSome = true;
			});
		SR.showStatus("END SEARCH INTERNET");

		if (foundSome) {
			// sr.show();
			sr.saveResult(sss, localSearch.results);// alle google ergebnisse
			// persistieren
		}
		SR.showStatus((sr.hits.length - 1) + " HITS");
		sr.searchIsActive = false;
		// sr.makeTooltips();
	});
	localSearch.execute(sss);

};

SR.prototype.navigationPush = function(nr) {
	var drin = false;
	var i = 0;
	for (i = 0; i < this.navigation.length; i++) {
		if (nr == this.navigation[i]) {
			drin = true;
		}
	}

	if (!drin) {

		if (this.navigation.length == 20) {
			alertERR(TEXT_NAVI_MAX);
			return;
		}

		this.navigation.push(nr);
		this.updateNavigtionImages();
		this.show();
	}

};

SR.prototype.switchOffRouting = function() {
	if (GLOBAL_ROUTING > 0) {// routing ausschalten
		this.doRouting(GLOBAL_ROUTING == CONST_PED_ROUTING);
	}
};

SR.prototype.navigationPop = function(number) {
	this.switchOffRouting();
	var newNavigation = new Array();
	for ( var i = 0; i < this.navigation.length; i++) {
		if (this.navigation[i] != number) {
			newNavigation.push(this.navigation[i]);
		}
	}
	this.navigation = newNavigation;
	this.show();
};

SR.prototype.getPoints = function() {
	var points = new Array();
	$A(this.hits).each(function(hit) {
		points.push(hit.point);
	});
	return points;
};

SR.prototype.getRoutingPoints = function() {
	var points = new Array();
	for ( var i = 0; i < this.navigation.length; i++) {
		points.push(sr.hits[this.navigation[i]].point);
	}
	return points;
};

SR.prototype.reset = function() {
	this.clear();
	this.navigation = new Array();
	mem = new MEM();
	this.user.goHome();
};

// alle marker löschen
SR.prototype.clear = function() {
	this.clearWiki();
	if (GLOBAL_ROUTING > 0) {
		this.doRouting(GLOBAL_ROUTING == CONST_PED_ROUTING);
	}
	hideNews();
	$('routing_button').hide();
	$('directions_canvas').hide();
	// setMapFocus(true);
	$('info_canvas').show();
	$('info_canvas').update("");
	$('directions_canvas').update("");
	this.navigation = new Array();
	this.opened = null;
	this.hits = new Array();
	map.clearOverlays();
	mem.clearChecked();
	this.drawUserMarker();

};

SR.prototype.countRouting = function() {
	var count = 0;
	$A(this.hits).each(function(hit) {
		if (hit.isNavigation)
			count++;
	});
	return count;
};

var printSearch = "";
SR.prototype.updatePrint = function() {
	var html = "";

	if (GLOBAL_ROUTING > 0) {
		html = $('directions_canvas').innerHTML;
	} else {
		if (this.hits.length > 0) {
			html += this.hits[0].getHTML(3);
		}
		var hitsOut = "";
		if (this.hits.length > 1) {
			for (i = 1; i < this.hits.length; i++) {
				if (this.hits[i].checked && this.hits[i].saved) {
					hitsOut += "<li class=checkedandsaved>";
					hitsOut += "<img src=\"images/icon_white_saved.png\">";
				} else if (this.hits[i].checked) {
					hitsOut += "<li class=checked>";
					hitsOut += "<img src=\"images/icon_white.png\">";
				} else if (this.hits[i].saved) {
					hitsOut += "<li class=saved>";
					hitsOut += "<img src=\"images/icon_green_saved.png\">";
				} else
					hitsOut += "<li>";
				hitsOut += this.hits[i].getHTML(3) + "</li>";
				// if (i%10==0){hitsOut+="</ul><ul>";}
			}
			if ((this.hits.length - 1) % 10 == 0) {
				// hitsOut=hitsOut.substring(0,
				// hitsOut.length-("<ul></ul>".length));
			}
			html += "<div><ul>" + hitsOut
					+ "</ul></div><div style=\"clear:both\"><br/></div>";
		}
	}

	$('print_content').update(html);
};

// alle marker anzeigen
SR.prototype.show = function() {
	for ( var i = 0; i < this.tooltips.length; i++) {
		this.tooltips[i] = null;
	}
	map.clearOverlays();
	this.drawUserMarker();

	var html = "";
	var i = 0;

	if (this.opened != null) {
		this.hits[this.opened].checked = false;
		this.opened = null;

	}

	// render user
	if (this.hits.length > 0) {
		html += this.hits[0].getHTML(1);
	}

	// render hits
	if (this.hits.length > 1) {

		for (i = 1; i < this.hits.length; i++) {
			map.addOverlay(this.hits[i].marker);
			html += this.hits[i].getHTML(1);
		}

		// render powered by
		html += poweredByGoogle;

	}

	$('info_canvas').update(html);

	this.updatePrint();
	this.updateNavigtionImages();
};

SR.prototype.updateNavigtionImages = function() {
	for (i = 0; i < this.navigation.length; i++) {
		var img = 'http://maps.gstatic.com/intl/de_ALL/mapfiles/icon_green' + ABC
				.charAt(i % 26) + '.png';
		if ((this.hits[this.navigation[i]].type != CONST_USER)
				&& (GLOBAL_ROUTING == 0)) {
			this.hits[this.navigation[i]].marker.setImage(img);
		}
		var cont = "CR";// possible contexts: content,right,navigation
		for ( var j = 0; j < 2; j++) {
			var object = $('CI' + cont.charAt(j) + this.navigation[i]);
			if (object)
				object.src = img;
		}
	}
};

/*
 * for (i = 0; i < this.navigation.length; i++) { if
 * (this.hits[this.navigation[i]].type != CONST_USER) {
 * this.hits[this.navigation[i]].marker
 * .setImage('http://maps.gstatic.com/intl/de_ALL/mapfiles/icon_green' + ABC
 * .charAt(i % 26) + '.png'); $('CI' + this.navigation[i]).src =
 * 'http://maps.gstatic.com/intl/de_ALL/mapfiles/icon_green' + ABC .charAt(i) +
 * '.png';
 *  } else { $('CI' + this.navigation[i]).src =
 * 'http://maps.gstatic.com/intl/de_ALL/mapfiles/icon_green' + ABC .charAt(i %
 * 26) + '.png'; }
 *  }
 * 
 */

SR.prototype.center = function(routingOnly) {
	var bounds = new GLatLngBounds;
	var found = false;
	$A(this.hits).each(function(hit) {
		if (routingOnly) {
			if (hit.isNavigation) {
				bounds.extend(hit.point);
			}
		} else {
			bounds.extend(hit.point);
		}
		found = true;
	});
	if (found) {
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	}
};

SR.prototype.zoom = function(mode) {
	var bounds = new GLatLngBounds;
	var found = false;
	switch (mode) {
	case 0:// go home
		goHome();
		break;
	case 1:// alle markierungen anzeigen
		if (this.hits.length > 1)
			$A(this.hits).each(function(hit) {
				bounds.extend(hit.point);
				found = true;
			});

		break;
	case 2:// gewählte markierungen anzeigen
		// wenn routing aktiviert darauf zoomen
		if (GLOBAL_ROUTING > 0 && routingBounds != null) {
			bounds = routingBounds;
			found = true;
		} else {
			if (this.hits.length > 1) {
				// 1. alle markierte
				$A(this.hits).each(function(hit) {
					if (hit.saved) {
						bounds.extend(hit.point);
						found = true;
					}

				});
				// 2. nichts gefunden? dann alle navipunkte, inkl. user
				if (!found) {
					$A(this.hits).each(function(hit) {
						if (hit.isNavigation || hit.name == 0) {
							bounds.extend(hit.point);
							found = true;
						}

					});

				}
			}

		}
		break;
	}
	if (found) {
		map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
	} else {
		if (mode > 0) {
			map.setCenter(homePoint, 13 + mode * 2);
		}
	}
	return false;
};

SR.prototype.saveResult = function(sss, resArr) {
	if (resArr.length > 0) {
		new Ajax.Request('save?q=' + escape(sss) + "&rnd=" + Math.random(), {
			method : 'post',
			postBody : JSON.stringify(resArr),
			onSuccess : function(transport) {
				if (transport.responseText != "ko") {
					var results = transport.responseText.evalJSON(true);
					for ( var i = 0; i < results.length; i++)
						sr.push(results[i]);
					sr.show();
				}
			}
		});
	}
};

SR.prototype.getResultType = function(result) {
	var type = CONST_GOOGLE; // google
	if (result.region == "p") {
		if (result.entry) {
			type = CONST_GOOGLE_CLIPPED_LINKED; // eigenes ergebnis mit eintrag
		} else
			type = CONST_GOOGLE_CLIPPED; // eigenes ergebnis
	} else if (result.region == "e") {
		type = CONST_PREMIUM;
	} else if (result.region == "i") {
		type = CONST_PRODUCT;
	}
	return type;
};

// Suchergebnis hinzufügen, sofern noch nicht vorhanden

SR.prototype.getPosition = function(result) {
	var drin = false;
	var nr = -1;
	var resulttype = this.getResultType(result);

	// if (resulttype==CONST_GOOGLE){ alert(result.city + result.streetAddress+
	// result.titleNoFormatting) }

	for ( var i = 1; i < this.hits.length; i++) {
		if (resulttype == this.hits[i].type) {
			switch (resulttype) {
			case CONST_PRODUCT:
				if (this.hits[i].getId() == result.product.id)
					return i;
				break;
			case CONST_PREMIUM:
				if (this.hits[i].getId() == result.entry.pid)
					return i;
			default:
				if (result.id && this.hits[i].getId() == result.id) {
					return i;
				}
				var hitstring = this.hits[i].result.city
						+ this.hits[i].result.streetAddress
						+ this.hits[i].result.titleNoFormatting;
				var dbstring = result.city + result.streetAddress
						+ result.titleNoFormatting;
				if (hitstring == dbstring) {
					return i;
				}
			}
		} else {// google überschreiben

			var hitstring = this.hits[i].result.city
					+ this.hits[i].result.streetAddress
					+ this.hits[i].result.titleNoFormatting;
			var dbstring = result.city + result.streetAddress
					+ result.titleNoFormatting;
			if (resulttype == CONST_GOOGLE) {

				if (hitstring == dbstring) {
					return i;
				}
			}
			if (this.hits[i].type == CONST_GOOGLE) {
				if (hitstring == dbstring) {
					return i;
				}
			}
		}

	}
	return nr;

};

SR.prototype.push = function(result) {
	var nr = this.getPosition(result);
	var resulttype = this.getResultType(result);
	if (nr > -1) {
		if (this.hits[nr].type == CONST_GOOGLE && resulttype != CONST_GOOGLE) {
			this.hits[nr].update(result, resulttype);
		} else {
			this.hits[nr].smallUpdate(result);
			// this.hits[nr].result.modified = result.modified;
		}
	} else {
		nr = this.hits.length;
		this.hits.push(new RES(result, nr, resulttype));
	}
	return nr;
};

SR.prototype.doRouting = function(peddy) {
	this.clearWiki();
	var tm = G_TRAVEL_MODE_WALKING;
	if (!peddy) {
		tm = G_TRAVEL_MODE_DRIVING;
	}

	// direkt umschalten
	if ((GLOBAL_ROUTING == CONST_CAR_ROUTING && peddy)
			|| (GLOBAL_ROUTING == CONST_PED_ROUTING && !peddy)) {
		GLOBAL_ROUTING = 0;
		normBut($('ped_routing'));
		normBut($('car_routing'));

		// $('ped_routing').src = "images/ped_pressed.gif";
		// $('car_routing').src = "images/car_pressed.gif";
	}

	map.clearOverlays();

	if (GLOBAL_ROUTING > 0) {
		GLOBAL_ROUTING = 0;
		normBut($('ped_routing'));
		normBut($('car_routing'));
		// $('ped_routing').src = "images/ped.gif";
		// $('car_routing').src = "images/car.gif";
		$('directions_canvas').update("");
		$('directions_canvas').hide();
		// setMapFocus(true);
		$('info_canvas').show();
		this.restoreMem();
		this.show();
	} else {

		$('directions_canvas').show();
		// setMapFocus(false);
		$('info_canvas').hide();

		if (peddy) {
			// $('ped_routing').src = "images/ped_pressed.gif";
			// $('car_routing').src = "images/car.gif";
			greenBut($('ped_routing'));
			normBut($('car_routing'));
			GLOBAL_ROUTING = CONST_PED_ROUTING;
		} else {
			normBut($('ped_routing'));
			greenBut($('car_routing'));

			// $('car_routing').src = "images/car_pressed.gif";
			// $('ped_routing').src = "images/ped.gif";
			GLOBAL_ROUTING = CONST_CAR_ROUTING;
		}
		this.drawUserMarker();
		directions.clear();
		directions.loadFromWaypoints(this.getRoutingPoints(), {
			travelMode : tm,
			getPolyline : true,
			getSteps : true,
			preserveViewport : true
		});
	}

	if (GLOBAL_ROUTING === 0)
		setMapFocus(true);

	return false;
};

SR.prototype.showInfoWindow = function(mode) {
	if (imprint_marker == null) {
		var point = new GLatLng(50.945125, 6.939631);
		map.setCenter(point, 13);
		imprint_marker = createSpotMarker(0, point, "norm4", false);
	}
	var html = "";
	map.addOverlay(imprint_marker);
	switch (mode) {
	case 1:
		html = $('imprint').innerHTML;
		break;
	case 2:
		html = $('disclaimer').innerHTML;
		break;
	case 3:
		html = $('agb').innerHTML;
		break;
	case 4:
		$('forgotPasswordResult').hide();
		$('forgotPasswordResult').update("");
		$('forgetEmail').value = $F('email');
		$('confirmCaptcha').value = "";
		$('forgotPasswordForm').show();
		html = $('forgot_password').innerHTML;

		break;

	}
	imprint_marker.bindInfoWindowHtml(html);
	imprint_marker.openInfoWindowHtml(html);
	return false;
};

SR.prototype.clearHelpWindow = function() {
	window.HELPFRAME.location = APPLICATION + "/leer.html";
	$('help_canvas').hide();
};

SR.prototype.openHelpWindowPreloadId = function(mode, submode, id, meta_id) {
	new Ajax.Request('transact?mode=preload&submode=' + submode + '&id=' + id
			+ (meta_id ? "&meta_id=" + meta_id : "") + '&rnd=' + Math.random(),
			{
				method : 'post',
				onSuccess : function(transport) {
					if (transport.responseText) {
						result = transport.responseText.evalJSON(true);
						if (result.status == 1) {
							top.sr.openHelpWindow(mode);
						} else {
							top.alertERR(result.error);
						}
					} else
						top.alertERR(top.TEXT_NOSERVERRESPONSE);

				}// endfunction
			});

};

SR.prototype.openHelpWindow = function(mode) {
	var url = 'help?mode=' + mode;
	if (mode == 'BASIC' || mode == 'PREMIUM')
		url += (!isNaN(sr.opened) ? "&adr="
				+ this.hits[this.opened].result.title : "");
	$('help_canvas').show();
	window.HELPFRAME.location = APPLICATION + "/" + url;
	return false;
};
