/*----------------------------------------------------------
 * google_map_helper.js
 *
 * This file contains all sorts of useful helper functions
 * for using Google Maps API V3 
 *--------------------------------------------------------*/

var gMap;
var gGeocoder;
var gMarkerList = new Object();
gMarkerList['master'] = new Array();
gMarkerList['place'] = new Array();
gMarkerList['photo'] = new Array();
gMarkerList['blogpost'] = new Array();
gMarkerList['suggestion'] = new Array();
gMarkerList['tweet'] = new Array();
gMarkerList['video'] = new Array();
gMarkerList['searchResult'] = new Array();
gMarkerList['infoWindow'] = new Array();
gMarkerList['videoInfoWindow'] = new Array();
var gInfoWindow = new google.maps.InfoWindow();


var markerLocation = "/images/markers/map/";
var markerImageArray = new Object();

markerImageArray['place'] = markerLocation + "route20.png";
markerImageArray['photo'] =  markerLocation + "photo20.png";
markerImageArray['blogpost'] =  markerLocation + "post20.png";
markerImageArray['suggestion'] =  markerLocation + "suggestion20.png";
markerImageArray['tweet'] =  markerLocation + "tweet.png";
markerImageArray['video'] =  markerLocation + "video20.png";
markerImageArray['searchResult'] =  markerLocation + "addpoint.png";

var clickHandlerArray = new Object();
clickHandlerArray['editAdd'] = "";


function clearMarkers(){
  //unshow all place markers
  for(var i=0; i<gMarkerList['place'].length; i++){
    gMarkerList['place'][i].setMap(null);
  }
  for(var i=0; i<gMarkerList['photo'].length; i++){
    gMarkerList['photo'][i].setMap(null);
  }
  for(var i=0; i<gMarkerList['blogpost'].length; i++){
    gMarkerList['blogpost'][i].setMap(null);
  }
  for(var i=0; i<gMarkerList['suggestion'].length; i++){
    gMarkerList['suggestion'][i].setMap(null);
  }
  for(var i=0; i<gMarkerList['video'].length; i++){
    gMarkerList['video'][i].setMap(null);
  }
  closeAllWindows();
}

function hideAllMarkers(){
  for( iType in gMarkerList ){
    for(var i=0; i<gMarkerList[iType].length; i++){
      gMarkerList[iType][i].setMap(null);
    }
  }
}

function closeAllWindows(){
   //close all open infoWindows
  for(var i=0; i<gMarkerList['infoWindow'].length; i++){
    gMarkerList['infoWindow'][i].close();
  }
  

}

function showOnly( type ){
  clearMarkers();
   for(var i=0; i<gMarkerList[type].length; i++){
    gMarkerList[type][i].setMap(gMap);
  }
  
}

function getNumberOfPointsByType( type ){
  if(type != "" ){
    if(gMarkerList[type]){
      return gMarkerList[type].length;
  	}
  }
  return 0;
}

function filterPointsByType( type ){
  if(type != "" ){
    if(gMarkerList[type]){
      for(var i=0; i<gMarkerList[type].length; i++){
        gMarkerList[type][i].setMap(gMap);
      }
    }
  }
}

function showNextPoint(){
	for( iPoint in gPlaceInfoList ){
	  if(iPoint > gSelectedPlaceNumber){
	    gPlaceInfoList[iPoint].click();
	    return;
	  }
	}
}

function showPrevPoint(){
	var prevMarker;
	for( iPoint in gPlaceInfoList ){
	
	  if(iPoint == gSelectedPlaceNumber){
	    prevMarker.click();
	    return;
	  }
	  prevMarker = gPlaceInfoList[iPoint];
	}
}

//----------------------------------------------------------

function makeNewMarker( map, lat, lng, iconPath, name ){
  var latLng = new google.maps.LatLng( lat, lng);
  var newMarker = new google.maps.Marker({
        position: latLng,
        map: map,
        icon: iconPath,
	title: name
  });
  return newMarker;
}

//----------------------------------------------------------

function addInfoWindow( map, contentHtml, marker , dataType){
    google.maps.event.addListener(marker, "click", function(e) {
      closeAllWindows();
      var infoBox = new InfoBox({latlng: marker.getPosition(), map: map, windowType: dataType}, contentHtml);
      gMarkerList['infoWindow'].push(infoBox);
    });
}

//----------------------------------------------------------

function makePlaceMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['place'], name );
}
//----------------------------------------------------------

function makeSearchMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['searchResult'], name );
}
//----------------------------------------------------------

function makePhotoMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['photo'], name );
}

//----------------------------------------------------------

function makeBlogpostMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['blogpost'], name );
}

//----------------------------------------------------------

function makeSuggestionMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['suggestion'], name );
}

//----------------------------------------------------------

function makeTweetMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['tweet'], name );
}

//----------------------------------------------------------

function makeVideoMarker( map, lat, lng, name ){
  return makeNewMarker( map, lat, lng, markerImageArray['video'], name );
}

//----------------------------------------------------------
function makePlaceInfoWindow ( name, description, street_address_1, street_address_2, city,  state, phoneNumber, country ){
  var html = "<div class='placeInfoWindow'>";
  html += "<div class='placeName'>"+ name +"</div>";
  if( (street_address_1) && (street_address_1 != "null") && (street_address_1 != "") && (street_address_1 != "No Street Address") && (street_address_1 != city) && (street_address_1 != state) ){
    html += "<div class='placeAddress1'>"+ street_address_1 +"</div>";
  }
  if( (city) && (city != "null") && (city != "") && (city != "No City") && (city != name)){
    html += "<div class='placeCity'>"+ city +"</div>";
  }
  if( (state) && (state != "null") && (state != "") && (state != "No State")){
    html += "<div class='placeState'>"+ state +"</div>";
  }
  if( (country) && (country != "null") && (country != "") && (country != "No Country") && (country != state)){
    html += "<div class='placeCountry'>"+ country +"</div>";
  }
  if( (!phoneNumber) || (phoneNumber != "null") || (phoneNumber != "")){
    html += "<div class='placePhoneNumber'>"+ phoneNumber +"</div>";
  }
  html += "<br /><div class='placeDescription'>"+description +"</div>";
  html += "</div>";
  return html;
}

//----------------------------------------------------------

function makePhotoInfoWindow ( title, ownerName, linkToImage, imageUrl ){
  var html = "<div class='photoInfoWindow'>";
  html += "<div class='photoTitle'>"+ title +"</div>";
  html += "<div class='photoOwner'>" + ownerName + "</div>";
  html += "<div class='photoImage'><a href='" + linkToImage + "'><img src='" + imageUrl  + "'/></a></div> <br/>"
  html += "</div>";
  return html;
}

//----------------------------------------------------------

function makeVideoInfoWindow ( title, length, videoId ){
  var html = "<div class='videoInfoWindow'>";
  html += "<div class='videoTitle'>"+ title +" ("+ length +" seconds)"+"</div>";
  html += '<div class="videoWindow">';
  //html += '<a href="http://www.youtube.com/v/'+videoId+'&hl=en&fs=1&hd=1"><img src="http://i4.ytimg.com/vi/'+ videoId +'/default.jpg" /></a>';
  html += '<object width="480" height="295"><param name="movie" value="http://www.youtube.com/v/'+videoId+'&hl=en&fs=1&hd=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ videoId +'&hl=en&fs=1&hd=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="295"></embed></object>';
  html += '</div>';
  return html;
}

//----------------------------------------------------------

function makeSearchResultInfoWindow ( result ){
  $("#addplace_title").val(result.titleNoFormatting);
  $("#addplace_street_address").val(result.streetAddress);
  $("#addplace_city").val(result.city);
  $("#addplace_state").val(result.region);
  $("#addplace_country").val(result.country);
  if(result.phoneNumbers){
    $("#addplace_phone_number").val(result.phoneNumbers[0].number);
  }
  else{
    $("#addplace_phone_number").val("");
  }
  $("#addplace_latitude").val(result.lat);
  $("#addplace_longitude").val(result.lng);
  $("#placeaddinfobox").dialog('open');

  /*var html = "<div class='results_box'>";
  html += '<form action="javascript:submitFormAjax();">';
  html += '<label for="title">Title</label>';
  html += '<input type="text" name="title" id="place_title" value="'+ result.titleNoFormatting+'" /><br />'; 
  html += '<label for="street_address">Street Address</label>';
  html += '<input type="text" name="street_address" id="place_street_address"  value="'+ result.streetAddress+'" /><br />'; 
  html += '<label for="city">City</label>';
  html += '<input type="text" name="city" id="place_city" value="'+ result.city+'" /><br />'; 
  html += '<label for="state">State/Region</label>';
  html += '<input type="text" name="state" id="place_state" value="'+ result.region+'" /><br />'; 
  html += '<label for="country">Country</label>';
  html += '<input type="text" name="country" id="place_country"  value="'+ result.country+'" /><br />';
  if(result.phoneNumbers){
    html += '<label for="phone_number">Phone Number</label>';
    html += '<input type="text" name="phone_number" id="place_phone_number" value="'+ result.phoneNumbers[0].number+'"  /><br />';
  }
  else{
    html += '<label for="phone_number">Phone Number</label>';
    html += '<input type="text" name="phone_number" id="place_phone_number" /><br />';
  }
  html += '<label for="description">Notes:</label><textarea rows="3" cols="30" name="description" id="description_field" onKeyDown="limitText(this.form.description,this.form.countdown,200);"></textarea><br><font size="1">(Maximum characters: 200)<br>You have <input readonly type="text" name="countdown" size="3" value="200"> characters left.</font>';

  
  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" />'; 
  html += '<input type="hidden" name="lat" id="place_latitude" value="'+result.lat +'" />'; 
  html += '<input type="hidden" name="lng" id="place_longitude"  value="'+result.lng +'" />'; 
  html += '</form>';
  html += '</div>';
  return html;
  */
}

