/* 文字コード認識用のコメント＠UTF8N */

function Is() {
  var agt = navigator.userAgent.toLowerCase();

  this.major = parseInt(navigator.appVersion);
  this.minor = parseFloat(navigator.appVersion);

  this.nav   = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
                && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
                && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
  this.nav2  = (this.nav && (this.major == 2));
  this.nav3  = (this.nav && (this.major == 3));
  this.nav4  = (this.nav && (this.major == 4));
  this.nav4up = (this.nav && (this.major >= 4));
  this.navonly      = (this.nav && ((agt.indexOf(";nav") != -1) ||
                                    (agt.indexOf("; nav") != -1)) );
  this.nav6   = (this.nav && (this.major == 5));
  this.nav6up = (this.nav && (this.major >= 5));
  this.gecko  = (agt.indexOf('gecko') != -1);
  this.ff     = (agt.indexOf('firefox') != -1);

  this.ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
  this.ie3    = (this.ie && (this.major < 4));
  this.ie4    = (this.ie && (this.major == 4) && (agt.indexOf("msie 4")!=-1) );
  this.ie4up  = (this.ie && (this.major >= 4));
  this.ie5    = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.0")!=-1) );
  this.ie5_5  = (this.ie && (this.major == 4) && (agt.indexOf("msie 5.5") !=-1));
  this.ie5up  = (this.ie && !this.ie3 && !this.ie4);
  this.ie5_5up =(this.ie && !this.ie3 && !this.ie4 && !this.ie5);
  this.ie6    = (this.ie && (this.major == 4) && (agt.indexOf("msie 6.")!=-1) );
  this.ie6up  = (this.ie && !this.ie3 && !this.ie4 && !this.ie5 && !this.ie5_5);

  this.aol   = (agt.indexOf("aol") != -1);
  this.aol3  = (this.aol && this.ie3);
  this.aol4  = (this.aol && this.ie4);
  this.aol5  = (agt.indexOf("aol 5") != -1);
  this.aol6  = (agt.indexOf("aol 6") != -1);

  this.opera = (agt.indexOf("opera") != -1);
  this.opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
  this.opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
  this.opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
  this.opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
  this.opera5up = (this.opera && !this.opera2 && !this.opera3 && !this.opera4);

  this.safari = (agt.indexOf("safari") != -1);

  if (this.nav2 || this.ie3) this.js = 1.0;
  else if (this.nav3) this.js = 1.1;
  else if (this.opera5up) this.js = 1.3;
  else if (this.opera) this.js = 1.1;
  else if ((this.nav4 && (this.minor <= 4.05)) || this.ie4) this.js = 1.2;
  else if ((this.nav4 && (this.minor > 4.05)) || this.ie5) this.js = 1.3;
  else if (this.hotjava3up) this.js = 1.4;
  else if (this.nav6 || this.gecko) this.js = 1.5;
  else if (this.nav6up) this.js = 1.5;
  else if (this.ie5up) this.js = 1.3
  else this.js = 0.0;
  this.win   = ( (agt.indexOf("win")!=-1) || (agt.indexOf("16bit")!=-1) );
  this.win95 = ((agt.indexOf("win95")!=-1) || (agt.indexOf("windows 95")!=-1));
  this.win16 = ((agt.indexOf("win16")!=-1) ||
                (agt.indexOf("16bit")!=-1) || (agt.indexOf("windows 3.1")!=-1) ||
                (agt.indexOf("windows 16-bit")!=-1) );
  this.win31 = ((agt.indexOf("windows 3.1")!=-1) || (agt.indexOf("win16")!=-1) ||
                (agt.indexOf("windows 16-bit")!=-1));
  this.winme = ((agt.indexOf("win 9x 4.90")!=-1));
  this.win2k = ((agt.indexOf("windows nt 5.0")!=-1));
  this.win98 = ((agt.indexOf("win98")!=-1) || (agt.indexOf("windows 98")!=-1));
  this.winnt = ((agt.indexOf("winnt")!=-1) || (agt.indexOf("windows nt")!=-1));
  this.win32 = (this.win95 || this.winnt || this.win98 ||
                ((this.major >= 4) && (navigator.platform == "Win32")) ||
                (agt.indexOf("win32")!=-1) || (agt.indexOf("32bit")!=-1));
  this.mac    = (agt.indexOf("mac")!=-1);
  if (this.mac && this.ie5up) this.js = 1.4;
  this.mac68k = (this.mac && ((agt.indexOf("68k")!=-1) ||
                              (agt.indexOf("68000")!=-1)));
  this.macppc = (this.mac && ((agt.indexOf("ppc")!=-1) ||
                              (agt.indexOf("powerpc")!=-1)));
  this.linux = (agt.indexOf("inux")!=-1);
  this.freebsd = (agt.indexOf("freebsd")!=-1);
  this.bsd = (agt.indexOf("bsd")!=-1);
  this.unix  = ((agt.indexOf("x11")!=-1) || this.sun || this.irix || this.hpux ||
                this.sco ||this.unixware || this.mpras || this.reliant ||
                this.dec || this.sinix || this.aix || this.linux || this.bsd || this.freebsd);
  this.dom = (document.getElementById) ? true : false;
}

