var blah;

function init() {

	var cellElements = $$('.calendarMonth td.edit');


	cellElements.each(function(e) {
		e.observe('click', function() {
				//alert("Clicked a cell. Should create a new event on this date: " + e.identify() + ".");
				
				//alert (typeof self.location);
				
				var pageLocation = self.location.toString();
				var pageLocationComp = new Array();
				pageLocationComp = pageLocation.split('?');
				pageLocationComp = pageLocationComp[0].split('#');
				
				pageLocation = pageLocationComp[0] + '?op=create&date=' + e.identify();
				
				//alert ( pageLocation );
				
				window.location.href = pageLocation;
		});
		e.observe('mouseover', function() {
				var allOtherCells = e.up('table').select('td:not([class~=today])');
			
				allOtherCells.each(function(g) {
					g.setStyle({ backgroundColor: ''});
				});
				
				e.setStyle({ backgroundColor: '#f90', cursor: 'pointer' });
				
		});
		e.observe('mouseout', function() {
				e.setStyle({ backgroundColor: '', cursor: 'arrow' });
		});
	});

	var eventElements = $$('.calendarMonth p.edit');
	//alert ( eventElements.size() );	
	eventElements.each(function(e) {
		//alert("buh");
		e.observe('click', function(f) {
			//alert("Clicked an event. Should edit the highlighted event (id: " + e.identify() +  ") which occurs on date: " + e.up().identify() + ".");
			
			if ( e.hasClassName( 'dupe' )  ) {
			
				blah = e; // stash this object in a global var so the dialog can see it.
				jQuery('#dialog').dialog('open');
			
			} 
			else {
			
				var pageLocation = self.location.toString();
				
				var pageLocationComp = new Array();
				pageLocationComp = pageLocation.split('?')[0].split('#');
				
				
				pageLocation = pageLocationComp[0] + '?op=edit&e=' + e.identify();
	
				
				window.location.href = pageLocation;
			
			}

			
			
			// eat the click
			Event.stop( f );
		});
		
		e.observe('mouseover', function(f) {
			e.setStyle({ backgroundColor: '#f90', cursor: 'pointer' });
			var allOtherCells = e.up('table').select('td:not([class~=today])');
			
			allOtherCells.each(function(g) {
				g.setStyle({ backgroundColor: ''});
			});
			
			// eat the event
			Event.stop( f );			
		});
		
		e.observe('mouseout', function(f) {
			e.setStyle({ backgroundColor: '', cursor: 'arrow' });
			e.up('td').setStyle({ backgroundColor: '#f90' });
			
			// eat the event
			Event.stop( f );
		});
	});
	
	// change up the colors for events on today. I know, I know.
	// you are overwhelmed by my thoroughalty and excellence
	var todayElements = $$('.calendarMonth td.today.edit');
	
	todayElements.each(function(e) {
		e.observe('mouseover', function() {
			e.setStyle({ backgroundColor: '#f99', cursor: 'pointer' });
		});
	});	
	
	var todayElements = $$('.calendarMonth td.today.edit p.event.edit');
	
	todayElements.each(function(e) {
		e.observe('mouseover', function(f) {
			e.setStyle({ backgroundColor: '#f99', cursor: 'pointer' });
			e.up('td').setStyle({ backgroundColor: ''});
			
			// eat the event
			Event.stop( f );
		});
		e.observe('mouseout', function(f) {
			e.setStyle({ backgroundColor: '', cursor: 'arrow' });
			e.up('td').setStyle({ backgroundColor: '#f99' });
			
			// eat the event
			Event.stop( f );
		});		
	});		
	
	
	jQuery("#dialog").dialog({
	bgiframe: true,
	autoOpen: false,
	height: 300,
	modal: true,
	buttons: {
		'Edit master': function() {
			jQuery(this).dialog('close');
			
			var pageLocation = self.location.toString();
			//alert ( pageLocation );
			var pageLocationComp = new Array();
			pageLocationComp = pageLocation.split('?')[0].split('#');
			//pageLocationComp = pageLocationComp[0].split('#');
			
			pageLocation = pageLocationComp[0] + '?op=edit&e=' + blah.identify();
			//pageLocation = pageLocationComp[0] + '?op=create';
			
			//alert( pageLocation );
			
			window.location.href = pageLocation;
		},
		'Edit this instance': function() {
			jQuery(this).dialog('close');
			
			var pageLocation = self.location.toString();
			var pageLocationComp = new Array();
			pageLocationComp = pageLocation.split('?')[0].split('#');
			
			pageLocation = pageLocationComp[0] + '?op=createinstance&e=' + blah.identify() + '&d=' + blah.up().identify();
			
			//alert( pageLocation );
			
			window.location.href = pageLocation;
		},
		Cancel: function() {
			jQuery(this).dialog('close');
		}
	},
	close: function() {
	
	}
});
	
	
	
	
	
	// alert ("debug");
	
}


