if ( typeof YouHolder == 'undefined' ) { YouHolder = {} }
if ( typeof YouHolder.Gookus == 'undefined' ) { YouHolder.Gookus = {} }

(function($){

    YouHolder.Gookus.url = window.location.href ;
    YouHolder.Gookus.reload_interval = 30000 ;

    YouHolder.Gookus.update_list  = function(text,textStatus){
      if(text != 'no change') $('#dialogues').html(text);
    }

    YouHolder.Gookus.get_list = function(){
      $.ajax({ url:         YouHolder.Gookus.url, 
               dataType:    'html', 
               data:        'last_gooku_id=' + YouHolder.Gookus.last_gooku_id, 
               success:     YouHolder.Gookus.update_list
             });
    }

    YouHolder.Gookus.start_reload = function(interval) {
      if(!interval ) interval = YouHolder.Gookus.reload_interval;
      YouHolder.Gookus.stop_reload();
      YouHolder.Gookus.interval_id = setInterval( YouHolder.Gookus.get_list, interval);
    }

    YouHolder.Gookus.stop_reload = function(){
      clearInterval(YouHolder.Gookus.interval_id);
      YouHolder.Gookus.interval_id = null;
    }

    YouHolder.Gookus.toogle_reload = function(insert, reload_now) {

      if(insert.attr('checked')) {
        $.cookie('gookus_auto_reload', '1',  { expires: 7 });
        YouHolder.Gookus.start_reload();
        if(reload_now) { YouHolder.Gookus.get_list(); }
      } else {
        $.cookie('gookus_auto_reload', '',  { expires: -1 });
        YouHolder.Gookus.stop_reload();
      }
    }
    
    YouHolder.Gookus.add_reload_checkbox = function() {
      var label  = $('<label for="auto_reload">自動更新</label>');
      var insert = $('<input type="checkbox" name="auto_reload" id="auto_reload"/>');
      var new_div = $('<div></div>').attr('class', 'selection');
      new_div.append(insert).append(label);


      if($.cookie('gookus_auto_reload') == '1' ) {
        $.cookie('gookus_auto_reload', '1',  { expires: 7 }); //set it for another 7 days
        insert.attr('checked', true);
      }else{
        insert.attr('checked', false);
      }

      insert.click(function() { YouHolder.Gookus.toogle_reload(insert, true)} );
      YouHolder.Gookus.toogle_reload(insert);

      $('#subMenu > *:last').before(new_div);
    }

    YouHolder.Gookus.bind_delete = function() {
      $('.delete').click(function() {
        if (confirm('確定要刪除?')) { 
          var f = document.createElement('form'); 
          f.style.display = 'none'; 
          this.parentNode.appendChild(f); 
          f.method = 'POST'; 
          f.action = this.href;
          f.submit(); 
        }
        return false;
      });
    }

    YouHolder.Gookus.refine_gookus = function(arr, prefix){
      prefix = prefix + 'gookus';

      for(var i=0 ; i < arr.length; i++){

        var that = arr[i];
        var g = $("[name="+ prefix + that.gid  +"]").parent();
        if(that.cnt > 0) { 
          g.find('.reply').prepend( that.cnt + '則') ;
        }

        if(!that.del){
          g.find('.delete').remove();
        }else{
          g.find('.delete').show();
        }
      }

      YouHolder.Gookus.bind_delete();
    }

    //new_gooku
    var default_val   = null;
    var form          = null;
    var textarea      = null;
    var submitted     = false;
    var login_url     = null;
    var valid_cookie  = null;

       function set_submitted() {
         $('#post textarea').attr('disabled',true)
         submitted = true;
       }

       function unset_submitted() {
         submitted = false;
         $('#post textarea').attr('disabled',false)
       }

       function check_len(){
         var val   = textarea.val();

         if(val.length > 168 ) {
           textarea.val( val.substring(0,168));
         }
       }

       function submit(){
         textarea.blur();

         if(submitted) return false;
         if(textarea.val().length == 0 || textarea.val() == default_val) return false;

         check_len();
         set_submitted();
         form.find('#gooku_content').val( textarea.val());

         textarea.css('color','gray').val('正在送出訊息...');
         form.get(0).submit();

         return false;
       }

       function check_key(e){
         check_len();
         if(e.keyCode == 13) {
           submit();
           return false;
         }
       }

       function confirm_activate(e){
         if(confirm('您需要先啟用「在一起」才能發佈訊息。確定要啟用嗎？') ){
           window.location = '/activate';
           textarea.blur();
           return false;
         }else{
           textarea.blur();
           return false;
         }
       }

       function confirm_login(e){
         if(valid_cookie){
             if(!submitted) {
               textarea.blur();
               set_submitted();
               $.ajax({ url: '/membercenter/plogin', dataType: 'script', 
                   complete: function() { unset_submitted();}
               });
             }
         } else {
            if(confirm('您需要先登入才能發佈訊息。確定要登入嗎？') ){
              window.location = login_url;
            }
         }
         textarea.blur();
         return false;
       }

       YouHolder.Gookus.new_gooku_init = function(opt) {
         form = $('#post form');
         var submit_btn = form.find('a.btnSubmit')
         textarea = form.find('textarea');
         textarea.val(default_val);

         if(opt.guest) {
           textarea.attr('disabled',false);
           if(opt.goyou_user) {
             textarea.click(confirm_activate);
             submit_btn.click(confirm_activate);
           }else{
             textarea.click(confirm_login);
             submit_btn.click(confirm_login);
           }
         }else{
           submit_btn.click( submit);
           textarea.keypress(check_key);
         }

         default_val  = opt.default_val;
         login_url    = opt.login_url;
         valid_cookie = opt.valid_cookie;

         YouHolder.Util.HintTextField(textarea.get(0), default_val);
         textarea.focus().blur();
       }
   

})(jQuery);