//----------------------------------------------------------
function makePlaceEditWindow (id, name, description, street_address_1, street_address_2, city,  state, phoneNumber, country ){
  var html = "<div class='results_box'>";
  html += '<form action="javascript:submitEditFormAjax();">';
  html += '<label for="title">Title</label>';
  html += '<input type="text" name="title" id="place_title" value="'+ name+'" /><br />'; 
  html += '<label for="street_address">Street Address</label>';
  html += '<input type="text" name="street_address" id="place_street_address"  value="'+ street_address_1+'" /><br />'; 
  html += '<label for="city">City</label>';
  html += '<input type="text" name="city" id="place_city" value="'+ city+'" /><br />'; 
  html += '<label for="state">State/Region</label>';
  html += '<input type="text" name="state" id="place_state" value="'+ state+'" /><br />'; 
  html += '<label for="country">Country</label>';
  html += '<input type="text" name="country" id="place_country"  value="'+ country+'" /><br />';
  html += '<label for="phone_number">Phone Number</label>';
  html += '<input type="text" name="phone_number" id="place_phone_number" value="'+ phoneNumber+'"  /><br />';
  html += '<label for="description">Notes:</label><textarea rows="3" cols="30" name="description" id="description_field" onKeyDown="limitText(this.form.description,this.form.countdown,200);">'+description+'</textarea><br><font size="1">(Maximum characters: 200)<br>You have <input readonly type="text" name="countdown" size="3" value="200"> characters left.</font>';

  
  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" />'; 
  html += '<a href="javascript:submitDeleteFormAjax()"><img src="/trips/images/buttons/deletebutton.png" id="delete_place_button" /></a>'; 
  html += '<input type="hidden" name="id" id="edit_place_id" value="'+id +'" />'; 
  html += '</form>';
  html += '</div>';
  return html;
}

//----------------------------------------------------------
function makePlaceAddWindow ( lat, lng, city, state, country ){
  var html = "<div class='results_box'>";
  html += '<form action="javascript:submitFormAjax();">';
  html += '<label for="title">Title</label>';
  html += '<input type="text" name="title" id="place_title" value="" /><br />'; 
  html += '<label for="street_address">Street Address</label>';
  html += '<input type="text" name="street_address" id="place_street_address"  value="" /><br />'; 
  html += '<label for="city">City</label>';
  html += '<input type="text" name="city" id="place_city" value="'+ city +'" /><br />'; 
  html += '<label for="state">State/Region</label>';
  html += '<input type="text" name="state" id="place_state" value="'+state+'" /><br />'; 
  html += '<label for="country">Country</label>';
  html += '<input type="text" name="country" id="place_country"  value="'+country+'" /><br />';
  html += '<label for="phone_number">Phone Number</label>';
  html += '<input type="text" name="phone_number" id="place_phone_number" value="" /><br />';
  html += '<label for="description">Notes:</label><textarea rows="3" cols="30" name="description" id="description_field" onKeyDown="limitText(this.form.description,this.form.countdown,200);"></textarea><br><font size="1">(Maximum characters: 200)<br>You have <input readonly type="text" name="countdown" size="3" value="200"> characters left.</font>';
  html += '<input type="hidden" name="latitude" id="place_latitude" value="'+lat +'" />'; 
  html += '<input type="hidden" name="longitude" id="place_longitude"  value="'+lng +'" />'; 

  
  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" />'; 
  html += '</form>';
  html += '</div>';
  return html;
}



