text: 
  'use strict';
  
  
  
  var getRoom;
  
  // Initiate firebase auth.
  function initFirebaseAuth() {
    // Listen to auth state changes.
    firebase.auth().onAuthStateChanged(authStateObserver);
    initMap();
  }
  
  // Returns the signed-in user's profile Pic URL.
  function getProfilePicUrl() {
    return firebase.auth().currentUser.photoURL || '/images/profile_placeholder.png';
  }
  
  // Returns the signed-in user's display name.
  function getUserName() {
    return firebase.auth().currentUser.displayName;
  }
  
  // Returns true if a user is signed-in.
  function isUserSignedIn() {
    return !!firebase.auth().currentUser;
  }
  
  
  var address;
  function initMap(){
  
    //위도 경도 받아오기
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };
      console.log(pos);
  
  
      var geocoder = new google.maps.Geocoder;
      geocoder.geocode({'location': pos}, function(results, status) {
        console.log(results);
        if (status === 'OK') {
          if (results[2]) {
            address=results[2].formatted_address;
          }else if (results[0]){
            address=results[0].formatted_address;
          } else {
            window.alert('No results found');
          }
        } else {
          window.alert('Geocoder failed due to: ' + status);
        }
  
  
      });
      });
    }
  }
  
  // Loads chat messages history and listens for upcoming ones.
  function loadMessages(getRoom) {
    console.log(getRoom);
    // Loads the last 12 messages and listen for new ones.
    var callback = function(snap) {
      var data = snap.val();
      displayMessage(snap.key, data.name, data.text, data.profilePicUrl, data.imageUrl, data.location, data.time);
    };
  
    firebase.database().ref('/messages/'+getRoom).limitToLast(12).on('child_added', callback);
    firebase.database().ref('/messages/'+getRoom).limitToLast(12).on('child_changed', callback);
  }
  
  function makeTime(){
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth()+1;
    var date = d.getDate();
    var time = year+"."+month+"."+date;
  
    return time;
  }
  
  // Saves a new message on the Firebase DB.
  function saveMessage(messageText) {
    // Add a new message entry to the Firebase database.
    return firebase.database().ref('/messages/'+getRoom).push({
      name: getUserName(),
      text: messageText,
      profilePicUrl: getProfilePicUrl(),
      location : address,
      time: makeTime()
  
    }).catch(function(error) {
      console.error('Error writing new message to Firebase Database', error);
    });
  }
  
  // Saves a new message containing an image in Firebase.
  // This first saves the image in Firebase storage.
  function saveImageMessage(file) {
    // 1 - We add a message with a loading icon that will get updated with the shared image.
    firebase.database().ref('/messages/'+getRoom).push({
      name: getUserName(),
      imageUrl: LOADING_IMAGE_URL,
      profilePicUrl: getProfilePicUrl()
    }).then(function(messageRef) {
      // 2 - Upload the image to Cloud Storage.
      var filePath = firebase.auth().currentUser.uid + '/' + messageRef.key + '/' + file.name;
      return firebase.storage().ref(filePath).put(file).then(function(fileSnapshot) {
        // 3 - Generate a public URL for the file.
        return fileSnapshot.ref.getDownloadURL().then((url) => {
          // 4 - Update the chat message placeholder with the image’s URL.
          return messageRef.update({
            imageUrl: url,
            storageUri: fileSnapshot.metadata.fullPath,
            location : address,
            time: makeTime()
          });
        });
      });
    }).catch(function(error) {
      console.error('There was an error uploading a file to Cloud Storage:', error);
    });
  }
  
  // Saves the messaging device token to the datastore.
  function saveMessagingDeviceToken() {
    firebase.messaging().getToken().then(function(currentToken) {
      if (currentToken) {
        console.log('Got FCM device token:', currentToken);
        // Saving the Device Token to the datastore.
        firebase.database().ref('/fcmTokens').child(currentToken)
            .set(firebase.auth().currentUser.uid);
      } else {
        // Need to request permissions to show notifications.
        requestNotificationsPermissions();
      }
    }).catch(function(error){
      console.error('Unable to get messaging token.', error);
    });
  }
  
  // Requests permissions to show notifications.
  function requestNotificationsPermissions() {
    console.log('Requesting notifications permission...');
    firebase.messaging().requestPermission().then(function() {
      // Notification permission granted.
      saveMessagingDeviceToken();
    }).catch(function(error) {
      console.error('Unable to get permission to notify.', error);
    });
  }
  
  // Triggered when a file is selected via the media picker.
  function onMediaFileSelected(event) {
    event.preventDefault();
    var file = event.target.files[0];
  
    // Clear the selection in the file picker input.
    imageFormElement.reset();
  
    // Check if the file is an image.
    if (!file.type.match('image.*')) {
      var data = {
        message: 'You can only share images',
        timeout: 2000
      };
      signInSnackbarElement.MaterialSnackbar.showSnackbar(data);
      return;
    }
    // Check if the user is signed-in
    if (checkSignedInWithMessage()) {
      saveImageMessage(file);
    }
  }
  
  // Triggered when the send new message form is submitted.
  function onMessageFormSubmit(e) {
    e.preventDefault();
    // Check that the user entered a message and is signed in.
    if (messageInputElement.value && checkSignedInWithMessage()) {
      saveMessage(messageInputElement.value).then(function() {
        // Clear message text field and re-enable the SEND button.
        resetMaterialTextfield(messageInputElement);
        toggleButton();
      });
    }
  }
  
  
  
  // Triggers when the auth state change for instance when the user signs-in or signs-out.
  function authStateObserver(user) {
    if (user) { // User is signed in!
      // Get the signed-in user's profile pic and name.
      var profilePicUrl = getProfilePicUrl();
      var userName = getUserName();
  
      // Set the user's profile pic and name.
      userPicElement.style.backgroundImage = 'url(' + profilePicUrl + ')';
      userNameElement.textContent = userName;
  
      // Show user's profile and sign-out button.
      userNameElement.removeAttribute('hidden');
      userPicElement.removeAttribute('hidden');
  
      // Hide sign-in button.
  
      // We save the Firebase Messaging Device token and enable notifications.
  
      saveMessagingDeviceToken();
      setRoom(userName);
  
    } else { // User is signed out!
      // Hide user's profile and sign-out button.
      userNameElement.setAttribute('hidden', 'true');
      userPicElement.setAttribute('hidden', 'true');
  
  
      // Show sign-in button.
  
    }
  }
  
  function setRoom(userName){
    var callback= function(snap){
      console.log(snap.val().status);
      getRoom = snap.val().status;
      loadMessages(getRoom);
      console.log(userName);
      htmlroom.innerHTML = getRoom;
    }
    firebase.database().ref('/status/'+userName+'/location').on('value', callback)
  }
  
  // Returns true if user is signed-in. Otherwise false and displays a message.
  function checkSignedInWithMessage() {
    // Return true if the user is signed in Firebase
    if (isUserSignedIn()) {
      return true;
    }
  
    // Display a message to the user using a Toast.
    var data = {
      message: 'You must sign-in first',
      timeout: 2000
    };
    signInSnackbarElement.MaterialSnackbar.showSnackbar(data);
    return false;
  }
  
  // Resets the given MaterialTextField.
  function resetMaterialTextfield(element) {
    element.value = '';
    element.parentNode.MaterialTextfield.boundUpdateClassesHandler();
  }
  
  // Template for messages.
  var MESSAGE_TEMPLATE =
      '<div class="message-container">' +
        '<div class="spacing"><div class="pic"></div></div>' +
        '<div class="message"></div>' +
        '<div class="time"></div>'+
        '<div class="name"></div>' +
        '<div class="location"></div>' +
        '<div class="bookmark" onclick="bookmarking(this)">'+
        "<i class='material-icons'>bookmark</i>"+
        '</div>'+
      '</div>';
  
  function bookmarking(event){
  
    // console.log(event);
  
    // console.log(event.parentElement);
  
    var parent = event.parentElement;
    var text = parent.querySelector('.message').innerHTML;
    var time = parent.querySelector('.time').innerHTML;
  
    var isimg = parent.getElementsByTagName('img')[0]
    var output
    if(isimg){
      output = isimg.getAttribute('src');
      text= null;
    }else{
      output = null;
    }
  
    console.log(isimg);
    console.log(text);
  
    firebase.database().ref('/status/'+getUserName()+"/bookmark").push({
      text: text,
      status: getRoom,
      time: time,
      image: output
    }, getToggle()).catch(function(error) {
      console.error('Error writing new message to Firebase Database', error);
    });
  }
  
  function getToggle(){
    var data = {
      message: 'This context saved in bookmark',
      timeout: 2000
    };
    BookmarkSnackbarElement.MaterialSnackbar.showSnackbar(data);
  }
  
  // Adds a size to Google Profile pics URLs.
  function addSizeToGoogleProfilePic(url) {
    if (url.indexOf('googleusercontent.com') !== -1 && url.indexOf('?') === -1) {
      return url + '?sz=150';
    }
    return url;
  }
  
  // A loading image URL.
  var LOADING_IMAGE_URL = 'https://www.google.com/images/spin-32.gif?a';
  
  // Displays a Message in the UI.
  function displayMessage(key, name, text, picUrl, imageUrl,location, time) {
    var div = document.getElementById(key);
    // If an element for that message does not exists yet we create it.
    if (!div) {
      var container = document.createElement('div');
      container.innerHTML = MESSAGE_TEMPLATE;
      div = container.firstChild;
      div.setAttribute('id', key);
      messageListElement.appendChild(div);
    }
  
    if (picUrl) {
      div.querySelector('.pic').style.backgroundImage = 'url(' + addSizeToGoogleProfilePic(picUrl) + ')';
    }
    div.querySelector('.name').textContent = name;
    var messageElement = div.querySelector('.message');
  
    if (text) { // If the message is text.
      messageElement.textContent = text;
      // Replace all line breaks by <br>.
      messageElement.innerHTML = messageElement.innerHTML.replace(/\n/g, '<br>');
    } else if (imageUrl) { // If the message is an image.
      var image = document.createElement('img');
      image.addEventListener('load', function() {
        messageListElement.scrollTop = messageListElement.scrollHeight;
      });
      image.src = imageUrl + '&' + new Date().getTime();
      messageElement.innerHTML = '';
      messageElement.appendChild(image);
    }
    var locmessage = div.querySelector('.location');
    if(location){
      locmessage.textContent = location;
      // messageElement.innerHTML = messageElement.innerHTML.replace(/\n/g, '<br>');
    }
    var timemessage = div.querySelector('.time');
    if(time){
      timemessage.textContent = time;
    }
  
    // Show the card fading-in and scroll to view the new message.
    setTimeout(function() {div.classList.add('visible')}, 1);
    messageListElement.scrollTop = messageListElement.scrollHeight;
    messageInputElement.focus();
  }
  
  // Enables or disables the submit button depending on the values of the input
  // fields.
  function toggleButton() {
    if (messageInputElement.value) {
      submitButtonElement.removeAttribute('disabled');
    } else {
      submitButtonElement.setAttribute('disabled', 'true');
    }
  }
  
  // Checks that the Firebase SDK has been correctly setup and configured.
  function checkSetup() {
    if (!window.firebase || !(firebase.app instanceof Function) || !firebase.app().options) {
      window.alert('You have not configured and imported the Firebase SDK. ' +
          'Make sure you go through the codelab setup instructions and make ' +
          'sure you are running the codelab using `firebase serve`');
    }
  }
  
  // Checks that Firebase has been imported.
  checkSetup();
  
  // Shortcuts to DOM Elements.
  var messageListElement = document.getElementById('messages');
  var messageFormElement = document.getElementById('message-form');
  var messageInputElement = document.getElementById('message');
  var submitButtonElement = document.getElementById('submit');
  var imageButtonElement = document.getElementById('submitImage');
  var imageFormElement = document.getElementById('image-form');
  var mediaCaptureElement = document.getElementById('mediaCapture');
  var userPicElement = document.getElementById('user-pic');
  var userNameElement = document.getElementById('user-name');
  
  var signInSnackbarElement = document.getElementById('must-signin-snackbar');
  
  var BookmarkSnackbarElement = document.getElementById('save-bookmark');
  
  var htmlroom = document.getElementById('roomTitle');
  
  // Saves message on form submit.
  messageFormElement.addEventListener('submit', onMessageFormSubmit);
  
  // Toggle for the button.
  messageInputElement.addEventListener('keyup', toggleButton);
  messageInputElement.addEventListener('change', toggleButton);
  
  // Events for image upload.
  imageButtonElement.addEventListener('click', function(e) {
    e.preventDefault();
    mediaCaptureElement.click();
  });
  mediaCaptureElement.addEventListener('change', onMediaFileSelected);
  
  // initialize Firebase
  initFirebaseAuth();
  // We load currently exist
  //ing chat messages and listen to new ones.
  