

function debug(obj) { 
    var result = "";
    for (var i in obj){
    	result += i + " = " + obj[i] + "\n";
    }
    return result;
} 

var webtv = {
	
	// Config
	
	liveTitle: 'Live',
	liveURL: null,
	liveText: 'Ingen Live',
	demandTitle: 'Præsentation',
	demandURL: null,
	demandText: 'Vælg en præsentation',
	demandDebateURL: false, // http://www.example.com/?id=_ID_
	demandNoVideoText: 'Der findes ingen video til denne præsentation',
	offTitle: 'Sluk',
	offText: 'Video slukket',
	
	videoNoPluginText: "<a href=\"http://www.adobe.com/go/getflashplayer\" target=\"_blank\">Flash Player</a> 9.0.115 eller nyere mangler!",
	
	presTitle: 'Præsentation',
	presURL: null,
	presText: 'Vælg en præsentation',
	presNoPluginText: "Der kræves <a href=\"http://www.adobe.com/products/acrobat/readstep2.html\" target=\"_blank\">Adobe Acrobat</a> for at kunne vises PDF-filer i browseren.<br /><br /><br /><a href=\"_URL_\">Download præsentation</a>",
	presNoSlidesText: "Der findes ingen PDF til denne præsentation",
	
	showLive: false,
	showDemand: true,
	showOff: true,
	
	tabDefault: null,
	
	headHeight: 114,
	menuVideoHeight: 314+2,
	menuWidth: 360+2,
	
	offMarginPaddingBorder: 64,
	
	// Code
	demandVideo: new Array(),
	phead: null,
	pvl: null,
	pvd: null,
	pvo: null,
	plist: null,
	ppres: null,
	videoWidth: null,
	videoHeight: null,
	
	havePDF: false,
	haveVideo: false,
	is_gecko: false,
	is_win: false,
	videoHideTabs: false,
	
	initialize: function() {
		
		if( this.demandDebateURL ) {
			$('showDebate').show();
		}
		
		
		// Init
		if( !this.showLive && this.showDemand ) {
			this.videoHideTabs = true;
		}
		
		// Plugin checks
		this.havePDF = (navigator.mimeTypes.length == 0 || (navigator.mimeTypes && navigator.mimeTypes['application/pdf'] && navigator.mimeTypes['application/pdf'].enabledPlugin) ) ? true : false;
		this.haveVideo = flashembed.isSupported([9, 115]);
		
		// Browser define
		var agt = navigator.userAgent.toLowerCase();
		this.is_gecko = (agt.indexOf('gecko') != -1);
		this.is_win = ( (agt.indexOf("win") != -1) || (agt.indexOf("16bit") != -1) );
		
		// Create panels
		if( this.videoHideTabs ) {
			this.menuVideoHeight = this.menuVideoHeight - 24;
		}
		this.phead = new Ext.ContentPanel('head', {closable: false});
		this.ppwrd = new Ext.ContentPanel('powered', {closable: false});
		if( this.showLive ) this.pvl   = new Ext.ContentPanel('video-live', {title: this.liveTitle, closable: false, background: true});
		if( this.showDemand ) this.pvd = new Ext.ContentPanel('video-demand', {title: this.demandTitle, closable: false, background: true});
		if( this.showOff ) this.pvo   = new Ext.ContentPanel('video-off', {title: this.offTitle, closable: false, background: true});
		this.plist = new Ext.ContentPanel('list', {closable: false, background: true});
		this.pdtls = new Ext.ContentPanel('dtls', {closable: false, background: true});
		this.ppres = new Ext.ContentPanel('pres', {title: this.presTitle, closable: false});
		
		// Menu Layout
		menuLayout = new Ext.BorderLayout('menu', {
			north: {
				initialSize: this.menuVideoHeight,
				tabPosition: "top",
				alwaysShowTabs: true,
				hideTabs: this.videoHideTabs
			},
			center: {
				hideTabs: true,
				titlebar: false,
				autoScroll:true
			},
			south: {
				hideTabs: true,
				initialSize: 30
			}
			});
		
		if(this.pvl) menuLayout.add('north', this.pvl);
		if(this.pvd) menuLayout.add('north', this.pvd);
		if(this.pvo) menuLayout.add('north', this.pvo);
		
		menuLayout.add('center', this.plist);
		menuLayout.add('center', this.pdtls);
		menuLayout.getRegion('center').showPanel('list');
		
		menuLayout.add('south', this.ppwrd);
		
		// Main Layout
		layout = new Ext.BorderLayout('container', {
			hideOnLayout: false,
			north: {
				initialSize: this.headHeight
			},
			east: {
				initialSize: this.menuWidth
			},
			center: {
				titlebar: true
			}
			});
			
		layout.beginUpdate();
		layout.add('north', this.phead);
		layout.add('center', this.ppres);
		layout.add('east', new Ext.NestedLayoutPanel(menuLayout));
		layout.endUpdate();
		
		// Calc video size
		if( this.pvl ) {
			this.videoWidth = parseInt(this.pvl.el.dom.parentNode.parentNode.parentNode.style.width);
			this.videoHeight = parseInt(this.pvl.el.dom.parentNode.style.height)-1;
		} else if( this.pvd ) {
			this.videoWidth = parseInt(this.pvd.el.dom.parentNode.parentNode.parentNode.style.width);
			this.videoHeight = parseInt(this.pvd.el.dom.parentNode.style.height)-1;
		} else if( this.pvo ) {
			this.videoWidth = parseInt(this.pvo.el.dom.parentNode.parentNode.parentNode.style.width);
			this.videoHeight = parseInt(this.pvo.el.dom.parentNode.style.height)-1;
		}
		
		// Make video tabs eventable
		if(this.pvl) this.pvl.on("activate", this.videoChange.bind(this, 'pvl'));
		if(this.pvd) this.pvd.on("activate", this.videoChange.bind(this, 'pvd'));
		if(this.pvo) this.pvo.on("activate", this.videoChange.bind(this, 'pvo'));
		
		// Determine default tab
		if( !this.tabDefault && this.showLive ) this.tabDefault = "live";
		if( !this.tabDefault && this.showDemand ) this.tabDefault = "demand";
		if( !this.tabDefault && this.showOff ) this.tabDefault = "off";
		
		// Select default tab
		menuLayout.getRegion('north').showPanel('video-'+this.tabDefault);
		
		// Load presentation
		this.presUpdate( );
		
		// Window Resize
		Event.observe( window, "resize", this.presUpdate.bind(this), false );
	},
	
	presUpdate: function( resize )
	{
		if( resize && this.presURL ) return;
		
		if( this.presTitle ) this.ppres.setTitle( "<big><b>"+this.presTitle+"</b></big>" );
		
		if( this.presURL && this.havePDF )
		{
			this.ppres.setContent('<iframe src="'+this.presURL+'" scrolling="off" width="100%" FRAMEBORDER="0" name="IFRAMEname" id="IFRAMEname" height="100%"></iframe>');
			return;
		}
		else if( this.presURL && !this.havePDF )
		{
			text = this.presNoPluginText.replace('_URL_', this.presURL);
			text = text.replace('_NAME_', this.presTitle);
		}
		else
		{
			text = this.presText;
		}
		
		this.ppres.setContent(
			this.frameCreate( ( text || "" ),
				parseInt(this.ppres.el.dom.parentNode.parentNode.style.width)-this.offMarginPaddingBorder,
				parseInt(this.ppres.el.dom.parentNode.style.height)-this.offMarginPaddingBorder
				) );
	},
	
	demandLoad: function( url, text )
	{
		this.demandURL = url;
		this.demandText = text;
		
		// Got video, load it!
		if( this.demandURL )
		{
			this.videoChange("pvd");
			this.pvd.region.showPanel("video-demand");
			return;
		}
		
		// No video, but showing live, keep live!
		if( this.pvl && this.pvl.active ) return;
		if( this.pvo && this.pvo.active ) return;

		// No video, and no live, show message
		this.videoChange("pvd");
		this.pvd.region.showPanel("video-demand");
	},
	
	videoChange: function(vp)
	{
		if( vp == "pvl" )
		{
			if( !this.pvl ) alert("Demand not activated");
			
			if( this.pvd ) this.pvd.setContent("");
			
			if( this.liveURL && this.haveVideo ) {
				this.pvl.setContent("<div id='pvlvideo' style='width: 360px;height: 288px;'></div>");
				this.videoElCreate(this.liveURL, true, 'pvlvideo');
			} else if( this.liveURL )	{
				this.pvl.setContent(this.frameCreate(this.videoNoPluginText.replace('_URL_',this.liveURL), this.menuWidth-this.offMarginPaddingBorder, this.videoHeight-this.offMarginPaddingBorder));
			} else {
				this.pvl.setContent(this.frameCreate(this.liveText, this.menuWidth-this.offMarginPaddingBorder, this.videoHeight-this.offMarginPaddingBorder));
			}
		}
		
		if( vp == "pvd" )
		{
			if( !this.pvd ) alert("Demand not activated");
			
			if( this.pvl ) this.pvl.setContent("");
	
			if( this.demandURL && this.haveVideo ) {
				//this.pvd.setContent(this.videoElCreate(this.demandURL));
				this.pvd.setContent("<div id='pvdvideo' style='width: 360px;height: 288px;'></div>");
				this.videoElCreate(this.demandURL, false, 'pvdvideo');
			} else if( this.demandURL )	{
				this.pvd.setContent(this.frameCreate(this.videoNoPluginText.replace('_URL_',this.demandURL), this.menuWidth-this.offMarginPaddingBorder, this.videoHeight-this.offMarginPaddingBorder));
			} else {
				this.pvd.setContent(this.frameCreate(this.demandText, this.menuWidth-this.offMarginPaddingBorder, this.videoHeight-this.offMarginPaddingBorder));
			}
		}
		
		if( vp == "pvo" )
		{
			if( !this.pvo ) alert("Off not activated");
			
			if(this.pvl) this.pvl.setContent("");
			if(this.pvd) this.pvd.setContent("");
			this.pvo.setContent(this.frameCreate(this.offText, this.menuWidth-this.offMarginPaddingBorder, this.videoHeight-this.offMarginPaddingBorder));
		}
	},
	
	detailsHide: function( )
	{
		$('dtls-options').hide();
		this.pdtls.region.showPanel('list');
	},
	
	detailsShow: function( id )
	{
		if( !this.demandVideo[id] ) {
			alert("Details does not exist!");
			return;
		}
		
		$('dtls-main').innerHTML = this.demandVideo[id].desc;
		$('dtls-extra').innerHTML = this.demandVideo[id].extra;
		
		if(this.demandDebateURL && $('dtls-debate-a')) $('dtls-debate-a').href = this.demandDebateURL.replace('_ID_', id);
		
		this.demandLoad( this.demandVideo[id].urlVideo || null, this.demandNoVideoText );
		
		this.presTitle = this.demandVideo[id].title;
		this.presURL = this.demandVideo[id].urlSlide || null;
		this.presText = this.presNoSlidesText;
		this.presUpdate();
		
		//this.pdtls.setTitle( this.demandVideo[id].title );
		
		$('dtls-options').show();
		this.plist.region.showPanel('dtls');
	},
	
	frameCreate: function(text, width, height) {
		return '<div class="frame"><div class="frame-inner" style="height: '+height+'; width: '+width+';">'+text+'</div></div>';
	},
	
	videoElCreate: function(url, live, obj) {
		config = {
			key: '#@0cff7371877a2828a3e', 
			clip: {
				autoPlay: true,
				url: url,
				scaling: 'orig',
				accelerated: true,
				live: live
			},
			plugins: { 
				controls: { 
					autoHide: 'always',
					backgroundColor: '#313137',
					progressColor: '#00424e',
					bufferColor: '#bddfdf',
					timeColor: '#FFFFFF',
					durationColor: '#FFFFFF',
					buttonColor: '#27565e',
					stop: true
				}
			},
			play: {
				replayLabel: 'Afspil igen'
			},
			canvas: {
				backgroundColor: '#000000', 
				backgroundGradient: 'none'
			},
			onLoad: function() { 
				this.setVolume(100); 
			}
		}
		if(live) {
			urlsplit = url.split("/");
			config.clip.url = urlsplit.pop();
			config.plugins.ntartmp = {
				url: 'flowplayer.rtmp-3.0.2.swf',
				netConnectionUrl: urlsplit.join("/")
			}
			config.clip.provider = 'ntartmp';
		} else {
			config.clip.url = this.videoconnect_prefix+url;
			config.plugins.ntacdn = {
				url: 'flowplayer.rtmp-3.0.2.swf',
				netConnectionUrl: this.videoconnect
			}
			config.clip.provider = 'ntacdn';
		}
		
		flowplayer(obj, this.libprefix+"/flowplayer-3/flowplayer.commercial-3.0.7.swf", config).load();
	}
	
};