//----------------------------------------------------------
function makeSuggestionEditWindow ( result ){
  var html = "<div class='results_box'>";
  html += '<form action="javascript:createSuggestionIdentPage();">';
  html += '<label for="title">Title</label>';
  html += '<input type="text" name="title" id="place_title" value="'+ result.titleNoFormatting+'" /><br />'; 
  html += '<label for="street_address">Street Address</label>';
  html += '<input type="text" name="street_address" id="place_street_address"  value="'+ result.streetAddress+'" /><br />'; 
  html += '<label for="city">City</label>';
  html += '<input type="text" name="city" id="place_city" value="'+ result.city+'" /><br />'; 
  html += '<label for="state">State/Region</label>';
  html += '<input type="text" name="state" id="place_state" value="'+ result.region+'" /><br />'; 
  html += '<label for="country">Country</label>';
  html += '<input type="text" name="country" id="place_country"  value="'+ result.country+'" /><br />';
  if(result.phoneNumbers){
    html += '<label for="phone_number">Phone Number</label>';
    html += '<input type="text" name="phone_number" id="place_phone_number" value="'+ result.phoneNumbers[0].number+'"  /><br />';
  }
  else{
    html += '<label for="phone_number">Phone Number</label>';
    html += '<input type="text" name="phone_number" id="place_phone_number" /><br />';
  }
  html += '<label for="description">Notes:</label><textarea rows="3" cols="30" name="description" id="description_field" onKeyDown="limitText(this.form.description,this.form.countdown,200);"></textarea><br><font size="1">(Maximum characters: 200)<br>You have <input readonly type="text" name="countdown" size="3" value="200"> characters left.</font>';

  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" />'; 
  html += '<input type="hidden" name="lat" id="place_latitude" value="'+result.lat +'" />'; 
  html += '<input type="hidden" name="lng" id="place_longitude"  value="'+result.lng +'" />'; 
  
  html += '</form>';
  html += '</div>';
  return html;
}

//----------------------------------------------------------
function makeManualSuggestionEditWindow ( city, region, country, lat, lng ){
  var html = "<div class='results_box'>";
  html += '<form action="javascript:createSuggestionIdentPage();">';
  html += '<label for="title">Title</label>';
  html += '<input type="text" name="title" id="place_title" value="" /><br />'; 
  html += '<label for="street_address">Street Address</label>';
  html += '<input type="text" name="street_address" id="place_street_address"  value="" /><br />'; 
  html += '<label for="city">City</label>';
  html += '<input type="text" name="city" id="place_city" value="'+ city+'" /><br />'; 
  html += '<label for="state">State/Region</label>';
  html += '<input type="text" name="state" id="place_state" value="'+ region+'" /><br />'; 
  html += '<label for="country">Country</label>';
  html += '<input type="text" name="country" id="place_country"  value="'+ country+'" /><br />';
  html += '<label for="phone_number">Phone Number</label>';
  html += '<input type="text" name="phone_number" id="place_phone_number" /><br />';
  html += '<label for="description">Notes:</label><textarea rows="3" cols="30" name="description" id="description_field" onKeyDown="limitText(this.form.description,this.form.countdown,200);"></textarea><br><font size="1">(Maximum characters: 200)<br>You have <input readonly type="text" name="countdown" size="3" value="200"> characters left.</font>';

  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" />'; 
  html += '<input type="hidden" name="lat" id="place_latitude" value="'+ lat +'" />'; 
  html += '<input type="hidden" name="lng" id="place_longitude"  value="'+ lng +'" />'; 
  
  html += '</form>';
  html += '</div>';
  return html;
}


