/**
 * @author h-sano
 */
var Maguchi=function(tpltype,mgl){
	if(document.referrer==undefined || document.referrer== null || document.referrer==""){
		this.ref="www.bridal-eshte.com";
	}else{
		this.ref=document.referrer;
	}
	this.mgl=mgl;
	this.afNO=0;
 	this.afid="";
 	this.domain="www.bridal-esthe.com";
 	if(tpltype=='small'){
		this.tpl_h="/tmpl/header_s.html"
	}else{
		this.tpl_h="/tmpl/header_h.html"		
	}
	this.tpl_f="";
	this.tpltyp=tpltype;
};


Maguchi.prototype.setCoookie=function(){
	var tplget;
	tplget=false;
 		//クッキーをセットするメソッド
	if(ReadCookie("afid")=="" ){
			//クッキーがないときはfromurlからセット
			if(this.ref == undefined || this.ref== null || this.ref==""){
				WriteCookie("afid", "local");
			}else{
				//fromurlよりクッキーセット		
				var pattern=/http(s)?:\/\/(([\w-]+\.)+[\w-]+)(\/[\w-.\/?%&=]*)?/i;
				var retref=this.ref.match(pattern);
				var result=[];
				result=retref;
				if(retref!=null && result.length>0){
					//ドメインとりだせたら
					this.domain=result[2];	
					for(js in this.mgl){
						if(this.mgl[js]["domain"]==this.domain){
							if(this.tpltyp=="small"){
								this.tpl_h=this.mgl[js]["tpl_h_s"];
								this.tpl_f=this.mgl[js]["tpl_f_s"];
							}else{
								this.tpl_h=this.mgl[js]["tpl_h"];
								this.tpl_f=this.mgl[js]["tpl_f"];
							}
							this.afid=this.mgl[js]["afid"];
							tplget=true;
						}
					}
					if(tplget==true){
						//JSONの一覧があったらクッッキー書き込み
						WriteCookie("afid", this.afid);
					}else{
						//JSONの一覧になかったら
	//					WriteCookie("afid", "local");
							}
				}else{
					//ドメインとりだせなかったら
//					WriteCookie("afid", "local");
				}
			}
  }else{
  	//クッッキ-がある時
		this.afid=ReadCookie("afid")	
		for(js in this.mgl){
			if(this.mgl[js]["afid"]==this.afid){
					if(this.tpltyp=="small"){
						this.tpl_h=this.mgl[js]["tpl_h_s"];
						this.tpl_f=this.mgl[js]["tpl_f_s"];
					}else{
						this.tpl_h=this.mgl[js]["tpl_h"];
						this.tpl_f=this.mgl[js]["tpl_f"];
					}
					tplget=true;							
			}
		}
  }
};
Maguchi.prototype.getHeadtpl = function() {
  return this.tpl_h;  
};
Maguchi.prototype.getFoottpl = function() {
  return this.tpl_f;  
};
//getJSON
//getjosonしてテンプレ＾ーとをセット
function getMGJOSN(tmpltype){
	
	var arr=[];
	jQuery.getJSON("/php/maguchi/maguchi_no.php" ,null,function(data){
		arr=data;
		var mg=new Maguchi(tmpltype,arr);
    var hpath;
    var fpath;
  	mg.setCoookie();
  	hpath=mg.getHeadtpl();
  	fpath=mg.getFoottpl();
  	if(mg.afid!=null && mg.afid!='' && mg.afid!='local'){
  		//jQuery('#divHlogo').html(null);
  		jQuery('#divHlogo').load(hpath);
  		//jQuery('#mgfooter').html(null); 
			if(fpath!=''){		
  			jQuery('#mgfooter').load(fpath);
			}
  	}
	});
	return arr;	
}
//cookie関連共通処理
/* Cookie への書き出し
     引数 key　 : データキー （半角英数 _ のみ）
     引数 value : データの値（日本語可）
     引数 days  : データを保持する日数（ 0 の時は有効期限は省略）*/
function WriteCookie(key, value, days) {
   var str = escape(value); 
   jQuery.cookie(key,str ,{expires: 30,path: '/', domain: '.bridal-esthe.com'});
            // Cookie に書き出し
}

/* Cookie の読み込み
     引数 key : 求める値のキー
     戻り値　 : 値（ない時は空文字""）*/
function ReadCookie(key) {
 		var str;
     str=jQuery.cookie(key)
     if(str!="" && str!=null && str.length>0 ){
     		return str;
     }else{
     		return "";
     }
     //return "";                                   // 見つからない時は空文字を返す
}

