/**
 * HTML5 and Flash Video Playback Application
 *
 * @author			chris 
 * @copyright (c) 	Yellowspace
 * @date      		25.9.2010
 * @version   		$Id$
 *
 * @license video.js is licensed under the terms of the Open Source
 * LGPL 3.0 license. Commercial use is permitted to the extent that the 
 * code/component(s) do NOT become part of another Open Source or Commercially
 * licensed development library or toolkit without explicit permission.
 * 
 * License details: http://www.gnu.org/licenses/lgpl.html
 *
 * keep in mind: this is under contruction
 
 Articles
 	http://diveintohtml5.org/video.html
 	http://wiki.whatwg.org/wiki/Video_type_parameters
 	http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#video
 	
 Last Changes 
 
 Flash Video Playback needs
	<script type="text/javascript" src="js/video/flowplayer/flowplayer-3.2.2.min.js"></script>
	<script type="text/javascript" src="js/video/video.js"></script> 
 
 Example (ORDER IS IMPORTANT: iphone_video, ipad_video, mp4_video, flash_video)
	<script type="text/javascript">
	var defaults = [{
			ikey:'iphone_video',
			src:'YOUR VIDEO SRC', 
			width:480,
			height:270,
			type: 'video/mp4;', 
			id:'flowmovie',
			poster_img:{src:'YOUR SPLASH SCREEN SRC',width:480,height:270}
		},{
			ikey:'mp4_video',
			src:'YOUR VIDEO SRC', 
			width:480,
			height:270,
			type: 'video/mp4;', // with codec: 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
			id:'flowmovie',
			flowplay_context:'class="flowmovie" id="flowmovie"',
			close_button:true,
			poster_img:{src:'YOUR SPLASH SCREEN SRC',width:480,height:270}
		},{
			ikey:'flash_video',
			src:'YOUR VIDEO SRC', 
			width:480,
			height:270,
			type: 'video/flash;',
			id:'flowmovie',
			flowplayer_context:'class="flowmovie"',
			loadPlayer:false, // or use your own conf
			poster_img:{src:'YOUR SPLASH SCREEN SRC',width:480,height:270}
		}];
	
	var tpl = TV.loadVideoPlayer(defaults);
	document.write(tpl);
	</script>
 */


