/*
----------------------------------------------------------------
	JavaScript for iwaki-alios.jp index page
----------------------------------------------------------------
	version			2.00
	last update		2011/05/20
	author			Yoshihisa Kato/YAH!-cyberstudio
----------------------------------------------------------------
*/

var iw_index={
	xmlHttp:new Array(),
	scheduleList:function(){
		var locateCheck=(location.search.indexOf("dbg")!=-1)?1:0;
		var FLAG0="%7E"+CD.NY+"."+CD.NM+"."+CD.ND+".0.0.0";
		var FLAG1="-p10080";
	
		var endDate=new Date();
		endDate.setTime(Date.UTC(CD.NY,CD.NM-1,CD.ND,0,0,0)+529200000);
		var endYear=endDate.getFullYear();
		var endMonth=endDate.getMonth()+1;
		var endDate=endDate.getDate();
		var FLAG2="%7E"+endYear+"."+endMonth+"."+endDate+".23.59.59";
	
		var scList="/cd/app/index.cgi?CID=schedule&TID=topList&LID=twList&FLAG=sc_time%3Ai"+FLAG0+FLAG1+"&TOP=1&END=max";
		var evList="/cd/app/index.cgi?CID=event&TID=topList&LID=twList&FLAG=";
		evList+="eventStart/eventEnd%3Ain"+FLAG0;
		evList+="%3BeventStart/eventEnd%3A*l"+FLAG2;
		evList+="%3BeventStart01%3A*i"+FLAG0+FLAG1;
		evList+="%3BeventStart02%3A*i"+FLAG0+FLAG1;
		evList+="%3BeventStart03%3A*i"+FLAG0+FLAG1;
		evList+="%3BeventStart04%3A*i"+FLAG0+FLAG1;
		evList+="%3BeventStart05%3A*i"+FLAG0+FLAG1;
		evList+="&TOP=1&END=max";
	
		var scRes="";
		var evRes="";
		var mArray=new Array();
		var eArray=new Array();
		var isTermToday=Date.UTC(CD.NY,CD.NM,CD.ND);
		var mC=0;
	
		this.xmlHttp[0]=CD_ajax.Query(0);
		if(this.xmlHttp[0]){
			this.xmlHttp[0].onreadystatechange=function(){
				if(iw_index.xmlHttp[0].readyState==4 && iw_index.xmlHttp[0].status==200){
					scRes=iw_index.xmlHttp[0].responseText;
					iw_index.xmlHttp[1]=CD_ajax.Query(1);
					if(iw_index.xmlHttp[1]){
						iw_index.xmlHttp[1].onreadystatechange=function(){
							if(iw_index.xmlHttp[1].readyState==4 && iw_index.xmlHttp[1].status==200){
								evRes=iw_index.xmlHttp[1].responseText;
								scRes+=evRes;
								scRes=scRes.replace(/<.+?>/g,"");
								var dataList=new Array();
								var termList=new Array();
								var termPos=new Array();
								var codeSrc='';
								listData=scRes.split(":;");
								if(listData[0]==0&&listData[1]==0){
									dataList[0]='<dt class="noList">催しなどはございません。</dt>';
								}else{
									for(var i=0;i<listData.length;i++){
										if(listData[i] && listData[i] !=0){
											listDataSplit=listData[i].split("::");
											if(listDataSplit[1]=="ev"){	//ordinary event
												mArray=listDataSplit[2].split("/");
												if((mArray[0]==CD.NM && mArray[1]>=CD.ND && mArray[1]<(CD.ND+7)) || (CD_timeControl.getMonthLastDate(CD.NY,CD.NM)-CD.ND<7 && mArray[0]==CD.NM+1) || (mArray[0]==1 && CD.NM==12)){
													mC=1;
												}else{
													mC=0;
												}
												var dayView=(listDataSplit[2]==CD.NM+"/"+CD.ND)?'今日':listDataSplit[2];
												var viewToday=(listDataSplit[2]==CD.NM+"/"+CD.ND)?' class="viewToday"':'';
	
												//臨時休館表示　2011/03/14対応
												if(listDataSplit[4].indexOf("臨時休館")!=-1)listDataSplit[4]='<span style="font-weight:bold;color:#ff0000">'+listDataSplit[4]+'</span>';
	
												if(listDataSplit[3]){
													if(mC==1){
														if(dataList[listDataSplit[0]]){
															dataList[listDataSplit[0]]+='<dt>&nbsp;</dt><dd><a href="'+listDataSplit[3]+'">'+listDataSplit[4]+'</a></dd>';
														}else{
															dataList[listDataSplit[0]]='<dt'+viewToday+'>'+dayView+'</dt><dd><a href="'+listDataSplit[3]+'">'+listDataSplit[4]+'</a></dd>';
														}
													}
												}else{
													if(dataList[listDataSplit[0]]){
														dataList[listDataSplit[0]]+='<dt>&nbsp;</dt><dd class="nonLink">'+listDataSplit[4]+'</dd>';
													}else{
														dataList[listDataSplit[0]]='<dt'+viewToday+'>'+dayView+'</dt><dd class="nonLink">'+listDataSplit[4]+'</dd>';
													}
												}
											}else{	//term event
												if(listDataSplit[3]){
													mArray=listDataSplit[2].split("/");
													eArray=listDataSplit[5].split("/");
													termPos[i]=(isTermToday>=Date.UTC(mArray[0],mArray[1],mArray[2]) && isTermToday<=Date.UTC(eArray[0],eArray[1],eArray[2]))?1:0;
													if(termPos[i]==1){
														termList[i]='<dt>&nbsp;</dt><dd><a href="'+listDataSplit[3]+'">'+listDataSplit[4]+'（'+eArray[1]+'/'+eArray[2]+'まで）</a></dd>';
													}else{
														termList[i]='<dt class="termList"><a href="'+listDataSplit[3]+'">'+listDataSplit[4]+'（'+mArray[1]+'/'+mArray[2]+'〜'+eArray[1]+'/'+eArray[2]+'まで）</a></dt>';
													}
												}
											}
										}
									}
								}
	
								//today data
								if(!dataList[CD.ND]){
									if(termList.length>0){
										var todayTermReplace=0;
										for(var i=0;i<termList.length;i++){
											if(termList[i] && termPos[i]==1){
												if(todayTermReplace==0){
													codeSrc+=termList[i].replace(/<dt>&nbsp;<\/dt>/,'<dt class="viewToday">今日</dt>');
													todayTermReplace=1;
												}else{
													codeSrc+=termList[i];
												}
											}
										}
									}
									if(!codeSrc)codeSrc+='<dt class="viewToday">今日</dt><dd class="nonLink">催しなどはございません</dd>';
								}else{
									codeSrc+=dataList[CD.ND];
									if(termList.length>0){
										for(var i=0;i<termList.length;i++){
											if(termList[i] && termPos[i]==1)codeSrc+=termList[i];
										}
									}
								}
								if(codeSrc)codeSrc+='<dt class="tLineE05"><hr /></dt>';
	
								if(CD_timeControl.getMonthLastDate(CD.NY,CD.NM)-CD.ND>=7){
									for(var i=0;i<dataList.length;i++){
										if(dataList[i] && i!=CD.ND){
											codeSrc+=dataList[i];
										}
									}
								}else{
									for(var i=CD.ND;i<=CD_timeControl.getMonthLastDate(CD.NY,CD.NM);i++){
										if(dataList[i] && i!=CD.ND)codeSrc+=dataList[i];
									}
									for(var i=0;i<7-(CD_timeControl.getMonthLastDate(CD.NY,CD.NM)-CD.ND);i++){
										if(dataList[i] && i!=CD.ND)codeSrc+=dataList[i];
									}
								}
	
								var isTerm0Code='';
								if(termList.length>0){
									for(var i=0;i<termList.length;i++){
										if(termList[i] && termPos[i]==0){
											isTerm0Code+=termList[i];
										}
									}
									if(isTerm0Code)codeSrc+='<dt class="tLineE10"><hr /></dt>'+isTerm0Code;
								}
								codeSrc=codeSrc.replace(/undefined/g,'');
								codeSrc+='</dl><br class="clear" clear="all" />';
								codeSrc='<dl>'+codeSrc;
								$('wtList').innerHTML=codeSrc;
							}else{
								$('wtList').innerHTML=indexAjax.loadingTag;
							}
						}
						iw_index.xmlHttp[1].open("get",evList);
						iw_index.xmlHttp[1].send(null);
					}
				}else{
					$('wtList').innerHTML=indexAjax.loadingTag;
				}
			}
			this.xmlHttp[0].open("get",scList);
			this.xmlHttp[0].send(null);
		}
	},
	
	/*twitter edit*/
	twitterEdit:function(){
		$("twitter_update_list").style.display="none";
		var tweets=$("twitter_update_list").getElementsByTagName("li");
		for(var i=0;i<tweets.length;i++){
			var tweetsWord=tweets[i].innerHTML;
			if(tweetsWord.indexOf("RT @")==-1){
				$("twitter_update_list").innerHTML="<li>"+tweetsWord+"</li>";
				break;
			}
		}
		if($("twitter_update_list").getElementsByTagName("a")[0]){
			var ancs=$("twitter_update_list").getElementsByTagName("a")
			var word=ancs[ancs.length-1].firstChild.nodeValue;
			switch(word){
				case "less than a minute ago":
					word="1分未満前";
				break;
				case "about a minute ago":
					word="約1分前";
				break;
				case "about an hour ago":
					word="約1時間前";
				break;
				case "1 day ago":
					word="1日前";
				break;
				default:
					if(word.indexOf("minutes")!=-1)word=word.split(/ /)[0]+"分前";
					if(word.indexOf("hours")!=-1)word="約"+word.split(/ /)[1]+"時間前";
					if(word.indexOf("days")!=-1)word=word.split(/ /)[0]+"日前";
			}
			ancs[ancs.length-1].firstChild.nodeValue=" …"+word;
			ancs[ancs.length-1].style.color="#666";
			for(var i=0;i<ancs.length;i++){
				ancs[i].setAttribute('target',"_blank");
			}
		}
		$("twitter_update_list").style.display="block";
	},

	/*HOT TOPICS*/
	HotTopicsTimer:null,
	HotTopicsViews:0,
	HotTopicsStops:0,

	loadHotTopics:function(){
		var list=$('importantNews').getElementsByTagName('li');
		for(var i=0;i<list.length;i++){
			if(i!=0){
				list[i].style.display="none";
				CD_core.setAlpha(list[i],0);
			}
			CD_core.setAttribute(list[i],'id','ht'+i);
		}
		this.HotTopicsViewItem();
	},

	HotTopicsFadeIn:function(num){
		var item=$('ht'+num);
		item.style.display="block";
		CD_tween.Add('ht'+num,'null','null',1,'null',10,"eq",'complete:iw_index.HotTopicsViewItem()');
	},

	HotTopicsViewItem:function(){
		this.HotTopicsTimer=setInterval('iw_index.HotTopicsStop()',1000);
	},

	HotTopicsStop:function(){
		this.HotTopicsStops++;
		if(this.HotTopicsStops==5){
			clearInterval(this.HotTopicsTimer);
			this.HotTopicsStops=0;
			this.HotTopicsFadeOut(this.HotTopicsViews);
		}
	},

	HotTopicsFadeOut:function(num){
		var item=$('ht'+num);
		CD_tween.Add('ht'+num,'null','null',0,'null',10,"eq",'complete:iw_index.HotTopicsFadeOutComplete('+num+')');
	},

	HotTopicsFadeOutComplete:function(num){
		var item=$('ht'+num);
		item.style.display="none";
		CD_core.setAlpha(item,0);
		this.HotTopicsViews++;
		if(this.HotTopicsViews==$('importantNews').getElementsByTagName('li').length)this.HotTopicsViews=0;
		this.HotTopicsFadeIn(this.HotTopicsViews);
	}
}
	
