var AE = window.AE || {};

var slider;

AE.initGrading = function() { 
	if($('grade-bttn')) {
		Event.observe('grade-bttn','click',function(e) {
			Event.stop(e);
			elm = e.target;
			Effect.toggle('grades','Blind',{duration:.1});
		});
	}
}

AE.initDimLights = function() {
	if($('dim-lights')) {
		Event.observe('dim-lights','click',function(e) {
			Event.stop(e);
			elm = e.target;
			AE.setOverlayHeight();
			Effect.toggle('lights-dimmed','Appear',{
				duration:2,
				// beforeStart: function() {
				// 	if($('lights-dimmed').visible()) {
				// 		$('dim-lights').setStyle({backgroundImage:'url(/img/icon-bulb-light.gif)'});
				// 	} else {
				// 		$('dim-lights').setStyle({backgroundImage:'url(/img/icon-bulb.gif)'});
				// 	}
				// 	
				// },
				afterFinish: function() {
					if($('lights-dimmed').visible()) {
						$('dim-lights').update('Lights up');
						AE.trackEvent('video-dim-lights');
					} else {
						$('dim-lights').update('Dim the lights');
						AE.trackEvent('video-raise-lights');
					}
				}
			});
			Effect.toggle('video-tools','Appear',{ duration:2 });
			
		});
	}
}

AE.initVideoTools = function() {
	if($('video-tools')) {
		if ($('settings-link'))
		{
			Event.observe('settings-link','click',function(e) {
				Event.stop(e);
				elm = e.target;
				AE.hideSettings('video-settings');
				new Effect.toggle('video-settings', 'Appear', {queue:'end', duration:.1});
			});
			Event.observe('close-settings', 'click', function(e) {
				Event.stop(e);
				AE.hideSettings('video-settings');
			});
		}
		
		Event.observe('embed-link','click',function(e) {
			Event.stop(e);
			elm = e.target;
			AE.hideVideoTools('video-embed');
			new Effect.toggle('video-embed','Appear',{
					queue:'end', 
					duration:.1,
					afterFinish: function() {
						if ($('video-embed').visible()) {
							$('video-embed').down('textarea').select();
							AE.trackEvent('video-embed');
						}
					}
				});
		});
		Event.observe('share-link','click',function(e) {
			Event.stop(e);
			elm = e.target;
			AE.hideVideoTools('video-share');
			new Effect.toggle('video-share','Appear',{queue:'end', duration:.1});
		});
		Event.observe('cite-link','click',function(e) {
			Event.stop(e);
			elm = e.target;
			AE.hideVideoTools('video-cite');
			new Effect.toggle('video-cite','Appear',{
				queue:'end',
				duration:.1,
				afterFinish: function() {
					if ($('video-cite').visible()) {
						$('video-cite').down('textarea').select();
						AE.trackEvent('video-citation');
					}	
				}
			});
		});
		if ($('download-link'))
		{
			Event.observe('download-link', 'click', function(e){
				Event.stop(e);
				elm = e.target;
				AE.hideVideoTools('download-choices');
				new Effect.toggle('download-choices','Appear',{queue:'end', duration:.1});
			});
		}
		if ($('podcast-link'))
		{
			Event.observe('podcast-link', 'click', function(e){
				Event.stop(e);
				elm = e.target;
				AE.hideVideoTools('podcast-choices');
				new Effect.toggle('podcast-choices','Appear',{queue:'end', duration:.1});
			});
		}

		$$('ul.sociable li a').each(function(el){
			Event.observe(el, 'click', function(e){
				var suffix = '';
				if (el.id=='share_Facebook')
					suffix = '-facebook';
				else if (el.id=='share_e-mail')
					suffix = '-email';
				else if (el.id=='share_del.icio.us')
					suffix = '-delicious';
				else if (el.id=='share_Digg')
					suffix = '-digg';
				else if (el.id=='share_Mixx')
					suffix = '-mixx';
				else if (el.id.match(/^share_Google/))
					suffix = '-googlebookmarks';
				else if (el.id=='share_StumbleUpon')
					suffix = '-stumbleupon';
				else if (el.id=='share_Reddit')
					suffix = '-reddit';
				AE.trackEvent('video-share'+suffix);
			});
		});
		
		$$('img.close-icon').each(function(elm) {
			Event.observe($(elm),'click',function(e) {
				Event.stop(e);
				AE.hideVideoTools();
			});
		});
		
		if ($('EmbedLecture'))
		{
			$('EmbedLecture').observe('click', function(e) {
				$('lecture-embed').show();
				$('clip-embed').hide();
			});
			$('EmbedClip').observe('click', function(e) {
				$('lecture-embed').hide();
				$('clip-embed').show();			
			});
			$('ShareLecture').observe('click', function(e) {
				$('lecture-share').show();
				$('clip-share').hide();			
			});
			$('ShareClip').observe('click', function(e) {
				$('lecture-share').hide();
				$('clip-share').show();		
			});
		}
	}
}

