var toursRoutesOverlay = new Array();
var toursRoutes = new Array();
function openTourInfoWindow(marker){
    /*var tab1 = new GInfoWindowTab('Stage','<center><b>Details</b></center><br /><div align=center class=tinyblack >' +
                '<span class=\"style10\">' + marker.days + '</span><br>' +
				marker.desc1 + '</div>');*/
   /* var tab2 = new GInfoWindowTab('Other details','<div align=center class=tinyblack >' + marker.desc2 + '</div>');
    var tabs = new Array(tab1,tab2);*/
	//var tabs = new Array(tab1);
    //marker.openInfoWindowTabsHtml(tabs,{maxWidth:max_width});
    marker.openInfoWindowHtml('<center><b>Details</b></center><br /><div align=center class=tinyblack >' +
                '<span>' + marker.days + '</span><br />' +
				marker.desc1 + '</div>');
	GEvent.addListener(map.getInfoWindow(), "closeclick", function() {
		current_marker = undefined;
	});
}
function openLocationInfoWindow(marker){
	marker.openInfoWindowHtml('<div align="center">' + marker.desc1 + '<br></div>');
}
function createTourMarker(point,order,days,desc1,desc2){	
	icon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
    var marker = new GMarker(point,icon);
	marker.order = order;
	marker.days = parseTagsToHtml(days);
	marker.desc1 = parseTagsToHtml(desc1);
	marker.desc2 = parseTagsToHtml(desc2);
	//path[marker.order] = point;
	
	map.addOverlay(marker);
/*
	GEvent.addListener(marker,'click', function(){
		current_marker = marker;
        openTourInfoWindow(marker);
	});
	*/
	GEvent.addListener(marker,'mouseover', function(){
        openTourInfoWindow(marker);
        current_marker = marker;
    });
    GEvent.addListener(marker,'mouseout', function(){
        map.closeInfoWindow();
        current_marker = null;
    });
	    
    return marker;
}
function createUserTourMarker( point, tourName, linkUrl, tourId, tourType, operatorName ) {
	 // allowed colors:
	 // red, green, blue, brown, purple, white, black, orange, yellow
	if( tourType == 'tour' ) {
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";	
	} else {
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
	}
	
    var marker = new GMarker( point, {icon:icon,title:tourName} );

	map.addOverlay(marker);
	
	GEvent.addListener(marker, "click", function() {
		var tabHtml = "<b>" + tourName + "</b><br>";
		tabHtml += "<b>" + operatorName + "</b><br>";
		tabHtml += "<div style='text-align: center;'><ul style='margin: 0; padding: 0; list-style-type: none'><li>";
		
		if (tourType == 'tour') {
			tabHtml += "<a href='" + linkUrl +"' class='learn'>View<\/a> details of this tour";
		} else  {
			tabHtml += "<a href='" + linkUrl +"' class='learn'>View</a> my recommendation";
		}
		
		tabHtml += "<\/li><\/ul></div>";
		marker.openInfoWindowHtml( tabHtml );
		current_marker = null;
	});

	if( !toursRoutes[tourId] ) {
		toursRoutes[tourId] = new Array();
	}
	toursRoutes[tourId].push( point );
    return marker;
}

function drawToursRoutes () {
	if( toursRoutes.length == 0 ) {
		return 0;
	}
	if( toursRoutes.length > 1 ) {
		for ( i in toursRoutes) {
			if( isNaN ( i ) == false ) {
				try { map.removeOverlay( toursRoutesOverlay[i] ); } catch( error ) {}
				toursRoutesOverlay[i] = new GPolyline( toursRoutes[i], '#ff0000' );
				map.addOverlay( toursRoutesOverlay[i] );
			}
		}	
	}
}

function createLocationMarker(point,desc1,title,seo_url){
	icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    var marker = new GMarker(point,{icon:icon,title:title});
	marker.desc1 = parseTagsToHtml(desc1);
	
	map.addOverlay(marker);

	GEvent.addListener(marker,'click', function(){
        if(seo_url){
			document.location.href = seo_url;
		}
        //openLocationInfoWindow(marker);
		current_marker = null;
	});
	    
    return marker;
}

function createDestinationMarker(point,desc1,title,type,seo_url){
	
	if(type == 'blog') {
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";	
	} else {
		icon.image = "http://labs.google.com/ridefinder/images/mm_20_green.png";
	}
	
    var marker = new GMarker(point,{icon:icon,title:title});
	marker.desc1 = parseTagsToHtml(desc1);

	map.addOverlay(marker);
	
	GEvent.addListener(marker, 'click', function(){
			var tabHtml = "<b>" + title + "<\/b><br>";
			tabHtml += "<div style='text-align: center;'><ul style='margin: 0; padding: 0; list-style-type: none;'><li>";
			if (type == 'blog') {
				tabHtml += "<a href='" + seo_url +"' class='learn'>View<\/a> my travel blog";
			} else {
				tabHtml += "<a href='" + seo_url +"' class='learn'>View<\/a> my recommendation";
			}
			tabHtml += "<\/li><\/ul></div>";
			marker.openInfoWindowHtml( tabHtml );
			current_marker = null;
		});
	
	/*
	GEvent.addListener(marker,'click', function(){
        if(seo_url){
			document.location.href = seo_url;
		}
        //openLocationInfoWindow(marker);
		current_marker = null;
	});
	*/ 
    return marker;
}

function initialiseMap(){
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(14,14),1);
    icon = new GIcon();
	icon.image = "http://labs.google.com/ridefinder/images/mm_20_orange.png";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, -20);
    
    GEvent.addListener(map, "click", function(marker, point) {
        if (!marker) {
			if(current_marker){
				openTourInfoWindow(current_marker);
			} else {
				map.closeInfoWindow();
			}
		}
    });
}