/* Teaching.js */

var graceChapel;
if(!graceChapel) graceChapel = {};

window.addEvent('domready', function() { 

	if($('home-page')) {
		graceChapel.checkLiveStream = new CheckLiveStream({
			testMode: 0
		});
	} else {
		if($('latest-message')) activateMsgControls();
	}

}); // end domready

// -----------------------------------------------------------------------------
// MP3 Player Class
// -----------------------------------------------------------------------------

var MP3Player = new Class({
	initialize: function(options) {
		this.date = options.date;
		this.el = $(options.el);
	},
	build: function() {
		this.el.set('html', '');
	//$('media-playback').setHTML(this.date);
		
		//console.log('date: ' + this.date);
	
		this.file = "http://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/MP3/" + this.date + ".mp3";
		this.so = new SWFObject('/swf/mp3player.swf', "mp3player", "340", "20", "8", "#ffffff");
		this.so.addVariable("file", this.file);
		this.so.addVariable("volume", "60");
		this.so.addVariable('autostart', true);
		
		//console.log(this.so);
	
		// find out the starting height
		var startHeight = this.el.getStyle('height');
		
		//console.log(this.el);
		//var myFx = Fx.Tween(this.el);
		var myFx = new Fx.Morph(this.el, {transition: Fx.Transitions.Elastic.easeOut, duration: 2500, onComplete: this.populate.bind(this)});
		//console.log(myFx);
		myFx.start({
			'height': 50
			});
		//this.el.tween('height', 50);
	},
	directLink: function() {
		var lnk = new Element('a');
		lnk.setProperties({
			href: this.file
		});
		lnk.setStyles({
			'display': 'block',
			'margin-top': '14px'
		});
		lnk.set('html', 'MP3 Download Link');
		this.el.adopt(lnk);
		//lnk.inject('inside', this.el);
		//console.log(lnk);
	},
	populate: function() {
		this.so.write(this.el);
		this.directLink();
	}
});


// -------------------------------------------------------------------------------------------------
// Video Player Class
// -------------------------------------------------------------------------------------------------
var VideoPlayer = new Class({
	initialize: function(options) {
		this.date = options.date;
		this.el = $(options.el);
		this.url = '';
		//console.log(this.el);
	},
	chooseSrc: function() {
		
		this.url = 'http://mfile.akamai.com/29062/wmv/multicastmed.download.akamai.com/29062/' + this.date + 'm.wmv';
		this.directLinkURL = 'http://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/' + this.date + 'm.wmv';
		var mediaSrc;
		
		// use a crude OS sniff to decide whether to send them to an ASX file or directly to the media.
		//(navigator.appVersion.indexOf("Windows") != -1) === true ? mediaSrc = '/asx/ondemand.asx?msgdate=' + this.date : mediaSrc = this.url;
		
		//if(navigator.appVersion.indexOf("Windows") != -1) {
			//console.log('windows...');
		//	mediaSrc = '/asx/ondemand.asx?msgdate=' + this.date;
	//	} else {
			//console.log('not windows');
			mediaSrc = this.url;
		//}
		
		//mediaSrc = '/asx/ondemand.asx?msgdate=' + this.date;
		
		//console.log(navigator.appVersion.indexOf("Windows"));
		//console.log('mediaSrc: ' + mediaSrc);
		
		this.embedSrc = '<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" width="320" height="304" id="WindowsMediaPlayer1"><param name="URL" value="' + mediaSrc + '"><param name="autoStart" value="true"><param name="uiMode" value="full"><param name="showControls" value="true"><param name="showStatusBar" value="true"><embed src="' + mediaSrc + '" width="320" height="304" rate="1" uimode="full" showControls="true" showStatusBar="true" autoStart="1"></embed></object>';
		//console.log('uimode: full');
		//console.log(this.embedSrc);
	},
	build: function() {
		this.chooseSrc();
		this.el.set('html','');
		
		var startHeight = this.el.getStyle('height');
		
		var myFx = new Fx.Morph(this.el, {transition: Fx.Transitions.Quart.easeInOut, duration: 1500, onComplete: this.populate.bind(this)});
		//console.log(myFx);
		myFx.start({
			'height': 400
			});
		
	},
	directLink: function() {
	
		
		var notice = new Element('div',
		{
			'class': 'notice'
		}).set('html', 'Note: If the video does not start automatically, click the Video Direct Link.');
	
		var lnk = new Element('a');
		lnk.setProperties({
			href: this.directLinkURL
		});
		lnk.setStyles({
			'display': 'block',
			'margin': '14px 0'
		});
		lnk.set('html', 'Video Direct Link');
		//this.el.adopt(notice).adopt(lnk);
		this.el.adopt(lnk);
		//lnk.injectInside(this.el);
	},
	populate: function() {
		this.el.set('html', this.embedSrc);
		this.directLink();
		//graceChapel.notesDisplay.hide();
	}
});

