/**
 * Script for BBS (use jQuery)
 * 
 */

//global valiable
var BBS_OPEN_IMG_SRC = '../img/bbs_icon_open.gif';
var BBS_OPEN_IMG_HOVER_SRC = '../img/bbs_icon_open_o.gif';
var BBS_CLOSE_IMG_SRC = '../img/bbs_icon_close.gif';
var BBS_CLOSE_IMG_HOVER_SRC = '../img/bbs_icon_close_o.gif';
var BBS_ALL_OPEN_BTN_VALUE = '記事を開いて表示';
var BBS_ALL_CLOSE_BTN_VALUE = '記事を閉じて表示';
var BBS_ONE_OPEN_BTN_VALUE = '記事を開く';
var BBS_ONE_CLOSE_BTN_VALUE = '記事を閉じる';
var BBS_HIDDEN_WRAP_BOX_PADDING = '5px 15px';
var BBS_SHOWN_WRAP_BOX_PADDING = '15px';
var BBS_HIDDEN_TOP_TITLE_BAR_MARGIN_BOTTOM = '1px';
var BBS_SHOWN_TOP_TITLE_BAR_MARGIN_BOTTOM = '0px';
var BBS_HIDDEN_RES_TABLE_PADDING_TOP = '0';
var BBS_SHOWN_RES_TABLE_PADDING_TOP = '6px';
var BBS_HIDDEN_RES_TABLE_BACKGROUND_POSITION = '0% 35%';
var BBS_SHOWN_RES_TABLE_BACKGROUND_POSITION = '0% 0%';
var BBS_COOKIE_HIDE_FLAG_NAME = 'bbs_hide';
var BBS_COOKIE_FLAG_EFFECTIVE_DAYS = 36500;

//core function
var dom = new Object();
dom.core = new Object();
dom.event = new Object();
//get class node array object
dom.core.getElementByClassName = function(elm, searchClass, tag) {
  var returnArr = [];
  tag = tag || '*';
  var elms = elm.getElementsByTagName(tag);
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (var i = 0; i < elms.length; i++) {
    if (pattern.test(elms[i].className)) {
      returnArr.push(elms[i]);
    }
  }
  return returnArr;
}
//prohibit default action
dom.event.preventDefault = function(evt) {
  /* W3C DOM browser */
  if(evt && evt.preventDefault) {
    evt.preventDefault();
    /* for Safari 1.3 */
    evt.currentTarget['on'+evt.type] = function() {return false;};
  /* Internet Explorer */
  } else if(window.event) {
    window.event.returnValue = false;
  }
};

//cookie function
var cookie = new Object();
//set cookie
cookie.set = function(cookie_name,cookie_value,effective_days,cookie_path) {
  var cookie_name = cookie_name;
  var bbs_cookie_display_flag = cookie_value;
  var exp = new Date();
  exp.setTime(exp.getTime()+1000*60*60*24*effective_days);
  ckstr = escape(bbs_cookie_display_flag);
  document.cookie = cookie_name + '=' + ckstr + '; expires=' + exp.toGMTString() + '; path=' + cookie_path;
}
//get cookie
cookie.get = function(cookie_name) {
  var cklng = document.cookie.length;
  var ckary = document.cookie.split('; ');
  var ckstr = '';
  var cookie_value = '';
  var cookie_name_length = cookie_name.length + 1;
  var i = 0;
  while (ckary[i]) {
    if (ckary[i].substr(0,cookie_name_length) == cookie_name + '=') {
      ckstr = unescape(ckary[i]);
      ckstr = ckstr.split('=');
      if (ckstr[1]) {
        cookie_value = 1;
      }
      break;
    }
    i++;
  }
  return cookie_value;
}

/**
 * set onload event
 * 
 */
