var timeoutTimer;
// EVENT: google-detail-search-field keyup with delay
op.initMapEvents = function(){
	op.googleDetailSearch = '';

	jQuery('input.googleDetailSearch',op.context).keyup(function(){	
			var inp = jQuery('input.googleDetailSearch', op.context);
			var inputvalue=inp.val();
			var par = inp.parents('.formsection');		
			if (inp.val().length) {
				op.geocodeAddress(par.find('input.googleMainSearch').val(),inputvalue,op[par.attr('rel')]);	
			}
			else {
				inp.attr('title', '');
				var code = par.find('input.code').val();
				if (code.length) {op.initMapWithCode(par.attr('rel'),code);}
			}
	});

		// TOGGLER
	jQuery('.toggler a, .toggler .icon').click(function(e){
		var t = jQuery(this).blur().parents('.toggler');
		if (!t.hasClass('init') && op.le_ie7 && t.hasClass('initSwitcher')) {
			t.addClass('init').parents('.formsection').find('.switcher').initSwitcher();
		}
		t.find('.show,.hide').toggleClass('dn');	
		if (t.hasClass('show')) {
			t.removeClass('show');
			if (!t.hasClass('map')) {
				jQuery('.vis_' + t.attr('id'),op.context).removeClass('dn');
				t.find('a').text(op.labels.hideExtSearch);
			}
			else {
				jQuery('.vis_' + t.attr('id'),op.context).toggleClass('mapHide');
				t.find('a').text(op.labels.hideMap);
			}
		} else {
			t.addClass('show');
			if (!t.hasClass('map')) {
				jQuery('.vis_' + t.attr('id'),op.context).addClass('dn');
				t.find('a').text(op.labels.showExtSearch);
			}
			else {
				jQuery('.vis_' + t.attr('id'),op.context).toggleClass('mapHide');
				t.find('a').text(op.labels.showMap);
			}
		}
		var forms = jQuery(this).parents('.formsection');
		op.updateSlider([forms.find('.source'),forms.find('.target')]);
		op.addDummyContentForIE();
		return false;
	});

}



