
var today ;
var month1 ;
var month2 ;
var month1No ;
var month2No ;

var year ;

var today ;
var dsScheduleM1;
var dsScheduleM2;
var dsScheduleDetail;
var sch_id = "";

var myHorizontalSlide1;
var myHorizontalSlide2;
var myHorizontalSlide3;

var pathName = "http://nodelounge.com/top_flyers/flyer/sch.xml";
//var pathName = "data/schedule.xml";
var xmlnodename = "month1/data";

function loadSchedule(){
	var para = window.location.search;
	if(para.length > 0){
		sch_id = para.replace("?id=","");
	}
	
	//myHorizontalSlide1 = new Fx.Slide('calenderAreaID', {mode: 'horizontal'});
	myHorizontalSlide2 = new Fx.Slide('ListareaID', {mode: 'horizontal'});
	myHorizontalSlide3 = new Fx.Slide('Iframe1', {mode: 'horizontal'});

	motionStart("cal");
	today = new Date();
	
	var pathNameTemp;
	if(sch_id != ""){
		pathNameTemp = pathName + "?id=" +sch_id;
		
	}else{
		pathNameTemp = pathName;
		
	}
	dsScheduleM1 = new Spry.Data.XMLDataSet(pathNameTemp, xmlnodename);
	dsScheduleM1.loadData();


		var myObserver = new Object;
		myObserver.onPostLoad = function()
		{
			//alert("onDataChanged called!");
			setDefaultDetailPage(sch_id);
			motionEnd("cal");

		};
		dsScheduleM1.addObserver(myObserver);

	
}
	
function motionStart(kind){
	document.getElementsByTagName("body")[0].style.opacity = ".5";
	document.getElementsByTagName("body")[0].style.filter = "alpha(opacity=50)";
	
							 
	//opacity:.7; filter:alpha(opacity=70);
//	document.getElementById("Iframe1").style.overflowY = "hidden";
//	document.getElementById("Iframe1").scrolling = "no";
	
//	myHorizontalSlide1.hide();
	if(kind == "cal")
		myHorizontalSlide2.hide();
	
	myHorizontalSlide3.hide();
	
}
function motionEnd(kind){
//	myHorizontalSlide1.slideIn();
	if(kind == "cal")
		myHorizontalSlide2.slideIn();
	//document.getElementById("Iframe1").style.overflowY = "auto";
	//document.getElementById("Iframe1").style.overflow = "hidden";
	//document.getElementById("ListAreaIDall").style.overflowY = "hidden";
	
}

function loadDataEnd(){
	myHorizontalSlide3.slideIn();
	document.getElementsByTagName("body")[0].style.opacity = "";
	document.getElementsByTagName("body")[0].style.filter = "";
}
function FrameOver(){
	//document.getElementById("Iframe1").scrolling = "yes";
	//document.getElementById("Iframe1").style.overflowY = "auto";
}
function ListOver(){
	//document.getElementById("ListAreaIDall").style.overflowY = "auto";
}



function changeMonthToEnglish(monthno){
	var MonthString = "";
	switch(monthno){
		case 1:
			MonthString = "JAN";
			break;
		case 2:
			MonthString = "FEB";
			break;
		case 3:
			MonthString = "MAR";
			break;
		case 4:
			MonthString = "APL";
			break;
		case 5:
			MonthString = "MAY";
			break;
		case 6:
			MonthString = "JUNE";
			break;
		case 7:
			MonthString = "JULY";
			break;
		case 8:
			MonthString = "AUG";
			break;
		case 9:
			MonthString = "SEP";
			break;
		case 10:
			MonthString = "OCT";
			break;
		case 11:
			MonthString = "NOV";
			break;
		case 12:
			MonthString = "DEC";
			break;
	}
	
	return MonthString;

}