// -------------------------------------------------------------------------------------------------
// MP4 Player Class
// -------------------------------------------------------------------------------------------------
var MP4Player = new Class({
	initialize: function(options) {
		this.date = options.date;
		this.el = $(options.el);
		//this.url = "rtmp://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/" + this.date + ".m4v";
		this.url = "http://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/" + this.date + ".m4v";
		//this.url = 'arrrgh';
		//console.log(this.el);
	},
	build: function() {
		this.el.set('html', '');
		
		this.so = new SWFObject("/swf/player.swf","ply","512","384","9","#FFFFFF");
		this.so.addParam("allowfullscreen","true");
		this.so.addParam("allowscriptaccess","always");
		this.so.addParam("flashvars","file=" + this.url + "&image=/images_general/panorama.jpg&autostart=true");
	
		// find out the starting height
		var startHeight = this.el.getStyle('height');
		
		//console.log(this.el);
		//var myFx = Fx.Tween(this.el);
		var myFx = new Fx.Morph(this.el, {transition: Fx.Transitions.Quart.easeInOut, duration: 1500, onComplete: this.populate.bind(this)});
		//console.log(myFx);
		myFx.start({
			'height': 460
			});
		//this.el.tween('height', 50);
	},
	directLink: function() {
	
	
		var lnk = new Element('a');
		lnk.setProperties({
			href: this.url
		});
		lnk.setStyles({
			'display': 'block',
			'margin': '14px 0'
		});
		lnk.set('html', 'Video Direct Link');
		this.el.adopt(lnk);
	},
	populate: function() {
		this.so.write(this.el);
		this.directLink();
	}
});

// -------------------------------------------------------------------------------------------------
// MP4 Player Class
// -------------------------------------------------------------------------------------------------
var VidegoPlayer = new Class({
	initialize: function(options) {
		this.date = options.date;
		this.el = $(options.el);
		//this.videgoId = 'v226ie1g';
		//this.url = "rtmp://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/" + this.date + ".m4v";
		//this.url = "http://a4.g.akamai.net/7/4/29062/v1/multicastsmb.download.akamai.com/29062/" + this.date + ".m4v";
		//this.url = 'arrrgh';
		//console.log(this.el);
	},
	build: function() {
		this.el.set('html', '');
		
		if($('videgoId')) this.videgoId = $('videgoId').get('text');
		
		this.vidego = new Element('iframe', {
			'width':'560px',
			'height':'480px',
			'frameborder':'no',
			'scrolling':'no',
			'src':'http://vidego.multicastmedia.com/player.php?v=' + this.videgoId
		});
	
		// find out the starting height
		var startHeight = this.el.getStyle('height');
		
		//console.log(this.el);
		//var myFx = Fx.Tween(this.el);
		var myFx = new Fx.Morph(this.el, {transition: Fx.Transitions.Quart.easeInOut, duration: 1500, onComplete: this.populate.bind(this)});
		//console.log(myFx);
		myFx.start({
			'height': 580
			});
		//this.el.tween('height', 50);
	},
	directLink: function() {
	
	
		var lnk = new Element('a');
		lnk.setProperties({
			href: this.url
		});
		lnk.setStyles({
			'display': 'block',
			'margin': '14px 0'
		});
		lnk.set('html', 'Video Direct Link');
		this.el.adopt(lnk);
	},
	populate: function() {
		//this.so.write(this.el);
		this.el.adopt(this.vidego);
		//this.directLink();
	}
});

