var M={
	datetime:"",
	path:"",
	anchor:""
};

M.exec=function(){
	// 外国語版の場合は SubNav をすべて展開済みにするため
	if(window.location.host=="matsuya.web.transer.com"){
		$('.subnav.local-nav ul ul').css("display","block");
	}
	M.cm.exec();
	M.gi.exec();
	M.as.exec();
	// www.matsuya.com以外からphpコールさせない
	if(window.location.host!="matsuya.web.transer.com"){
		M.ut.json2Html("/php/shared/datetime.php",M.callBackDate);
	}
}

M.callBackDate=function(json){
	M.datetime=json;
	M.cm.execD();
}

M.cm={
	exec:function(){
		this.setPath();
		this.setFontSize();
		this.setEventFontSize();
		this.setGoBack();
		this.setPageTop();
		this.adjustHeight();
		this.setSubNavi();
		this.setSubNaviEvent();
		this.setGlobalNavi();
		this.setImageOver();
		this.mapBox();
		this.setPrintEvent();
		this.setPrintCloseEvent();
		this.setFloorMap();
		this.setSarchValue();
		this.showThickbox();
	},
	execD:function(){
		this.setBgImage();
	},
	setPath:function(){
		M.path=location.pathname;
		M.anchor=location.hash;
	},
	showThickbox:function(){
		$(".print-btn .thickbox img").click(function(){$("#TB_wrap").attr("class", "open"); $("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/shared/thickbox_print.css\" media=\"print\">").appendTo("head"); M.cm.adjustWindowHeight(); });
		$("#TB_overlay, #TB_closeWindowButton").click(function(){$("#TB_wrap").attr("class", "close"); $("link[href=\"/css/shared/thickbox_print.css\"]").remove();});
		
	},
	setPrintEvent:function(){
		$(".print .print-btn img").click(function(){window.print();return false;});
	},
	setPrintCloseEvent:function(){
		if(window!=window.parent){
			$("#TB_closeWindowButton").click(window.parent.tb_remove);
		}
	},
	setImageOver:function(){
		$(".imgover:not([src*='_o.']):not(img[src*='header_fc_btn'])").hover(
			this.imageMouseOver,
			this.imageMouseLeave
		);
	},
	imageMouseOver:function(){var src=$(this).attr("src");$(this).attr("src",src.replace(/\.([^\.]+)$/,"_o.$1"));},
	imageMouseLeave:function(){var src=$(this).attr("src");$(this).attr("src",src.replace(/_o\.([^\.]+)$/,".$1"));},
	setFontSize:function(){
		var history=$.cookie("fs");
		if(!history){history="fontM";}
		$(".container").removeClass("fontM");
		$(".container").removeClass("fontL");
		$(".container").addClass(history);
		$("img[src*='header_fc_btn']").unbind("mouseenter").unbind("mouseleave");
		if(history=="fontM"){
			$("img[src*='header_fc_btn']:eq("+(0)+")").attr("src","/img/shared/header_fc_btn01_o.gif");
			$("img[src*='header_fc_btn']:eq("+(1)+")").attr("src","/img/shared/header_fc_btn02.gif");
		}else{
			$("img[src*='header_fc_btn']:eq("+(0)+")").attr("src","/img/shared/header_fc_btn01.gif");
			$("img[src*='header_fc_btn']:eq("+(1)+")").attr("src","/img/shared/header_fc_btn02_o.gif");
		}
		$("img[src*='header_fc_btn']:not([src*='_o.'])").hover(
			this.imageMouseOver,
			this.imageMouseLeave
		);
	},
	setEventFontSize:function(){
		var ref=this;
		$("li:has(img[src*='header_fc_btn'])").click(function(){
			var index=$("li:has(img[src*='header_fc_btn'])").index(this);
			if(index==0){$.cookie("fs","fontM",{expires:365,path:'/'});}else{$.cookie("fs","fontL",{expires:365,path:'/'});}
			ref.setFontSize();
			ref.adjustHeight();
		});
	},
	setSarchValue:function(){
		var tagAry = $(".clearvalue");
		for(i=0;i<tagAry.length;i++){
			if(tagAry[i]["id"]=="q"){
				if($(tagAry[i]).val()==""){
					$(tagAry[i]).val("キーワード入力");
				}
				if(tagAry[i]["value"] != "キーワード入力"){
					$(tagAry[i]).css("color","#000000");
				}		
			}else if(tagAry[i]["id"]=="search-input"){
				if($(tagAry[i]).val()==""){
					$(tagAry[i]).val("ブランド名を入力");
				}
				if(tagAry[i]["value"] != "ブランド名を入力"){
					$(tagAry[i]).css("color","#000000");
				}
			}else if(tagAry[i]["id"]=="form-shop"){
				if($(tagAry[i]).val()==""){
					$(tagAry[i]).val("具体的にショップ名（売場名）をご記入下さい");
				}
				if(tagAry[i]["value"] != "具体的にショップ名（売場名）をご記入下さい"){
					$(tagAry[i]).css("color","#000000");
				}
			}
		}
		var sarchVal = "";
		var valNum = "";
		$(".clearvalue").focus(function(){
			sarchVal = this.value;
        	if(sarchVal == "キーワード入力"){
				valNum = 1;
				$(this).val("").css("color","#000000");
			}else if(sarchVal == "ブランド名を入力"){
				valNum = 2;
				$(this).val("").css("color","#000000");
			}else if(sarchVal == "具体的にショップ名（売場名）をご記入下さい"){
				valNum = 3;
				$(this).val("").css("color","#000000");
			}
     	});
		$(".clearvalue").blur(function(){
			if(this.value == ""){
				switch(valNum){
					case 1:
						$(this).val("キーワード入力").css("color","#999999");
						break;
					case 2:
						$(this).val("ブランド名を入力").css("color","#999999");
						break;
					case 3:
						$(this).val("具体的にショップ名（売場名）をご記入下さい").css("color","#999999");
						break;
					default:
						break;
				}
			}
		});
	},
	adjustHeight:function(){
		this.adjustHeightImpl(".article-list .item-wrap",".item");
	},
	adjustHeightImpl:function(wrapClassName,itemClassName){
		var list=$(wrapClassName);
		for(i=0;i<list.length;i++){
			var childList=$(list[i]).find(itemClassName);
			var maxHeight=0;
			$(childList).removeAttr("style");
			for(j=0;j<childList.length;j++){
				maxHeight=Math.max(maxHeight,$(childList[j]).height());
			}
			$(childList).css("height",maxHeight+"px");
		}
	},
	adjustWindowHeight:function(){
		var winheight=document.all?document.documentElement.clientHeight:window.innerHeight;
		winheight=Math.floor(winheight/7*5);
		$("#TB_iframeContent").height(winheight);
		var marginT=(winheight/2)+15;
		if($.browser.version==6){
			var scrollPoint=$(window).scrollTop();
			marginT=marginT-scrollPoint;
		}
		var elem=document.getElementById("TB_window");
		elem.style.marginTop=-marginT+'px';
	},
	setGoBack:function(){
		var list=$(".go-back");
		$(list).bind("click",function(){history.back();return false;});
	},
	setPageTop:function(){
		var list=$("a[href$='#page-top']");
		$(list).bind("click",function(){$.scrollTo($("#page-top"),500);return false;});
	},
	setBgImage:function(){
		M.ut.csv2Html("/csv/shared/bg.csv",this.callBackBgImage);
	},
	callBackBgImage:function(csv){
		var datetime=""+M.datetime.hour+M.datetime.minute;
		var timeZone="day";
		for(i=0;i<csv.length;i++){
			if(csv[i][0]>datetime){
				if(i==0){
					timeZone=csv[csv.length-1][1];
					break;
				}
				timeZone=csv[i-1][1];
				break;
			}
			if(i==csv.length-1){
				timeZone=csv[i][1];
			}
		}
		if(timeZone!="day"){
			var list=$("[class$='-day']");
			for(i=0;i<list.length;i++){
				var clazz=$(list[i]).attr("class");
				$(list[i]).removeClass(clazz);
				$(list[i]).addClass(clazz.replace("-day","-night"));
			}
		}
	},
	openSubNavi:function(ancName){	
		location.href = "#snav-"+ancName;
		this.setPath();
		this.setSubNavi();
		this.setSubNaviEvent();
	},
	setSubNavi:function(){
		var matchStr=M.ut.cutString(M.path);
		var level=M.ut.getLevel(matchStr);
		var anchor=M.anchor;
		if(anchor.indexOf("#snav-")!=-1){
			M.ut.matchingUrl(matchStr, null);
			return;
		}
		if(level==3){
			return;
		}else if(level==4){
			M.ut.matchingUrl(matchStr, null);
		}else if(level>=5){
			var matchFlg = M.ut.matchingUrl(matchStr, 5);
			if(!matchFlg){
				M.ut.matchingUrl(matchStr, 4);
			}
		}
	},
	setSubNaviEvent:function(){
		var list=$(".subnav dl[class*='-open'],.subnav dl[class*='-close']").children("dt");
		$(list).click(function(){
			var clazz=$(this).parent("dl").attr("class");
			if(clazz.indexOf("-open")!=-1){
				$(this).parent("dl").attr("class",clazz.replace("-open","-close"));
			}else{
				$(this).parent("dl").attr("class",clazz.replace("-close","-open"));
			}
		});
	},
	setGlobalNavi:function(){
		var matchStr=M.ut.cutPath(M.path);
		var list=$("#globalnav-inner a");
		var imgList=$("#globalnav-inner img");
		for(i=0;i<list.length;i++){
			var matchHref=M.ut.cutPath($(list[i]).attr("href"));
			if(matchStr==matchHref){
				var src=$(list[i]).children("img").attr("src");
				$(imgList[i]).attr("src",src.replace(/\.([^\.]+)$/,"_o.$1"));
			}
		}
	},
	setFloorMap:function(){
		var img=$(".floormap-all>img");
		var imgPath=img.attr("src");
		var al=$(".floormap-all area");
		$("map").hover(function(){},function(){img.attr("src",imgPath);});
		for(i=0;i<al.length;i++){
			$(al[i]).hover(
				function(){
					//var id=M.ut.getParentName($(this).attr("href"));
                                        var id=$(this).attr("title");
					var imgPath4area=imgPath.replace("index_img01.jpg","index_img01_"+id+".jpg");
					img.attr("src",imgPath4area);},
				function(){});
		}
	},
	topBanner:{
		bannerNumber:-1,
		timer:null,
		speed:4000,
		execute:function(){
			if($(".top-banner").length!=0){
				this.init();
			}
		},
		init:function(){
			$(".top-banner-img p").hide();
			M.cm.topBanner.setup();
		},
		setup:function(){
			var ref=this;
			var img_href = "";
			var img_target = "";
			var img_alt = "";
			var thumb_href = "";
			var thumb_target = "";
			var thumb_alt = "";
			for(i=0;i<3;i++){
				$(".top-banner-img>p:eq("+(i)+")").css("z-index",""+(3-i));
				img_href = $(".top-banner-img a:eq("+i+")").attr("href");
				img_target = $(".top-banner-img a:eq("+i+")").attr("target");
				img_alt = $(".top-banner-img a:eq("+i+")").attr("alt");
				thumb_href = $(".top-banner-thumb a:eq("+i+")").attr("href");
				thumb_target = $(".top-banner-thumb a:eq("+i+")").attr("target");
				thumb_alt = $(".top-banner-thumb a:eq("+i+")").attr("alt");
				$(".top-banner-img a:eq("+i+")").attr("href",img_href).attr("target",img_target);
				$(".top-banner-img img:eq("+i+")").attr("alt",img_alt);
				$(".top-banner-thumb a:eq("+i+")").attr("href",thumb_href).attr("target",thumb_target);
				$(".top-banner-thumb img:eq("+i+")").attr("alt",thumb_alt);
				$(".top-banner-thumb img:eq("+i+")").bind("mouseover",{index:i},function(event){ref.thumbnailMouseOver(event);});
			}
			$(".mainvisual").hover(function(){ref.stop();},function(){if(!ref.timer){ref.start();};});
			this.start();
		},
		start:function(){
			var ref=this;
			var nowNumber=this.bannerNumber;
			if(nowNumber!=-1){
				if(nowNumber==0){
					$(".top-banner-img>p:eq("+(nowNumber)+")").fadeTo(500,1.0,function(){$(".top-banner-img>p").fadeTo(0,1.0);});
				}else{
					$(".top-banner-img>p:eq("+((nowNumber+3)%4)+")").fadeTo(500,0,function(){$(".top-banner-img>p:eq("+((nowNumber+3)%4)+")").hide();});
				}
			}
			this.timer=setTimeout(function(){ref.bannerNumber=((nowNumber+1)%4);ref.start();},ref.speed);
		},
		stop:function(){
			this.timer=clearTimeout(this.timer);
			$(".top-banner-img>p").stop(true,true);
		},
		thumbnailMouseOver:function(event){
			var index=event.data.index;
			var nowNumber=this.bannerNumber;
			if(nowNumber!=index){
				this.stop();
				if(nowNumber==3||nowNumber==-1){
					$(".top-banner-img>p:eq("+(index)+")").fadeTo(500,1.0,function(){$(".top-banner-img>p:eq("+(index)+"),.top-banner-img>p:gt("+(index)+")").fadeTo(0,1.0);});
				}else if(nowNumber>index){
					$(".top-banner-img>p:eq("+(index)+")").fadeTo(500,1.0,function(){$(".top-banner-img>p:eq("+(index)+"),.top-banner-img>p:gt("+(index)+")").fadeTo(0,1.0);});
				}else if(nowNumber<index){
					if((index-nowNumber)==2){
						$(".top-banner-img>p:eq("+(index-1)+")").hide();
					}
					$(".top-banner-img>p:eq("+(nowNumber)+")").fadeTo(500,0,function(){$(".top-banner-img>p:eq("+(nowNumber)+")").hide();});
				}
				this.bannerNumber=index;
			}
		}
	},
	mapBox:function(){
		$(".map-control a:eq(0)").hover(function(){
			if(!$(this).hasClass("zoomnone")){$(this).attr("class", "zoomhover")}},
			function(){if(!$(this).hasClass("zoomnone")){$(this).attr("class", "zoom")}}
		);
		$(".map-control a:eq(1)").hover(function(){
			if(!$(this).hasClass("backnone")){$(this).attr("class", "backhover")}},
			function(){if(!$(this).hasClass("backnone")){$(this).attr("class", "back")}}
		);
		$(".map-control a:eq(0)").click(function(){
			if(!$(this).hasClass("zoomnone")){
				var viewport=$(".floormap-image");
				viewport.mapbox(this.className.replace("hover",""));
			}
			return false;
		});
		$(".map-control a:eq(1)").click(function(){
			if(!$(this).hasClass("backnone")){
				var viewport=$(".floormap-image");
				viewport.mapbox(this.className.replace("hover",""));
			}
			return false;
		});
		$(".floormap-image").mapbox({
			zoom:true,
			pan:true,
			defaultLayer:0,
			layerSplit:8,
			mapContent:".mapcontent",
			defaultX:null,
			defaultY:null,
			zoomToCursor:true,
			doubleClickZoom:false,
			clickZoom:false,
			doubleClickZoomOut:false,
			clickZoomOut:false,
			doubleClickMove:false,
			clickMove:false,
			doubleClickDistance:1,
			clickDistance:1,
			callBefore:function(layer,xcoord,ycoord,viewport){},
			callAfter:function(layer,xcoord,ycoord,viewport){},
			beforeZoom:function(layer,xcoord,ycoord,viewport){},
			afterZoom:function(layer,xcoord,ycoord,viewport){},
			mousewheel:true
		});
	},
	gMap:{
		x:0,
		y:0,
		title:"",
		execute:function(x,y,title){
			if($("#accessmap").length!=0){
				this.x=x;
				this.y=y;
				this.title=title;
				$(window).bind("load",M.cm.gMap.initialize);
			}
		},
		initialize:function(){
			var map;
			var myLatlng=new google.maps.LatLng(M.cm.gMap.x,M.cm.gMap.y);
			var myOptions={
				zoom:17,
				center:myLatlng,
				mapTypeId:google.maps.MapTypeId.ROADMAP
			};
			map=new google.maps.Map(document.getElementById("accessmap"),myOptions);
			var image="/img/shared/marker.png";
			var marker=new google.maps.Marker({
				position:myLatlng,
				map:map,
				icon:image,
				title:M.cm.gMap.title
			});
		}
	},
	gMapPrt:{
		x:0,
		y:0,
		title:"",
		execute:function(x,y,title){
			if($("#accessmap-print").length!=0){
				this.x=x;
				this.y=y;
				this.title=title;
				$(window).bind("load",M.cm.gMapPrt.initialize);
			}
		},
		initialize:function(){
			var map;
			var myLatlng=new google.maps.LatLng(M.cm.gMapPrt.x,M.cm.gMapPrt.y);
			var myOptions={
				zoom:17,
				center:myLatlng,
				mapTypeId:google.maps.MapTypeId.ROADMAP
			};
			map=new google.maps.Map(document.getElementById("accessmap-print"),myOptions);
			var image="/img/shared/marker.png";
			var marker=new google.maps.Marker({
				position:myLatlng,
				map:map,
				icon:image,
				title:M.cm.gMapPrt.title
			});
		}
	}
}

