function M_object(element) {
	if (arguments.length > 1) {
		for (var i = 0, elements = [], length = arguments.length; i < length; i++)
			elements.push(M_object(arguments[i]));
		return elements;
	}
	if (typeof element == 'string')
		element = document.getElementById(element);
	return element;
}

//event stuff
var eventCount = 0;
var rowCount = 0;
var today = new Date();
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var weekdays = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var lasteid = 0;
//timeout for the tooltip
var timeout = "";

willow.ready(function($){
	//emergency bulletin options
	var options = {
		emButtonPosition: "right",
		emButton: true
	};
	//emergency bulletin
	willow.getNews("9556",function(data){$('#Form1').bulletin(data,options);},{"backlink":window.location});

	$("ul#events li .date").each(function(){
		var startdate = new Date();
		startdate.setFullYear(parseInt($(this).attr("year")),parseInt($(this).attr("month"))-1,parseInt($(this).attr("day")));
		startdate.setHours(0,0,0,0);
		
		var enddate = new Date();
		if($(this).attr("endyear") != undefined){
			enddate.setFullYear(parseInt($(this).attr("endyear")),parseInt($(this).attr("endmonth"))-1,parseInt($(this).attr("endday")));
			enddate.setHours(23,59,59,0);
		}else{
			enddate = startdate;
			enddate.setHours(23,59,59,0);
		}
		
		if(eventCount < 3){
			
			if(startdate == today || enddate >= today){
				if(lasteid == $(this).attr("id")){
					$(this).parent().css("display","none");
				}else{
					eventCount++;
					lasteid = $(this).attr("id");
					if(startdate < today){
						$(this).children(".weekday").html(weekdays[today.getDay()].substring(0,3));
						$(this).children(".day").html(today.getDate());
						$(this).children(".month").html(months[today.getMonth()].substring(0,3));
					}
				}
			}else{
				$(this).parent().css("display","none");
			}
		}else{
			$(this).parent().css("display","none");
		}
						
	});
	//calendar starts here
	$("ul#events").before("<div id='cal'></div>");
	
	var $cal = $("#cal");
	
	//$cal.prepend("<div id='current-month'>"+months[today.getMonth()]+"</div>");
	
	//print out date headers
	//$cal.append("<div id='day-headers'><span id='sun'>su</span><span id='mon'>mo</span><span id='tue'>tu</span><span id='wen'>we</span><span id='thu'>th</span><span id='fri'>fr</span><span id='sat'>sa</span></div>");
	$cal.append("<div class='clear'><!-- --></div>");
	
	//TEST
	//today.setFullYear(2008,1,1);
	//TEST
	
	var currentMonth = getMonthDays(today.getMonth(),today.getFullYear());
	var lastMonth;
	if(today.getMonth() == 0){
		//for december fo last year
		lastMonth = getMonthDays(11,today.getFullYear()-1);
	}else{
		lastMonth = getMonthDays(today.getMonth()-1,today.getFullYear());
	}
	
	var nextMonth;
	if(today.getMonth() == 11){
		//for december fo last year
		nextMonth = getMonthDays(0,today.getFullYear()+1);
	}else{
		nextMonth = getMonthDays(today.getMonth()+1,today.getFullYear());
	}
	
	var weekCount = 0;
	var rowCount = 0;			
	
	var newRow = "<div class='calrow' id='calrow"+rowCount+"'>";
	
	//write out pre days first
	for(var p = 0; p < currentMonth.monthStart; p++){
		currentDay = lastMonth.numOfDays - (currentMonth.monthStart-1) + p;
		
		var classname = months[lastMonth.month].substring(0,3).toLowerCase() + currentDay;
		
		var $events = $("."+classname);
		var indclass = "";
		
		if($events.length > 0){
			indclass = " class='e-ind pre'";
		}else{
			indclass = " class='pre'";
		}
		newRow += "<a"+indclass+" href='#"+(lastMonth.month)+"/"+currentDay+"'>"+currentDay+"</a>";
		weekCount++;
	}
	//load the month
	for(var i = 0; i < currentMonth.numOfDays; i++){
		if(weekCount == 7){
			newRow += "<div class='clear'><!-- --></div></div><div class='calrow' id='calrow"+rowCount+"'>";
			rowCount++;
			weekCount = 0;
		}
		var currentDay;
		if(i < 9){
			currentDay = "0"+(i+1);
		}else{
			currentDay = (i+1);
		}
		
		var classname = months[currentMonth.month].substring(0,3).toLowerCase() + (i+1);
		
		var $events = $("."+classname);
		var indclass = "";
		
		if($events.length > 0){
			indclass = " class='e-ind'";
		}else{
			indclass = "";
		}
		
		newRow += "<a"+indclass+" href='#"+currentMonth.month+"/"+(i+1)+"'>"+currentDay+"</a>";
		weekCount++;
	}
	//extra days now...
	var extraDays = 6-currentMonth.monthEnd;
	for(var n = 0; n < extraDays; n++){
		if(n < 9){
			currentDay = "0"+(n+1);
		}else{
			currentDay = (n+1);
		}
		
		var classname = months[nextMonth.month].substring(0,3).toLowerCase() + (n+1);
		
		var $events = $("."+classname);
		var indclass = "";
		
		if($events.length > 0){
			indclass = " class='e-ind pre'";
		}else{
			indclass = " class='pre'";
		}
		
		newRow += "<a"+indclass+" href='#"+nextMonth.month+"/"+(n+1)+"'>"+currentDay+"</a>";
		weekCount++;
	}
	
	newRow += "<div class='clear'><!-- --></div></div><div class='calrow' id='calrow"+rowCount+"'>";
	$cal.append(newRow);
	//$cal.append("<div id='calbtm'></div>");
	var $tip = $(document.createElement("div")).attr("id","tooltip");
	$cal.append($tip);	
	
	$tip.hover(function(){
		clearTimeout(timeout);
	},function(){
		timeout = setTimeout('hideTip()',50);
	});	
	
	$(".calrow a").hover(function(){
		//over
		//$.log("Over");
		
		var $obj = $(this);
		$obj.addClass("on");
		var foundEvents = false;
		
		var myday = $obj.attr("href");
		//console.log(myday.split("#"));
		myday = myday.split("#")[1].split("/");
		//alert(myday[0]);
		//alert(parseInt(myday[0]));
		var classname = months[parseInt(myday[0])].substring(0,3).toLowerCase() + myday[1];
		//$.log(classname);
		var $events = $("."+classname);
		
		$events.each(function(){
			//$.log("LOOP");
			if($(this).hasClass(months[parseInt(myday[0])].toLowerCase())){
				//$.log($(this).parent());
				$tip.html($tip.html() + "<div>"+$(this).parent().children('#event-container').children(".headline").html()+"</div>");
			}
		});
		
		//position tooltip
		var offset = $obj.getTopLeft();
		//$.log(offset.top-$obj.height-$tip.height());
		$tip.css({"top":(offset.top-$obj.height()-$tip.height()-6)+"px","left":(offset.left-$tip.width()/2)})
		
		if($tip.html().length > 0){
			$tip.show();
		}
		
	},function(){
		//off
		timeout = setTimeout('hideTip()',50);
	});
	 
});