// -------------------------------------------------------------------------------------------------
// CheckLiveStream Class: Changes the page when the live stream is active.
// -------------------------------------------------------------------------------------------------
var CheckLiveStream = new Class({
	initialize: function(options) {
		if(options.testMode === 1) {
			this.interval = 5000;
			this.testReps = 100;
		} else {
			this.interval = 30000;
			this.testReps = 180;
		}
		this.periodicalId = this.poll.bind(this).periodical(this.interval);
		this.counter = 0;
		this.preload = 1;
		this.loadingDiv = $('loading');
		this.container = $('latest-message-container');
		this.noticeContainer = $('message-notice-container');
		this.poll();
	},
	poll: function() {
	
		var query;
		
		if(this.preload === 1) {
			query = 'p=poll_livestream&preload=1';
		} else {
			query = 'p=poll_livestream';
		}
	
		var request = new Request.JSON({
			url: '/services.php',
			evalResponse: 'true',
			method: 'post',
			onSuccess: this.pollCallback.bind(this)
		}).send(query);
	},
	pollCallback: function(response) {
		if(this.loadingDiv) {
			this.loadingDiv.dispose();
			this.preload = 0;
		}
		
		var stream = response[0];
		var message = response[1];
		var notice = response[2];

		if(stream == '1') {
			this.clearPeriodical();
			this.noticeContainer.empty();
			this.container.set('html', message);
			
			/*var testel = new Element('script', {
				'type':'text/javascript'
			}).set('html', 'console.log("nice");');
			$('livestream').grab(testel);*/
			
			
		} else {
			this.counter++;
			if(this.counter > this.testReps) this.clearPeriodical(); // 360 --> every 15 seconds for 90 minutes
			// Only load the message the first time.
			if(message != 0) {
				this.container.set('html', message);
				activateMsgControls();
			}
			if(notice != 0) {
				if(this.noticeContainer.get('html').length < 1) this.counter = 0; // reset counter if the "services begin soon" notice comes up
				this.noticeContainer.set('html', notice);
			}
		}
	},
	clearPeriodical: function() {
		this.periodicalId = $clear(this.periodicalId);
	}
});

// -------------------------------------------------------------------------------------------------
// Function: If there's an archived message showing, assign events to the buttons and controls
// -------------------------------------------------------------------------------------------------
function activateMsgControls() {
	
		graceChapel.notesDisplay = new NotesDisplay({});
			
		graceChapel.podCastHelp = new HelpDisplay({
			div: $('msg-podcast-help'),
			btn: $('help-btn-podcast'),
			file: '/js/templates/teaching/podcast_help.html'
		});
		
		graceChapel.avHelp = new HelpDisplay({
			div: $('msg-av-help'),
			btn: $('help-btn-av'),
			file: '/js/templates/teaching/av_help.html'
		});
		
		if($('msg-date')) {
			graceChapel.mp3player = new MP3Player({
				date: $('msg-date').innerHTML,
				el: 'media-playback'
			});
		}
		
		if($('msg-date')) {
			graceChapel.videoplayer = new VideoPlayer({
				date: $('msg-date').innerHTML,
				el: 'media-playback'
			});
		}
		
	/*	if($('msg-date')) {
			graceChapel.mp4Player = new MP4Player({
				date: $('msg-date').innerHTML,
				el: 'media-playback'
			});
		}*/
		
		if($('msg-date')) {
			graceChapel.videgoPlayer = new VidegoPlayer({
				date: $('msg-date').innerHTML,
				el: 'media-playback'
			});
		}
		
		
		if($('mp364-listen')) {			
			$('mp364-listen').addEvent('click',
				function(evt) {
					(new Event(evt)).stop();
					graceChapel.mp3player.build();
				}
			);
		};
		
		
		if($('wmv-watch')) {			
			$('wmv-watch').addEvent('click',
				function(evt) {
					(new Event(evt)).stop();
					graceChapel.videoplayer.build();
				}
			);
		};
		
		if($('btn-mp4')) {
			$('btn-mp4').addEvent('click',
				function(evt) {
					(new Event(evt)).stop();
					graceChapel.mp4Player.build();
				}
			);
		};
		
		if($('btn-vidego')) {
			$('btn-vidego').addEvent('click',
				function(evt) {
					(new Event(evt)).stop();
					graceChapel.videgoPlayer.build();
				}
			);
		};
		
		
		
		

}