function RollOver (obj,val) {
  obj.src = val;
}

function winopen(url, name, width, height, left, top, dependent, location, menubar, resizable, status, scrollbars, titlebar, toolbar) {
  var param = new Array();
  if(width)      param[0]  = 'width='      + width;
  if(height)     param[1]  = 'height='     + height;
  if(left)       param[2]  = 'left='       + left;
  if(top)        param[3]  = 'top='        + top;
  if(dependent)  param[4]  = 'dependent='  + dependent;
  if(location)   param[5]  = 'location='   + location;
  if(menubar)    param[6]  = 'menubar='    + menubar;
  if(resizable)  param[7]  = 'resizable='  + resizable;
  if(status)     param[8]  = 'status='     + status;
  if(scrollbars) param[9]  = 'scrollbars=' + scrollbars;
  if(toolbar)    param[10] = 'toolbar='    + toolbar;

  if(param.length > 0) {
    var option = param.join(',');
  } else {
    var option = '';
  }
  win = window.open(url, name, option);
}

function winclose() {
  top.name   = 'close_window';
  top.opener = window;
  var win = window.open(location.href, window.name);
  top.close();
}

function reset_text(id, x) {
  var obj = document.getElementById(id);

  if(!obj) {
    return false;
  }

  obj['size'] = x;
}

function resize_textarea(id, dir, size) {
  var obj = document.getElementById(id);

  if(!obj) {
    return false;
  }

  if((obj[dir] + size) <= 0) {
    return false;
  }

  if(dir == 'cols') {
    alert('幅のリサイズは制限されています。');
    return false;
  }

  obj[dir] += size;
  
}

function reset_textarea(id, x, y) {
  var obj = document.getElementById(id);

  if(!obj) {
    return false;
  }

  obj['cols'] = x;
  obj['rows'] = y;
}

function confirming(msg, url) {
  if(confirm(msg)) {
    location.href = url;
    return true;
  } else {
    return false;
  }
}

function insert_tag(area, stag, etag) {
  var is = new Is();

  area.focus();

  switch(true) {
  case (is.nav && !is.macppc):  ///< Mozzila:Firefox/Netscape/etc...
    var prefix = area.value.substring(0, area.selectionStart);
    var select = area.value.substring(area.selectionStart, area.selectionEnd);
    var suffix = area.value.substring(area.selectionEnd, area.value.length);
    area.value = prefix + stag + select + etag + suffix;
    break;

  case (is.ie && !is.macppc):  ///< Microsoft Internet Explorer
    var text = document.selection.createRange().text;
    if(text) {
      document.selection.createRange().text = stag + text + etag;
    } else {
      document.selection.createRange().text = stag + etag;
    }
    break;

  default:  ///< Other Browsers & Platforms
    area.value += stag + etag;
    break;
  }

  return;
}