M.gi={
	exec:function(){
		if(!M.ut.isGinza()){return;}
		M.cm.topBanner.execute();
		M.cm.gMap.execute(35.6722863,139.7666867,"松屋銀座");
		M.cm.gMapPrt.execute(35.6722863,139.7666867,"松屋銀座");
	},
	execD:function(){
		if(!M.ut.isGinza()){return;}
		M.ut.csv2Html("/csv/shared/ginzaOpen.csv",M.cm.callBackOpen);
	}
}

M.as={
	exec:function(){
		if(!M.ut.isAsakusa()){return;}
		M.cm.gMap.execute(35.7120658,139.7981998,"松屋浅草");
		M.cm.gMapPrt.execute(35.7120658,139.7981998,"松屋浅草");
	},
	execD:function(){
		if(!M.ut.isAsakusa()){return;}
		M.ut.csv2Html("/csv/shared/asakusaOpen.csv",M.cm.callBackOpen);
	}
}

M.ut={
        isGinza:function(){
                if(M.path.slice(1,8)=="m_ginza"){
                        return true;
                }
                if(window.location.search.substring(1).match("EntryDataBrandname")){
                        return true;
                }
                if(window.location.host=="matsuya.web.transer.com"){
                        if ($(".cilogo a img").size() > 0){
                                var cilogoPath=$(".cilogo a img").attr("src");
                                return cilogoPath.match("/m_ginza/");
                        }
                }
                return false;
        },
        isAsakusa:function(){
                if(M.path.slice(1,10)=="m_asakusa"){
                        return true;
                }
                if(window.location.host=="matsuya.web.transer.com"){
                        if ($(".cilogo a img").size() > 0 ){
                                var cilogoPath=$(".cilogo a img").attr("src");
                                return cilogoPath.match("/m_asakusa/");
                        }
                }
                return false;
        },
       	changePath:function(path){
		if(!path){return ""};
		path=path.replace("/index.html","/");
		
		path=path.split("#",1);
		return path[0];
	},
	cutPath:function(path){
		if(!path){return ""};
		path=this.changePath(path);
		path=path.split("/");
		path="/"+path[1]+"/"+path[2]+"/";
		return path;
	},
	cutString:function(path){
		if(!path){return ""};
		path=path.split("/");
		path.splice(0,1);
		var length=path.length-1;
		if((length>=0) && path[path.length-1].indexOf("html")){
			path[path.length-1] = "";
			path.splice(length,1);
		}
		path="/"+path.join("/")+"/";
		return path;
	},
	getLevel:function(path){
		if(!path){return ""};
		path=path.split("/");
		return path.length-1;
	},
	matchingUrl:function(matchStr, cutNum){
		var matchFlg=false;
		if(cutNum!=null){
			var matchStr=this.cutStringArray(matchStr, cutNum);
		}
		var list=$(".subnav a");
		for(i=0;i<list.length;i++){
			var matchHref=this.cutString($(list[i]).attr("href"));
			if(matchStr==matchHref){
				matchFlg=true;
				$(list[i]).parent("li").children("ul").attr("class","localnav-open");
				$(list[i]).parents(".subnav li").addClass("current");
				$(list[i]).parents(".subnav ul").attr("class","localnav-open");
				$(list[i]).parents(".subnav dl").attr("class","subnav-open");
			}
		}
		return matchFlg;
	},
	cutStringArray:function(path, cutNum){
		if(!path){return ""};
		path=path.split("/");
		if(cutNum==4){
			path="/"+path[1]+"/"+path[2]+"/"+path[3]+"/";
		}else if(cutNum==5){
			path="/"+path[1]+"/"+path[2]+"/"+path[3]+"/"+path[4]+"/";
		}
		return path;
	},
	takePath:function(path, num){
		if(!path){return ""};
		path=path.split("/");
		return path[num];
	},
	getPathName:function(path){
		if(!path){return ""};
		path=this.changePath(path);
		path=path.split("/");
		path[path.length-1]=""
		return path.join("/");
	},
	getFileName:function(path){
		if(!path){return ""};
		path=this.changePath(path);
		path=path.split("/");
		return path[path.length-1];
	},
	getParentName:function(path){
		if(!path){return ""};
		path=this.changePath(path);
		path=path.split("/");
		return path[path.length-2];
	},
	json2Html:function(jsonPath,callback){
		$.ajax({
			type: "GET",
			url: jsonPath,
			dataType: "json",
			cache: false,
			success: function(json){
				callback(json);
			}
		});
	},
	csv2Html:function(csvPath,callback){
		$.ajax({
			type: "GET",
			url: csvPath,
			dataType: "text",
			cache: false,
			success: function(data){
				var csvOrg=$.csv()(data);
				var csv=new Array();
				for(i=0;i<csvOrg.length;i++){
					if(csvOrg[i].length==0){
						continue;
					}else if(csvOrg[i].length==1&&csvOrg[i][0]==""){
						continue;
					}else if(csvOrg[i][0].charAt(0)=="#"){
						continue;
					}
					csv.push(csvOrg[i]);
				}
				callback(csv);
			}
		});
	}
}

$(function(){
	M.exec();
})