// -------------------------------------------------------------------------------------------------
// NotesDisplayt Class: Display the Message Notes
// -------------------------------------------------------------------------------------------------
var NotesDisplay = new Class({
	initialize: function(options) {
		this.div = $('msg-notes');
		this.btn = $('btn-scripture');
		this.id = $('msg-id').innerHTML;
		this.maxHeight = $('home-page') ? '200px' : '400px'; 
		this.fx = new Fx.Morph(this.div,{transition: Fx.Transitions.Quint.easeInOut, duration: 1500});
		this.btn.addEvent('click', function(evt) {
			new Event(evt).stop();
			this.toggle();
		}.bindWithEvent(this));
	},
	toggle: function() {
		//console.log('height: ' + this.div.getStyle('height'));
		if(this.div.getStyle('height') != '0px') {
			this.hide();
		} else {
			this.show();
		}
	},
	show: function() {
		this.div.setStyles({
			'display':'block',
			'height':'0px',
			'overflow':'auto'
		});
		this.fx.start({'height':['0px',this.maxHeight]});
		
		var loading = new Element('div', {
			'id':'loader',
			'styles': {
				'height':'32px',
				'background-image':'url(/images_ui/ajax-loading/ajax-loader-4a180c.gif)',
				'background-repeat':'no-repeat',
				'background-position':'50% 50%'
			}
		}).inject(this.div, 'top');
		
		new Request.HTML({
			url: '/services.php',
			onSuccess: this.ajaxCallback.bind(this)
		}).get({'p': 'msg_notes', 'id': this.id});
	},
	hide: function() {
		//console.log('hide');
		this.fx.start({'height':[this.maxHeight,'0px'], onComplete: function() {this.div.set('html', '');}.bind(this)});
	},
	ajaxCallback: function(responseTree, responseElements, responseHTML, responseJavaScript) {
		//console.log('yeah');
		if($('loader')) $('loader').dispose();
		var enclosingDiv = new Element('div', {
			'styles': {
				'padding':'10px',
				'background-color':'#4A180C'
			}
		});
		enclosingDiv.set('html', responseHTML);
		
		this.fade = new Fx.Tween(enclosingDiv).set('opacity', '0');
		
		this.div.adopt(enclosingDiv);
		this.fade.start('opacity', '0', '1');
	}
});

// -------------------------------------------------------------------------------------------------
// HelpDisplay Class: Display the Message Notes
// -------------------------------------------------------------------------------------------------
var HelpDisplay = new Class({
	initialize: function(options) {
		this.state = 'closed';
		this.testMode = options.testMode;
		this.file = options.file;
		this.div = options.div;
		this.btn = options.btn;
		this.maxHeight = '150px';
		
		this.fx = new Fx.Morph(this.div,{transition: Fx.Transitions.Quint.easeInOut, duration: 1500});
		this.btn.addEvent('click', function(evt) {
			new Event(evt).stop();
			this.toggle();
		}.bindWithEvent(this));
	},
	toggle: function() {
		//console.log('height: ' + this.div.getStyle('height'));
		if(this.div.getStyle('height') != '0px') {
			this.hide();
		} else {
			this.show();
		}
	},
	show: function() {
		this.div.setStyles({
			'display':'block',
			'height':'0px',
			'overflow':'auto'
		});
		this.fx.start({'height':['0px',this.maxHeight]});
		
		new Request.HTML({onSuccess: this.ajaxCallback.bind(this)}).get(this.file);
		this.state = 'open';
	},
	hide: function() {
		//console.log('hide');
		this.fx.start({'height':[this.maxHeight,'0px'], onComplete: function() {this.div.set('html', '');}.bind(this)});
	},
	ajaxCallback: function(responseTree, responseElements, responseHTML, responseJavaScript) {
		//console.log('yeah');
		var enclosingDiv = new Element('div', {
			'styles': {
				'padding':'10px',
				'margin': '5px'
			}
		});
		enclosingDiv.set('html', responseHTML);
		this.div.adopt(enclosingDiv);
		
	}
});