function re(i){return document.getElementById(i);}

var map;
  var geocoder;

  function initialize() {
    geocoder = new google.maps.Geocoder();
    var myLatlng = new google.maps.LatLng(default_lat, default_long);
    var myOptions = {
      zoom: 8,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
	  streetViewControl:false,
	  mapTypeControl: true,
	  MapTypeId: google.maps.MapTypeId.TERRAIN,
      mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
          position: google.maps.ControlPosition.BOTTOM_LEFT
      }
    }
    map = new google.maps.Map(re("map_canvas"), myOptions);
	
	if(logged_in == true){
		google.maps.event.addListener(map, 'idle', function() {
			readytoshowpeople();
		});
	}
		
	//
		
		for (var i = 0; i < locations.length; i++) {
			var dta = locations[i][0].split("::");
			
			switch(dta[3]){
			case "me":
			var image = new google.maps.MarkerImage('img/iconme.png',
			  new google.maps.Size(16, 31),
			  new google.maps.Point(0,0),
			  new google.maps.Point(0, 31));
			break;
			case "other":
			var image = new google.maps.MarkerImage('img/iconothers.png',
			  new google.maps.Size(16, 31),
			  new google.maps.Point(0,0),
			  new google.maps.Point(0, 31));
			break;	
			}
			
			var myLatlng = new google.maps.LatLng(dta[1],dta[2]);
			var marker = new google.maps.Marker({ map: map, 
				position: myLatlng,
				icon: image
			});
			
			//var j = i + 1;
    		//marker.setTitle(j.toString());
   		 	markeroverlay(marker, dta[0], dta[4]);
		}
		
	
  }
  
function markeroverlay(marker, tripid, locid) {
 
  google.maps.event.addListener(marker, 'click', function() {
  	
	getOverlayData(marker, tripid, locid);
	
  });
}

var infowindow = null;
function getOverlayData(marker, tripid, locid){

//return 'bb';
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttpo=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttpo=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttpo.onreadystatechange=function()
  {
  if (xmlhttpo.readyState==4 && xmlhttpo.status==200)
    {
	var d = xmlhttpo.responseText;
	
	if (infowindow) {
        infowindow.close();
		map.panBy(-130,20);
    }
	
	infowindow = new google.maps.InfoWindow(
      { content: d
      });
 	infowindow.open(map,marker);
	map.panBy(130,-20);
	}
	
  }
xmlhttpo.open("GET","data.php?q=getoverlaydata&uid="+uid+"&id="+tripid,true);
xmlhttpo.send();

//showpeople('locid:'+locid+':nodaterestrict','0');
}

  function zoomonlocation(lat,long){
	
	var myLatlng = new google.maps.LatLng(lat, long);
	map.setCenter(myLatlng);
	map.setZoom(11);
  
  }
  
  function addinterest(interestid){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	var d = xmlhttp.responseText;

    }
  }
xmlhttp.open("GET","data.php?q=addinterest&uid="+uid+"&data="+interestid,true);
xmlhttp.send();
}

function removeinterest(interestid){
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	var d = xmlhttp.responseText;

    }
  }
xmlhttp.open("GET","data.php?q=removeinterest&uid="+uid+"&data="+interestid,true);
xmlhttp.send();
}

