$(document).ready(function(){
	shortenEvents();
	detectMultipleDayEvents();
	detectEventBeginAndEnd();
	$('.event-link').hover(function(){
		showTooltip($(this).parent().find('.event-details:first').html(), $(this).css('background-color'));
	});
	$('.event-link').mouseout(function(){
		hideTooltip();
	});
	$(document).mousemove(function(e){
		var theX = e.pageX;
		if(theX > $(window).width() - $("#tooltip").width() - 40){
			theX = $(window).width() - $("#tooltip").width() - 40;
		}
		$("#tooltip").css('left', (theX) + 'px');
		$("#tooltip").css('top', (e.pageY - $("#tooltip").height() - 35) + 'px');
	})
})
function showTooltip(msg, color){
	$('#tooltip').css('background-color', color);	
	$('#tooltip').css('display', 'block');
	$('#tooltip').css('visibility', 'visible');
	$('#tooltip').html(msg);
	$('#tooltip').width(300);
}
function hideTooltip(){
	$('#tooltip').css('display', 'none');
	$('#tooltip').css('visibility', 'hidden');
}
function shortenEvents(){
	$('.event-link').each(function(){
		var t = $(this).text();
		while($(this).outerHeight() > 20){
			t = t.substring(0, t.length - 6);
			$(this).text(t);
			$(this).append("...");
		};
	});
}
function detectMultipleDayEvents(){
	var i = 0;
	var allDaysEvents = new Array();
	$('.has-events').each(function(){
		allDaysEvents[i] = new Array();
		var j = 0;
		var isMonday = false;
		if($(this).find('span:first').attr('class') == "day1"){
			isMonday = true;
		}
		$(this).find('a').each(function(){
			allDaysEvents[i].push($(this).text());
			if(i > 0){
				if($(this).text() == allDaysEvents[i-1][j] && isMonday != true){
					$(this).text('...');
				}
			}
			j ++;
		});
		i ++;
	});
}
function detectEventBeginAndEnd(){
	var i=0;
	$('.has-events').each(function(){
		var currentDayNumber = $(this).attr('class').split('day ')[1];
		currentDayNumber = currentDayNumber.split(' day-')[0];
		currentDayNumber = currentDayNumber.split(' current-')[0];
		//alert(currentDayNumber);
		i++;
		var j = 0;
		var row = 0;
		$(this).find('.event-link').each(function(){
			var begin = $(this).attr('class').split("begin-")[1];
			if(begin != undefined){
				row ++;
				begin = begin.split(" end-")[0];
				begin = begin.substr(begin.lastIndexOf("-")+1)
			}
			if(begin == currentDayNumber){
				//$(this).corners("4px top-left bottom-left");
			}
			var end = $(this).attr('class').split("end-")[1];
			if(end != undefined){
				end = end.split(" all-day")[0];
				end = end.substr(end.lastIndexOf("-")+1)
			}
			if(end == currentDayNumber){
				//$(this).css('width', '99%');
				$(this).css('width', '138px');
				//$(this).corners("4px top-right bottom-right");
			}
			/*$(this).css('position', 'absolute');
			$(this).css('top', (14 * row) + 'px');*/
			//alert(14 * row);
			j++;
		});
	})
}







