var waitHTML = webkit.waitHTML;

document.observe("dom:loaded", function() {
	initAjaxLinks('menublock');	
	initAjaxLinks('nm_header');
	initAjaxLinks();
	uoft.createObservers();
	uoft.contactObserver();
	webkit_playsong('random','mediaplayer');
});

function ajaxpage_callback(page) {
	uoft.createObservers();
}

function uoft() {
	
	this.contactObserver = function() {
		$$('#nm_id_contact a').invoke('stopObserving','click');
		$$('#nm_id_contact a').each(function(o) {
			o.observe('click', function(e) {
				e.stop();
				Modalbox.show('/contact', { title:'Send email to U of T Jazz', overlayClose:false, width:600, afterLoad:function() {
					emailformObservers();
					$('MB_close').setStyle({display:'block'});
					$('MB_close').addClassName('MB_closeactive');

					$$('#MB_content .emailaddress').each(function(o) {
						o.observe('click', function(e) {
							e.stop();
							new Ajax.Updater('MB_content', '/contact/' + o.title, {
								method:'get',
								onComplete:function() {
									emailformObservers();
									Modalbox.resizeToContent();
									$('MB_caption').update('Send email to ' + o.title);
								}
							});
						});
					});
				}});
			});	
		});
	}
	
	this.studentGigsObservers = function() {		
		var defaultComment = "Please enter any additional notes about your gig here...";
		$('comment').observe('focus', function() {
			if ($('comment').getValue() == defaultComment) {
				$('comment').value = '';
			}
		});
		
		$('submitbutton').observe('click', function(e) {
			e.stop();			
			if (!$('month').getValue() || !$('day').getValue() || !$('year').getValue()) {
				uoft.clearFeedback();				
				$('whenfeedback').update('ERROR: this field is required');
				uoft.pulsateFeedback('whenfeedback');				
				return;
			}
			else if (!$('time').getValue()) { 
				uoft.clearFeedback();	
				$('timefeedback').update('ERROR: this field is required');											
				uoft.pulsateFeedback('whenfeedback');				
				return;
			}
			
			if (!$('where').getValue()) {
				uoft.clearFeedback();				
				$('wherefeedback').update('ERROR: this field is required');
				uoft.pulsateFeedback('wherefeedback');				
				return;
			}
			
			if (!$('author').getValue()) {
				uoft.clearFeedback();				
				$('whofeedback').update('ERROR: this field is required');
				uoft.pulsateFeedback('whofeedback');				
				return;
			}
			
			if (!$('email').getValue().match(/^.+?@.+?\..+/)) {
				$('emailfeedback').update('ERROR: you must enter a valid email address');
				uoft.pulsateFeedback('emailfeedback');
				return;
			}
			
			if ($('comment').getValue() == defaultComment) {
				$('comment').value = '';
			}
			
			$('comment').value = '<div class="when"><div class="label">When:</div><div class="userinput">' + $('year').getValue() + '-' + $('month').getValue() + '-' + $('day').getValue() + ' 00:00:00</div></div><div class="time"><div class="label">Time:</div><div class="userinput">' + $('time').getValue() + '</div></div><div class="where"><div class="label">Where:</div><div class="userinput">' + $('where').getValue() + '</div></div><div class="notes"><div class="label"></div><div class="userinput">' + $('comment').getValue() + '</div></div>';
			$('commentform').submit();
		});
	}
	
	this.pulsateFeedback = function(ID) {		
		new Effect.Pulsate(ID, { duration:2, pulses: 3});
	}
	
	this.clearFeedback = function() {
		$$('#commentform .feedback').each(function(o) {
			o.update('');
		});
	}
	
	this.createObservers = function() {
		// observers for anchors
		$$(".anchors ul li").each(function(o) {
			o.observe('mouseover', function() {
				cursorLink();
			});
			o.observe('mouseout', function() {
				cursorClear();
			});
			o.observe('click', function(e) {
				e.stop();
				var thisAnchor = o.id.replace(/_anchor$/,'');
				new Effect.ScrollTo(thisAnchor, { duration:0.5 });
			})
		});

		// email address links
		$$('#pagecontent .emailaddress').each(function(o) {
			o.stopObserving('click');
			o.observe('click', function(e) {
				e.stop();
				Modalbox.show('/contact/' + o.title, { title:'Send email to ' + o.title, overlayClose:false, width:600, afterLoad:function() {
					emailformObservers();
					$('MB_close').setStyle({display:'block'});
					$('MB_close').addClassName('MB_closeactive');
				}});
			});
		});

		if ($(document.body).hasClassName('nm_currentPage_clips')) {
			webkit.modinit('soundclips');
		}
		else if ($(document.body).hasClassName('nm_currentPage_gallery')) {
			webkit.modinit('photogallery');
		}
		else if ($(document.body).hasClassName('nm_currentPage_students') && $('studentgigs')) {
			uoft.studentGigsObservers();
		}
		else if ($(document.body).hasClassName('nm_currentPage_outreach')) {
			$$('.gallery-item a').each(function(o) {
				o.observe('click', function(e) {
					e.stop();
				});
			});
		}
	}
}

var uoft = new uoft();