/* 
 * 
 * Subtitler
 * 
 */
var Subtitler = new NanoClass({
	options:{
		videoel:'videoid',// no # needed
		containerel:'#subtitlevideowrapper', //# needed
		gotourl:'#fertig',
		videolen:14000,//ms 12000
		subtitles: '[ [ 0.0 ,3.0 ,300 ,300, " "] ]'
	},
	initialize:function(options){
		if(!swfobject.getFlashPlayerVersion()){
			return;
		}
		this.setOptions(options);
		this.initTimes();
		this.initVideo();
		this.activeTime = 0;
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))) {
			jQuery('#menu').hide();
			/*jQuery('.skip-video').hide();*/
		}	
	},
	initTimes:function(ev){
		var instance = this;
		this.times = this.options.subtitles;
		this.subtitles = eval("(" + this.times + ")"); 

		
		$.each(this.subtitles, function(i, el) {
			el.push(false);//el[5] stands for "done displayed"
			el.push(false);//el[6] stands for "done not anymore"
			var div = jQuery('<div style="top:'+el[3]+'px;left:'+el[2]+'px; position:absolute; display:none;" class="subtitle">'+el[4]+'</div>');
			jQuery(instance.options.containerel).append(div);
  			el.push(div);
			});
		
	},
	onTime:function(ev){
		if(this.activeTime > ev.position){
			this.resetSubtitles();
		}
		this.activeTime = ev.position;
		$.each(this.subtitles, function(i, el) {
			//show
			if(!el[5] && el[0] <= ev.position && el[1]>=ev.position){
				el[7].fadeIn();
				el[5] = true;
				
			}
			if((!el[6] && (el[1]) <= ev.position) ){
				el[7].fadeOut();
				el[6] = true;
			}
			});
		
		
	},
	onPause:function(ev){
		//nothing
	},
	onPlay:function(ev){
		//fake onComplete
		if(navigator.userAgent.match(/Firefox/i)) {
			window.setTimeout(this.onComplete, this.options.videolen, this.options.gotourl);
		}
		
		var isIDevice = navigator.userAgent.match(/(iphone|ipod|ipad)/ig) != null;

		if(isIDevice) {
			$('#videoid').width(918);
			$('#videoid').height(463);
			$('#skip').css('color','#000000');
		}
		
		this.resetSubtitles();
		
		
	},
	onComplete:function(option){
		var url;
		if(this.options){
			url = this.options.gotourl;
		}
		else if(option){
			url = option;
		}
		window.location.href = url;
	},
	resetSubtitles:function(ev){
		$.each(this.subtitles, function(i, el) {
			el[7].hide();
			el[5] = el[6] = false;
		});
	},
	initVideo:function(){
		var instance = this;
		
		
		jwplayer(this.options.videoel).setup({
			autostart:true,
			controlbar:'none',
			players: [{ type: "html5" }],
			stretching:'exactfit',
			events: {
				 onTime: function(ev) {
					instance.onTime(ev);
				 },
				 onAbort: function(ev) {
					instance.onComplete(ev);
				 },
				 onError: function(ev) {
					instance.onComplete(ev);
				 },
				 onPause: function(ev) {
					instance.onComplete(ev);
				 },
				 onPlay: function(ev) {
					instance.onPlay(ev);
				 },
				 onComplete: function(ev) {
					instance.onComplete(ev);
				 }
			}
		});
	}
});