function ro_init() {

	var cellElements = $$('.calendarMonth td.day');


	cellElements.each(function(e) {
		e.observe('click', function() {
				//alert("Clicked a cell. Should create a new event on this date: " + e.identify() + ".");
				
				//alert (typeof self.location);
				
				var pageLocation = self.location.toString();
				var pageLocationComp = new Array();
				pageLocationComp = pageLocation.split('?');
				pageLocationComp = pageLocationComp[0].split('#');
				
				pageLocation = pageLocationComp[0] + '?date=' + e.identify();
				
				//alert ( pageLocation );
				
				window.location.href = pageLocation;
		});
		e.observe('mouseover', function() {
				var allOtherCells = e.up('table').select('td:not([class~=today])');
			
				allOtherCells.each(function(g) {
					g.setStyle({ backgroundColor: ''});
				});
				
				e.setStyle({ backgroundColor: '#f90', cursor: 'pointer' });
				
		});
		e.observe('mouseout', function() {
				e.setStyle({ backgroundColor: '', cursor: 'arrow' });
		});
	});
	
	// change up the colors for events on today. I know, I know.
	// you are overwhelmed by my thoroughalty and excellence
	var todayElements = $$('.calendarMonth td.today.day');
	
	todayElements.each(function(e) {
		e.observe('mouseover', function() {
			e.setStyle({ backgroundColor: '#f99', cursor: 'pointer' });
		});
	});	
	


	// change up the colors for events on today. I know, I know.
	// you are overwhelmed by my thoroughalty and excellence
	var selectedElements = $$('.calendarMonth td.selected.day');
	
	selectedElements.each(function(e) {
		e.observe('mouseover', function() {
			e.setStyle({ backgroundColor: '#9f9', cursor: 'pointer' });
		});
	});	
	

	//alert ("debug");

}


function form_init() {

	var recur_type = $('recur_type');
	var notime = $('notime');
	//alert( recur_type.identify );

	recur_type.observe('change', function() {
		//alert( recur_type.value );
		if ( recur_type.value == 'none' ) {
			$('recur_options').setStyle({ display: 'none' })
		}
		if ( recur_type.value == 'weekly' ) {
			$('recur_options').setStyle({ display: 'block' })
		}			
	});

	notime.observe('click', function() {
		//alert( notime.checked );
		if ( notime.checked == true) {
			$('startampm').setStyle({ display: 'none' });
			$('starttime').setStyle({ display: 'none' });
			$('endampm').setStyle({ display: 'none' });
			$('endtime').setStyle({ display: 'none' });
			$('timezone').setStyle({ display: 'none' });
			$('timezonelabel').setStyle({ display: 'none' });
		}
		else {
			$('startampm').setStyle({ display: 'inline' });
			$('starttime').setStyle({ display: 'inline' });
			$('endampm').setStyle({ display: 'inline' });
			$('endtime').setStyle({ display: 'inline' });
			$('timezone').setStyle({ display: 'inline' });
			$('timezonelabel').setStyle({ display: 'inline' });			
		}
		
	});

	//alert ("debug");

}



var submitted = 0;