function onclickMonthMove(id){
	
		motionStart("cal");
	
	var pathNameTemp;

		if(id == "today"){
			sch_id = "";
			today = null;
			today = new Date();
			
			//alert(today.getMonth());
		
			dsScheduleM1 =null;
			dsScheduleM1 = new Spry.Data.XMLDataSet(pathName, xmlnodename);
			dsScheduleM1.loadData();


			var myObserver = new Object;
			myObserver.onPostLoad = function(dataSet, data)
			{
				//alert("onDataChanged called!");
				setDefaultDetailPage(sch_id);
				
				motionEnd("cal");

	
			};
			dsScheduleM1.addObserver(myObserver);
			
			
		}
		else if(id == "PREV"){
			
			sch_id = "";
			if(month1No == 1){
				month1No =12;
				year--;
			}
			else{
				month1No--;
			}
			
			var monthstring = month1No.toString();
			if(monthstring.length == 1)
				monthstring = "0" + monthstring;
				
			var para = year.toString() + monthstring;
			
			pathNameTemp = pathName + "?month=" +para;
					
			dsScheduleM1 =null;
			dsScheduleM1 = new Spry.Data.XMLDataSet(pathNameTemp, xmlnodename);
			dsScheduleM1.loadData();


			var myObserver = new Object;
			myObserver.onPostLoad = function(dataSet, data)
			{
				//alert("onDataChanged called!");
				setDefaultDetailPage(sch_id);
				motionEnd("cal");

	
			};
			dsScheduleM1.addObserver(myObserver);
			
		}
		
		else if(id == "NEXT"){
			sch_id = "";
			if(month1No == 12){
				month1No =1;
				year++;
			}
			else{
				month1No++;
			}
			
			var monthstring = month1No.toString();
			if(monthstring.length == 1)
				monthstring = "0" + monthstring;
				
			var para = year.toString() + monthstring;
			//alert(para);

			pathNameTemp = pathName + "?month=" +para;

			dsScheduleM1 =null;
			dsScheduleM1 = new Spry.Data.XMLDataSet(pathNameTemp, xmlnodename);
			dsScheduleM1.loadData();


			var myObserver = new Object;
			myObserver.onPostLoad = function(dataSet, data)
			{
				//alert("onDataChanged called!");
				setDefaultDetailPage(sch_id);
				motionEnd("cal");

			};
			dsScheduleM1.addObserver(myObserver);
		}
}
function onclickSch(id){

	var kind;
	if(id.search("id__") != -1)
		kind ="cal";
	else
		kind ="list";
	
	motionStart(kind);

	var idtemp = id.replace("id_","");
	idtemp = idtemp.replace("_","");


	if(parseInt(idtemp,10) > 0)
		setDefaultDetailPage(idtemp);

	motionEnd(kind);

//	myHorizontalSlide.toggle();


}

function setSelected(){
	
	
	for (i = 0; i < document.getElementsByTagName("span").length; i++) {
			
			var idtemp = document.getElementsByTagName("span")[i].id.replace("id_","");
			idtemp = idtemp.replace("_","");
			var Daytemp = document.getElementsByTagName("span")[i].id.replace("Day_","");
			Daytemp = Daytemp.replace("_","");
			

   			if(document.getElementsByTagName("span")[i].id.search("id_") != -1 ){
				if(idtemp == sch_id){
					document.getElementsByTagName("span")[i].className = "title selected";
					
				}
				else{
					document.getElementsByTagName("span")[i].className = "title";
				}
				
			}
			else if(document.getElementsByTagName("span")[i].id.search("Day_") != -1 ){
				
				if(Daytemp == sch_id){
					document.getElementsByTagName("span")[i].className = "day selected";
					
				}
				else{
					document.getElementsByTagName("span")[i].className = "day";
				}
			
			}
		}

		for (i = 0; i < document.getElementsByTagName("li").length; i++) {
			var idtemp = document.getElementsByTagName("li")[i].id.replace("id_","");
			idtemp = idtemp.replace("_","");

			if(document.getElementsByTagName("li")[i].id.search("id__") != -1 ){
				//alert(document.getElementsByTagName("li")(i).id);
				
				if(idtemp == sch_id){
					document.getElementsByTagName("li")[i].className = "selected";
					
				}
				else if(document.getElementsByTagName("li")[i].id == "id__"){
					document.getElementsByTagName("li")[i].className = "";
				}
				else{
					document.getElementsByTagName("li")[i].className = "on";
				}
				
			}
		}

	
}


function getDataValueUrl(id){
	
	var rows = dsScheduleM1.getData();
	

		for (var i = 0; i < rows.length; i++)
		{
		  if (rows[i]["@id"] == id) {
			//alert( rows[i]["link"]);
			//return rows[i]["link"];
		  }
		}
	
}