/*EXEC*/
var indexAjax=new CD_ajax.Setting();
indexAjax.loadingTag='<div align="center" style="background-color:#fff;width:100%;padding:10px"><img src="/cd/include/image/loading.gif"></div>';
CD_core.setEvent(function(){
	iw_index.twitterEdit();
	iw_index.scheduleList();
	iw_index.loadHotTopics();
});
	
/*=================================================
 Promotion area
 switch iPhone,iPod Touch & iPad or other device
=================================================*/
if(CD.UA.os=="iOS"){
	var promoFuncs={
		imagTimer:null,
		imageCount:0,
		slideStop:5000,
		preImage:new Array(),
		nowDate:new Date(),
		nowTime:0,

		disableListNavi:function(obj){
			$(obj).style.backgroundImage="none";
			$(obj).style.cursor="default";
		},

		enableListNavi:function(obj,imageURL){
			$(obj).style.backgroundImage="url("+imageURL+")";
			$(obj).style.cursor="pointer";
		},

		fadeIn:function(){
			var obj;
			var linkArg=new Array();
			for(var n=this.imageCount;n<promoItem.length;n++){
				if(promoFuncs.preImage[n]){
					this.imageCount=n;
					break;
				}
			}
			for(var n=0;n<promoItem[this.imageCount].childNodes.length;n++){
				obj=promoItem[this.imageCount].childNodes[n];			
				if(obj.nodeName=="imageURL"){
					if(obj.getAttribute("pressAct"))linkArg=obj.getAttribute("pressAct").split(/::/)[1].split(/,/);
					CD_core.setAttribute($('promotionImages'),'src',obj.firstChild.nodeValue);
					CD_core.setAttribute($('promotionMainLink'),'href',linkArg[0]);
					if(linkArg[1])CD_core.setAttribute($('promotionMainLink'),'target',linkArg[1]);
				}
			}
			CD_tween.Add("promotionImages","null","null",1,"null",20,"eq","");
		},

		fadeOut:function(indexNum){
			clearTimeout(this.imageTimer);
			if(indexNum!=null){this.imageCount=indexNum;}
			CD_tween.Add("promotionImages","null","null",0,"null",20,"eq",'complete:promoFuncs.slideChange()');
		},

		slideChange:function(){
			this.fadeIn();
			this.imageCount++;
			if(this.imageCount==promoItem.length)this.imageCount=0;
			this.imageTimer=setTimeout(function(){
				promoFuncs.fadeOut();
			},this.slideStop);
		},

		slideStart:function(){
			this.imageTimer=setTimeout(function(){
				promoFuncs.fadeOut();
			},1000);
		}
	}

	var promoXML=null;
	var promoItem=null;
	CD_core.setEvent(function(){
		CD_ajax.Load('promotion/content_list.xml',promoXML,'XML','','');
		var promoXMLTimer=setInterval(function(){
			promoFuncs.nowTime=promoFuncs.nowDate.getTime();

			if(CD_ajax.receiveObj[promoXML]){
				clearInterval(promoXMLTimer);
				/*iPad用タグ生成*/
				/*parseXML*/
				promoItem=CD_ajax.receiveObj[promoXML].getElementsByTagName("item");

				$('promotion').innerHTML='';
				var mainDiv=CD_core.createObject("div","promotionMain",'',$('promotion'),0);
				var mainLink=CD_core.createObject("a","promotionMainLink",'',mainDiv,0);
				var mainImage=CD_core.createObject("img","promotionImages",'',mainLink,0);
				CD_core.setAttribute(mainImage,'src',"image/index/promotion.jpg");
				CD_core.setAlpha("promotionImages",0);

				var menuDiv=CD_core.createObject("div","promotionMenu",'',$('promotion'),0);
				var prevLink=CD_core.createObject("a","promotionPrev",'',menuDiv,0);
				var menuArea=CD_core.createObject("div","promotionMenuArea",'',menuDiv,0);
				var nextLink=CD_core.createObject("a","promotionNext",'',menuDiv,0);
				var menuListDiv=CD_core.createObject("div","promotionList",'',menuArea,0);
				var menuList=CD_core.createObject("ul","",'',menuListDiv,0);

				//loading slide images
				var itemL;	var itemA;	var itemI;
				var startTime=0;
				var endofTime=0;
				var stp=new Array();
				var slideExist=1;
				var slideCount=0;

				for(var i=0;i<promoItem.length;i++){
					slideExist=1;
					if(promoItem[i].getAttribute("endoftime")){
						stp=promoItem[i].getAttribute("endoftime").split(".");
						endoftime=Date.UTC(stp[0],stp[1]-1,stp[2],stp[3],stp[4],stp[5]);
						if(promoFuncs.nowTime>endoftime){
							slideExist=0;
						}
					}
					if(slideExist==1){
						itemL=CD_core.createObject("li","",'',menuList,0);
						itemA=CD_core.createObject("a","",'',itemL,0);
						itemA.indexNum=i;
						itemA.style.cursor="pointer";
						CD_core.addEvent(itemA,'click',function(){
							promoFuncs.fadeOut(this.indexNum);
						});
						itemI=CD_core.createObject("img","",'',itemA,0);
						for(var n=0;n<promoItem[i].childNodes.length;n++){
							if(promoItem[i].childNodes[n].nodeName=="imageURL"){
								promoFuncs.preImage[i]=new Image();
								promoFuncs.preImage[i].src=promoItem[i].childNodes[n].firstChild.nodeValue;
							}
							if(promoItem[i].childNodes[n].nodeName=="thumbURL"){
								CD_core.setAttribute(itemI,'src',promoItem[i].childNodes[n].firstChild.nodeValue);
								break;
							}
						}
						slideCount++;
					}
				}

				//add functions

				CD_core.addEvent($('promotionNext'),'mousedown',function(){
					CD_tween.Add("promotionList",((promoListMaxWidth-720)*-1)+":null","null","null","null",15,"eq","start:promoFuncs.enableListNavi('promotionPrev','image/index/promo_prev.gif');complete:promoFuncs.disableListNavi('promotionNext')");
				});
				CD_core.addEvent($('promotionNext'),'mouseup',function(){
					CD_tween.Del('promotionList');
				});
				CD_core.addEvent($('promotionPrev'),'mousedown',function(){
					CD_tween.Add('promotionList',"0:null","null","null","null",15,"eq","start:promoFuncs.enableListNavi('promotionNext','image/index/promo_next.gif');complete:promoFuncs.disableListNavi('promotionPrev')");
				});
				CD_core.addEvent($('promotionPrev'),'mouseup',function(){
					CD_tween.Del('promotionList');
				});
				//format promotion Item

				var promoListMaxWidth=slideCount*90;
				$('promotionList').style.width=promoListMaxWidth+"px";
				promoFuncs.disableListNavi('promotionPrev');

				CD_tween.Add('promotionImages','null','null',1,'null',10,"eq",'complete:promoFuncs.slideStart()');
			}
		},100);
	});	
}else{
	CD_embed.Set("promotion",'Flash',9,"promotion/index.swf",780,360,"quality=true,menu=false,wmode=opaque");
}
	