function VideoPlayback() {
	
	this.flashplayer = { // use your own conf (best way for lots of videos!) or overwirte these settings...
		id:'flowplayer',
		path: 'js/video/flowplayer/flowplayer-3.1.5.swf',
		settings: {
			//key: f_key,
			clip:  { 
				autoPlay: true, 
				autoBuffering: true,
				title:''
			},
			onLoad: function(){
				this.setVolume(100);
			},
			canvas: {
				backgroundColor: "#000000",
				backgroundGradient: 'none'
			},
			onStart: function(){
				//alert("player loaded");
			},	
			//log:{level:'debug'},
			plugins: {
				controls: {
					tooltipColor: '#3d3d3d',
					backgroundGradient: 'none',
					bufferGradient: 'none',
					timeBgColor: '#3d3d3d',
					sliderGradient: 'none',
					borderRadius: '0px',
					buttonColor: '#000000',
					backgroundColor: '#3d3d3d',
					buttonOverColor: '#3d3d3d',
					progressColor: '#3d3d3d',
					progressGradient: 'medium',
					volumeSliderColor: '#000000',
					timeColor: '#cbcbcb',
					tooltipTextColor: '#ffffff',
					volumeSliderGradient: 'none',
					bufferColor: '#cbcbcb',
					durationColor: '#cbcbcb',
					sliderColor: '#000000',
					height: 24,
					opacity: 1.0,
					stop:true
				}
			}			
		}
	};
	
	this.close_button = {
		src:'/js/video/_img/close.png',
		wr_style:'position:relative;',
		style:'float:right;cursor:pointer;display:none;right:-10px;top:-10px;position:absolute;z-index:2;'
	};
	this.html5_controls = 'preload controls'; //preload="none"
	this.verbose = false;
	
	//---------------------------------------------------------------------------------------
	
	this.loadVideoPlayer=function(p) {
		
		// start with iphone, ipad, mp4, flash
	
		if(this.verbose == true && typeof console == 'object') console.log('x start '+p.length+' player '+ navigator.userAgent+': %o',p);
		if(!p) return 'no conf defined...';
		var tpl = '';
		var playableVideo = false;
		var html5Playback = false;
		var matchingtype = false;
		var vd = {};
		
		for(i=0;i<p.length;i++) { //vformats in p
			
			var vformat = p[i].ikey;
			vd[vformat] = p[i]; // conversion to object to get a better handle
						
			var playable = this.SupportsHTML5VideoFormat(p[i]['type']);
			
			if(this.verbose == true && typeof console == 'object') console.log('x video '+vformat+' playable? '+ playable +': %o',p[i]);
			
			// if we got an iphone and an iphone version is defined: used it
			if(
				((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
				&& (vformat == 'iphone_video')
				&& (playable)
			) {
				var playableVideo = p[i];
				var html5Playback = true;	
				var matchingtype = playable;
				break;
			} 
			// same on ipad...
			else if(
				(navigator.userAgent.match(/iPad/i))
				&& (vformat == 'ipad_video')
				&& (playable)
			) {
				var playableVideo = p[i];
				var html5Playback = true;	
				var matchingtype = playable;
				break;				
			}	
			
			// find the best matching version
			if(playable == 'probably') {
				var playableVideo = p[i];
				var html5Playback = true;
				var matchingtype = playable;
			} else if(playable == 'maybe') {
				if(matchingtype == 'probably') continue; // we found allready a better matching... 
				var playableVideo = p[i];
				var html5Playback = true;
				var matchingtype = playable;
			}
			
		}
		
		if(this.verbose == true && typeof console == 'object') console.log('x html5Playback? '+ html5Playback+' with '+playableVideo.ikey+': %o',playableVideo);
		
		// flash fallback
		if(html5Playback == false) {
			if(vd.flash_video) var playableVideo = vd.flash_video; // play flash
			else if(vd.mp4_video) var playableVideo = vd.mp4_video; // play mp4 through flash
		}
		
		if(html5Playback) {
			// || Ext.isGecko3 --> needs different format
			
			if(playableVideo) {
				
				var video_style = '';
				if(playableVideo.poster_img) {
					tpl += '<img style="cursor:pointer;" src="'+playableVideo.poster_img.src+'" ';
					if(playableVideo.poster_img.width) tpl += 'width="'+playableVideo.poster_img.width+'" ';
					if(playableVideo.poster_img.height) tpl += 'width="'+playableVideo.poster_img.height+'" ';
					tpl += 'id="'+playableVideo.id+'_splash" ';
					tpl += 'onclick="TV.playHTML5Video(\''+playableVideo.id+'\');" />';
					var video_style = 'style="display:none;" '
				}

				if(playableVideo.close_button) {
					if(typeof playableVideo.close_button == 'object') {
						if(playableVideo.close_button.src) this.close_button.src = playableVideo.close_button.src;
						if(playableVideo.close_button.wr_style) this.close_button.wr_style = playableVideo.close_button.wr_style;
						if(playableVideo.close_button.style) this.close_button.style = playableVideo.close_button.style;
					}
					tpl += '<div class="close-video-wr" '; 
					tpl += 'style="width:'+playableVideo.width+'px;'+this.close_button.wr_style+'">';
					tpl += '<img src="'+this.close_button.src+'" id="'+playableVideo.id+'_closeimg" ';
					tpl += 'style="'+this.close_button.style+'" ';
					tpl += 'onclick="TV.controlHTML5Video(\''+playableVideo.id+'\',\'stop\');" class="close-video" />';
					tpl += '</div>';					
				}
				
				tpl += '<video onerror="TV.failed(event)" '+video_style;
				if(playableVideo.poster_img) tpl += 'poster="'+playableVideo.poster_img.src+'" ';
				tpl += 'id="'+playableVideo.id+'" ';
				tpl += 'width="'+playableVideo.width+'" height="'+playableVideo.height+'"';
				if(playableVideo.html5_controls) this.html5_controls = playableVideo.html5_controls;
				tpl += this.html5_controls+'>';
				tpl += '<source ';
				tpl += 'src="'+playableVideo.src+'" type=\''+playableVideo.type+'\' />';
				tpl += '</video>';
			}
		} else { // flash
			
			tpl += '<a '+playableVideo.flowplayer_context+' href="'+playableVideo.src+'" id="'+playableVideo.id+'"';
			tpl += 'style="display:block;width:'+playableVideo.width+'px;height:'+playableVideo.height+'px;">';
			if(playableVideo.poster_img) {
				//tpl += '<div style="overflow:hidden;width:'+playableVideo.width+'px;height:'+playableVideo.height+'px;">';
				tpl += '<img src="'+playableVideo.poster_img.src+'" ';
				if(playableVideo.poster_img.width) tpl += 'width="'+playableVideo.poster_img.width+'" ';
				if(playableVideo.poster_img.height) tpl += 'height="'+playableVideo.poster_img.height+'"  />'
				//tpl += '</div>';
			}
			tpl += '</a>';			
			if(playableVideo.loadPlayer) {
				OnLoadFunctions[OnLoadFunctions.length] = "TV.setFlashPlayerConf({id:'"+playableVideo.id+"'});";
			}
		}
				
		return tpl;
	}
	
	//---------------------------------------------------------------------------------------
	
	this.videos = [];
	this.loadOverlayVideoPlayer = function(p) {

		if(this.verbose == true && typeof console == 'object') console.log('x start '+p.length+' player '+ navigator.userAgent+': %o',p);
		if(!p) return 'no conf defined...';
		var tpl = '';
		var playableVideo = false;
		var html5Playback = false;
		var matchingtype = false;
		//var vd = {};
		
		var pv = this.getPlayableVideo(p);
		
		if(this.verbose == true && typeof console == 'object') console.log('x  FOUND  pv?: %o',pv);
		
		var playableVideo = pv.playableVideo;
		var html5Playback = pv.html5Playback;
		var matchingtype = pv.matchingtype;
		this.videos[playableVideo.id] = pv;
		
		if(html5Playback) {
			// || Ext.isGecko3 --> needs different format
			
			if(playableVideo) {
				
				var video_style = '';
				var video_style = 'style="display:none;"';
				var ov_w = playableVideo.width;
				var ov_h = playableVideo.height;
				var ov_style = '';
				if(playableVideo.player_bg) {
					var ov_w = playableVideo.player_bg.width;
					var ov_h = playableVideo.player_bg.height;
					var ov_style = 'background-image:url('+playableVideo.player_bg.img+');padding-top:'+((playableVideo.player_bg.height-playableVideo.height)/2)+'px;padding-left:'+((playableVideo.player_bg.width-playableVideo.width)/2)+'px;background-repeat:no-repeat;';
				}
				tpl += '<div id="'+playableVideo.id+'_overlay" class="vp-window" style="position:absolute;display:none;z-index:10000;width:'+ov_w+'px;height:'+ov_h+'px;'+ov_style+'">';
				
				
				tpl += '<div class="videocontainer" style="position:relative;">';
				if(playableVideo.poster_img) {
					var img_display = '';
					if(this.autoplay) var img_display = 'display:none;';
					tpl += '<img style="cursor:pointer;'+img_display+'" src="'+playableVideo.poster_img.src+'" ';
					if(playableVideo.poster_img.width) tpl += 'width="'+playableVideo.poster_img.width+'" ';
					if(playableVideo.poster_img.height) tpl += 'width="'+playableVideo.poster_img.height+'" ';
					tpl += 'id="'+playableVideo.id+'_splash" ';
					tpl += 'onclick="TV.playHTML5Video(\''+playableVideo.id+'\');" />';
					if(!this.autoplay) var video_style = 'style="display:none;" '
				}

				if(playableVideo.close_button) {
					this.close_button = {
						src:'/images/songpier/video/close_button_sp.png',
						//wr_style:'position:relative;',
						style:'margin:0;cursor:pointer;display:none;right:20px;top:-25px;position:absolute;z-index:10001;'
					};
					if(typeof playableVideo.close_button == 'object') {
						if(playableVideo.close_button.src) this.close_button.src = playableVideo.close_button.src;
						if(playableVideo.close_button.wr_style) this.close_button.wr_style = playableVideo.close_button.wr_style;
						if(playableVideo.close_button.style) this.close_button.style = playableVideo.close_button.style;
					}
					tpl += '<div class="close-video-wr" '; 
					tpl += 'style="width:'+playableVideo.width+'px;'+this.close_button.wr_style+'">';
					tpl += '<img src="'+this.close_button.src+'" id="'+playableVideo.id+'_closeimg" ';
					tpl += 'style="'+this.close_button.style+'" ';
					if(playableVideo.modal) tpl += 'onclick="TV.hideOverlay(event,\''+playableVideo.id+'\',true);" class="close-video" />';
					else tpl += 'onclick="TV.controlHTML5Video(\''+playableVideo.id+'\',\'stop\');" class="close-video" />';
					tpl += '</div>';					
				}
				
				tpl += '<video onerror="TV.failed(event)" '+video_style;
				if(playableVideo.poster_img /*&& (!playableVideo.autoplay)*/) tpl += 'poster="'+playableVideo.poster_img.src+'" ';
				tpl += 'id="'+playableVideo.id+'" ';
				tpl += 'width="'+playableVideo.width+'" height="'+playableVideo.height+'"';
				if(playableVideo.html5_controls) this.html5_controls = playableVideo.html5_controls;
				tpl += this.html5_controls+'>';
				tpl += '<source ';
				tpl += 'src="'+playableVideo.src+'" type=\''+playableVideo.type+'\' />';
				tpl += '</video>';
				tpl += '</div>';
				if($(playableVideo.id+'_mvif')) tpl += '<div class="videohead">'+$(playableVideo.id+'_mvif').innerHTML+'</div>';
				tpl += '</div>';
			}
		} else { // flash
			
			//(playableVideo.player_bg)
			var ov_w = playableVideo.width;
			var ov_h = playableVideo.height;
			var ov_style = '';
			if(playableVideo.player_bg) {
				var ov_w = playableVideo.player_bg.width;
				var ov_h = playableVideo.player_bg.height;
				var ov_style = 'background-image:url('+playableVideo.player_bg.img+');padding-top:'+((playableVideo.player_bg.height-playableVideo.height)/2)+'px;padding-left:'+((playableVideo.player_bg.width-playableVideo.width)/2)+'px;background-repeat:no-repeat;';
			}
			tpl += '<div id="'+playableVideo.id+'_overlay" class="vp-window" style="position:absolute;display:none;z-index:10000;width:'+ov_w+'px;height:'+ov_h+'px;'+ov_style+'">';
			
			tpl += '<a '+playableVideo.flowplayer_context+' href="'+playableVideo.src+'" id="'+playableVideo.id+'"';
			tpl += 'style="display:block;width:'+playableVideo.width+'px;height:'+playableVideo.height+'px;">';
			if((playableVideo.poster_img) /*&& (!playableVideo.autoplay)*/) {
				tpl += '<img src="'+playableVideo.poster_img.src+'" ';
				if(playableVideo.poster_img.width) tpl += 'width="'+playableVideo.poster_img.width+'" ';
				if(playableVideo.poster_img.height) tpl += 'height="'+playableVideo.poster_img.height+'"  />'
			}
			tpl += '</a>';			
			if(playableVideo.loadPlayer) {
				OnLoadFunctions[OnLoadFunctions.length] = "TV.setFlashPlayerConf({id:'"+playableVideo.id+"'});";
			}
			if($(playableVideo.id+'_mvif')) tpl += '<div class="videohead">'+$(playableVideo.id+'_mvif').innerHTML+'</div>';
			tpl += '</div>';	
		}
				
		return tpl;
	}

	this.autoplay=true;
	this.playOverlayMovie = function(id) {
		
		var playableVideo = this.videos[id].playableVideo;
		
		//TV.hideOverlay(event,\''+playableVideo.id+'\',true);
		
		if(playableVideo.ikey == 'iphone_video') {
			var overlay = Ext.get(id+'_overlay');
			overlay.setStyle('display','block');
			overlay.setStyle('top','-10055px');
			this.playHTML5Video(id);
		} else {
			// position
			var overlay = Ext.get(id+'_overlay');
			var st = this.docScrollTop();//Ext.getBody().dom.scrollTop;
			var b = {};
			b.width = Ext.getBody().getWidth();
			b.height = (typeof window.innerHeight == 'undefined') ? document.documentElement.clientHeight : window.innerHeight;//Ext.getBody().getHeight();
			if(playableVideo.player_bg) {
				var h = playableVideo.height;
				var w = playableVideo.width;
			} else {
				var h = playableVideo.player_bg.height;
				var w = playableVideo.player_bg.width;				
			}
			var x = (b.width/2) - (w/2);
			var y = (st+(b.height/2)) - (h/2);		
			y = y -60;
			if(y<0) y=10;

			// modal
			if(playableVideo.modal) {
				this.coverbackground(true,playableVideo.modal);
				this.addEvent(this.cover,'click',this.hideme = function(event) {TV.hideOverlay(event,playableVideo.id); });
			}
			
			// play
			if(this.autoplay || playableVideo.autoplay) {
				if(playableVideo.ikey == 'flash_video') this.playFlashVideo(id);
				else {
					this.playHTML5Video(id);
					Ext.get(playableVideo.id).addListener('ended',TV.autohideOverlayHTML5,this,{video_id:id});
				}
			}
			overlay.setStyle('display','');
			overlay.setXY([x,y]);		
		}

	}
	
	//---------------------------------------------------------------------------------------
	this.autohideOverlayHTML5 = function(e,el,o) {
		this.hideOverlay(e,o.video_id,true);
	}
	//---------------------------------------------------------------------------------------
	this.hideOverlay = function(event,id,force) {
		
		if(!force) {
			var el;
			if(event) {
				el = this.gettarget(event);
				if (el == $(id+'_overlay')) return;
				if (el == $(id+'')) return;
			}
			el = el.parentNode;
			while(el) {
				if(el.parentNode
				&& el.parentNode.id
				&& el.parentNode.id == (id+'_overlay')) return;
				el = el.parentNode;
			}
		}
		
		var overlay = Ext.get(id+'_overlay');
		if(overlay) overlay.setStyle('display','none');
		this.coverbackground(false);
		this.controlHTML5Video(id,'stop');
		this.removeEvent(this.cover,'click',this.hideme);
	}	
	
	this.sau_Effect_rmNode = function(obj) {
		PAGEdom.removeNode(obj.element);
		IF.magnifier_is_visible = false;
	}
	

	this.coverbackground = function(vs,p) {

		if(typeof p != 'object') p = {};
		if(!p.bg_color) p.bg_color = "#ffffff";
		if(!p.op) p.op = 1;
		
		if(!this.cover) {
			this.cover = document.createElement('div');
			this.cover.style.position = 'absolute';
			this.cover.style.zIndex = 5;
			this.cover.style.left = 0+'px';
			this.cover.style.top = 0+'px';
			this.cover.style.opacity = p.op; // set opacity for effect : filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5; 
			this.cover.style.filter = "alpha(opacity:"+Math.round(p.op*100)+")";
			this.cover.style.backgroundColor = p.bg_color;
			document.getElementsByTagName('body')[0].appendChild(this.cover);
		}
		
		if(!vs) {
			this.cover.style.visibility = 'hidden';
		} else {
			var ww = Ext.getBody().getWidth();//PAGEdom.getWindowWidth();
			var wh = Ext.getBody().getHeight();//PAGEdom.getWindowHeight();
			var st = Ext.getBody().getScroll();//PAGEdom.docScrollTop();
			bodyheight = st.top+wh;
			bodyheight = document.body.offsetHeight;
			if(wh > bodyheight) bodyheight = wh;
			this.cover.style.width = '100%';//(ww-15)+'px';
			this.cover.style.height = ((bodyheight))+'px';
			this.cover.style.visibility =  'visible';			
		}		
	}	
	
	
	
	this.getPlayableVideo = function(p) {
	
		// start with iphone, ipad, mp4, flash
	
		if(!p) return 'no conf defined...';
		//var tpl = '';
		var playableVideo = false;
		var html5Playback = false;
		var matchingtype = false;
		var vd = {};
		
		for(i=0;i<p.length;i++) { //vformats in p
			
			var vformat = p[i].ikey;
			vd[vformat] = p[i]; // conversion to object to get a better handle
						
			var playable = this.SupportsHTML5VideoFormat(p[i]['type']);
			
			// if we got an iphone and an iphone version is defined: used it
			if(
				((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
				&& (vformat == 'iphone_video')
				&& (playable)
			) {
				var playableVideo = p[i];
				var html5Playback = true;	
				var matchingtype = playable;
				break;
			} 
			// same on ipad...
			else if(
				(navigator.userAgent.match(/iPad/i))
				&& (vformat == 'ipad_video')
				&& (playable)
			) {
				var playableVideo = p[i];
				var html5Playback = true;	
				var matchingtype = playable;
				break;				
			}	
			
			// find the best matching version
			if(playable == 'probably') {
				var playableVideo = p[i];
				var html5Playback = true;
				var matchingtype = playable;
			} else if(playable == 'maybe') {
				if(matchingtype == 'probably') continue; // we found allready a better matching... 
				var playableVideo = p[i];
				var html5Playback = true;
				var matchingtype = playable;
			}
			
		}
		
		if(this.verbose == true && typeof console == 'object') console.log('x html5Playback? '+ html5Playback+' with '+playableVideo.ikey+': %o',playableVideo);
		
		// flash fallback
		if(html5Playback == false) {
			if(vd.flash_video) var playableVideo = vd.flash_video; // play flash
			else if(vd.mp4_video) var playableVideo = vd.mp4_video; // play mp4 through flash
		}
		
		//if(this.verbose == true && typeof console == 'object') console.log('x html5Playback? '+ html5Playback+' with '+playableVideo.ikey+': %o',playableVideo);
		
		return {
			playableVideo:playableVideo,
			html5Playback:html5Playback,
			matchingtype:playable
		};
		
	}

	this.controlHTML5Video = function(id,c) {
				
		switch(c) {
			case'close':
			case'stop':
				if(typeof $(id).pause == 'function') $(id).pause();
				if($(id+'_splash')) $(id+'_splash').style.display='block';
				if($(id+'_closeimg')) {
					$(id+'_closeimg').style.display='none';
				}
				$(id).style.display='none';
				break;
			case'pause':
				if(typeof $(id).pause == 'function') $(id).pause();
		}	
	}

	this.playFlashVideo = function(id) {
		//if($(id+'_splash')) $(id+'_splash').style.display='none';
		$(id).style.display='block';	
	}
	
	this.playHTML5Video = function(id) {
		if(typeof SP.crsl == 'object') SP.crsl.pause(); // stop carousel
		if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) { 
			$(id).style.position='absolute';
			$(id).style.left='-5000px';
			$(id).style.top='-5000px';
		} else {
			if($(id+'_splash')) $(id+'_splash').style.display='none';
			if($(id+'_closeimg')) {
				$(id+'_closeimg').style.display='block';
			}
			Ext.get(id).addListener('ended',TV.autohideHTML5Video,this,{video_id:id});
		}
		$(id).style.display='block';
		$(id).play();
	}
	
	this.pauseHTML5HeadPlayer = function(e,id) {
		if(typeof $(id).pause == 'function') $(id).pause();
	}

	this.SupportsHTML5Video=function() {
	  return !!document.createElement('video').canPlayType;
	}

	this.SupportsHTML5VideoFormat=function(format) {
		if (!this.SupportsHTML5Video()) { return false; }
		var v = document.createElement("video");
		if(v) return v.canPlayType(format);
		else return false
	}

	this.setFlashPlayerConf = function(params) {
		if(document.domain.indexOf('yellowspace')) {}; // develop?
		if(this.flashplayer.id == 'flowplayer') var player = flowplayer(params.id, this.flashplayer.path,this.flashplayer.settings);
	}
	
	//---------------------------------------------------------------------------------------
	
	this.stopVideos = function() {
		
		if(typeof flowplayer == 'function') {
			flowplayer("*").each(function() {
				if(this.isPlaying()) this.pause();
			});	
		}
		
		var videos = document.getElementsByTagName('video');
		if(typeof videos == 'object' || typeof videos == 'function') {
			for(vidx=0;vidx<videos.length;vidx++) {
				if(typeof videos[vidx].pause == 'function') this.controlHTML5Video(videos[vidx].id,'stop'); //videos[vidx].pause();				
			}
		}
	}
	
	//---------------------------------------------------------------------------------------
	this.failed = function(e) {
	   // video playback failed - show a message saying why
		var message = 'Unknown error';
		switch (e.target.error.code) {
			case e.target.error.MEDIA_ERR_ABORTED:
				var message = 'You aborted the video playback.';
				break;
			case e.target.error.MEDIA_ERR_NETWORK:
				var message = 'A network error caused the video download to fail part-way.';
				break;
			case e.target.error.MEDIA_ERR_DECODE:
				var message = 'The video playback was aborted due to a corruption problem or because the video used features your browser did not support.';
				break;
			case e.target.error.MEDIA_ERR_SRC_NOT_SUPPORTED:
				var message = 'The video could not be loaded, either because the server or network failed or because the format is not supported.';
				break;
			default:
				var message = 'An unknown error occurred.';
				break;
		}
	   
	    if(typeof console == 'object') console.log('x '+ message+': %o',e);
	   
	 }

	//---------------------------------------------------------------------------------------
	this.preloadImages = function(images){
        var preload = Ext.get(document.body).createChild({tag:"div", style:"display:none"});
        if(images && images.length > 0) {
			Ext.each(images, function(image){
				if (typeof(image)=='string'){
				 image={src:image}
				}
								
				if (image.src){
					preload.createChild({tag:"img", src:image.src});
				}
				
				if (image.fullSrc){
					preload.createChild({tag:"img", src:image.fullSrc});
				}
			}, this);
        } else if(typeof images == 'object') {
        	for(img_sid in images) {
  				if (images[img_sid].src){
					preload.createChild({tag:"img", src:images[img_sid].src});
				}      		
        	} 
        }
    }
	
	//---------------------------------------------------------------------------------------
    this.docScrollTop = function() {
      if ( document.documentElement && document.documentElement.scrollTop ) {
         return document.documentElement.scrollTop;
      } else if ( document.body ) {
         //alert(document.body.scrollTop);
        return document.body.scrollTop;
	} else if ( window.pageYOffset ) {
         return window.pageYOffset;
     } else {
         return 0;
   		}
   }    
    //---------------------------------------------------------------------------------------
    
	this.gettarget = function(event) {

		if(window
		&& (window.event)
		&& (window.event.srcElement)) {
			t = window.event.srcElement;
		} else {
			t = (event.target.tagName ? event.target : event.target.parentNode);
		}
		return t;
	}	
	

	//---------------------------------------------------------------------------------------
	this.addEvent = function(el,evname,func) {
	
		if (el.attachEvent) { // IE
			el.attachEvent("on" + evname, func);
		} else if (el.addEventListener) { // Gecko / W3C
			el.addEventListener(evname, func, true);
		} else {
			el["on" + evname] = func;
		}
	
	}
	
	//---------------------------------------------------------------------------------------
	this.removeEvent = function(el,evname,func) {
	
		if (el.detachEvent) { // IE
			el.detachEvent("on" + evname, func);
		} else if (el.removeEventListener) { // Gecko / W3C
			el.removeEventListener(evname, func, true);
		} else {
			el["on" + evname] = null;
		}
	
	}        
}


TV = new VideoPlayback();


function loadFlowConf() {
	flowplayer("a.flowmovie", '/js/video/flowplayer/flowplayer-3.1.5.swf', {
	key: false,
	  clip:  { 
		  //scaling: 'fit',
		  autoPlay: true, 
		  autoBuffering: true,
		  play: null
		},
    	//log:{level:'debug'},
	  canvas: {
		  backgroundColor: "#1a1a1a",
		  backgroundGradient: 'none'
		  },
		onLoad: function(clip,h){
			if(typeof SP.crsl == 'object') SP.crsl.pause(); // stop carousel
		},	
		onStart: function(clip,h){},
		onFinish: function(clip,h) { 
			TV.hideOverlay(false,this.getParent().id,true);
		},  
		onStop: function(clip,h) { 
			TV.hideOverlay(false,this.getParent().id,true);
		},	
	  plugins: {
		/*controls: {
		  tooltipColor: '#5F747C',
		  backgroundGradient: 'none',
		  bufferGradient: 'none',
		  timeBgColor: '#555555',
		  sliderGradient: 'none',
		  borderRadius: '0px',
		  buttonColor: '#d8030a',
		  backgroundColor: '#222222',
		  buttonOverColor: '#050505',
		  progressColor: '#eb0003',
		  progressGradient: 'medium',
		  volumeSliderColor: '#000000',
		  timeColor: '#0b0a0a',
		  tooltipTextColor: '#ffffff',
		  volumeSliderGradient: 'none',
		  bufferColor: '#000000',
		  durationColor: '#de020a',
		  sliderColor: '#000000',
		  height: 24,
		  opacity: 1.0,
		  stop:true
		}*/
	  }
	});
	
}

