app.videoviewer = {

	init: function(){		
		this.makePlayer()
		this.unwatched = new tc.widget.pageControl(app.unwatched_shell, this.clickLecture);
		if (app.watched_shell) {
			this.watched = new tc.widget.pageControl(app.watched_shell, this.clickLecture);
			
		}
		
		if (app.big) {
			app.big.event('click', function(e){
				app.videoviewer.clickBig(e);
			});
		}
		
	},
	
	linkAccess: 1,
	
	clickLecture: function(e){
		var targ = sb.events.target(e);
		
		if (app.videoviewer.linkAccess == 0) {
			app.warn('\n\n Please click the alert message in the video player window before selecting a new video.');
		}
		else {
			targ = (targ.nodeName == 'A') ? targ : targ.parentNode;
			
			if (targ.nodeName == 'A') {
				app.videoviewer.getDetails(targ.href.match(/\d*$/), targ);
			}
		}
	},
	
	clickBig: function(e){
		var targ = sb.events.target(e);
		
		if(targ.nodeName == 'BUTTON'){
			switch(targ.id){
				
				case 'cancel':
					this.hideBig();
				break;
				
				case 'agree':					
					this.startVideo();		
				break;
			}			
		}
		
	},
	
	getDetails : function(lid, targ){ 
		
		this.ajax = new sb.ajax({ 
			data : 'c=video_valid&lid=' + lid,
			handler : function(data){ 
			
				//sb.objects.alert(data);
				
				if (data) {
				
					app.details.innerHTML = data.details;

					if (data.inprogress == 0 && data.loggedin == 1) {
						app.videoviewer.videoplayer.stopVideo();
						if (app.big && data.complete == 0) {
							app.videoviewer.goBig(data.notice);
							app.videoviewer.vidIdHold = lid;
						}else{
							app.videoviewer.setSelected(targ);
							app.videoviewer.videoplayer.setVideoId(lid);
						}
					}else{
						
						app.videoviewer.setSelected(targ);
						app.videoviewer.vidIdHold = -1;
						app.videoviewer.videoplayer.setVideoId(lid);
					}
				}else{
					//error no data retured
				}
			}, 
			format : 'json'
		}).fetch();
		
	},
	
	startVideo : function(){
		
		var targ = $('#')
		if(this.vidIdHold != -1){
			this.ajax = new sb.ajax({
				data : 'c=video_start&lid=' + this.vidIdHold,
				handler : function(data){
					
					var that = 	app.videoviewer;
					that.hideBig();	
					that.videoplayer.setVideoId(that.vidIdHold);
					that.vidIdHold = -1;
				}
			}).fetch();		
			
		}else{
			//error
		}	
	},
	
	vidIdHold : -1,
	
	goBig : function(sMessage){
		
		app.big_gutz.innerHTML = sMessage;	
		app.big.styles({
			display: 'block',
			height : (app.appcontent.offsetHeight + 20) +'px'
		});		
		window.scroll(0,0);
	},
	
	hideBig : function(){
		app.big.css('display', 'none');
	},
	
	makePlayer : function(){ 
		//debug: add a random number to the swf request 
		//remove before prod
		var debug = ((typeof sb.browser.$_GET['debug'] != 'undefined'))?true:false;	
		var end = (debug)?'?r=' + sb.math.rand():'';
		
		this.swf = new sb.swf({
			src : '../media/' + app.swfname + end,
			bgColor : '#000000',
			wmode : 'transparent',
			allowFullScreen : 'true',
			width : 320,
			height : 300,
			version : 9,
			id : 'swify',
			
			alt : '<div>You need at least flashplayer 8 to play view lecture videos</div>'
		});
		
		this.swf.embed('#videoplayer');
		this.videoplayer =  this.swf.getInterface();
		
		if(debug){
			alert('loading');
			app.videoviewer.videoplayer.setVideoId(512);			
		}
	},
	
	printVideo : function(path){ 
		new sb.swf({
				src : '../media/' + app.swfname + '?video=' + path,
				bgColor : '#FFFFFF',
				wmode : 'transparent',
				width : 320,
				height : 285,
				version : 8,
				id : 'flvpreview',
				alt : '<div>You need at least flashplayer 8 to play the swf</div>'
			}).embed(app.videoplayer);
	},
	
	selected : null, 
	
	setSelected : function(oList){ 
		this.selected = oList;
		var list = this.unwatched.list.$('a')
		if(this.watched){
			list = list.nodes.concat(this.watched.list.$('a').nodes);
		}
		
		list.forEach(function(v){
			v.className = v.className.replace(/selected/i, '');						
		});
		
		this.selected.className += ' selected';
		this.selected.blur();	
	},
	
	videoBegun : function(info){ 
		this.watched.turnToPage(1, 1);
		alert(info);
	},
	
	videoDone : function(info){ 			
		if(app.videoviewer.watched){
			app.videoviewer.watched.turnToPage(1,1);	
		}
		
		var title = $("#lid_" + info).$(".name").nodes[0].innerHTML;
		//title = "Title of Record";
		
		var msg = "Congratulations! You've just earned a new credit!  Click ok to view your certificate for watching: '" + title + "' (lecture#" + info + ").  You can view this an all of your credit certificates under My Account";
		if(confirm(msg)){
			window.open('/app/cert.php?lid=' + info, 'certificate_' + info, 
			'height=800px,width=820px,toolbar=0,status=0,menubar=0,top=0,left=0resizable=1,scrollbars=1');
		}
				
	}
};


/*-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_*/

var videoDone = function(data){	
	app.videoviewer.videoDone(data);
}

var videoBegun = function(data){
	app.videoviewer.videoBegun(data);
}

var toggleLinkAccess = function(flip){ 
	app.videoviewer.linkAccess = flip;
};