$(document).ready(function() {
  //jump
  if ($('#jump_def')[0].value == 'list') {
    location.href='#bbs_list';
  } else if ($('#jump_def')[0].value == 'search') {
    location.href='#bbs_search';
  }
  //bbs contents
  if (!$('#bbs_entry_flg')[0].value) {
    return false;
  }
  var bbs_disp_default_flag = 1; //default show
  var bbs_post_flag = null;
  //check cookie flag
  if (cookie.get(BBS_COOKIE_HIDE_FLAG_NAME)) {
    //hide
    bbs_disp_default_flag = '';
    if ($('#bbs_disp_default')[0].value) {
      //serach
      bbs_disp_default_flag = 1;
    } else {
      if ($('#bbs_post_flag')[0].value) {
        //post message
        bbs_post_flag = 1;
      }
    }
  }
  var cookie_path = $('#bbs_path')[0].value;
  //object
  var aObj = $('#bbs_disp_flag_button')[0];
  var aSpanObj = aObj.getElementsByTagName('span')[0];
  var flag = $('#bbs_disp_flag')[0];
  flag.value = bbs_disp_default_flag;
  var ua=navigator.userAgent;
  //set hide default
  if (!bbs_disp_default_flag) {
    //create button default
    var oldTextNode = aSpanObj.firstChild;
    var newTextNode = document.createTextNode(BBS_ALL_OPEN_BTN_VALUE);
    aSpanObj.replaceChild(newTextNode, oldTextNode);
    aObj.getElementsByTagName('img')[0].src = BBS_OPEN_IMG_SRC;
    //set padding
    $('.post_box').each(function() {
      this.style.padding = BBS_HIDDEN_WRAP_BOX_PADDING;
      if (this.id == 'bbs_last_box') {
          this.style.marginBottom = 0;
      }
      this.getElementsByTagName('table')[0].style.marginBottom = BBS_HIDDEN_TOP_TITLE_BAR_MARGIN_BOTTOM;
    });
    $('.post_box_res').each(function() {
      this.style.paddingTop = BBS_HIDDEN_RES_TABLE_PADDING_TOP;
      this.style.backgroundPosition = BBS_HIDDEN_RES_TABLE_BACKGROUND_POSITION;
    });
    //display
    $('.bbs_comments').hide(); //default hide
  }
  var i = 0; //for post_flag
  //loop of text_contensts -----
  $('.post_box').each(function() {
    var imgObj = this.getElementsByTagName('table')[0].getElementsByTagName('img')[0];
    //set hide default
    if (!bbs_disp_default_flag) {
      //default of one group button
      imgObj.src = BBS_OPEN_IMG_SRC;
      imgObj.alt = BBS_ONE_OPEN_BTN_VALUE;
    }
    //check post flag
    if (bbs_post_flag && i == 0) {
      //default show (only first thread) 
      imgObj.src = BBS_CLOSE_IMG_SRC;
      imgObj.alt = BBS_ONE_CLOSE_BTN_VALUE;
      var bbs_postBoxObj_post = imgObj.parentNode.parentNode.parentNode.parentNode.parentNode;
      var bbs_comments_post = dom.core.getElementByClassName(bbs_postBoxObj_post, 'bbs_comments', 'div');
      var post_box_res_post = dom.core.getElementByClassName(bbs_postBoxObj_post, 'post_box_res', 'div');
      bbs_postBoxObj_post.style.padding = BBS_SHOWN_WRAP_BOX_PADDING;
      bbs_postBoxObj_post.getElementsByTagName('table')[0].style.marginBottom = BBS_SHOWN_TOP_TITLE_BAR_MARGIN_BOTTOM;
      $(post_box_res_post).each(function() {
        this.style.paddingTop = BBS_SHOWN_RES_TABLE_PADDING_TOP;
        this.style.backgroundPosition = BBS_SHOWN_RES_TABLE_BACKGROUND_POSITION;
      });
      $(bbs_comments_post).each(function() {
        $(this).show();
      });
    }
    //click event of one group button ---
    $(imgObj).click(function() {
      var postBoxObj = this.parentNode.parentNode.parentNode.parentNode.parentNode;
      var bbs_comments = dom.core.getElementByClassName(postBoxObj, 'bbs_comments', 'div');
      var display_flag = bbs_comments[0].style.display;
      var post_box_res = dom.core.getElementByClassName(postBoxObj, 'post_box_res', 'div');
      //change image button src
      if (display_flag == 'none') {
        //show
        postBoxObj.style.padding = BBS_SHOWN_WRAP_BOX_PADDING;
        postBoxObj.getElementsByTagName('table')[0].style.marginBottom = BBS_SHOWN_TOP_TITLE_BAR_MARGIN_BOTTOM;
        $(post_box_res).each(function() {
          this.style.paddingTop = BBS_SHOWN_RES_TABLE_PADDING_TOP;
          this.style.backgroundPosition = BBS_SHOWN_RES_TABLE_BACKGROUND_POSITION;
        });
      } else {
        //hide
        postBoxObj.style.padding = BBS_HIDDEN_WRAP_BOX_PADDING;
        postBoxObj.getElementsByTagName('table')[0].style.marginBottom = BBS_HIDDEN_TOP_TITLE_BAR_MARGIN_BOTTOM;
        $(post_box_res).each(function() {
          this.style.paddingTop = BBS_HIDDEN_RES_TABLE_PADDING_TOP;
          this.style.backgroundPosition = BBS_HIDDEN_RES_TABLE_BACKGROUND_POSITION;
        });
      }
      //motion
      $(bbs_comments).each(function() {
        $(this).slideToggle('fast'); //hide or show motion
      });
      //change hover icon src ---
      $(imgObj).hover(
        function() {
          var postBoxObj = this.parentNode.parentNode.parentNode.parentNode.parentNode;
          var bbs_comments = dom.core.getElementByClassName(postBoxObj, 'bbs_comments', 'div');
          var display_flag = bbs_comments[0].style.display;
          if (display_flag == 'none') {
            this.src = BBS_OPEN_IMG_HOVER_SRC;
          } else {
            this.src = BBS_CLOSE_IMG_HOVER_SRC;
          }
        },
        function() {
          var postBoxObj = this.parentNode.parentNode.parentNode.parentNode.parentNode;
          var bbs_comments = dom.core.getElementByClassName(postBoxObj, 'bbs_comments', 'div');
          var display_flag = bbs_comments[0].style.display;
           if (display_flag == 'none') {
            this.src = BBS_OPEN_IMG_SRC;
          } else {
            this.src = BBS_CLOSE_IMG_SRC;
        }
      });
      setTimeout(function() {
        if (display_flag == 'none') {
          //show
          imgObj.src = BBS_CLOSE_IMG_SRC;
          imgObj.alt = BBS_ONE_CLOSE_BTN_VALUE;
        } else {
          //hide
          imgObj.src = BBS_OPEN_IMG_SRC;
          imgObj.alt = BBS_ONE_OPEN_BTN_VALUE;
        }
      }, 10); //wait 0.01sec
    });
    //change hover icon src default---
    $(imgObj).hover(
      function() {
        var postBoxObj = this.parentNode.parentNode.parentNode.parentNode.parentNode;
        var bbs_comments = dom.core.getElementByClassName(postBoxObj, 'bbs_comments', 'div');
        var display_flag = bbs_comments[0].style.display;
        if (display_flag == 'none') {
          this.src = BBS_OPEN_IMG_HOVER_SRC;
        } else {
          this.src = BBS_CLOSE_IMG_HOVER_SRC;
        }
      },
      function() {
        var postBoxObj = this.parentNode.parentNode.parentNode.parentNode.parentNode;
        var bbs_comments = dom.core.getElementByClassName(postBoxObj, 'bbs_comments', 'div');
        var display_flag = bbs_comments[0].style.display;
        if (display_flag == 'none') {
          this.src = BBS_OPEN_IMG_SRC;
        } else {
          this.src = BBS_CLOSE_IMG_SRC;
        }
      }
    );
    i++;
  });
  //set click event of changing all-change-button display & value -----
  $('#bbs_disp_flag_button').click(function() {
    if (flag.value == 1) {
      //hide ---
      $('.bbs_comments').hide(); //hide motion
      flag.value = null;
      //change margin,padding
      $('.post_box').each(function() {
        this.style.padding = BBS_HIDDEN_WRAP_BOX_PADDING;
        this.getElementsByTagName('table')[0].style.marginBottom = BBS_HIDDEN_TOP_TITLE_BAR_MARGIN_BOTTOM;
      });
      $('.post_box_res').each(function() {
        this.style.paddingTop = BBS_HIDDEN_RES_TABLE_PADDING_TOP;
        this.style.backgroundPosition = BBS_HIDDEN_RES_TABLE_BACKGROUND_POSITION;
      });
      //change icon src
      aObj.getElementsByTagName('img')[0].src = BBS_OPEN_IMG_SRC;
      $('.bbs_open_close_btn').each(function() {
        this.src = BBS_OPEN_IMG_SRC;
        this.alt = BBS_ONE_CLOSE_BTN_VALUE;
      });
      //hover
      $('.post_box').each(function() {
        $(this.getElementsByTagName('table')[0].getElementsByTagName('img')[0]).hover(
          function() {
            this.src = BBS_OPEN_IMG_HOVER_SRC;
          },
          function() {
            this.src = BBS_OPEN_IMG_SRC;
          }
        );
      });
      //set cookie of bbs display flag
      cookie.set(BBS_COOKIE_HIDE_FLAG_NAME, 1, BBS_COOKIE_FLAG_EFFECTIVE_DAYS, cookie_path);
      //make text node
      var oldTextNode = aSpanObj.firstChild;
      var newTextNode = document.createTextNode(BBS_ALL_OPEN_BTN_VALUE);
    } else {
      //show ---
      $('.bbs_comments').show(); //show motion
      flag.value = 1;
      $('.post_box').each(function() {
        this.style.padding = BBS_SHOWN_WRAP_BOX_PADDING;
        if (ua.indexOf("Safari")>-1) {
          //for safari
          this.getElementsByTagName('table')[0].style.marginBottom = BBS_SHOWN_TOP_TITLE_BAR_MARGIN_BOTTOM;
        } else {
          //other browser
          this.getElementsByTagName('table')[0].removeAttribute('style');
        }
      });
      $('.post_box_res').each(function() {
        if (ua.indexOf("Safari")>-1) {
          //for safari
          this.style.paddingTop = BBS_SHOWN_RES_TABLE_PADDING_TOP;
          this.style.backgroundPosition = BBS_SHOWN_RES_TABLE_BACKGROUND_POSITION;
        } else {
          //other browser
          this.removeAttribute('style');
        }
      });
      //change icon src
      aObj.getElementsByTagName('img')[0].src = BBS_CLOSE_IMG_SRC;
      //hover
      $('.post_box').each(function() {
        $(this.getElementsByTagName('table')[0].getElementsByTagName('img')[0]).hover(
          function() {
            this.src = BBS_CLOSE_IMG_HOVER_SRC;
          },
          function() {
            this.src = BBS_CLOSE_IMG_SRC;
          }
        );
      });
      $('.bbs_open_close_btn').each(function() {
        this.src = BBS_CLOSE_IMG_SRC;
        this.alt = BBS_ONE_CLOSE_BTN_VALUE;
      });
      //set cookie of bbs display flag
      cookie.set(BBS_COOKIE_HIDE_FLAG_NAME, '', BBS_COOKIE_FLAG_EFFECTIVE_DAYS, cookie_path);
      //make text node
      var oldTextNode = aSpanObj.firstChild;
      var newTextNode = document.createTextNode(BBS_ALL_CLOSE_BTN_VALUE);
    }
    aSpanObj.replaceChild(newTextNode, oldTextNode); //change A tag text
    return false; //for A tag invalidation
  });
  //change hover icon src default -----
  $(aObj).hover(
    function() {
      if (flag.value == 1) {
        this.getElementsByTagName('img')[0].src = BBS_CLOSE_IMG_HOVER_SRC;
      } else {
        this.getElementsByTagName('img')[0].src = BBS_OPEN_IMG_HOVER_SRC;
      }
    },
    function() {
      if (flag.value == 1) {
        this.getElementsByTagName('img')[0].src = BBS_CLOSE_IMG_SRC;
      } else {
        this.getElementsByTagName('img')[0].src = BBS_OPEN_IMG_SRC;
      }
    }
  );
});