var BadgeAdmin = {

init : function (ref_type, ref_obj, badge_id) {
   this.ref_type=ref_type;
   this.ref_obj=ref_obj;
   this.badge_id = badge_id ? badge_id : 0;
   this.confirmDlg=new Dialog();
   this.confirmDlg.setButtons(Dialog.OK_AND_CANCEL);
   this.confirmDlg.setModal(true);
   this.okDlg=new Dialog();
   this.okDlg.setButtons(Dialog.OK);
   this.okDlg.setModal(true);

   // for check text length
   if(this.badge_id > 0) {
      this.titleLength = 30;
      this.descLength = 256;
      this.targetLength = 128;
      this.checkTextLength('badge_title', 'titleCounter', this.titleLength);
      this.checkTextLength('badge_desc', 'descCounter', this.descLength);
      this.checkTextLength('badge_target', 'targetCounter', this.targetLength);
   }
},

badgeFileClear : function () {
   $('badge_upload_file').value='';
   Element.hide('badge_uploading');
},

badgeFileCheck : function () {
   pname = $('badge_upload_file').value;
   if(pname == '') return false;
   ext = pname.substring(pname.lastIndexOf('.')+1).toLowerCase();
   if(ext == 'jpg' || ext == 'jpeg' || ext == 'bmp' || ext == 'png' || ext == 'gif'){
      return true;
   }
   this.showOk('格式錯誤','<p style="text-align:center;line-height:60px;vertical-align:middle;">您選擇的圖檔格式錯誤，必須是 jpg/bmp/png/gif 格式。</p>', 'BadgeAdmin.badgeFileClear();' );
   return false;
},

badgeUpload : function () {
   if( this.badgeFileCheck() ) {
      Element.show('badge_uploading');
      $('badge_upload_form').submit();
   }
},

badgeRedirect : function (badge_id) {
   url='';

   if(this.ref_type == 1) //group
      url = badge_id ? 
            '/people/group/info.php?group_id=' + this.ref_obj + '&tab=badge&badge_id=' + badge_id : 
            '/people/group/info.php?group_id=' + this.ref_obj + '&tab=badge' ;

   location.href = url;
},

badgeModify : function () {
   // trim length
   this.checkTextLength('badge_title', 'titleCounter', this.titleLength);
   this.checkTextLength('badge_desc', 'descCounter', this.descLength);
   this.checkTextLength('badge_target', 'targetCounter', this.targetLength);
   tthis=this;
   new Ajax.Request(
      PEOPLE_P+'badge/modify.ajax.php', {
         method : 'post',
         parameters : $('badge_basic_form').serialize(true),
         onSuccess : function(rs) {
            rsobj=ej(rs.responseText);
            if(rsobj.rs) {
               tthis.showOk('修改徽章', '<p style="line-height:60px;vertical-align:middle;text-align:center;">徽章的修改已經儲存了。</p>');
            }
         }
      }
   );
},

badgeDelete : function (confirm) {
   if(!this.badge_id) return;
   img = $('badgeimg_'+this.badge_id).innerHTML;
   if(!confirm) this.confirm('刪除徽章', '<p style="margin:10px;text-align:center;">' + img + '</p><p style="line-height:20px;vertical-align:middle;text-align:center;">您確定要刪除這個徽章？刪除之後所有的使用者都不能再使用這個徽章。</p>', 'BadgeAdmin.badgeDelete(1);');
   if(confirm && this.badge_id) {
      tthis=this;
      new Ajax.Request(
         PEOPLE_P+'badge/delete.ajax.php', {
            method : 'post',
            parameters : {'ref_type':this.ref_type,'ref_obj':this.ref_obj,'badge_id':this.badge_id},
            onSuccess : function(rs) {
               rsobj=ej(rs.responseText);
               if(rsobj.rs) {
                  tthis.showOk('刪除徽章', '<p style="line-height:60px;vertical-align:middle;text-align:center;">這個徽章被刪除了。</p>', 'BadgeAdmin.badgeRedirect();');
               }
            }
         }
      );
   }
},

selectAll : function (name, check) {
   userlist = $A($(name).getElementsByClassName('userlist'));
   userlist.each( function(ele) { ele.checked = check; } );
   return false;
},

saveUsers : function() {
   tthis=this;
   if(this.badge_id) {
      new Ajax.Request(
         location.href, {
            method : 'post',
            parameters : $('badge_userlist_form').serialize(true),
            onSuccess : function(rs) {
               location.reload();
            }
         }
      );
   }
},

checkTextLength : function (text_id, counter_id, max) {
   cLength = $(text_id).value.length;
   $(counter_id).innerHTML = max - cLength;
   if( $(counter_id).innerHTML < 0 ) {
      $(text_id).value = $(text_id).value.substr(0, max);
      $(counter_id).innerHTML = max - $(text_id).value.length;
   }
},

confirm : function (title, body, handler) {
  this.confirmDlg.setTitle(title);
  this.confirmDlg.setBody(body);
  this.confirmDlg.setHandler(handler);
  this.confirmDlg.show();
},

showOk : function (title, body, handler) {
  this.okDlg.setTitle(title);
  this.okDlg.setBody(body);
  if( handler ) this.okDlg.setHandler(handler);
  this.okDlg.show();
},

debug : function(content) {
  this.showOk('debug', content);
}

}