AE.hideSettings = function(elm) {
	new Effect.Fade(elm, {queue:'front', duration:.1});
}
AE.hideVideoTools = function(skipElm) {
	$$('#video-tool-container > div').each(function(elm) {
		if(elm.id != skipElm) {
			new Effect.Fade(elm, {queue:'front', duration:.1});
		}
	});
}
AE.setOverlayHeight = function() {
	$('lights-dimmed').setStyle({'height':AE.getPageSize().height+'px'});
}

AE.getPageSize = function() {
        
     var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	return {width:pageWidth,height:pageHeight};
}

AE.initScrollbar = function() 
{
	
	// vertical slider control
	slider = new Control.Slider('scroll_handle', 'scroll_slider', {
		axis: 'vertical',
		onSlide: function(v) { AE.scrollVertical(v, $('clip-list-window'), slider);  },
		onChange: function(v) { AE.scrollVertical(v, $('clip-list-window'), slider); }
	});

	// disable vertical scrolling if text doesn't overflow the div
	if ($('clip-list-window').scrollHeight <= $('clip-list-window').offsetHeight) {
		slider.setDisabled();
		$('scroll_wrap').hide();
	}
	else
	{
		//$('scroll_wrap').show();
	}
	
	// mozilla mousewheel
	Event.observe('player-sidebar', 'DOMMouseScroll', AE.wheel);

	// IE/Opera mousewheel
	Event.observe('player-sidebar', 'mousewheel', AE.wheel);
	

}

// scroll the element vertically based on its width and the slider maximum value
AE.scrollVertical = function(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}

// mouse wheel code from http://adomas.org/javascript-mouse-wheel/
AE.handle = function(delta) {
	slider.setValueBy(-delta);
}

/** Event handler for mouse wheel event. */
AE.wheel = function(event){
	var delta = 0;
	if (!event) /* For IE. */
		event = window.event;
	if (event.wheelDelta) { /* IE/Opera. */
		delta = event.wheelDelta/120;
		/** In Opera 9, delta differs in sign as compared to IE. */
		if (window.opera)
			delta = -delta;
	} else if (event.detail) { /** Mozilla case. */
		/** In Mozilla, sign of delta is different than in IE.
		* Also, delta is multiple of 3.
		*/
		delta = -event.detail/3;
	}

	/** If delta is nonzero, handle it.
	* Basically, delta is now positive if wheel was scrolled up,
	* and negative, if wheel was scrolled down.
	*/
	if (delta)
		AE.handle(delta/10.0);

	/** Prevent default actions caused by mouse wheel.
	* That might be ugly, but we handle scrolls somehow
	* anyway, so don't bother here..
	*/
	if (event.preventDefault)
		event.preventDefault();
	
	event.returnValue = false;
}




document.observe('dom:loaded',function() {
	AE.initGrading();
	AE.initDimLights();
	AE.initVideoTools();
	AE.initScrollbar();
});