function preview_form_update(form, item, defvalue) {
  var name = item.name.replace("mobile_", "");
  switch(name) {
  case 'form_style':
    document.getElementById('style').href="/themes/_"+item.value+"/css/base.css";
    break;
  case 'form_title':
  case 'form_header':
  case 'form_footer':
    if(item.value == '') {
      document.getElementById('preview_'+name).innerHTML = defvalue;
    } else {
      var value = item.value;
      value = value.replace(/\r\n/g, '<br />');
      value = value.replace(/\n/g,   '<br />');
      document.getElementById('preview_'+name).innerHTML = value;
    }
    break;
  case 'form_button':
    var button = document.getElementById('preview_'+name);
    if(item.value == '') {
      button.value = defvalue;
    } else {
      button.value = item.value;
    }
    break;
  case 'field_mail_title':
  case 'field_name_title':
  case 'field_sex_title':
  case 'field_age_title':
  case 'field_prefecture_title':
  case 'field_company_name_title':
  case 'field_url_title':
    var title = document.getElementById('preview_'+name);
    if(item.value == '') {
      title.innerHTML = defvalue;
    } else {
      title.innerHTML = item.value;
    }
    break;
  case 'field_name':
  case 'field_sex':
  case 'field_age':
  case 'field_prefecture':
  case 'field_company_name':
  case 'field_url':
    if(item.value == '1') {
      document.getElementById('preview_'+name).style.display = 'block';
    } else {
      document.getElementById('preview_'+name).style.display = 'none';
    }
    break;
  case 'field_name_limit':
  case 'field_sex_limit':
  case 'field_age_limit':
  case 'field_prefecture_limit':
  case 'field_company_name_limit':
  case 'field_url_limit':
    if(item.value == 'false') {
      document.getElementById('preview_'+name).innerHTML = '*';
    } else {
      document.getElementById('preview_'+name).innerHTML = '';
    }
    break;
  default:
    break;
  }
}

function preview_mail_update(form, item, defvalue) {
  var name = item.name.replace("mobile_", "");
  switch(name) {
  case 'reply_name':
  case 'reply_from':
  case 'reply_subject':
  case 'reply_header':
  case 'reply_footer':
    if(item.value == '') {
      document.getElementById('preview_'+name).innerHTML = defvalue;
    } else {
      var value = item.value;
      value = value.replace(/\r\n/g, '<br />');
      value = value.replace(/\n/g,   '<br />');
      document.getElementById('preview_'+name).innerHTML = value;
    }
    break;
  default:
    if(item.value == '') {
      var value = document.getElementById('preview_'+name).innerHTML = defvalue;
    } else {
      var value = item.value;
      value = value.replace(/\r\n/g, '<br />');
      value = value.replace(/\n/g,   '<br />');
    }
    if(name.indexOf('reply_title', 0) >= 0) {
      document.getElementById('preview_'+name).innerHTML = value;
    } else if(name.indexOf('reply_outcome', 0) >= 0) {
      document.getElementById('preview_'+name).innerHTML = value;
    }
    break;
  }
}


function get_mode() {
  var is = new Is();
  var mode;
  if(is.opera){
    mode = 4;
  } else if(is.ie) {
    if(is.macppc) {
      mode = 4;
    }
    else {
      mode = 2;
    }
  } else if(is.safari) {
    mode = 4;
  } else if(is.nav) {
    if(is.macppc) {
      mode = 4;
    }
    else {
      mode = 1;
    }
  } else if(is.ff) {
    mode = 1;
  } else if(is.gecko) {
    mode = 1;
  } else {
    mode = 4;
  }
  return mode;
}

TextInserter = Class.create();
TextInserter.prototype = {
/**
 *  Constructor
 *  
 *  @param  element the textarea element
 *  @param  mode    a browser mode
*/
initialize: function(element, mode) {
  this.textelement = element;
  this.start = 0;
  this.sel   = null;
  this.rang  = null;
  this.mode  = mode;

  if(this.mode == 1 || this.mode == 4) {
    if(navigator.userAgent.indexOf('Sony/COM2') != -1) {
      this.start = element.value.length;
    }else {
      this.start = element.selectionStart;
    }
  }
  else if(this.mode == 2) {
    this.textelement.focus();
    this.sel = document.selection.createRange();
    this.rang = element.createTextRange();
    this.start = element.selectionStart;
    this.rang.moveToPoint(this.sel.offsetLeft,this.sel.offsetTop);
    this.rang.moveEnd("textedit");
    if(this.rang.text.replace(/\r/g,"").length != 0){
      var las = (element.value.match(/(\r\n)*$/),RegExp.lastMatch.length);
      this.start = (element.value.length - (this.rang.text.length + las));
    }else{
      this.start = (element.value.length - (this.rang.text.length));
    }
  }else{
    this.start = element.value.length;
  }
},

/**
 *    adds text to the specified position.
 *    @param code insert character
 */
insertText: function(code) {
  var bl1 = this.textelement.value.substring(0, this.start);
  var bl2 = this.textelement.value.substring(this.start,this.textelement.value.length);
  this.textelement.value = bl1 + code + bl2;
  //If browser is InternetExplorer, revert the cursor postion.
  if(this.mode == 2) {
    this.rang.moveStart("character",bl1.length + code.length);
    this.rang.moveEnd("character",-(bl2.replace(/\r/g,"").length));
    this.rang.select();
  }
}

}

