<?php
$hashtag = '';
if (isset($_GET['tab1']) && $_GET['tab1'] == 'hashtag' && !empty($_GET['hash'])) {
  $hashtag = '#' . $_GET['hash'];
}
?>
<form action="#" method="post" class="post publisher-box">
   <div class="panel panel-white post panel-shadow">
      <div id="post-textarea">
         <i class="fa fa-spinner fa-spin" id="loading_indicator"></i>
         <textarea name="postText" class="form-control postText" cols="10" rows="3" placeholder="<?php echo $wo['lang']['publisher_box_placeholder']; ?>" dir="auto"><?php echo $hashtag;?></textarea> 
      </div>
      <?php if ($wo['config']['video_upload'] == 1) { ?>
      <div class="publisher-hidden-option" id="video-form">
         <div class="inner-addon <?php echo Wo_RightToLeft('left-addon');?>">
            <i class="glyphicon fa fa-video-camera"></i>
            <input name="videocount" type="text" class="form-control" readonly>
         </div>
      </div>
      <?php } ?>
      <?php if ($wo['config']['audio_upload'] == 1) { ?>
      <div class="publisher-hidden-option" id="music-form">
         <div class="inner-addon <?php echo Wo_RightToLeft('left-addon');?>">
            <i class="glyphicon fa fa-music"></i>
           <input name="musiccount" type="text" class="form-control" readonly>
         </div>
      </div>
      <?php } ?>
      <div id="album-form">
        <div class="publisher-hidden-option">
           <input name="album_name" type="text" class="form-control" placeholder="<?php echo $wo['lang']['album_name'];?>">
        </div>
       </div>
      <div class="publisher-hidden-option" id="photo-form">
         <div class="inner-addon <?php echo Wo_RightToLeft('left-addon');?>">
            <i class="glyphicon fa fa-file-image-o"></i>
            <input name="phtoscount" type="text" class="form-control" readonly>
         </div>
         <div class="create-album <?php echo Wo_RightToLeft('pull-right');?>" onclick="Wo_OpenAlbum();">
           <div class="create-text">
             <?php echo $wo['lang']['create_album'];?>
           </div>
         </div>
         <div class="clear"></div>  
      </div>
      <div class="publisher-hidden-option" id="file-form">
         <div class="inner-addon <?php echo Wo_RightToLeft('left-addon');?>">
            <i class="glyphicon fa fa-file-text-o"></i>
            <input name="filename" type="text" class="form-control" readonly>
         </div>
      </div>
      <div class="publisher-hidden-option" id="map-form">
         <div class="inner-addon <?php echo Wo_RightToLeft('left-addon');?>">
            <i class="glyphicon fa fa-map-marker"></i>
            <input name="postMap" id="searchTextField" type="text" class="form-control" placeholder="<?php echo $wo['lang']['maps_placeholder']?>">
         </div>
      </div>
      <div id="emo-form">
        <div class="publisher-hidden-option">
            <div class="feelings-type-to <?php echo Wo_RightToLeft('pull-left');?> pointer" onclick="Wo_RestFeelings();"></div>
            <span class="feelings-value"></span>
            <input name="feeling" id="feelings-text" type="text" class="form-control" placeholder="<?php echo $wo["lang"]["feel_d"]?>" onclick="Wo_ShowFeelings();" autocomplete="no">
            <input name="feeling_type" type="hidden" id="feeling-type" value="">
            <div class="clear"></div>
        </div>
        <div class="feeling-type feeling-types">
          <ul>
            <li class="pointer" onclick="Wo_ShowFeelingType('feelings', '<?php echo $wo['lang']['feeling'];?>', '<?php echo $wo['lang']['feeling_q'];?>', 'smile-o');">
              <i class="fa fa-fw fa-smile-o fa-wh"></i> <?php echo $wo['lang']['feeling'];?>
            </li>
            <li class="pointer" onclick="Wo_ShowFeelingType('traveling', '<?php echo $wo['lang']['traveling'];?>' , '<?php echo $wo['lang']['traveling_q'];?>', 'plane');">
              <i class="fa fa-fw fa-plane"></i> <?php echo $wo['lang']['traveling'];?>
            </li>
            <li class="pointer" onclick="Wo_ShowFeelingType('watching', '<?php echo $wo['lang']['watching'];?>', '<?php echo $wo['lang']['watching_q'];?>', 'eye');">
              <i class="fa fa-fw fa-eye"></i> <?php echo $wo['lang']['watching'];?>
            </li>
            <li class="pointer" onclick="Wo_ShowFeelingType('playing', '<?php echo $wo['lang']['playing'];?>', '<?php echo $wo['lang']['playing_q'];?>', 'gamepad');">
              <i class="fa fa-fw fa-gamepad"></i> <?php echo $wo['lang']['playing'];?>
            </li>
            <li class="pointer" onclick="Wo_ShowFeelingType('listening', '<?php echo $wo['lang']['listening'];?>', '<?php echo $wo['lang']['listening_q'];?>', 'headphones');">
              <i class="fa fa-fw fa-headphones"></i> <?php echo $wo['lang']['listening'];?>
            </li>
          </ul>
        </div>
        <div class="feeling-type feelings">
          <ul>
            <?php 
            foreach ($wo['feelingIcons'] as $icon_name => $icon_code) {
            ?>
            <li class="pointer" onclick="Wo_AddFeeling('<?php echo $icon_name;?>', '<?php echo $wo['lang'][$icon_name]; ?>', '<?php echo $icon_code;?>');"><i class="twa-lg twa twa-<?php echo $icon_code; ?>"></i> <?php echo $wo['lang'][$icon_name]; ?></li>
            <?php
            }
            ?>
          </ul>
        </div>
         <?php  
        foreach ($wo['emo'] as $code => $name) {
            $code   = $code;
            echo  '<span onclick="Wo_AddEmo(\'' . $code . '\',\'#post-textarea textarea\');"><i class="pointer twa-lg twa twa-' . $name . '"></i></span>';
        } 
        ?>
      </div>
      <div id="results"></div>
      <div class="publisher-hidden-option">
         <div id="progress">
            <span id="percent">0%</span>
            <div class="progress">
             <div id="bar" class="progress-bar progress-bar-striped active"></div> 
            </div>
            <div class="clear"></div>
         </div>
      </div>
      <div class="publisher-box-footer">
         <div class="row col-lg-2 <?php echo Wo_RightToLeft('pull-right');?>">
            <button type="submit" id="publisher-button"class="form-control btn btn-main">
            <i class="fa fa-edit progress-icon " data-icon="edit"></i> <?php echo $wo['lang']['post_label']; ?></button>
         </div>
        <div class="row <?php echo Wo_RightToLeft('pull-left');?>">
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file">
                  <i class="fa fa-camera" ></i><input type="file" id="publisher-photos" accept="image/x-png, image/gif, image/jpeg" name="postPhotos[]" multiple="multiple">
                  </span>
               </div>
            </div>
         </div>
         <?php if ($wo['config']['video_upload'] == 1) { ?>
         <div class="row <?php echo Wo_RightToLeft('pull-left');?>">
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file">
                  <i class="fa fa-video-camera"></i><input type="file" id="publisher-video" name="postVideo" accept="video/*">
                  </span>
               </div>
            </div>
         </div>
         <?php } ?>
         <?php if ($wo['config']['audio_upload'] == 1) { ?>
         <div class="row <?php echo Wo_RightToLeft('pull-left');?>">
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file">
                  <i class="fa fa-music"></i><input type="file" id="publisher-music" name="postMusic" accept="audio/*">
                  </span>
               </div>
            </div>
         </div>
         <?php } ?>
         <?php if($wo['config']['fileSharing'] == 1) { ?>
         <div class="row <?php echo Wo_RightToLeft('pull-left');?>">
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file">
                  <i class="fa fa-file-text-o"></i><input type="file" id="publisher-file" name="postFile">
                  </span>
               </div>
            </div>
         </div>
         <?php } ?>
         <div class="row <?php echo Wo_RightToLeft('pull-left');?>">
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file map-form">
                  <i class="fa fa-map-marker"></i>
                  </span>
               </div>
            </div>
         </div>
         <div class="row <?php echo Wo_RightToLeft('pull-left');?> emo-form" >
            <div class="poster-left-buttons">
               <div class="input-group">
                  <span class="btn btn-default btn-file">
                  <i class="fa fa-smile-o "></i>
                  </span>
               </div>
            </div>
         </div>
         <div class="input-group">
            <div class="<?php echo Wo_RightToLeft('pull-right');?> charsLeft-post"><span id="charsLeft"></span></div>
         </div>
         <div id="status"></div>
         <?php if (!isset($wo['input']['recipient']) && $wo['page'] != 'group') { ?>
         <div class="row <?php echo Wo_RightToLeft('pull-right');?>">
            <div class="poster-left-buttons">
               <div class="publisher-hidden-option inputsm">
                  <select name="postPrivacy" class="form-control inputsm">
                     <?php if (empty($_GET['p'])) {?>
                        <option value="3"><?php echo $wo['lang']['only_me'];?></option>
                        <option value="0" selected><?php echo $wo['lang']['everyone'];?></option>
                        <?php if ($wo['config']['connectivitySystem'] == 0) { ?>
                        <option value="1"><?php echo $wo['lang']['people_i_follow'];?></option>
                        <option value="2"><?php echo $wo['lang']['people_follow_me'];?></option>
                        <?php } else {?>
                        <option value="1"><?php echo $wo['lang']['my_friends'];?></option>
                        <?php }?>
                     <?php } else { ?>
                        <option value="0"><?php echo $wo['lang']['everyone'];?></option>
                        <option value="2"><?php echo $wo['lang']['liked_my_page'];?></option>
                     <?php } ?>
                  </select>
               </div>
            </div>
         </div>
         <?php } ?>
         <div class="clear"></div>
      </div>
   </div>
   <?php if (isset($wo['input']['recipient'])) { ?>
   <input type="hidden" name="recipient_id" value="<?php echo $wo['user_profile']['user_id']; ?>">
   <?php } ?>
   <?php if ($wo['page'] == 'page') {?>
   <input type="hidden" name="page_id" value="<?php echo $wo['page_profile']['page_id']; ?>">
   <?php } else if ($wo['page'] == 'group') {?>
   <input type="hidden" name="group_id" value="<?php echo $wo['group_profile']['id']; ?>">
   <?php } ?>
