//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//
//　■共通処理クラス
//　依存関係：なし
//
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//クラス生成
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
clsCommonLib = new Object();
clsCommonLib = function(){
    
  //*******************************************************
  //動作フラグ
  //*******************************************************
  this.blnDebug = 0;
  this.blnCSetEsc = 0;
  this.blnCGetEsc = 1;

  //*******************************************************
  //プロパティ
  //*******************************************************
  //ブラウザ判定
  this.isWin = 0;
  this.isMac = 0;
  
  this.isIE = 0;      //IE
  this.isIEC = 0;     //IE（6以下）
  this.isFF = 0;      //Firefox
  this.isSafari = 0;  //Safari
  
  this.version = ""   //バージョン
  
  //*******************************************************
  //コンストラクタ
  //*******************************************************
  if (this.blnDebug){
    this.setDebug();
  }
  
  //*******************************************************
  //ブラウザ判定
  //概　要：　ブラウザ判定
  //引　数：　なし
  //戻り値：　なし
  //*******************************************************
  this.getUA = function(){

    var strUA = navigator.userAgent.toLowerCase();
    
    //OS
    if (/windows/.test(strUA)) { this.isWin = 1; }
    if (/macintosh/.test(strUA)) { this.isMac = 1; }
    if (/mac_/.test(strUA)) { this.isMac = 1; }
    
    //ブラウザ
    if (/msie/.test(strUA)) { this.isIE = 1; }
    if (/firefox/.test(strUA)) { this.isFF = 1; }
    if (/safari/.test(strUA)) { this.isSafari = 1; }

    //バージョン
    if (this.isIE){
      this.version = strUA.match(new RegExp("msie([^;]*)", "gi")).toString().replace("msie ", "");
    }else{
      this.version = strUA.replace(new RegExp("^.*/", "gi"), "");
    }
    
    //IE6以下判定
    if (!this.isIE){ return; }
    if (parseFloat(this.version) < 7){ this.isIEC = 1; }
    
  }
  this.getUA();

  //*******************************************************
  //デバッグ領域出力
  //概　要：　デバッグ領域を生成
  //引　数：　なし
  //戻り値：　なし
  //*******************************************************
  this.setDebug = function(){
    if (document.getElementById('_debug')){ return; }
      
    var intAlpha = 75;
    var strWrite = "";
    strWrite += "<div id=\"_debug\" ";
    strWrite += "style=\"";
    //strWrite += "position:absolute; top:0; left:0; ";
    strWrite += "position:fixed; top:0; left:0; ";
    strWrite += "z-index:999; ";
    strWrite += "border:1px solid black; background:#ffffff; ";
    strWrite += "filter: alpha(opacity="+intAlpha+"); ";
    strWrite += "-moz-opacity:0."+intAlpha+"; ";
    strWrite += "opacity:0."+intAlpha+";);\">■DEBUG</div>\n";
    document.write(strWrite);
  }
  
  //*******************************************************
  //デバッグプリント
  //概　要：　デバッグプリント
  //引　数：　[0]出力文字列
  //　　　　　[1] 0：追記 , 1：上書き
  //戻り値：　なし
  //*******************************************************
  this.debug = function(vstrString, vblnPreserve){

    if (!document.getElementById('_debug')){ return; }
    
    var objDebug = document.getElementById('_debug');
    if (vblnPreserve){
      objDebug.innerHTML = vstrString;
    }else{
      objDebug.innerHTML += vstrString;
    }
  }
  
  //*******************************************************
  //Undefined判定
  //概　要：　Undefined判定
  //引　数：　[0]ターゲットオブジェクト
  //戻り値：　１：Undefined　,　０：存在するオブジェクト
  //*******************************************************
  this.isUnDef = function(vobjTarget){
    if (vobjTarget == undefined || vobjTarget == "undefined"){
      return 1;
    }
    return 0;
  }
  
  //*******************************************************
  //カンマ編集
  //概　要：　デバッグプリント
  //引　数：　[0]対象数値
  //戻り値：　編集後の数値
  //*******************************************************
  this.canmaf = function(vintNum){
      var strNum = vintNum + "";
      var strFNum = "";
      for (var i=0; i<=strNum.length; i++){
        strFNum = strNum.substr(strNum.length - i,1) + strFNum;
        if (i % 3 == 0 && i != 0 && i != strNum.length){
          strFNum = "," + strFNum;
        }
      }
      return strFNum ;
  }

  //*******************************************************
  //タグオブジェクト索引
  //概　要：　getElementsByTagName 代替
  //          ※safari2等でgetElementsByTagNameが存在しないため
  //引　数：　[0]対象DOM
  //          [1]検索対象タグ名
  //戻り値：　DOM配列
  //*******************************************************
  this.getTag = function(objDom, strTagName) {
    
    if (objDom.getElementsByTagName(strTagName)){
      return objDom.getElementsByTagName(strTagName);
    }
    
    // getElementsByTagName is have to bug for safari.
    var objArray = new Array();
    if (objDom == undefined || objDom.childNodes == undefined)
       return null;
   
    for (var i=0; i < objDom.childNodes.length; i++) {
      if (objDom.childNodes[i].tagName == strTagName) {
        objArray.push(objDom.childNodes[i]);
      }
      if (objDom.childNodes[i].childNodes.length > 0) {
        var r = getTag(objDom.childNodes[i], strTagName);
        if (r != null) {
          // adjusting
          for (var j=0; j < r.length; j++)
            objArray.push(r[j]);
        }
      }
    }
   
    if (objArray.length > 0){
      return objArray;
    }
    
    return null;
  }

  //*******************************************************
  //イベントハンドラ設定
  //概　要：　オブジェクトに対してイベントを設定
  //引　数：　[0]対象オブジェクト
  //          [1]イベント種別
  //          [2]実装関数（関数オブジェクト）
  //戻り値：　なし
  //*******************************************************
  this.addEvent = function(objElement, strListener, objFunction){
    try{
      objElement.addEventListener(strListener, objFunction, false);
    }catch(e){
      objElement.attachEvent("on"+strListener, objFunction);
    }
  }

  //*******************************************************
  //関数オブジェクト生成
  //概　要：　関数オブジェクト生成（メソッド内でのイベントハンドラ設定時など）
  //引　数：　[0]対象オブジェクト
  //          [1]実装関数（文字列）
  //戻り値：　関数オブジェクト
  //*******************************************************
  this.getFnc = function(vstrFunction, vobjInstanse){

    //ダブルクロージャを用いて関数オブジェクトをカプセル化
    var strFunction = vstrFunction.replace(/this\./g,"vobjArges.");
    
    var objFunction = (function(vobjArges){
      return function(){ eval(strFunction) };
    })(vobjInstanse);
    
    return objFunction;
  }

  //*******************************************************
  //クッキー制御
  //概　要：　クッキーの書き込み、取得を実行
  //
  //書き込み
  //　引　数：　[0]キー
  //          　[1]値
  //          　[2]有効日数
  //          　[2]有効パス
  //　戻り値：　なし
  //
  //読み込み
  //　引　数：　[0]キー
  //　戻り値：　値
  //*******************************************************
  //書き込み
  this.setcookie = function(vstrKey, vstrValue, vstrPath, vstrDate){
    
    //キー
    if (this.blnCSetEsc){
      vstrValue = escape(vstrValue);
    }
    strItem = "@" + vstrKey + "=" + vstrValue + ";";
    
    //パス
    if (vstrPath == ""){
      vstrPath = "/"
    }
    strPath = "path=" + vstrPath + ";";
    
    //有効日数
    strTerm = "";
    if (vstrDate != ""){
      objExp = new Date();
      objExp.setTime(objExp.getTime()+(vstrDate * 24 * 60 * 60 * 1000));
      strTerm = "expires="+objExp.toGMTString();
    }
    
    document.cookie =  strItem + strPath + strTerm;
  }
  
  //読み込み
  this.getcookie = function(vstrKey){
  	var i = 0;
  	var strKey      = "@" + vstrKey + "=";
  	var strCookie   = '';
  	var astrCookies = document.cookie.split(';');
  	var strValue    = '';

  	for (i = 0; i < astrCookies.length; i ++) {
  		if (astrCookies[i].indexOf(strKey) >= 0) {
  			strCookie = astrCookies[i].replace(/ /i,'');
  		}
  	}
  	if (strCookie.indexOf(strKey) >= 0) {
  		strValue = strCookie.substr(strCookie.indexOf(strKey) + strKey.length, strCookie.length);	
  	}

    if (this.blnCGetEsc){
      strValue = unescape(strValue);
    }
  	return strValue;
  }

  //*******************************************************
  //マウス座標取得
  //概　要：　ブラウザ上のマウス座標を取得
  //引　数：　[0]イベントオブジェクト
  //戻り値：　座標オブジェクト
  //*******************************************************
  this.getMousePos = function(objEvent){
     
   //======================================================
   //座標オブジェクト生成
   //======================================================
   objPos = new Array();
    
    if (navigator.userAgent.indexOf("Opera",0) != -1){
      objPos['x'] = window.event.clientX;
      objPos['y'] = window.event.clientY;
    }else if (document.all){
      objPos['x'] = window.event.clientX + document.body.scrollLeft;
      objPos['y'] = window.event.clientY + document.body.scrollTop;
    }else if (document.getElementById){
      objPos['x'] = objEvent.clientX + window.pageXOffset;
      objPos['y'] = objEvent.clientY + window.pageYOffset;
    }
     
    return objPos;
   
  }

  //*******************************************************
  //読み込み遅延待機
  //概　要：　判定関数が実装可能になるまでスリープ状態にして繰り返し実行
  //引　数：　[0]実装可否判定関数（文字列）
  //          [1]実装関数（オブジェクト）
  //戻り値：　なし
  //*******************************************************
  this.wait = function(vstrChkFunction,objExecFunc){
    if (this.blnDebug){
      this.debug(vstrChkFunction + "<br>\n");
    }
    
  	var check = 0;

  	try{eval("check = " + vstrChkFunction);}
    catch(e){}
    
  	if(check){
  		objExecFunc()
  	}else{
  		var objWaitFunc = function(){com.wait(vstrChkFunction,objExecFunc)};
  		setTimeout(objWaitFunc,100);
  	}
  }

  //*******************************************************
  //連想配列ソート
  //概　要：　連想配列をソートする
  //引　数：  [0]多次元連想配列
  //　        [1]ソートキー
  //戻り値：　ソート後の配列
  //*******************************************************
  this.asort = function(vobjHash, vstrKey, vstrType){
    //昇順
    if (vstrType == "asc"){
      return vobjHash.sort ( function (b1, b2) { return b1[vstrKey] > b2[vstrKey] ? 1 : -1; } );
    }else if(vstrType == "desc"){
      return vobjHash.sort ( function (b1, b2) { return b1[vstrKey] > b2[vstrKey] ? -1 : 1; } );
    }
  }
  
  //*******************************************************
  //前後空白除去関数
  //概　要：　前後空白除去関数
  //引　数：  文字列
  //戻り値：　処理後文字列
  //*******************************************************
  this.trim = function(vstrValue){
    return vstrValue.replace(/^\s+|\s+$/g, "");
  }
  
  //*******************************************************
  //GETパラメータ取得
  //概　要：　GETパラメータを取得する
  //引　数：  なし
  //戻り値：　GETパラメータ配列
  //*******************************************************
  this.getParam = function(){
    
    var strQuery = window.location.search.substring(1);
    var astrParams = strQuery.split('&');
    var aobjQuery = new Array();
    
    for (var i=0; i<astrParams.length; i++) {
      if (astrParams[i].indexOf('=') < 0){ continue; }
      var aobjTemp = astrParams[i].split('=');
      aobjQuery[aobjTemp[0]] = aobjTemp[1];
    }
    
    return aobjQuery;
  }
  
  //*******************************************************
  //オフセット座標取得
  //概　要：　オフセット座標取得
  //引　数：  対象オブジェクト
  //戻り値：　座標配列
  //*******************************************************
  this.getOffsetPos = function(vobjElement) {

    var intPosY = 0;
    var intPosX = 0;
    
    do {
      intPosY += vobjElement.offsetTop  || 0;
      intPosX += vobjElement.offsetLeft || 0;
      vobjElement = vobjElement.offsetParent;
    } while (vobjElement);
    
    return [intPosY, intPosX];
  }
  
  //*******************************************************
  //相対パス取得
  //概　要：　絶対パスから相対パスに変換
  //引　数：  絶対パス
  //戻り値：　相対パス
  //*******************************************************
  this.getRelativePath = function(vstrAbsolutePath){
    
    var objProtocol = new RegExp(/^.+?:\/\//);
    var strThisPath = location.href;

    if (!strThisPath.match(objProtocol)){ 
      return vstrAbsolutePath; 
    }

    strThisPath = strThisPath.replace(objProtocol, "");
    strThisPath = strThisPath.match(new RegExp(/^.*\//));
    strThisPath = strThisPath.toString();

    strAbsolutePath = vstrAbsolutePath.replace(objProtocol, "");
    
    var astrDirs = strThisPath.split('/');
    
    var intRelativeCnt = 0;
    for (var i=0; i<astrDirs.length; i++){
      objMatch = new RegExp("^" + astrDirs[i] + "/");
      if (!strAbsolutePath.match(objMatch)){
        intRelativeCnt = astrDirs.length - i - 1;
        break;
      }else{
        strAbsolutePath = strAbsolutePath.replace(objMatch, "");
      }
    }
    
    var strRelativePath = "";
    for (var i=0; i<intRelativeCnt; i++){
      strRelativePath += "../";
    }
    strRelativePath += strAbsolutePath;
    
    return strRelativePath;
  }
  //*******************************************************
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//インスタンス生成
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
com = new clsCommonLib;

