var applicationXml;


window.addEvent('domready', function() {


	function loadYearlyTimeline(){
		new Ajax(filename, {
			method: 'get',
			onComplete: function(){onLoadYearlyTimeline(this.response.xml)}
		}).request();
	}
	
	function onLoadYearlyTimeline(XML){
		applicationXml = XML;
		var workSheetNames = getWorkSheets(applicationXml);
		
		for(var sheet=0; sheet<getWorkSheets(applicationXml).length;sheet++){
			var sheetName = getWorkSheets(applicationXml)[sheet];
		}
		
		//buildYearlyTimeline(searchFrom);
		buildAoyamaYearlyTimeline();
	}
	
	loadYearlyTimeline();
	
	
	function buildAoyamaYearlyTimeline(){
		
		var html = '';
		var line = 'odd';
		var sheet = 0;
		
		var sheetName = getWorkSheets(applicationXml)[sheet];
		var wsObj = getWorkSheet(applicationXml,sheetName);
		var w = getWorkSheetColCount(wsObj);
		var h = getWorkSheetRowCount(wsObj);

		
		html += '<table width="420" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff">';
				for(var j=0; j<h; j++){
					
					if(j==0){
						html += '<caption>'+getCellData(applicationXml,sheetName,0,j)+'</caption>';
					}
					else if(j==1){
						html +='<thead height="12">';
						for(var i=0; i<w;i++){
							value = getCellData(applicationXml,sheetName,i,j);
							if(value=='/') value ='';
							else{
								if(value.split('.')[0]) value = value.split('.')[0];
							}
							if(i==0){
								html += '<th width="110" height="12">'+value+'</th>';
							}else if(i==1){
				                html += '<th width="70" height="12">'+value+'</th>';
							}
							else{
								html += '<th class="month">'+value+'</th>';
							}
						}
						html +='</thead><tbody>';
					}
					else{
						html +='<tr>';
						for(var i=0; i<w;i++){
							value = getCellData(applicationXml,sheetName,i,j);
							if(value=='/') value ='';
							else{
								if(value.split('.')[0]) value = value.split('.')[0];
							}
							if(i==0 || i==1){
								html += '<th>'+value+'</th>';
							}
							else{
								html += '<td><div class="sc-'+value+'">&nbsp;</div></td>';
							}
						}
						html +='</tr>';
					}
				}
				html +'</tbody></table>';
				
		$('residence_schedule').empty();
		$('residence_schedule').setHTML(html);
	
	}
	
	
	

	
	function buildYearlyTimeline(date){
	
		var today = cloneDate(date);
		var y = today.getFullYear();
		var m = today.getMonth()+1;
		
		
		var currentMonth = m;
		
		var labelsrc = new Array();
		labelsrc[1]='/images/annual_head_exhib.gif'
		labelsrc[2]='/images/annual_head_concert.gif'
		labelsrc[3]='/images/annual_head_lab.gif'
		labelsrc[4]='/images/annual_head_residence.gif'
		labelsrc[5]='/images/annual_head_external.gif'
		
		var html = '';
		var line = 'odd';
		for(var sheet=0; sheet<getWorkSheets(applicationXml).length;sheet++){
			var sheetName = getWorkSheets(applicationXml)[sheet];
			var wsObj = getWorkSheet(applicationXml,sheetName);
			var w = getWorkSheetColCount(wsObj);
			var h = getWorkSheetRowCount(wsObj);
			
			if(sheetName == y){
				html += '<table>';
				for(var j=0; j<h; j++){
					
					if(j%2==0){
							line = 'even';
						}
					else{
							line = 'odd';
					}
					html += '<tr class='+line+'>';
					
					for(var i=0; i<w;i++){
						value = getCellData(applicationXml,sheetName,i,j);
						if(i==0){			
							if(j==0){
								html += '<th style="background-color:#666">';
								html+=sheetName;
								html += '</th>';
							} 
							else{
								html += '<th>';
							 	html += '<img src="'+labelsrc[j]+'" alt="'+value+'" />';
							 	html += '</th>';
							}
						}
						else{
							var num =i;
							var idname = 'cell_'+sheetName+'_x'+i+'_y'+j;
							var classname = 'x'+i+' y'+j;
							var currentStr = ''
							if(currentMonth == i){
								currentStr ='current_';
								classname +=' current';
							}
							if(j==0){
								if(i==w-1){
									html += '<td style="width:32px;">';
								}
								else{
									html += '<td>';
								}
								//html +=i;
								var formatNum = i;
								if(i<10) formatNum = '0'+i;
								html+= '<a href="javascript:;" onclick="mouseClick(\''+idname+'\');" onmouseover="mouseOnCell(\''+idname+'\');" onmouseout="mouseOutCell(\''+idname+'\');"><img class="'+classname+'" id="'+idname+'" alt="exist" src="/images/annual_head_'+currentStr+formatNum+'.gif"/></a>';
							}
							else{
								if(i==w-1){
									html += '<td style="width:32px;">';
								}
								else{
									html += '<td>';
								}
								if(value =='1.0' || value =='1' && value!='null'){
									html+= '<a href="javascript:;" onclick="mouseClick(\''+idname+'\');" onmouseover="mouseOnCell(\''+idname+'\');" onmouseout="mouseOutCell(\''+idname+'\');"><img class="'+classname+'" id="'+idname+'" alt="exist" src="/images/annual_cell_'+currentStr+line+'_exist.gif"/></a>';
								}
								else{
									html+= '<a href="javascript:;" onclick="mouseClick(\''+idname+'\');" onmouseover="mouseOnCell(\''+idname+'\');" onmouseout="mouseOutCell(\''+idname+'\');"><img class="'+classname+'" id="'+idname+'" alt="none" src="/images/annual_cell_'+currentStr+line+'_none.gif"/></a>';
									
								}
							}	
							html += '</td>';
						}
					}
					html += '</tr>';
				}
				html += '</table>';
			}
			
		}
		$('residence_schedule').empty();
		$('residence_schedule').setHTML(html);

	}
});