function hideTip(){
	var $tip = $("#tooltip");
	$tip.html("").hide();
	$(".calrow a.on").removeClass("on");
}



function getMonthDays(month,year){
	var today = new Date();
	today.setFullYear(year,month,today.getDate())
	
	var data = {
		numOfDays:0,
		monthStart:0,
		monthEnd:6,
		month:month
	};
	
	var day = new Date();
	day.setFullYear(today.getFullYear(),today.getMonth(),1);
	
	data.monthStart = day.getDay();
	
	var nod = 0;
	if(today.getMonth() == 1){
		if(checkleapyear(today.getFullYear())){
			nod = 29;
		}else{
			nod = 28;
		}
	}else if(today.getMonth() == 3 || today.getMonth() == 5 || today.getMonth() == 8 || today.getMonth() == 10){
		nod = 30;
	}else{
		nod = 31;
	}
				
	day.setFullYear(today.getFullYear(),today.getMonth(),nod);
	
	data.numOfDays = nod;
	data.monthEnd = day.getDay();
	
	return data;
}		

function checkleapyear(datea){
	datea = parseInt(datea);

	if(datea%4 == 0){
		if(datea%100 != 0){
			return true;
		}else{
			if(datea%400 == 0)
				return true;
			else
				return false;
		}
	}
	return false;
}
