el = new EventList();
var index;
var calendar_img_path = "/wm_shared/img/home_calendar/";

$(document).ready(function(){
	setTodayImage();
	
	$.get('calendar.xml', function(data){
		el.dataImport(data);
		el.sort(Array('event_date'),'asc');
		index = 999;
		el.output();
		setInterval("el.output()",5*1000);
	});
});

// set today image ============================================================
function setTodayImage(){
	var nowdate = new Date();
	var mon  = ZeroFormat(nowdate.getMonth() + 1, 2); // 月 
	var day  = ZeroFormat(nowdate.getDate(), 2); // 日 
	$("#today_month").attr('src', calendar_img_path+"m_"+mon+".gif");
	$("#today_month").attr('alt', mon+"月");
	$("#today_day").attr('src', calendar_img_path+"d_"+day+".gif");
	$("#today_day").attr('alt', day+"日");}

function ZeroFormat(num,max){
	var tmp=""+num;
	while(tmp.length<max){
		tmp="0"+tmp;
	}
	return tmp;
}

// CLASS eventlist =============================================================
function EventList(){

	this.inData  = [];
	this.ecData  = [];
	this.page    = 0;
	this.sortKey = [];

	this.dataImport = function(xml){
		var ouData = [];

		$.each($(xml).find("event"), function(){
			var obj = {};
			var title = $(this).attr('title');
			if(title.length > 22){
				title = title.substring(0,22)+"．．";
			}
			obj.title = title;
			obj.link = $(this).attr('link');
			obj.event_date = $(this).attr('event_date');
			ouData.push(obj);
		});
		
		this.ecData = ouData;
	}

	this.sort = function(key, desc){
	
		if(key){
			this.sortKey = key;
		}
	
		for(var i=0; i < this.ecData.length; i++){
			this.ecData[i]['sortkey'] = make_sortword(this.ecData[i], this.sortKey);
		}
	
		if(desc == "desc"){
			this.ecData.sort(sort_func_desc);
		}else{
			this.ecData.sort(sort_func);
		}

		function sort_func(a, b){
			rtn = 0;
			if( a['sortkey'] > b['sortkey']){
				rtn = +1;
			}
			if( a['sortkey'] < b['sortkey']){
				rtn = -1;
			}
			return rtn;
		}

		function sort_func_desc(a, b){
			rtn = 0;
			if( a['sortkey'] > b['sortkey']){
				rtn = -1;
			}
			if( a['sortkey'] < b['sortkey']){
				rtn = +1;
			}
			return rtn;
		}

		function make_sortword(ary, skey){
			var rtn = "";
			for(var i=0; i < skey.length; i++){
				rtn = rtn + ary[skey[i]];
			}
			if(rtn.match(/[0-9]+/g) == rtn){
				rtn = parseInt(rtn);
			}
			return rtn;
		}

		
	}
	
	this.output = function(){
		//alert(index);
		index++;
		if(index >= this.ecData.length){
			index=0;
		}
		var d = this.ecData[index];
		$("#calendar_content .unit").fadeOut("slow", function(){
			var t="";
			var link = '';
			if(d.link.match(/#target=_self/)){
				link =d.link.split('#target=_self').join("");
				t= "";
			}
			if(d.link.match(/#target=_blank/)){
				link = d.link.split('#target=_blank').join("");
				t= 'target="_blank"';
			}
			var out = '<p><a href="'+link+'" '+t+' >'+d.title+'</a></p><p class="data wm_right">'+d.event_date+'</p>';
			$(this).html(out);
			$(this).fadeIn("slow");
		});
	}

}