var Badge = {
init : function () {
   // prepare dialog
   this.confirmDlg=new Dialog();
   this.confirmDlg.setButtons(Dialog.OK_AND_CANCEL);
   this.confirmDlg.setModal(true);
   this.okDlg=new Dialog();
   this.okDlg.setButtons(Dialog.OK);
   this.okDlg.setModal(true);
},

get : function(confirm) {
   tthis=this;
   if(!confirm) this.confirm('取得徽章', '<p style="line-height:60px;vertical-align:middle;text-align:center;">你確定要取得這個徽章嗎？</p><p style="text-align:right;margin-right:20px;"><input type="checkbox" onclick="$(\'badge_on_profile\').value = this.checked ? 1 : 0;" id="check_on_dlg"/>直接安裝在我的個人檔案</p><iframe onload="$(\'check_on_dlg\').checked = ($(\'badge_on_profile\').value==1 ? true : false);" style="display:none;"><!-- IE --></iframe>', 'Badge.get(1);');
   if(confirm) {
      new Ajax.Request(
         location.href, {
            method : 'post',
            parameters : $('badge_basic_form').serialize(true),
            onSuccess : function(rs) {
               location.reload();
            }
         }
      );
   }
},

toggle : function(log_id) {
   tthis=this;
   img=$('badgeimg_'+log_id).innerHTML;
   checked = $('badge_enable_'+log_id).checked ? 1 : 0 ;
   new Ajax.Request(
      PEOPLE_P+'badge/togglelog.ajax.php', {
         method : 'post',
         parameters : {'log_id':log_id,'checked':checked},
         onSuccess : function(rs) {
            rsobj=ej(rs.responseText);
            if(rsobj.rs) {
               act = checked ? '開啟' : '關閉' ;
               tthis.showOk(act+'徽章', '<p style="margin:10px;text-align:center;">' + img + '</p><p style="line-height:20px;vertical-align:middle;text-align:center;">這個徽章被'+act+'了。</p>');
            } else {
               $('badge_enable_'+log_id).checked = 0;
               if(checked) tthis.showOk('開啟徽章', '<p style="line-height:60px;vertical-align:middle;text-align:center;">你已經使用了 5 個徽章，請關閉一些使用中的徽章再開啟其他徽章。</p>');
            }
         }
      }
   );
},

remove : function(log_id, confirm) {
   tthis=this;
   img=$('badgeimg_'+log_id).innerHTML;
   if(!confirm) this.confirm('刪除徽章', '<p style="margin:10px;text-align:center;">' + img + '</p><p style="line-height:20px;vertical-align:middle;text-align:center;">你確定要刪除這個徽章嗎？</p>', 'Badge.remove('+log_id+',1);');
   if(confirm) {
      new Ajax.Request(
         PEOPLE_P+'badge/deletelog.ajax.php', {
            method : 'post',
            parameters : {'log_id':log_id},
            onSuccess : function(rs) {
               rsobj=ej(rs.responseText);
               if(rsobj.rs) {
                  tthis.showOk('刪除徽章', '<p style="line-height:60px;vertical-align:middle;text-align:center;">這個徽章被刪除了。</p>', function() {location.reload();} );
               }
            }
         }
      );
   }
},

updateOrder : function () {
   params = Sortable.serialize('badges', {tag:'div',name:'badgelog'});
   new Ajax.Request(
      PEOPLE_P+'badge/order.ajax.php', {
         method : 'post',
         parameters : params,
         onSuccess : function(rs) {
            rsobj=ej(rs.responseText);
            if(rsobj.rs) {
               $('badges_preview').innerHTML = rsobj.badges;
            }
         }
      }
   );
},

confirm : function (title, body, handler) {
  this.confirmDlg.setTitle(title);
  this.confirmDlg.setBody(body);
  this.confirmDlg.setHandler(handler);
  this.confirmDlg.show();
},

showOk : function (title, body, handler) {
  this.okDlg.setTitle(title);
  this.okDlg.setBody(body);
  if( handler ) this.okDlg.setHandler(handler);
  this.okDlg.show();
},

debug : function(content) {
  this.showOk('debug', content);
}

}