/**
 *  Insertion Popup abstract class
 */
Insertion = Class.create();
Insertion.prototype = {
/**
 *  Constructor
 *  @param  poupp_name     create a popup name
 *  @param  width          popup width(size specified by css)
 *  @param  adv_x          max popup width
 *  @param  closefunction  popup close function
 */
initialize: function(popup_name, width, adv_x, closefunction){
  this.pos_x = 0;
  this.pos_y = 10;
  this.onclick_function = closefunction;
  this.width = width;
  this.adv_x = adv_x;
  this.textinsert = null;

  this.popup_name = popup_name;
  this.popup = Popup.create(this.popup_name);
  this.popup.loadPlugin('Overlay');
  Element.setOpacity(Overlay.getInstance().getElement(), 0);
},

/**
 *    Open popup
 *    @param    element     insert text element
 *    @param    event
 *    @param    x
 *    @param    y
 */
openPopup: function(element, event, x, y){

  this.textinsert = new TextInserter(element, get_mode());

  var id = this.popup_name;

  var x = 0;
  var y = 0;
  x = (typeof(x) == 'undefined' || x == null) ? this.pos_x : x;
  y = (typeof(y) == 'undefined' || y == null) ? this.pos_y : y;

  var mouse = this.getMousePosition(event);

  this.showPopup();
  var scroll_x = (window.opera) ? window.pageXOffset : 0;
  var scroll_y = (window.opera) ? window.pageYOffset : 0;
  this.pos_x   = mouse.x + x + scroll_x;
  this.pos_y   = mouse.y + y + scroll_y;
  var limit_x  = this.adv_x - this.width;

  if(this.pos_x > limit_x) {
    this.pos_x -= (this.pos_x - limit_x);
  }

  var obj = (document.getElementById) ? document.getElementById(id).style :
  (document.all) ? document.all[id].style : null;

  obj.display = "block";
  obj.left = this.pos_x + "px";
  obj.top  = this.pos_y + "px";
  obj.zIndex = 2;

  //Overlay
  obj.zIndex = 2;
  Event.observe(Overlay.getInstance().getElement(), 'click', this.onclick_function);

  Overlay.setZIndex(1);
  Overlay.appear();
},

closePopup: function(){
  id = this.popup_name;
  var obj = (document.getElementById) ? document.getElementById(id).style :
  (document.layers) ? document.layers[id]    :
  (document.all)    ? document.all[id].style : null;

  obj.display="none";
  Event.stopObserving(
    Overlay.getInstance().getElement(),
    'click',
    this.onclick_function
    );
  Overlay.fade();
},

/**
 *  insert tag
 *  @param code insert character
 */
inputTag: function(tag){
  this.textinsert.insertText(tag);
},

getMousePosition: function(evt) {
  var mouse = {x:null, y:null};

  if(window.opera) {
    mouse.x = evt.clientX;
    mouse.y = evt.clientY;
  } else if(document.all) {
    var pos = Position.realOffset(document.body);
    mouse.x = event.clientX + pos[0];
    mouse.y = event.clientY + pos[1];
  } else if(document.layers || document.getElementById) {
    mouse.x = evt.pageX;
    mouse.y = evt.pageY;
  }
  return mouse;
},

/**
 *  Display of popup contents
 */
showPopup: function() {
  Element.show(this.popup_name);
}
}

var insertion_popup;

Event.observe(window, 'load', function() {
  if($('insertion_popup')) {
    insertion_popup = new Insertion('insertion_popup', 160, 719, function(){closePopup()});
  }
});

function openPopup(element, event) {
  insertion_popup.openPopup(element, event, 0, 10);
}

function closePopup() {
  insertion_popup.closePopup();
}

function inputTag(tag) {
  insertion_popup.inputTag(tag);
}