//----------------------------------------------------------
function makeSuggestionAttributionWindow (name, description, street_address_1, street_address_2, city,  state, phoneNumber, country, latitude, longitude ){

 var html = "<div class='results_box'>";
   html += '<form id="suggest_attrib_form" action="javascript:submitSuggestFormAjax();">';
   html += '<input type="hidden" name="title" id="hidden_place_title" value="'+ name +'" />'; 
   html += '<input type="hidden" name="street_address" id="hidden_place_street_address"  value="'+ street_address_1 +'" /><br />'; 
  html += '<input type="hidden" name="city" id="hidden_place_city" value="'+ city +'" />'; 
  html += '<input type="hidden" name="state" id="hidden_place_state" value="'+ state +'" />'; 
  html += '<input type="hidden" name="country" id="hidden_place_country"  value="'+ country +'" />';
  html += '<input type="hidden" name="phone_number" id="hidden_place_phone_number" value="'+ phoneNumber +'"  />';
  html += '<input type="hidden" name="description" id="hidden_description_field" value="'+ description +'"  />';
  html += '<input type="hidden" name="lat" id="place_latitude" value="'+ latitude +'"  />';
  html += '<input type="hidden" name="lng" id="place_longitude" value="'+ longitude +'"  />';

  html += '<label for="suggestor_name">Name*</label><input type="text" name="suggestor_name" id="suggestor_name_field"/><br />';
  html += '<label for="suggestor_email">E-mail*</label><input type="text" name="suggestor_email" id="suggestor_email_field" /><br />';
  html += '<label for="suggestor_twitter">Twitter</label><input type="text" name="suggestor_twitter" id="suggestor_twitter_field" /><br />';
  html += '<label for="suggestor_uri" >Website</label><input type="text" name="suggestor_uri" id="suggestor_uri_field"/><br />';
  html += '<input type="image" src="/trips/images/buttons/savebutton.png" name="submit_place" id="submit_place_button" value="Submit" /><br />'; 
  html += '<div id="attrib_form_text">* required.  Email will remain private.</div>';
  html += '</form>';
  html += '</div>';

  return html;
}

//----------------------------------------------------------
function makeSuggestionInfoWindow ( name, description, street_address_1, city, state, phoneNumber, country, suggestor_name, suggestor_twitter, suggestor_uri ){
  var html = "<div class='suggestionInfoWindow'>";
  html += "<div class='suggestionName'>"+ name +"</div>";
  html += "<div class='suggestionDescription'>"+ description +"</div>";
  if (suggestor_name != ""){
    html += "<div class='suggestionSuggestorName'>";
    if( suggestor_uri != ""){
      html += "Suggested by: <a href='"+ suggestor_uri +"'>"+ suggestor_name +"</a> <br />";
      html += "twitter: <a href='http://twitter.com/"+suggestor_twitter+"'>"+ suggestor_twitter +"</a>";
    }
    html += "</div>";
  }
  else{
    html += "<div class='suggestionSuggestorName'>From: Anonymous</div>";
  }
  html += "</div>";
  return html;
}

//----------------------------------------------------------
function makeFullSuggestionInfoWindow ( name, description, street_address_1, city, state, phoneNumber, country, suggestor_name, suggestor_twitter, suggestor_uri ){
  var html = "<div class='suggestionInfoWindow'>";
  html += "<div class='suggestionName'>"+ name +"</div>";
  html += "<div class='suggestionDescription'>"+ description +"</div>";
  html += "<div class='suggestionAddress1'>"+ street_address_1 +"</div>";
  if( (!city) || (city != "null") || (city != "")){
    html += "<div class='suggestionCity'>"+ city +"</div>";
  }
  if( (!state) || (state != "null") || (state != "")){
    html += "<div class='suggestionState'>"+ state +"</div>";
  }
  html += "<div class='suggestionCountry'>"+ country +"</div>";
  if( (!phoneNumber) || (phoneNumber != "null") || (phoneNumber != "")){
    html += "<div class='suggestionPhoneNumber'>"+ phoneNumber +"</div>";
  }
  if (suggestor_name != ""){
    html += "<div class='suggestionSuggestorName'>";
    if( suggestor_uri != ""){
      html += "Suggested by: <a href='"+ suggestor_uri +"'>"+ suggestor_name +"</a> <br />";
      html += "twitter: <a href='http://twitter.com/"+suggestor_twitter+"'>"+ suggestor_twitter +"</a>";
    }
    html += "</div>";
  }
  else{
    html += "<div class='suggestionSuggestorName'>From: Anonymous</div>";
  }
  html += "</div>";
  return html;
}

//----------------------------------------------------------
function makeBlogpostInfoWindow ( title, url, postdate ){
  var html = "<div class='blogInfoWindow'>";
  html += "<div class='bloppostTitle'><a href="+url+">"+ title +"</a></div>";
  html += "<div class='blogpostDate'>"+ postdate +"</div>";
  html += "</div>";
  return html;
}






