
function eventcalendar( caldata, month, year, prevM, nextM, img ) {
	dayWidth = 30;
	var calDate = new Date();
	calDate.setDate( 1 );
	calDate.setMonth( month - 1 );
	calDate.setFullYear( year );
	
	prevB = nextB = "";
	if( prevM ) prevB = '<a href="' + prevM + '" class="back">&laquo; Edellinen</a>';
	if( nextM ) nextB = '<a href="' + nextM + '" class="forward">Seuraava &raquo;</a>';	
	// Create base
	document.writeln( '<div id="eventcalendar"></div>' );
	$(	'	<img src="' + img + '" alt=""/>' + 
		'	<div class="header">' + 
		'		<h2>' + monthName( month - 1 ) + ' ' + year + '</h2>' + 
				prevB +
				nextB +
		'	</div>' + 
		'	<div class="calendar">' + 
		'		<div class="back"></div>' + 
		'		<div class="holder">' + 
		'			<ul>' + 
		'			</ul>' + 
		'		</div>' + 
		'		<div class="forward"></div>' + 
		'		<div class="clear"></div>' + 
		'	</div>' ).appendTo( $('#eventcalendar' ) );
	
	createMonth( calDate );
	$('#eventcalendar .calendar .forward').click( function() { scrollCal( "+" ) } );
	$('#eventcalendar .calendar .back').click( function() { scrollCal( "-" ) } );
	
	function createMonth( calDate ) {
		var startMonth = calDate.getMonth();
		$('#eventcalendar UL').empty();
		maxHeight = 0;
		days = 0;
		
		// Create days
		while( calDate.getMonth() == startMonth ) {
			d = calDate.getDate();
			var newLI = $('<li></li>').appendTo('#eventcalendar UL');
			newLI.html( '<span class="day">' + d + '<span>' + dayName( calDate ) + '</span></span>' );
			if( isWeekend( calDate ) ) newLI.addClass("weekend");
			calDate.setDate( d + 1 );
			$('#eventcalendar UL').attr( 'style', 'width:' + ( 30 * d - 1 ) + 'px' );
			days++;
		}
		$('#eventcalendar UL').children().last().attr( 'style', 'margin-right: 0;' );
		
		// Attach data
		for( i = 0; i < caldata.length; i++ ) {
			var startD	= new Date( Date.parse( caldata[i].start ) );
			var endD	= new Date( Date.parse( caldata[i].end ) );
			if( startD.getMonth() < startMonth ) {
				startD.setDate( 1 );
				startD.setMonth( startMonth );
			}
			if( endD.getMonth() > startMonth ) {
				endD.setMonth( startMonth );
				endD.setDate( days );
			}
			var dif = ( endD.getTime() - startD.getTime() ) / 60000 / 60 / 24 + 1;
			var newEvent = $('<div></div>').appendTo( $('#eventcalendar UL').children()[startD.getDate() - 1] );
			var height	= 32 + ( newEvent.height() + parseInt( newEvent.css( 'margin-bottom' ) ) ) * caldata[i].row;
			if( height > maxHeight ) maxHeight = height;
			newEvent.attr( 'style', 'width:' + ( ( dif * dayWidth ) - 2 ) + 'px; top:' + height + 'px;' );
			if( caldata[i].type == "tip" ) {
				newEvent.addClass("tip");
				newEvent.html( '<img src="/extension/nvkotipuutarhuri/design/nvkotipuutarhuri2010/images/calendar/star.gif" alt=""/>' );
			}
	
			$( newEvent ).data( {
				'name': caldata[i].name,
				'description': caldata[i].description,
				'location': caldata[i].location,
				'start': new Date( Date.parse( caldata[i].start ) ),
				'end': new Date( Date.parse( caldata[i].end ) ),
				'type': caldata[i].type,
				'eventLink': caldata[i].link
			} );
	
			newEvent.mouseenter( function( e ) {
				newHover = showHover( e, $( this ) );
			} );
			
			//if( caldata[i].row > maxRow ) maxRow = caldata[i].row;
		}
		var calendarHeight = maxHeight + newEvent.height() + parseInt( $('#eventcalendar UL').css('margin-bottom') ); //57 + 20* maxRow;
		$('#eventcalendar UL').parent().attr( 'style', 'height:' + calendarHeight + 'px' );
		$('#eventcalendar .calendar .forward').attr( 'style', 'height:' + calendarHeight + 'px' );
		$('#eventcalendar .calendar .back').attr( 'style', 'height:' + calendarHeight + 'px' );
	}
	
	function isWeekend( d ) {
		d = d.getDay();	
		if( d == 0 || d == 6 ) return true;
		return false;
	}
	
	function dayName( d ) {
		d = d.getDay();
		if( d == 0 ) return "SU";
		if( d == 1 ) return "MA";
		if( d == 2 ) return "TI";
		if( d == 3 ) return "KE";
		if( d == 4 ) return "TO";
		if( d == 5 ) return "PE";
		if( d == 6 ) return "LA";
	}
	function monthName( d ) {
		if( d == 0 ) return "Tammikuu";
		if( d == 1 ) return "Helmikuu";
		if( d == 2 ) return "Maaliskuu";
		if( d == 3 ) return "Huhtikuu";
		if( d == 4 ) return "Toukokuu";
		if( d == 5 ) return "Kesäkuu";
		if( d == 6 ) return "Heinäkuu";
		if( d == 7 ) return "Elokuu";
		if( d == 8 ) return "Syyskuu";
		if( d == 9 ) return "Lokakuu";
		if( d == 10 ) return "Marraskuu";
		if( d == 11 ) return "Joulukuu";
	}
	
	function scrollCal( dir ) {
		$('#eventcalendar .holder').scrollTo( dir + '=210px', 600, {axis:'x'} );	
	}
	
	function showHover( e, o ) { // Event, Data Object 
		var newLeft = 30;
		while( newLeft < e.clientX - $('#eventcalendar').offset().left ) newLeft += 30;
		pos	= {
/*			'left': e.clientX - $('#eventcalendar').offset().left - 126,*/
			'left': newLeft - 126 - 13,
			'top': o.position().top + $('#eventcalendar .calendar').position().top - 2
		}
		start		= o.data().start;
		end			= o.data().end;
		duration	= start.getDate() + '.' + (start.getMonth() + 1) + '.' + start.getFullYear();
		eventLocation = eventLink = "";
		if( start.getDate() != end.getDate() ) duration += ' - ' + end.getDate() + '.' + (end.getMonth() + 1) + '.' + end.getFullYear();
		if( o.data().location ) eventLocation = '	<h3>Tapahtumapaikka</h3> <p>' + o.data().location + '</p>';
		if( o.data().eventLink ) eventLink = '<p>' + o.data().eventLink + '</p>';
		
		var newHover = $('<div class="hover ' + o.data().type + '" style="left:' + pos.left + 'px; top:' + pos.top + 'px;">' +
		' 	<img src="/extension/nvkotipuutarhuri/design/nvkotipuutarhuri2010/images/calendar/hover_top_' + o.data().type + '.png" alt=""/>' +
		'   <img src="/extension/nvkotipuutarhuri/design/nvkotipuutarhuri2010/images/calendar/close.png" class="close" alt="Sulje"/>' +
		'	<div class="content">' +
		'		<h3>' + o.data().name + '</h3>' +
		'		<p>' + duration + '</p>' +
				eventLocation +
		'		<p>' + o.data().description + '</p>' +
				eventLink + 
		'	</div>' +
		'	<img src="/extension/nvkotipuutarhuri/design/nvkotipuutarhuri2010/images/calendar/hover_bottom_' + o.data().type + '.png" alt=""/>' +
		'</div>' ).appendTo( $('#eventcalendar') );

		newHover.children('.close').click( function() { closeHover( newHover ); } );
		
		$(newHover).mouseleave( function( e ) {
			closeHover( newHover );
		} );
		
		if($.browser.msie && $.browser.version=="6.0") {
			pngfix();
		}
	}
	
	function closeHover( h ) {
		h.fadeOut( 250, function() { h.remove(); } );
	}
}