function submitEvent(  ) 
{

	var forwardUrl;
	var replyForm;
	var dummyParentId;



	// let's check some fields
	if ( $( 'eventEditorForm' ).date.value == '' ) {
		$( 'date' ).focus();
		alert( 'Please enter a start date.' );
		
		return false;
		
	}
	var starttime;
	starttime = $( 'eventEditorForm' ).starttime.value.split( ":" );	
	
	starttime[0] = parseInt( starttime[0] );
	starttime[1] = parseInt( starttime[1] );

	
	if ( ( starttime[0] < 1 ) || ( starttime[0] > 12 ) || ( starttime[1] < 0 ) || ( starttime[1] > 59 ) || isNaN( starttime[0] ) || isNaN( starttime[1] ) ) {
		$( 'starttime' ).focus();
		alert( 'Start time needs to be between 1:00 and 12:59.' );
		
		return false;
		
	}	
	var endtime;
	endtime = $( 'eventEditorForm' ).endtime.value.split( ":" );
	
	endtime[0] = parseInt( endtime[0] );
	endtime[1] = parseInt( endtime[1] );
	
	if ( ( endtime[0] < 1 ) || ( endtime[0] > 12 ) || ( endtime[1] < 0 ) || ( endtime[1] > 59 ) || isNaN( endtime[0] ) || isNaN( endtime[1] ) ) {
		$( 'endtime' ).focus();
		alert( 'End time needs to be between 1:00 and 12:59.' );
		
		return false;
		
	}	
	
	//alert ("debug");
	
	// silly locking mechanism to keep comments from being added twice
	if ( submitted == 0 ) {
		
		// set lock
		submitted = 1;

		// grab the form
		$( 'eventEditorForm' ).request({
			onComplete: function( transport ) {
			
				// check if we return an error
				if ( transport.responseText.indexOf( "rror" ) > 0 ) {
					alert( transport.responseText );
					
					// release the lock so that they can try again maybe
					submitted = 0;
				}
				else {
					
					//alert( transport.responseText );
					
					
					
					// add the returned comment id to the url and redirect to load it
					forwardUrl = window.location.href.substring( 0, window.location.href.indexOf( '?' ) );
					
					//alert( forwardUrl + "?r=" + transport.responseText );
					
					// here is the IE silliness
					//window.location.href = "#" + dummyParentId;
					window.location.href = forwardUrl + "?r=" + transport.responseText;
					//document.location.reload();
					
				}
			
			}
		})
	}

}