function setDefaultDetailPage(id){
	
	//データセットからデータ取り出し
	var rows = dsScheduleM1.getData();
	//日付チェック用
	var tempDate = "0";
	
	//詳細スケジュール用				
	document.getElementById("Iframe1").src = "";
	
	//IDがセットされていない場合
	if(id == ""){
	
		for (var i = 0; i < rows.length; i++)
		{
		  if ((rows[i]["sch"] == "on") && 
					   ((today.getDate() <= parseInt(rows[i]["date"],10)) || (today.getMonth()+1 < parseInt(rows[i]["monthno"],10)) || (today.getFullYear() < parseInt(rows[i]["year"],10)))){
			//alert( rows[i]["link"]);
			//document.all.item("Iframe1").src = rows[i]["link"];
			document.getElementById("Iframe1").src = rows[i]["link"];;

			//alert( document.getElementById("Iframe1").src);

			sch_id = rows[i]["@id"];
			break;
			//return rows[i]["link"];
		  }
		}
		
	}else{
		for (var i = 0; i < rows.length; i++)
		{
		  if (rows[i]["@id"] == id) {
			//alert( rows[i]["link"]);
			document.getElementById("Iframe1").src = rows[i]["link"];
			sch_id = rows[i]["@id"];
			break;
			//return rows[i]["link"];
		  }
		}
		
	}

	
	if(document.getElementById("Iframe1").src == ""){
		for (var i = 0; i < rows.length; i++)
		{
		  if (rows[i]["sch"] == "on"){
			document.getElementById("Iframe1").src = rows[i]["link"];;

			sch_id = rows[i]["@id"];
			break;
		  }
		}
		if(document.getElementById("Iframe1").src == "")
			document.getElementById("Iframe1").src = "sch_detail_dummy.html";
	}
	
    var textCal1 = ""; // カレンダー用タグ作成Temp
    var text1 = "";// リスト用タグ作成Temp
	
	for (var i = 0; i < rows.length; i++)
	{
	  if (rows[i]["list"] == "1") {
			switch(parseInt(rows[i]["day"],10)){
			case 1:
			textCal1 = "<li></li>";
			break;
			case 2:
			textCal1 = "<li></li><li></li>";
			break;
			case 3:
			textCal1 = "<li></li><li></li><li></li>";
			break;
			case 4:
			textCal1 = "<li></li><li></li><li></li><li></li>";
			break;
			case 5:
			textCal1 = "<li></li><li></li><li></li><li></li><li></li>";
			break;
			case 6:
			textCal1 = "<li></li><li></li><li></li><li></li><li></li><li></li>";
			break;
			
	  		}
			month1No = parseInt(rows[i]["monthno"],10);
			
			month1=changeMonthToEnglish(month1No);
			
			year = parseInt(rows[i]["year"],10);

		
			document.getElementById("Listmonth1").innerHTML = month1;
		
			document.getElementById("Listmonth1_2").innerHTML = month1;
		
		
			if(month1No.toString().length==1)
				document.getElementById("Listmonth1no").innerHTML = "0" +month1No.toString();
			else
				document.getElementById("Listmonth1no").innerHTML = month1No.toString();
			


			break;
	  	}
	  
	}
	
	
	for (var i = 0; i < rows.length; i++)
	{
	  if ((rows[i]["sch"] == "on") && (rows[i]["list"] == "1")) {
		//alert( rows[i]["link"]);
		text1 += "<li><span class='day' id='Day_"+rows[i]["@id"]+"'>"+rows[i]["date"]+"</span><span class='title'  id='id_"+rows[i]["@id"]+"' onclick='onclickSch(this.id);'>" + rows[i]["title"] +"</span></li>"
		if(tempDate != rows[i]["date"])
			textCal1 += "<li class='on' id='id__" +rows[i]["@id"]+ "' onclick='onclickSch(this.id)'>" +rows[i]["date"]+ "</li>"
		tempDate = rows[i]["date"];
	  }
	  else if(rows[i]["list"] == "1"){
		if(tempDate != rows[i]["date"])
			textCal1 += "<li>" +rows[i]["date"]+ "</li>"
		tempDate = rows[i]["date"];
		  
	  }
	  
	}
	  document.getElementById("Listarea1UL1").innerHTML = text1;
	  document.getElementById("calenderListID1").innerHTML = textCal1;
	  
	text1 = "";
	textCal1 = "";
	for (var i = 0; i < rows.length; i++)
	{
	  if (rows[i]["list"] == "2") {
			switch(parseInt(rows[i]["day"],10)){
			case 1:
			textCal1 = "<li></li>";
			break;
			case 2:
			textCal1 = "<li></li><li></li>";
			break;
			case 3:
			textCal1 = "<li></li><li></li><li></li>";
			break;
			case 4:
			textCal1 = "<li></li><li></li><li></li><li></li>";
			break;
			case 5:
			textCal1 = "<li></li><li></li><li></li><li></li><li></li>";
			break;
			case 6:
			textCal1 = "<li></li><li></li><li></li><li></li><li></li><li></li>";
			break;
			
	  		}
			
			month2No = parseInt(rows[i]["monthno"],10);
			
			month2=changeMonthToEnglish(month2No);
			
			document.getElementById("Listmonth2").innerHTML = month2;		
			document.getElementById("Listmonth2_2").innerHTML = month2;
		
			
			if(month2No.toString().length==1)
				document.getElementById("Listmonth2no").innerHTML = "0" +month2No;
			else
				document.getElementById("Listmonth2no").innerHTML = month2No;

			break;
	  	}
	  
	}
	
	for (var i = 0; i < rows.length; i++)
	{
	  if ((rows[i]["sch"] == "on") && (rows[i]["list"] == "2")) {
		//alert( rows[i]["link"]);
		text1 += "<li><span class='day' id='Day__"+rows[i]["@id"]+"'>"+rows[i]["date"]+"</span><span class='title'  id='id_"+rows[i]["@id"]+"' onclick='onclickSch(this.id);'>" + rows[i]["title"] +"</span></li>"
		
		if(tempDate != rows[i]["date"])
			textCal1 += "<li class='on' id='id__" +rows[i]["@id"]+ "' onclick='onclickSch(this.id)'>" +rows[i]["date"]+ "</li>"
		tempDate = rows[i]["date"];

	  }
	  else if(rows[i]["list"] == "2"){
		if(tempDate != rows[i]["date"])
			textCal1 += "<li>" +rows[i]["date"]+ "</li>"
		tempDate = rows[i]["date"];
		  
	  }
	}
	
	  document.getElementById("Listarea1UL2").innerHTML = text1;
	  document.getElementById("calenderListID2").innerHTML = textCal1;

	setSelected();

	
}