</form>
<div id="hidden_inputbox"></div>
<script type="text/javascript">
$(function () {
  $('.postText').triggeredAutocomplete({
     hidden: '#hidden_inputbox',
     source: Wo_Ajax_Requests_File() + "?f=mention",
     trigger: "@" 
  });
  <?php if ($wo['config']['maxCharacters'] != 10000) { ?>
  $('.postText').limit("<?php echo $wo['config']['maxCharacters']?>", '#charsLeft');
  <?php } ?>
  $('.emo-form').click(function () {
    $('#emo-form').slideToggle(200);
    Wo_RestFeelings();
  });

  $('.map-form').click(function () {
    $('#map-form').slideToggle(200);
  });

  $("#publisher-file").change(function () {
    var filename = $(this).val().replace(/C:\\fakepath\\/i, '');
    $("#file-form input").val(filename);
    $("#file-form").slideDown(200);
  });

  $("#publisher-photos").change(function () {
    var numFiles = this.files.length;
    $("#photo-form input").val(numFiles + ' photo(s) selected');
    $("#photo-form").slideDown(200);
  });

  $("#publisher-video").change(function () {
    var filename = $(this).val().replace(/C:\\fakepath\\/i, '');
    $("#video-form input").val(filename);
    $("#video-form").slideDown(200);
  });

  $("#publisher-music").change(function () {
    var filename = $(this).val().replace(/C:\\fakepath\\/i, '');
    $("#music-form input").val(filename);
    $("#music-form").slideDown(200);
  });

  var bar = $('#bar');
  var percent = $('#percent');
  var status = $('#status');
  var publisher_button = $('#publisher-button');

  $('form.post').ajaxForm({
    url: Wo_Ajax_Requests_File() + '?f=posts&s=insert_new_post',
    beforeSend: function () {
      var percentVal = '0%';
      bar.width(percentVal);
      percent.html(percentVal);
      publisher_button.attr('disabled', true);
      publisher_button.css('color', '#333');
      Wo_progressIconLoader($('form.post').find('button'));
    },
    uploadProgress: function (event, position, total, percentComplete) {
      var percentVal = percentComplete + '%';
      bar.width(percentVal);
      $('#progress').slideDown(200);
      if(percentComplete > 50) {
        percent.addClass('white');
      }
      percent.html(percentVal);
    },
    success: function (data) {
      $('#progress').slideUp(200);
      publisher_button.attr('disabled', false);
      publisher_button.css('color', '#fff');
      percent.removeClass('white');
      if(data.status == 200) {
        if(data.html.length > 0) {
          $('#posts, #posts_hashtag').find('.posts-container').remove();
        }
        $('#posts, #posts_hashtag').prepend(data.html);
        $('#video-form, #emo-form, #music-form, #map-form, #file-form, #photo-form, #album-form, .feeling-type').slideUp(200);
        Wo_RemoveFetchedUrl();
        $('form.post').clearForm();
        $('.feelings-value').html('');
        $('select').prop('selectedIndex', 1);
        <?php if ($wo['config']['maxCharacters'] != 10000) { ?>
        $('#charsLeft').text("<?php echo $wo['config']['maxCharacters']?>");
        <?php } ?>
      }
      Wo_progressIconLoader($('form.post').find('button'));
    }
  });

  var pac_input = document.getElementById('searchTextField');
  (function pacSelectFirst(input) {
    // store the original event binding function
    var _addEventListener = (input.addEventListener) ? input.addEventListener : input.attachEvent;
    function addEventListenerWrapper(type, listener) {
      // Simulate a 'down arrow' keypress on hitting 'return' when no pac suggestion is selected,
      // and then trigger the original listener.
      if(type == "keydown") {
        var orig_listener = listener;
        listener = function (event) {
          var suggestion_selected = $(".pac-item-selected").length > 0;
          if(event.which == 13 && !suggestion_selected) {
            var simulated_downarrow = $.Event("keydown", {
              keyCode: 40,
              which: 40
            });
            orig_listener.apply(input, [simulated_downarrow]);
          }
          orig_listener.apply(input, [event]);
        };
      }
      // add the modified listener
      _addEventListener.apply(input, [type, listener]);
    }
    if(input.addEventListener)
      input.addEventListener = addEventListenerWrapper;
    else if(input.attachEvent)
      input.attachEvent = addEventListenerWrapper;
  })(pac_input);

  $(function () {
     var autocomplete = new google.maps.places.Autocomplete(pac_input);
  });
  
  var getUrl  = $('.form-control.postText'); //url to extract from text field
  
  getUrl.bind("change keyup input", function() { //user types url in text field    
    
    //url to match in the text field
    var match_url = /\b(https?):\/\/([\-A-Z0-9.]+)(\/[\-A-Z0-9+&@#\/%=~_|!:,.;]*)?(\?[A-Z0-9+&@#\/%=~_|!:,.;]*)?/i;
    //var match_youtube_url = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    
    if (($(".extracted_url").length > 0)){
       return false;
     }
    //returns true and continue if matched url is found in text field
    if (match_url.test(getUrl.val())) {
        //!match_youtube_url.test(getUrl.val())
        $("#results").hide();
        $("#loading_indicator").show(); //show loading indicator image
        
        var extracted_url = getUrl.val().match(match_url)[0]; //extracted first url from text filed
        
        //ajax request to be sent to extract-process.php
        $.post(Wo_Ajax_Requests_File() + '?f=posts&s=fetch_url',{'url': extracted_url}, function(data){         
                  
          extracted_images = data.images;
          total_images = parseInt(data.images.length-1);
          img_arr_pos = total_images;
          
          if(total_images > 0){
            inc_image = '<div class="extracted_thumb" id="extracted_thumb"><img src="'+data.images[img_arr_pos]+'" width="100" height="100"></div>';
            input_image = data.images[img_arr_pos];
          } else {
            inc_image = '';
            input_image = '';
          }
          //content to be loaded in #results element
          var img_content = '';
          if (total_images > 1) {
            img_content = '<div class="thumb_sel"><span class="prev_thumb" id="thumb_next"><i class="fa fa-arrow-circle-right"></i></span><span class="next_thumb" id="thumb_prev"><i class="fa fa-arrow-circle-left"></i></span> </div><span class="small_text" id="total_imgs">'+img_arr_pos+' of '+total_images+'</span></div></div>';
          }
          var content = '<div class="extracted_url"><span class="pull-right remove-fetched-url" onclick="Wo_RemoveFetchedUrl();"><i class="fa fa-remove"></i></span>'+ inc_image +'<div class="extracted_content"><input class="form-control url-input" type="text" name="url_title" value="'+data.title+'" dir="auto"><br><textarea class="form-control url-input" name="url_content" placeholder="Description" dir="auto">' + data.content + '</textarea><input type="hidden" id="url_image" name="url_image" value="'+ input_image +'"><input type="hidden" id="url_link" name="url_link" value="'+data.url+'"><div class="clear"></div>' + img_content;
          //load results in the element
          $("#results").html(content); //append received data into the element
          $("#results").fadeIn(); //show results with slide down effect
          $("#loading_indicator").hide(); //hide loading indicator image
                },'json');
    }
  });


  //user clicks previous thumbail
  $("body").on("click","#thumb_prev", function(e){    
    if(img_arr_pos>0) 
    {
      img_arr_pos--; //thmubnail array position decrement
      
      //replace with new thumbnail
      $("#extracted_thumb").html('<img src="'+extracted_images[img_arr_pos]+'" width="100" height="100">');

      $("#url_image").attr('value', extracted_images[img_arr_pos]);
      
      //show thmubnail position
      $("#total_imgs").html((img_arr_pos) +' of '+ total_images);
    }
  });
  
  //user clicks next thumbail
  $("body").on("click","#thumb_next", function(e){    
    if(img_arr_pos<total_images)
    {
      img_arr_pos++; //thmubnail array position increment
      
      //replace with new thumbnail
      $("#extracted_thumb").html('<img src="'+extracted_images[img_arr_pos]+'" width="100" height="100">');

      $("#url_image").attr('value', extracted_images[img_arr_pos]);
      
      //replace thmubnail position text
      $("#total_imgs").html((img_arr_pos) +' of '+ total_images);
    }
  });

});
function Wo_RemoveFetchedUrl() {
  var content = $('form.publisher-box').find('.extracted_url');
  content.slideUp(function () {
    $(this).remove();
  });
}
function Wo_OpenAlbum() {
  $('#album-form').slideToggle(200);
}
function Wo_ShowFeelings() {
  if ($('.feelings-type-to').is(':empty')) {
    $('.feeling-types').slideToggle(200);
  }
  
}
var pac_input2 = document.getElementById('feelings-text');
function Wo_ShowFeelingType(type, text, placeholder, icon) {
  $('.feeling-types').slideUp(200, function () {
    $('.feelings-type-to').html('<i class="fa fa-' + icon + '"></i> ' + text + '');
    $('#feelings-text').attr('placeholder', placeholder).focus();
    $('#feeling-type').val(type);
    if (type != 'feelings') {
      $('#feelings-text').attr('readonly', false);
    }
    if (type == 'traveling') {
      var autocomplete = new google.maps.places.Autocomplete(pac_input2);
    } else {
      google.maps.event.clearInstanceListeners(pac_input2);
    }
    $('.' + type).slideToggle(200);
  });
}
function Wo_RestFeelings() {
  $('.feelings-type-to, .feelings-value').empty();
  $('#feelings-text').val('');
  $('#feelings-text').attr('readonly', true);
  $('#feelings-text').attr('placeholder', "<?php echo $wo["lang"]["feel_d"]?>");
  $('.feeling-type, .feelings-type').slideUp(200);
  google.maps.event.clearInstanceListeners(pac_input2);
  Wo_ShowFeelings();
}
function Wo_AddFeeling(type, text, icon) {
  $('#feelings-text').val(type);
  $('.feelings-value').html('<span><i class="twa-lg twa twa-' + icon + '"></i> ' + text + '</span>')
  Wo_ShowFeelingType('feelings', 'Feeling', 'What are you feeling ?', 'smile-o');
}
function smokeTheAt(str) {
  var n = str.search("@");
  if(n != "-1"){
    alert('sd');
  }
}
</script>