function addtrip(){
	if(locok == false){
		alert('Please select your location from the drop down list which appears while entering it. You can use a comma to include the country, e.g. London, United Kingdom');
		return false;
		}
		
	if(re('from').value == '' || re('to').value == ''){
		alert('Please enter your trip dates.');
		return false;
		}
		
	re('tripbox').style.display = 'none';
	if (window.XMLHttpRequest){xmlhttp=new XMLHttpRequest();}
else{xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
xmlhttp.onreadystatechange=function()
  {if (xmlhttp.readyState==4 && xmlhttp.status==200) {
	var d = xmlhttp.responseText;
	
	re('newtripalrt').innerHTML = ' <br/><span style = "color:#ccc;font-size:11px;font-weight:normal">NEW TRIPS! <a href = "" style = "color:#666">REFRESH</a></span>';
	locok = false;
    }
  }
  var dest = re('destination').value;
  var from = re('from').value;
  var to = re('to').value;
  var loc_string = re('loc_string').value;
  var loc_latlong = re('loc_latlong').value;
  var posttofb = '0';
  if(re('posttofb')) posttofb = re('posttofb').checked;

  var dest_string = re('loc_string').value;
  var dest_latlong = re('loc_latlong').value;

  re('destination').value = '';
  re('from').value = '';
  re('to').value = '';

xmlhttp.open("GET","data.php?q=addtrip&uid="+uid+"&loc_string="+dest_string+"&loc_latlong="+dest_latlong+"&location="+dest+'&from='+from+'&to='+to+'&posttofb='+posttofb+'&loc_latlong='+loc_latlong+'&loc_string='+loc_string,true);
xmlhttp.send();

codeAddress(dest);
}

 function codeAddress(address) {
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
		
		var image = new google.maps.MarkerImage('img/iconme.png',
		  new google.maps.Size(16, 31),
		  new google.maps.Point(0,0),
		  new google.maps.Point(0, 31));
	
        var marker = new google.maps.Marker({
            map: map, 
            position: results[0].geometry.location,
			icon: image
        });
      } else {
        /*alert("Geocode was not successful for the following reason: " + status);*/
      }
    });
  }
  
  
   function placemarkers(){
return false;
		  var image = new google.maps.MarkerImage('img/iconothers.png',
		  new google.maps.Size(16, 31),
		  new google.maps.Point(0,0),
		  new google.maps.Point(0, 31));
		  
  for (var i = 0; i < locations.length; i++) {
	   var beach = locations[i];
  
  
  geocoder.geocode( { 'address': beach[0]}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
       
    var myLatLng = new google.maps.LatLng(results[0].geometry.location);
    var marker = new google.maps.Marker({
        position: results[0].geometry.location,
        map: map,
        icon: image
    });
	   
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  
  
    
  }
  }
  
  var selectedNav = '';
  function toggleNav(which){
	switch(which){
		case "interests":
		re('tripbox').style.display = 'none';
		if(selectedNav == 'interests'){
		re('interestsbox').style.display = 'none';
		selectedNav = '';
			}else{
		re('interestsbox').style.display = 'block';
		selectedNav = 'interests';
			}
		break;
		case "newtrip":
		re('interestsbox').style.display = 'none';
		if(selectedNav == 'newtrip'){
		re('tripbox').style.display = 'none';
		selectedNav = '';
			}else{
		re('tripbox').style.display = 'block';
		selectedNav = 'newtrip';
			}
		break;
		}  
	}
	
	function updatecustominterests(data){
	
	if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
	var d = xmlhttp.responseText;
	
    }
  }
	xmlhttp.open("GET","data.php?q=updatecustominterests&uid="+uid+"&data="+data,true);
	xmlhttp.send();
	}
	
	
	function mtteditDates(id){
	var html = '';
	
	html += '<form action = "" method = "post">';
	html += '<input type = "hidden" name = "act" value = "changedates">';
	html += '<input type = "hidden" name = "trip" value = "'+id+'">';
	html += 'From <input type = "text" name = "from" id = "mttfrom_'+id+'"/>';
	html += '<br/>';
	html += 'Until <input type = "text" name = "to" id = "mttto_'+id+'"/>';
	html += '<br/>';
	html += '<input type = "submit"></form>';
	
	re('mttdate'+id).innerHTML = html;
	
			//$(function(){
			// Datepicker
			//	$('#mttfrom_'+id+'').datepicker({dateFormat:'dd/mm/yy'});
			//	$('#mttto_'+id+'').datepicker({dateFormat:'dd/mm/yy'});				
			//});
			
			$(function() {
					var dates = $( "#mttfrom_"+id+","+"#mttto_"+id ).datepicker({
						changeMonth: true,
						numberOfMonths: 1,
						dateFormat:'dd/mm/yy',
						onSelect: function( selectedDate ) {
							var option = this.id == "mttfrom_"+id ? "minDate" : "maxDate",
								instance = $( this ).data( "datepicker" );
								date = $.datepicker.parseDate(
									instance.settings.dateFormat ||
									$.datepicker._defaults.dateFormat,
									selectedDate, instance.settings );
							dates.not( this ).datepicker( "option", option, date );
						}
					});
				});
	}
	
	function showmessages(){
	re('right_default').style.display = 'none';
	re('right_message_compose').style.display = 'none';
	re('right_settings').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_messages').style.display = 'block';
	}
	
	function hidemessages(){
	re('right_messages').style.display = 'none';
	re('right_message_compose').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_settings').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_default').style.display = 'block';
	}
	
	function composemessage(toid, toname){
	re('right_messages').style.display = 'none';
	re('right_default').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_settings').style.display = 'none';
	re('right_message_compose').style.display = 'block';
	
	re('msgto').value = toid;
	re('msgtoname').innerHTML = toname;
	}
	
	function showsettings(){
	re('right_messages').style.display = 'none';
	re('right_message_compose').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_default').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_settings').style.display = 'block';
	}
	
	function hidesettings(){
	re('right_messages').style.display = 'none';
	re('right_message_compose').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_settings').style.display = 'none';
	re('right_default').style.display = 'block';
	}
	
	function readytoshowpeople(){
		re('right_messages').style.display = 'none';
		re('right_message_compose').style.display = 'none';
		re('right_message_view').style.display = 'none';
		re('right_settings').style.display = 'none';
		re('right_default').style.display = 'none';
		re('right_people').style.display = 'block';

		re('right_people').innerHTML = '';
		$("#right_people").addClass('loading');

		setTimeout(showpeople(0),1000);	
	}
	
	function showpeople(limit){
		
		if (window.XMLHttpRequest)
		  {// code for IE7+, Firefox, Chrome, Opera, Safari
		  xmlhttp=new XMLHttpRequest();
		  }
		else
		  {// code for IE6, IE5
		  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		xmlhttp.onreadystatechange=function()
		  {
		  if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
				var d = xmlhttp.responseText;
				re('right_people').innerHTML = '<strong class = "rb_head">PEOPLE HERE</strong><br/><a href = "#" onclick = "hidepeople();return false;" class = "rb_backto">View my trips</a><br/><br/><div id = "rightPeopleCont">'+d+'</div>';
				$("#right_people").removeClass();
			}
		  }
		  //get current map bounds...
		  	var bounds = map.getBounds();
			var ne = bounds.getNorthEast(); // LatLng of the north-east corner
			var sw = bounds.getSouthWest(); // LatLng of the south-west corder
			var nw = new google.maps.LatLng(ne.lat(), sw.lng());
			var se = new google.maps.LatLng(sw.lat(), ne.lng());
			
			var latRange =  sw.lat()+','+nw.lat();
			var longRange = sw.lng()+','+ne.lng()
			
			var bounds = latRange+':'+longRange;
		xmlhttp.open("GET","data.php?q=getpeopleatloc&uid="+uid+"&limit="+limit+"&bounds="+bounds,true);
		xmlhttp.send();
	}
	
	function hidepeople(){
	re('right_messages').style.display = 'none';
	re('right_message_compose').style.display = 'none';
	re('right_message_view').style.display = 'none';
	re('right_people').style.display = 'none';
	re('right_default').style.display = 'block';
	}
	
	var dfactive = false;
	function datefilter(){
		if(dfactive == false){
			re('datefiltercont').style.display = 'block'; 
			dfactive = true;
		}else{
			re('datefiltercont').style.display = 'none';
			dfactive = false;
		}
	}
	
	var vrr = '';
	function activateLoader(w){
		w.style.background = "#ffffff url('/stayova/img/loader.gif') no-repeat right";
		vrr = w;
		setTimeout ( 'vrr.style.background = ""' , 5000 );
	}
