var Transcript = {
	toggle_button : null,
	output : null,
	panel : null,
	panel_wrapper : null,
	collapsed : false,
	current_line : null,
	current_line_element : null,
	next_line : null,
	next_line_element : null,
	transcription_player : null,
	caption : null,
	caption_button : null,
	caption_wrapper : null,
	show_caption : false,
	output_scroll : 0,
	disable_auto_scroll : false,
	timeout: null,
	
	transcript_array : null,
	transcription_string: '',
	
	_click_select_line : null,
	
	initialized : false,
	
	initialize : function() {
		if (!this.initialized) {
			this.transcript_array = transcript_array;
			this.toggle_button = $('transcription').down('.collapse');
			this.caption_button = $('caption_button');
			this.output = $('transcript');
			this.panel = $('transcription');
			this.panel_wrapper = $('transcription_wrapper');
			this.caption = $('caption');
			this.caption_wrapper = $('caption_wrapper');

			if (navigator.appName.indexOf("Microsoft") != -1) {
				this.transcription_player = window['transcription_player'];
			} else {
				this.transcription_player = document['transcription_player'];
			}
			this.prepare_transcript();
			this.output_transcript();
			
			this.show_caption = true;
			this.show_caption = Cookie.read('show_realtime_transcription_caption');
			this.show_caption = this.show_caption == 'false' ? false : true;
			
			if (this.show_caption) {
				this.toggle_caption(true);
			}
			
			this._click_select_line = this.click_select_line.bindAsEventListener(this);
			
			Event.observe(this.toggle_button, 'mousedown', function(event) {event.stop()});
			Event.observe(this.toggle_button, 'mouseup', this.toggle.bind(this));
			Event.observe(this.panel, 'mouseup', this._click_select_line);
			
			this.initialized = true;
		}
	},
	
	ready : function() {
		Search.initialize.bind(Search)(initial_search);
		Slide.initialize.bind(Slide)();
	},
	
	get_current_line : function(timestamp) {
		var line;
		var previous_line;
		var next_line;
		for (var i=0, c=this.transcript_array.length; i<c; i++) {
			line = this.transcript_array[i];
			next_line = this.transcript_array[i+1];
			previous_line = this.transcript_array[i-1];
			if (line.time <= timestamp && (!next_line || next_line.time > timestamp)) {
				if (!this.current_line || this.current_line != line) {
					this.current_line = line;
					this.next_line = next_line;
					this.highlight_current_line();
					break;
				}
			}
		}
	},
	
	toggle_caption : function(show) {
		if (show === true) {
			this.show_caption = true;	
		} else if (show === false) {
			this.show_caption = false;
		} else {
			this.show_caption = !this.show_caption;
		}
		if (this.show_caption) {
		    Cookie.create('show_realtime_transcription_caption', true);
			this.caption.show();	
			this.caption_button.addClassName('on');
			this.caption_button.removeClassName('off');
		} else {
		    Cookie.create('show_realtime_transcription_caption', false);
			this.caption.hide();
			this.caption_button.addClassName('off');
			this.caption_button.removeClassName('on');			
		}
	},
	
	click_select_line : function(event) {
		var span = event.findElement('span');
		span = span && span.parentNode != this.output ? span.up() : span;
		if (span) {
			var id = id || parseInt(span.className.match(/[0-9]+/));
			this.select_line(id);
		}
	},
	
	select_line : function(id) {
		this.transcription_player.seekFromJavascript(this.transcript_array[id].time);
	},
	
	highlight_current_line : function() {
		if (this.current_line_element) {
			this.current_line_element.removeClassName('current_line');	
		}
		
		if (this.next_line_element) {
			this.next_line_element.removeClassName('current_line');	
		}

		var current_line = $$('#transcript .line_' + this.current_line.id)[0];
		var next_line = $$('#transcript .line_' + this.next_line.id)[0];

		this.current_line_element = current_line;
		this.next_line_element = next_line;
		this.caption.innerHTML = this.current_line.text;
		current_line.addClassName('current_line');
		//next_line.addClassName('current_line');
	},
	
	scroll_to_line : function() {
		if (this.enable_auto_scroll && this.current_line) {
			var line = this.current_line_element;
			var position = line.positionedOffset()[1];
	
			var scrollTop = Transcript.output.scrollTop;
			if (scrollTop == this.output_scroll && !this.disable_auto_scroll) {
				if (!Search.searching && (position > (scrollTop + this.output.getHeight()/2 - 40) || position < (scrollTop + this.output.getHeight()/2 - 40))) {
					this.output.scrollTop += (position - (scrollTop + this.output.getHeight()/2 - 40)) * .15;
					this.output_scroll = this.output.scrollTop;
				};
			} else if (scrollTop != this.output_scroll) {
				this.output_scroll = scrollTop;
				this.disable_auto_scroll = true;
				clearTimeout(this.timeout);
				this.timeout = setTimeout(this.enable_auto_scroll.bind(this), 3000);
			}
		}		
	},
	
	enable_auto_scroll : function() {
		this.output_scroll = Transcript.output.scrollTop;
		this.disable_auto_scroll = false;	
	},
	
	prepare_transcript : function() {
		for (var i=0, c=this.transcript_array.length; i<c; i++) {
			var line = this.transcript_array[i];
			line.time = line.time / 1000;
			line.id = i;
			
			line.text = ((line.text + ' ').replace(/ +/g, ' ')).unescapeHTML().replace('&apos;', "'");

			line.index = this.transcription_string.length;
			line.length = line.text.length;
			this.transcription_string += line.text;
		}
	},
	
	output_transcript : function() {
		this.output.innerHTML = '';
		var html = '';
		for (var i=0, c=this.transcript_array.length; i<c; i++) {
			var line = this.transcript_array[i];
			html += '<span class="line_' + i + '">';
			if (!line.matches) {
				html += line.text;
			} else {
				var last_matched_end = 0;
				for (var j=0, d=line.matches.length; j<d; j++) {
					var matched = line.matches[j];
					html += line.text.substring(last_matched_end, matched.index);
					html += '<span class="highlight match_' + matched.id + '">';
					html += line.text.substr(matched.index, matched.length);
					html += '</span>';
					last_matched_end = matched.index + matched.length;
				}
				html += line.text.substr(last_matched_end);
			}
			html += '</span>';
		}
		this.output.innerHTML = html;
		Search.highlight_selected_result();
	},
	
	toggle : function(event) {
		this.collapsed = !this.collapsed;
		if (this.collapsed) {
			this.toggle_button.addClassName('collapsed');
			Slide.left.bind(Slide)();
		} else {
			this.toggle_button.removeClassName('collapsed');
			Slide.right.bind(Slide)();
		}
	}
}
