function MapTripsControl() {
	var this1 = this;
	var popupImages = {nextIdx:0,imgA:[]};

	this1.trips_source = "flickr";
	var tripPlayControl = null;

	var initTripPlay = function() {
		//
		// first add prototype to TripControl
		// function is passed that will be called on a click
		//
		initTripPlayControl(function(me,pause) {
			//logit("Clicked was:"+me.id+"/pause="+pause);
			switch(me.id) {
				case "tripPlayButton":
					if (intervalId) window.clearTimeout(intervalID); 
				  if (pause) {
				  	logit("Pause pressed");
				  } else {
				  	logit("Play pressed");
				  	tripTimeout();
				  	//tripInterval();
				  }
				  break;
				case "tripStopButton": 
					this1.stopPlay();
				  break;
				case "tripNextButton":
				  logit("next:"+this1.visitIdx);
					this1.stopPlay(true);
					nextMarker("next");
					break;
				case "tripPrevButton":
					logit("prev");
					this1.stopPlay(true);
					nextMarker("prev");
					break;				
				case "tripFirstButton": 
			  	logit("First pressed");
					if (intervalId) window.clearTimeout(intervalID);
					nextSlide("prev");
			  	//tripTimeoutFast();
					//tripIntervalFast();
				  break;
				case "tripEndButton": 
			  	logit("End pressed");
					if (intervalId) window.clearTimeout(intervalID);
					nextSlide("next");
			  	//tripTimeoutFast();
					//tripIntervalFast();
				  break;
			}
		});
		//console.trace();
		tripPlayControl = new TripPlayControl();
		map1.addControl(tripPlayControl);
	}
	this1.stopPlay = function(nostartnew) {
		popupImages = {nextIdx:0,imgA:[]};
		if (intervalId) window.clearTimeout(intervalID);
		if (slideInterval) window.clearTimeout(slideInterval);
		if (!nostartnew) this1.highlightVisitStop(); // do not reset Idx when prev/next pressed
		gmapPopup.hide();
		tripPlayControl.stopPlay();
	}
	function tripTimeout() {
		logit("tripTimeout");
		if (intervalId) window.clearTimeout(intervalID);
		if (tripPlayControl.pause) return;
		
		var point = null;
		var time = 2000;
	
		// First check if there are still images that can be displayed in popup
		if ((popupImages.curIdx + 1) < popupImages.imgA.length) {
			nextSlide("next",tripNextTimeout);
		} else {
			logit("show next marker");
			nextMarker("next",tripNextTimeout);
		}
	}
	function tripNextTimeout() {
		if (tripPlayControl.pause) return;
		
		intervalID = window.setTimeout(function() {
				tripTimeout();
			},2000);
	}
	
	function nextSlide(direction,CB) {
		if (direction == "next") {
			if ((popupImages.curIdx + 1) < popupImages.imgA.length) {
				popupImages.curIdx++;
				imgLoaded(popupImages.imgA[popupImages.curIdx].src,function(img) {
					logit("Next Index/Slide:"+popupImages.curIdx+'/'+img.src);
					gmapPopup.nextImg(popupImages.imgA[popupImages.curIdx],"",CB);
				});
				//img.src = popupImages.imgA[popupImages.curIdx].src;  // show next image
			}
		} else {
			if (popupImages.curIdx > 0) {
				popupImages.curIdx--;
				imgLoaded(popupImages.imgA[popupImages.curIdx].src,function(img) {
					logit("Next Index/Slide:"+popupImages.curIdx+'/'+img.src);
					gmapPopup.nextImg(popupImages.imgA[popupImages.curIdx],"",CB);
				});
				//img.src = popupImages.imgA[popupImages.curIdx].src;  // show next image
			}
		}
	}
	function nextMarker(direction,CB) { 
		logit("displayImg:"+direction);
	
		// first see if there is a next marker 
		var nextIdx = this1.getNextIdx(direction);
	
		if (nextIdx === null) {
			logit("no more markers,end nextMarker");
			this1.stopPlay();
			return;
		}
		// get point to this idx
		var point 		= this1.getPoint(nextIdx);
		var vcity 		= this1.getCity(nextIdx);
		var vcountry	= this1.getCountry(nextIdx);
		var vdate			= this1.getDate(nextIdx);
	 	var img 			= new Image();
		var waitEvent = new WaitEvent(); // because we want to load new image and move marker run parralel
	
		gmapPopup.hide(); // we do not care for callback, this should be finished after all next is finished
		
		// 1.Wait till image is there and loaded
		// 2.till Cursor on next Position
		// 3.map is not moving
		
		waitEvent.addCB(function() {
			logit("waitEvent finshed now show popup");
			//gmapPopup.show(point,vcity,CB);
	
			if (popupImages.imgA.length > 0) {
				gmapPopup.newImg(popupImages.imgA[0],vcity);
			} else {
				gmapPopup.newImg(null,vcity);
			}
			popupImages.curIdx = 0;
			this1.HLmarker.iconready();
			gmapPopup.show(point,function() {
				if (CB)	CB();
			});
		});
	  waitEvent.add("newimg");
	  waitEvent.add("nextmarker");
	  // load new Panoramio images
		logit("getPanoramioImg");
		var img_tags=vcity+","+vcountry;
		var size = "small";
		if ($("#map_container").width() > 1000) {
			size = "medium";
		}		
		switch(this1.trips_source) {
			case 'flickr':
				getFlickrImg(point.lat(),point.lng(),1,img_tags,size,received_img);	
			break;
			case 'picasa':
				getPicasaImg(point.lat(),point.lng(),0.1,img_tags,size,received_img);	
			break;
			default 			: //panoramio
				getPanoramioImg(point.lat(),point.lng(),0.03,size,received_img);	
		}

		function received_img(imgA) {
			if (imgA === null  || imgA.length == 0) {
				popupImages.imgA = [];
				waitEvent.ready("newimg");
			} else {
				popupImages.imgA = imgA; // store array for later use
				imgLoaded(imgA[0].src,function(img) {
					logit("Panoramio"+img.src);
					waitEvent.ready("newimg");
				});
				//img.src = imgA[0].src;  // show first image first
				//logit("Panoramio"+img.src);
			}
		}
		// move cursor HG to new city
		this1.highlightVisit(nextIdx,function() {
			tripPlayControl.setCity(vcity);
			tripPlayControl.setDate(vdate);
	
			if (mapMoving) {
				waitEvent.add("moveend");
				addMoveendCB(function() {
					logit("moveend callback");
					if (this1.HLmarker) this1.HLmarker.iconloading();
					waitEvent.ready("moveend");
				});
			} else {
				if (this1.HLmarker) this1.HLmarker.iconloading();
			}
			waitEvent.ready("nextmarker");
		});
	}
	this1.resetClicked = function() {
 		if (this1.clickedTrip !== null) {
			this1.unhighlight(this1.clickedTrip);
 		}
 		this1.clickedTrip = null; // reset
 		tripPlayControl.hide();
	}
	var initControl = function() {
		//
		// Call this function after the markers have been loaded
		//
		var tripA = this1.markerInfo.trip;

		if (!tripA) {
			logit("Cannot init Control since there is no marker Info");
			return;
		}

		this1.clickedTrip = null; // remember highlighted trip
		this1.mouseoverTrip = null; // remember highlighted trip

		//
		// load tripsControl (latest first)
		//
		$("#trips").empty();
		for (var i = tripA.length - 1; i >= 0 ; i -= 1) {
			logit("found trip:"+tripA[i].name);
			// note ie6 needs href for rollover
	  	$("#trips").append('<li><a id="'+tripA[i].name+'" idx="'+i+'" href="#">'+tripA[i].name+'</a></li>');
		}
		//
		// mouse over events
		//
		$("#trips a").hover(function() {
			//
			// mouseover a trip
			//
			var idx = parseInt($(this).attr("idx"));
	
			if (this1.mouseoverTrip != null) {
				//
				// This because in FireFox the mouseout event is not triggered
				// when mouse moved out via scroll-bar
				//
				this1.unhighlight(this1.mouseoverTrip);
				this1.mouseoverTrip = null;
			}
			this1.mouseoverTrip = idx;
		  logit("mouseover:"+this.id);
			this1.highlight(idx,1);
		},function() {
			//
			// mouseout a trip
			//
			var idx = parseInt($(this).attr("idx"));
			if (this1.mouseoverTrip != null && this1.mouseoverTrip != idx ) {
				this1.unhighlight(this1.mouseoverTrip);
				this1.mouseoverTrip = null;
			}
			if (this1.mouseoverTrip != idx) { 
				// only when this is not clicked 
				this1.unhighlight(idx);
			}
		  logit("mouseout:"+this.id);
		});
		$("#trips a").click(function() {
			//
			// Clicked a Trip
			//
			var idx = parseInt($(this).attr("idx"));
			logit("Clicked:"+this.id+'('+idx+')');
			this1.clickedTrip = idx;
			showText(this.id);
			this1.setZoomCenter(idx);				
			$("#tripbar").hide();
			this1.hide(idx); // hide all markers except this one
			if (this1.clickedTrip == null) {
				this1.highlight(idx,1);
			}
			if (this1.tripHighlighted) {
				tripPlayControl.show();
			}
		});
	}
	this1.loadXmlAndControl = function(xmlfile,show_trip,CB) {
		// trips markers NOT loaded
		if (!show_trip) map1.setCenter(new GLatLng(0,0),2);
		this1.load(null,xmlfile,null,function(xml) {
			logit("ready receiving:"+this1.markerInfo.name);
			this1.show(show_trip);
			if (show_trip && this1.curTripIdx === null) {
				// special starting trip requested was not found
			 	map1.setCenter(new GLatLng(0,0),2);
			}	
			initControl(); // create control bars & setup events
			initTripPlay();  // create play control for a trip
			// if a special show_trip trip was requested at startup
			if (this1.curTripIdx === null) {
				logit("all trips shown");
				// all trips shown
				$("#tripbar").show();
			} else {
				// only one trip shown
				logit("only one trip shown"+this1.curTripIdx);
				//var idx = this1.curTripIdx;
				var trip = this1.markerInfo.trip[this1.curTripIdx];
				showText(trip.name);
				this1.setZoomCenter(this1.curTripIdx);				
				tripPlayControl.show();
			}
			CB();
		},true);
	}
	function logit(text) {
		if (debug) logitAll("MapTripsControl-"+text);
	}
}
logit("extend SlideImage to SlideControl");
MapTripsControl.prototype = new MapTrips();
MapTripsControl.prototype.constructor = MapTripsControl;