// HELPER: load map
	op.initMapWithCode = function(map,code){
		var url = (op.paths.locationObjURL);
		$.ajaxSetup({
			contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
		})
		jQuery.post(url,{locationId:code},function(data){
			if (!op.mapsLoaded) {
				google.load('maps', '3.x', {
					callback: function(){
						op.mapsLoaded = true;
						op.updateLocationObject(data, map);
					op.geocodeAddress(jQuery('input.googleMainSearch').val(),jQuery('input.googleDetailSearch').val(),op[jQuery('input.googleDetailSearch', op.context).parents('.formsection').attr('rel')]);	
					},
					other_params:'sensor=false'
				});
			} else {
				op.updateLocationObject(data, map);
				op.geocodeAddress(jQuery('input.googleMainSearch').val(),jQuery('input.googleDetailSearch').val(),op[jQuery('input.googleDetailSearch', op.context).parents('.formsection').attr('rel')]);	
			}
		},'html');
		$.ajaxSetup({
			contentType: 'application/x-www-form-urlencoded;'
		})	
	
	};

	// HELPER: update googleMap with POI
	op.geocodeAddress = function(mainVal,detailVal,map){
		if (mainVal.length) {
			var geoCoder = new google.maps.Geocoder();		
			geoCoder.geocode({
				address: mainVal + ' ' + detailVal
			}, function(geo, status){			
				(status != google.maps.GeocoderStatus.OK)?$('#googleMap1').addClass('dn'):'';
				if (status == google.maps.GeocoderStatus.OK) {
					if (typeof(geo[0].geometry.bounds) != 'undefined') {
						map.fitBounds(geo[0].geometry.bounds);
					}
					else {
						map.setCenter(geo[0].geometry.location);
						map.setZoom(16);
					}
					//Geocode object transfered to op.locObj via while, for IE Fix
					op.setMarker(map, geo[0].geometry.location);
					op.locObj = {};
					op.locObj.longitude = geo[0].geometry.location.za; 
					op.locObj.latitude = geo[0].geometry.location.xa;
					op.locObj.googleSearch = true;
					op.locObj.destinationName='';
					var loop=true
					var y=0;
					while (loop)
					{
						if (geo[0].address_components[y].types[0] =='street_number'){
							op.locObj.address = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='route' && op.locObj.address){
							op.locObj.address = geo[0].address_components[y].long_name +' '+ op.locObj.address;
						}
						if (geo[0].address_components[y].types[0] =='route' && !op.locObj.address){
							op.locObj.address = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='postal_code' && op.locObj.destinationName){
							op.locObj.destinationName = geo[0].address_components[y].long_name + ' ' + op.locObj.destinationName;
						}
						if (geo[0].address_components[y].types[0] =='postal_code' && !op.locObj.destinationName){
							op.locObj.destinationName = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='locality'){
							op.locObj.destinationName = geo[0].address_components[y].long_name;
							op.locObj.googleDestination = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='country'){
							op.locObj.googleCountryCode = geo[0].address_components[y].short_name;
						}
						y++;
						(typeof geo[0].address_components[y] == 'undefined') ? loop=false:'';			
					}
				}				
				op.setMapFormValues('', op.locObj);		
			});
		} 
	};
	// HELPER: set marker on map
	op.setMarker = function(map,point){
		var mapID = jQuery(map.c).parents('.formsection').attr('id');
		if (typeof(op['marker_' + mapID]) == 'undefined'){
			op['marker_' + mapID] = new google.maps.Marker({position:point,draggable:true});
			op['marker_' + mapID].setMap(map);
			google.maps.event.addListener(op['marker_' + mapID],'dragend',function(latLng){op.handleDraggedMarker(latLng,map);});
		} else {
			op['marker_' + mapID].setPosition(point);
		}
	};

	// EVENT: marker has been dragged
	op.handleDraggedMarker = function(latLng,map){
		var geoCoder = new google.maps.Geocoder();
		var mapID = jQuery(map.c).parents('.formsection').attr('id');
		// alert("muh"+mapID);
		geoCoder.geocode({latLng:latLng.latLng},function(geo,status){
			if (status == google.maps.GeocoderStatus.OK) {
				op.locObj = {};
				op.locObj.longitude = geo[0].geometry.location.za; 
				op.locObj.latitude = geo[0].geometry.location.xa;
				op.locObj.googleSearch = true;
				op.locObj.destinationName='';
					var loop=true
					var y=0;
					while (loop)
					{
						if (geo[0].address_components[y].types[0] =='street_number'){
							op.locObj.address = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='route' && op.locObj.address){
							op.locObj.address = geo[0].address_components[y].long_name +' '+ op.locObj.address;
						}
						if (geo[0].address_components[y].types[0] =='route' && !op.locObj.address){
							op.locObj.address = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='postal_code' && op.locObj.destinationName){
							op.locObj.destinationName = geo[0].address_components[y].long_name + ' ' + op.locObj.destinationName;
						}
						if (geo[0].address_components[y].types[0] =='postal_code' && !op.locObj.destinationName){
							op.locObj.destinationName = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='locality'){
							op.locObj.destinationName = geo[0].address_components[y].long_name;
							op.locObj.googleDestination = geo[0].address_components[y].long_name;
						}
						if (geo[0].address_components[y].types[0] =='country'){
							op.locObj.googleCountryCode = geo[0].address_components[y].short_name;
						}
						y++;
						(typeof geo[0].address_components[y] == 'undefined') ? loop=false:'';			
					}
				/*	Old Code from Florian, doesnt work in IE			
				jQuery.each(geo[0].address_components,function(){
					if (this.types.indexOf('street_number') != -1){
						op.locObj.address += (' ' + this.long_name);
					}
					if (this.types.indexOf('route') != -1){
						op.locObj.address = this.long_name + op.locObj.address;
					}
					if (this.types.indexOf('postal_code') != -1){
						op.locObj.destinationName = this.long_name + ' ' + op.locObj.destinationName;
					}
					if (this.types.indexOf('locality') != -1){
						op.locObj.destinationName += this.long_name;
						op.locObj.googleDestination = this.long_name;
					}
					if (this.types.indexOf('country') != -1){
						op.locObj.googleCountryCode = this.short_name;
					}
				});
				*/
				jQuery('input.googleDetailSearch').val(op.locObj.address).attr('title', op.locObj.address);
				jQuery('input.googleMainSearch').val(op.locObj.destinationName).attr('title', op.locObj.destinationName);
				op.setMapFormValues(jQuery('#' + mapID), op.locObj);
			}
		});
	};
	// HELPER: googleLocationObject => update object with response
	op.updateLocationObject = function(data,mapID){
		var inp = jQuery(data).filter('input');
		op.locObj = {};
		for (var i = 0; i < inp.length; i += 1) {
			op.locObj[jQuery(inp[i]).attr('id')] = jQuery(inp[i]).val()
		}
		var domMap = jQuery('#' + mapID);
		//jQuery('[rel=' + mapID + '] input.googleDetailSearch',op.context).val('');
		if (op.mapsLoaded && op.locObj.latitude && op.locObj.longitude) {
			domMap.removeClass('dn');
			op.addDummyContentForIE();
			var latLng = new google.maps.LatLng(op.locObj.latitude, op.locObj.longitude);
			if (typeof(op[mapID]) == 'undefined') {
				op[mapID] = new google.maps.Map(domMap.find('.googleMap:first').get()[0],
					{
						center:latLng,
						disableDefaultUI:false,
						zoom:12,
						mapTypeId:google.maps.MapTypeId.ROADMAP
					}
				);
				// workaround: load map-detail-param by URL if map is ready
				if (typeof(op.geocodeMapFromUrl) != 'undefined'){
					op.geocodeAddress(op.helperMain, op.helperDetail, op[op.helperMap]);
					jQuery('[rel=' + mapID + '] input.googleDetailSearch',op.context).val(op.shortText(op.helperDetail,35));
				};
			} else {
				op[mapID].setCenter(latLng);
				op[mapID].setZoom(12);
			}
			op.setMarker(op[mapID],latLng);
			op.setMapFormValues(domMap,op.locObj);
		} else {
			domMap.addClass('dn');
		}
	};
	// HELPER: set form-values depending on map
	op.setMapFormValues = function(map,obj){
		/* decommented, because i dont need the map value
		if (typeof(obj.destinationName) != 'undefined') {
			jQuery('[rel=' + map.attr('id') + '] input.googleMainSearch',op.context).val(obj.destinationName);
		}
		*/
		(jQuery('input.googleDetailSearch').val()!='')?jQuery('#googleSearch').val(obj.googleSearch):jQuery('#googleSearch').val('false');
		jQuery('#longitude').val(obj.longitude);
		jQuery('#latitude').val(obj.latitude);
		jQuery('#googleDestination').val(obj.googleDestination);
		jQuery('#googleCountryCode').val(obj.googleCountryCode);
	};



	// HELPER: re-defines height of switcher and wheather is en-/disabled
	op.updateSlider = function(arr){
		for (var i=0; i<arr.length; i+=1){
			var list = arr[i].find('.list').css('margin-top','0');
			list.find('p.empty').remove();
			var hV = arr[i].find('.view').height();
			var hL = list.height();
			var sli = arr[i].find('.slider');
			//alert("hallo?"+sli);
			if(sli != undefined){
				if(sli.slider){
				sli.slider('option',{value:1,disabled:(hV > hL)});
				if (!list.find('p:not(.dn)').length) {
					list.append(jQuery('<p/>').attr('className','empty').html(op.labels.emptySwitcher));
				}
			}
		}
	}
	};
	