/*


eventElements = document.select( '[class="calendarMonth"]' ).select('p').select( '[class="edit"]' );

eventElements.observe('click', function() {
alert("Clicked an event. Should edit the event.");
});

eventElements.observe('mouseover', function(e) {
e.setStyle({ background-color: #06c; cursor: pointer; });
});
eventElements.observe('mouseout', function(e) {
e.setStyle({ background-color: #fff; cursor: arrow; });
});



/*

parentText = parentElement.select( '[class="bodyText"]' );
parentPostedBy = parentElement.select( '[class="postedBy"]' );




function updateRating( updateValue, commentId )
{
	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'rating' );
	myParams.set( 'rating', updateValue );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			var selectedComment;
			var selectedElement;
			
			selectedComment = $( 'comment_' + commentId );
			selectedElement = selectedComment.select( '[class="rating"]' );
			selectedElement[0].update( 'rating:&nbsp;' + transport.responseText );
			//selectedElement[0].addClassName( 'success' );
			
			// update rating dongus
			var tempRating = "";
			switch ( updateValue ) {
			
				case 1:
					tempRating += "&nbsp;(&nbsp;+&nbsp;/&nbsp;<a  href=\"javascript:updateRating(-1," + commentId + ")\" title=\"Decrease the rating of this comment\">-</a>&nbsp;)";
					break;
					
				case -1:
					tempRating += "&nbsp;(&nbsp;<a href=\"javascript:updateRating(1," + commentId + ")\" title=\"Increase the rating of this comment\">+</a>&nbsp;/&nbsp;-&nbsp;)";
					break;
					
			}			
			
			selectedElement = selectedComment.select( '[class="doRating"]' );			
			selectedElement[0].update( tempRating );

		}
	});	
				
}

function replyToComment( commentId ) {

	var parentElement;
	var parentText;
	var parentPostedBy;
	var replySection;	
	var selectedElement;
	var replyForm;
	
	// let's get some stuff from the parent comment
	parentElement = $( 'comment_' + commentId );
	parentText = parentElement.select( '[class="bodyText"]' );
	parentPostedBy = parentElement.select( '[class="postedBy"]' );
	
	parentPostedBy = parentPostedBy[0].innerHTML;
	parentText = parentText[0].innerHTML;

	// now alter some fields in the form
	replySection = $( 'composeReply' );
	
	selectedElement = replySection.select( '[class="replyHead"]' );
	selectedElement[0].update( 'Reply to comment #' + commentId );
	
	selectedElement = replySection.select( '[class="instructions"]' );
	
	// IEEEEEEEEEEEEEEEEE!
	try {
		selectedElement[0].update( '<p>You are replying directly to a comment on this page. For your convenience, the text of comment #' + commentId + ' is duplicated below.</p>\n<div class="parentComment"><div class="postedBy">' + parentPostedBy + '</div><div class="bodyText">' + parentText +  '</div></div>\n<p>Please enter your reply in the following space and click the "Submit comment" button when complete. If you decide you don\'t want to reply to this comment, please <a href=\"javascript:resetEntryForm()\">click here</a> to reset this form.</p>' );	
	}
	catch (e) { };

	// set the parentid field in the form to match the parent comment id
	replyForm = $( 'replyForm' );
	replyForm['parentid'].value = commentId;
	replyForm['message'].select();

	
	window.location.href = "#postReply";
	
}

function resetEntryForm( ) {

	var replySection;
	var selectedElement;
	var replyForm
	
	// first, let's mess with some of the content in the post reply section to
	// make it more inviting and such
	replySection = $( 'composeReply' );
	
	selectedElement = replySection.select( '[class="replyHead"]' );
	selectedElement[0].update( 'Reply to Article' );
	
	selectedElement = replySection.select( '[class="instructions"]' );
	selectedElement[0].update( 'Type your comment into the field below. Click the button labeled \'Submit comment\' when finished.' );	
	
	// set the parentid field in the form to reflect stark nothingness
	// trying to reference the form field breaks IE

	replyForm = $( 'replyForm' );
	replyForm['parentid'].value = "";
	replyForm['message'].select();		

	window.location.href = "#postReply";
	
}

var submitted = 0;

function submitComment( ) 
{

	var forwardUrl;
	var replyForm;
	var dummyParentId;

	// silly locking mechanism to keep comments from being added twice
	if ( submitted == 0 ) {
		
		// set lock
		submitted = 1;

		// grab the form
		$( 'replyForm' ).request({
			onComplete: function( transport ) {
			
				// check if we return an error
				if ( transport.responseText.indexOf( "rror" ) > 0 ) {
					alert( transport.responseText );
					
					// release the lock so that they can try again maybe
					submitted = 0;
				}
				else {
					
					// clear out text area set it does not persist
					replyForm = $( 'replyForm' );
					replyForm['message'].value = "";
					
					// try to fool the whackness that is IE by navigating it pretty close to the new message location
					// before reload
					dummyParentId = replyForm['parentid'];
					replyForm['parentid'].value = "";	

					// those case won't scroll anywhere useful, so substitute the end of the page.
					// this will probably be close enough.
					if ( dummyParentId == "" ) {
						dummyParentId = "postReply";
					}

				
					// add the returned comment id to the url and redirect to load it
					forwardUrl = window.location.href.substring( 0, window.location.href.indexOf( '#' ) );
					
					// here is the IE silliness
					//window.location.href = "#" + dummyParentId;
					window.location.href = forwardUrl + "#" + transport.responseText;
					document.location.reload();
				}
			
			}
		})
	}

}


function requestBlock( commentId )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requestblock' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			var selectedComment;
			var selectedElement;
			
			selectedComment = $( 'comment_' + commentId );
			selectedElement = selectedComment.select( '[class="reportThis"]' );
			selectedElement[0].update( transport.responseText );
			selectedElement[0].addClassName( 'success' );

		}
	});	

}


function resetHiddenComments( articleId, articleType )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'resetviewed' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			document.location.reload();

		}
	});	

}



// The user wants to read this blocked comment. Make it so, with extreme prejudice.
function removeBlock( commentId, userLoggedIn ) {

	// don't do all this ajax jibba jabba if the user isn't logged in
	if ( userLoggedIn != -1 ) {
		var url = '/ajax/comments_handler.php';	
		
		var now = new Date();
		var x = now.getTime();
		
		// create a hash with the AJAX parameters
		var myParams = new Hash();
		myParams.set( 'type', 'view' );
		myParams.set( 'commentid', commentId );
		myParams.set( 'x', x );
		
		// send the dong
		// reflect sending field with updated data
		new Ajax.Request( url, {
			method: 'get',
			parameters: myParams,
			onComplete: function( transport ) {
				// alert ( ">" + userId + "< " + transport.responseText );
				var selectedElement;
				var blockedClasses;
				
				blockedClasses = new Array( 'neg', 'blocked' );
				
				// find the comment with an id matching the input and remove the classes that are
				// hiding it.
				selectedElement = $( 'comment_' + commentId );
				
				blockedClasses.each( function( item ) {	
					if ( selectedElement.hasClassName( item ) ) {
						selectedElement.removeClassName( item );
					}
				} )
			}
		});	
	}
	else {
	
		var selectedElement;
		var blockedClasses;
		
		blockedClasses = new Array( 'neg', 'blocked' );
		
		// find the comment with an id matching the input and remove the classes that are
		// hiding it.
		selectedElement = $( 'comment_' + commentId );
		
		blockedClasses.each( function( item ) {	
			if ( selectedElement.hasClassName( item ) ) {
				selectedElement.removeClassName( item );
			}
		} )	
	
	
	}
	
}

function blockComment( commentId, action )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'blockcomment' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
		
			var selectedComment;
						
			if ( transport.responseText.indexOf( "rror" ) > 0 ) {
				alert ( transport.responseText );	
			}
			else {
				switch (action) {
					case 'hide':
						selectedComment = $( 'comment_' + commentId );
						selectedComment.hide();
						break;
					
					case 'reload':
						document.location.reload();
						break;
				}

			}
			
		}
	});	

}

function approveComment( commentId, action )
{

	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'approvecomment' );
	myParams.set( 'commentid', commentId );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
			var selectedComment;
						
			if ( transport.responseText.indexOf( "rror" ) > 0 ) {
				alert ( transport.responseText );	
			}
			else {
				switch (action) {
					case 'hide':
						selectedComment = $( 'comment_' + commentId );
						selectedComment.hide();
						break;
					
					case 'reload':
						document.location.reload();
						break;
				}
			}

		}
	});	

}

function requestViewComments( articleId, articleType )
{


	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requestviewcomments' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
			//Effect.BlindDown('commentHider', { duration: 0.5 });
			$('commentHider').show();
			window.location.href = "#commentMessage";
		}
	});	


}

function requestHideComments( articleId, articleType )
{


	var url = '/ajax/comments_handler.php';	
	
	var now = new Date();
	var x = now.getTime();
	
	// create a hash with the AJAX parameters
	var myParams = new Hash();
	myParams.set( 'type', 'requesthidecomments' );
	myParams.set( 'articleid', articleId );
	myParams.set( 'articletype', articleType );
	myParams.set( 'x', x );
	
	// send the dong
	// reflect sending field with updated data
	new Ajax.Request( url, {
		method: 'get',
		parameters: myParams,
		onComplete: function( transport ) {
			
				//Effect.BlindUp('commentHider', { duration: 0.5 });
				//alert ( transport.responseText );
				$('commentHider').hide();

		}
	});	


}

*/