/**
 * Ozein Common
 *  (c) 2007 NEXT CO.,LTD. (http://www.next-group.jp/)
 */

Ozein.Popup = new Class.create();
Ozein.Popup.prototype = {
  win: null,
  initialize: function(node, options) {
    this.setOptions(options);
    Event.observe(node, 'click', this.click.bindAsEventListener(this, node));
  },
  setOptions: function(options) {
    this.options = {
      width: 650,
      height: 620,
      scrollbars: 'yes',
      resizable: 'yes'
    }
    Object.extend(this.options, options || {});
  },
  click: function(e, node) {
    href = (this.options.href || node.getAttribute('href'));
    name = node.getAttribute('target');
    if (this.win == null || this.win.closed) {
      var settings = $H(this.options).toQueryString().replace(/&/g, ',');
      this.win = window.open(href, name, settings);
    } else {
      this.win.location.replace(href);
    }
    this.win.focus();
    Event.stop(e);
  }
}

Ozein.FixImage = new Class.create();
Ozein.FixImage.prototype = {
  initialize: function(node, options) {
    if (!node.tagName.eqi('img')) return;
    this.setOptions(options);
	this.load(node);
//    Event.observe(node, 'load', this.load.bindAsEventListener(this, node));
  },
  load: function(node) {
    max_w = this.options.width;
    max_h = this.options.height;
    w = node.getWidth();
    h = node.getHeight();
    scale = w / h;
    if (h > max_h) {
      w = scale * max_h;
      h = max_h;
    }
    if (w > max_w) {
      h = max_w / scale;
      w = max_w;
    }
    if (w != node.getWidth() || h != node.getHeight()) {
      node.width = w;
      node.height = h;
    }
    if (this.options.autoDisplay && node.getStyle('display')=='none') {
      node.style.display = '';
    }
  },
  setOptions: function(options) {
    this.options = {
      width: 50,
      height: 50,
      autoDisplay: true
    }
    Object.extend(this.options, options || {});
  }
}

Ozein.VerticalText = Class.create();
Ozein.VerticalText.prototype = {
  initialize: function(node) {
    if(/MSIE/.test(navigator.userAgent)){
      node.style.writingMode = 'tb-rl';
    } else {
      var text = new String(node.innerHTML);
      var vtext = "";
      for(var i=0; i<text.length; ++i) {
        var ch = text.charAt(i);
        if (ch == 'ー') {ch = '｜'};
        vtext += ch + "\n";
      }
      node.innerHTML = '<pre style="line-height: 1em;">'+vtext+'</pre>';
    }
  }
}

Ozein.Beacon = Class.create();
Ozein.Beacon.prototype = {
  initialize: function(node, options) {
    this._q = new Array();
    this.setOptions(options);
    if (this.options.salt) {
      this.dt = new Date();
    }
    if(this.options.autoSend){
      this.send();
    }
  },
  setOptions: function(options) {
    this.options = {
      autoSend: false,
      salt: 'slt',
      async: true
    }
    Object.extend(this.options, options || {});
  },
  stacker: new Array,
  send: function() {
    if (this.options.salt) {
      this.addQuery(this.options.salt + '=' + this.dt.getTime());
    }
    
    if (this.options.async) {
      Event.observe(document, 'click', this.denyClick, true);
    }
    
    var url = this.options.url
    
    if(this._q.length > 0) {
        url += '?' + this._q.join('&');
    }
    var img = new Image();
    if (this.options.async) {
      Event.observe(img, 'load', this.allowClick.bindAsEventListener(this));
      this.timeoutId = setTimeout(this.allowClick.bindAsEventListener(this), 3000);
    }
    img.src = url;
    this.stacker.push(img);
  },
  addQuery: function(q) {
    this._q.push(q);
  },
  denyClick: function(e) {
    Event.stop(e);
  },
  allowClick: function(e) {
    Event.stopObserving(document, 'click', this.denyClick, true);
    clearTimeout(this.timeoutId);
  }
}

