/**
 * 
 * 
 * @author  Keizo Miyata <miyata@able.ocn.ne.jp>
 * @create  2008/02/26
 * @copyright 2007 Sunrise Digital Corporation.
 * @version $id: v 1.0 2008/02/26 12:13:49 Miyata Exp $
 **/
 
var sdIsIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;

Array.prototype.sdShuffle = function() {
    var i = this.length;
    while(i){
        var j = Math.floor(Math.random()*i);
        var t = this[--i];
        this[i] = this[j];
        this[j] = t;
    }
    return this;
}

Array.prototype.sdSearch = function(value, lazy) {
  
  if(lazy){
    var reg = new RegExp(lazy, "igm");
    value = value.replace(reg, '');
  }
  
  for(var i=0; i<this.length; i++){
    var current = lazy ? this[i].replace(reg, '') : this[i];
    if(current == value) return i;
  }
  return false;
}

Array.prototype.sdImplode = function(delim)
{
  var string = '';
  for(var i=0; i<this.length; i++)
  {
    if(this[i])
    {
      string += this[i];
    }
    
    if(i != (this.length-1))
    {
      string += delim;
    }
  }
  return string;
}

String.prototype.sdTrim = function(tirm_char) {
  if(!tirm_char) tirm_char = " @";
  var pattern = "^["+tirm_char+"]+|["+tirm_char+"]+$";
  var reg = new RegExp(pattern, "g");
  
  return this.replace(reg, '');
}

String.prototype.sdWidth = function() {
  len = 0;
  str = escape(this);
  for (i = 0; i < str.length; i++, len++) {
    if (str.charAt(i) == "%") {
      if (str.charAt(++i) == "u") {
        i += 3;
        len++;
      }
    i++;
    }
  }
   return len;
}

String.prototype.sdExplode = function(delim)
{
  var values = new Array();
  var len = 0;
  var key = 0;
  for (i = 0; i < this.length; i++, len++)
  {
    if (this.charAt(i) == delim)
    {
      ++key;
    }
    else if(!values[key])
    {
      values[key] = this.charAt(i);
    }
    else
    {
      values[key] += this.charAt(i);
    }
  }
  return values;
}

//////////////////////////////////////////////////////////////////
var SdDom = new Object();

SdDom.getChildNodes = function(parent){
  
  var nodes = parent.childNodes;
  if(!nodes) return null;
  
  var list = new Array();
  var j = 0
  for (var i = 0; i < nodes.length; i++) {
    if(this._isNotTag(nodes[i])) list[j++] = nodes[i];
  }
  return list;
}

SdDom.getFirstChild = function(parent){
  var nodes = parent.childNodes;
  if(!nodes) return null;
  
  for (var i = 0; i < nodes.length; i ++) {
    if(this._isNotTag(nodes[i])) return nodes[i];
  }
  
  return null;
}

SdDom.getChildByClass = function(parent, class_name){
  var nodes = parent.childNodes;
  if(!nodes) return null;
  
  var node;
  for (var i = 0; i < nodes.length; i ++) {
    if(this._isNotTag(nodes[i]) && nodes[i].className == class_name){
      node = nodes[i];
      break;
    }
  }
  return node;
}

SdDom.getSelectedOption = function(dom_select){
  var node;
  for (var i = 0; i < dom_select.options.length; i ++) {
    if(dom_select.options[i].selected){
      node = dom_select.options[i];
      break;
    }
  }
  return node;
}

SdDom.getElemByName = function(form, name){
  var doms = Form.getElements(form);
  for(var ii=0; ii<doms.length; ii++){
    if(doms[ii].name == name) return doms[ii];
  }
  return
}

SdDom.onLoad = function(id, func){
  if(!this.interval_id)
    this.interval_id = new Object();
  
  if(this.interval_id[id])
    clearInterval(this.interval_id[id]);

  this.interval_id[id] = setInterval("SdDom._isLoadedDom('"+id+"',"+func+")", 300);
}

SdDom._isLoadedDom = function(id, func){
  //if(!sdIsIE) console.info(this.interval_id);
  if($(id)){
    func($(id));
    clearInterval(this.interval_id[id]);
  }
}

SdDom._isNotTag = function(node){
  return typeof node.tagName != "undefined";
}



/////////////////////////////////////////////////////////////////
var SdCookie = new Object();

SdCookie.set = function(name, value, second){
	var exp_date = new Date();
	
	exp_date.setTime(exp_date.getTime()+(second * 1000));

	var item = "@" + name + "=" + escape(value) + ";";
	item += "path=/;"
	item += "expires="+exp_date.toGMTString();
	
	document.cookie = item;
}

SdCookie.get = function(name){
   name = "@" + name + "=";
   var value = "";
   var cookie_string = document.cookie + ";" ;
   var pos_name = cookie_string.indexOf(name);
   
   if (pos_name != -1){
      var start = pos_name + name.length;
      var end   = cookie_string.indexOf(";" , start);
      value = unescape(cookie_string.substring(start,end));
   }
   return value;
}

SdCookie.del = function(name){
   this.set(name,"",0);
}