Ozein.Cookie = Class.create();
Ozein.Cookie.prototype = {
    initialize: function(node,options) {
        this._v = new Array();
        this.dt = new Date();
        this.setOptions(options);
        if(this.options.name && this.options.value) {
            this.setValue(this.options.name,this.options.value);
        }
        if(this.options.path) {
            this.setPath(this.options.path);
        }
        if(this.options.expires) {
            this.setExpires(this.options.expires);
        }
    },
    setOptions: function(options) {
      this.options = {
          name: null,
          value: null,
          path: null,
          expires: null
      }
      Object.extend(this.options, options || {});
    },
    setCookie: function() {
        if(!this._v.name || !this._v.value) return ;
        var str = this._v.name + '=' + escape(this._v.value) + ';';
        if(this._v.path) {
            str += 'path='+this._v.path+';';
        } else {
            str += 'path=/;'
        }
        if(this._v.expires) {
            this.dt.setTime(this.dt.getTime()+this._v.expires);
            str += "expires = "+ this.dt.toGMTString() + ';';
        }
        document.cookie = str;   
    },
    getCookie: function(name) {
        var cookie = document.cookie + ";";
        var start = cookie.indexOf(name + "=" , 0);
        if(start != -1) {
            return unescape(cookie.substring(start + name.length + 1, cookie.indexOf(';',start)));
        } else {
            return null;
        }
    },
    deleteCookie: function() {
        if(!this._v.name) return ;
        var str = this._v.name + '=;';
        if(this._v.path) {
            str += 'path='+this._v.path+';';
        } else {
            str += 'path=/;'
        }
        this.dt.setYear(this.dt.getYear() - 1);
        str += "expires=" + this.dt.toGMTString() + ';';
        document.cookie = str;   
    },
    setValue: function(name,value) {
        this._v['name'] = name;
        this._v['value'] = value;
    },
    setPath: function(path) {
        this._v['path'] = path;
    },
    setExpires: function(expires) {
        this._v['expires'] = expires;
    }
}


//FormTextBox HintMessage Component
Ozein.TextHint = new Class.create();
Ozein.TextHint.prototype = {
    initialize: function(node, options) {
        this.node = node;
        this.setOptions(options);
        this.blur();
        Event.observe(this.node, 'focus', this.focus.bindAsEventListener(this));
        Event.observe(this.node, 'blur', this.blur.bindAsEventListener(this));
    },
    setOptions: function(options) {
        this.options = {
            hint: '',
            hintClass: '',
            textClass: ''
        }
        Object.extend(this.options, options || {});
    },
    focus: function() {
        var node = this.node;
        if (node.value == this.options.hint) {
            node.value = '';
            if(this.options.textClass) {
                node.className = this.options.textClass;
            }
        }
    },
    blur: function() {
        var node = this.node;
        if (node.value == '') {
            node.value = this.options.hint;
            if(this.options.hintClass) {
                node.className = this.options.hintClass;
            }
        } else {
            if(this.options.textClass){
                node.className = this.options.textClass;
            }
        }
    }
}

Ozein.Switch = new Class.create();
Ozein.Switch.prototype = {
  initialize: function(node, options) {
    this.node = node;
    this.setOptions(options);
    if(this.options.display) {
        this.switchPanel();
    }
    if (!this.options.onSwitch && (e = node.getAttribute('onSwitch'))) {
      eval('this.onSwitch = function(){'+e+'}.bind(this.node)');
    }
    Event.observe(this.node, 'click', this.click.bindAsEventListener(this, this.node));
  },
  setOptions: function(options) {
      this.options = {
      ids: new Array(),
      main: null,
      display: null
    }
    Object.extend(this.options, options || {});
  },
  click: function(e, node) {
    this.switchPanel();
    Event.stop(e);
  },
  switchPanel: function() {
      if(!this.options.main) return null;
      //if($(this.options.main).style.display == 'none') {
        $(this.options.main).show();
        for(i=0;i<this.options.ids.length;i++) {
            if(this.options.ids[i] != this.options.main) {
                $(this.options.ids[i]).hide();
            }
        }
        this.onSwitch();
    //}
  },
  onSwitch: function(){
  }
}

//Format number
Ozein.NumberFormat = new Class.create();
Ozein.NumberFormat.prototype = {
    initialize: function(node, options) {},
    setOptions: function(options) {
        this.options = {}
        Object.extend(this.options, options || {});
    },
    addFigure: function(number) {
        var num = new String(number).replace(/,/g, "");
        while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
        return num;
    }
}