

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: 'http://www.example.com/?id=_ID_',
	demandNoVideoText: 'Der findes ingen video til denne præsentation',
	offTitle: 'Sluk',
	offText: 'Slukket',
	
	videoNoPluginText: "<a href=\"http://go.microsoft.com/fwlink/?LinkId=34682\" target=\"_blank\">Windows Media Player</a> mangler! Alternativt: <a href=\"_URL_\">åben direkte!</a>",
	
	presTitle: 'Præsentation',
	//presURL: 'slidetest2.pdf',
	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 />Alternativt, hvis du har et PDF-program, kan du: <a href=\"_URL_\">downloade PDF-filerne!</a>",
	presNoSlidesText: "Der findes ingen PDF-fil til denne præsentation",
	
	showLive: false,
	showDemand: true,
	showOff: true,
	
	tabDefault: null,
	
	headHeight: 114,
	menuVideoHeight: 210,
	menuWidth: 260,
	
	offMarginPaddingBorder: 64,
	
	// Code
	demandVideo: new Array(),
	phead: null,
	pvl:   null,
	pvd:   null,
	pvo:   null,
	plist: null,
	ppres: null,
	videoWidth: null,
	videoHeight: null,
	
	havePDF: false,
	haveWMV: false,
	
	initialize: function() {
		
		// Plugin checks
		this.havePDF = (navigator.mimeTypes.length == 0 || (navigator.mimeTypes && navigator.mimeTypes['application/pdf'] && navigator.mimeTypes['application/pdf'].enabledPlugin) ) ? true : false;
		this.haveWMV = (navigator.mimeTypes.length == 0 || (navigator.mimeTypes && navigator.mimeTypes['video/x-ms-wmv'] && navigator.mimeTypes['video/x-ms-wmv'].enabledPlugin) ) ? true : false;
		
		// Create panels
		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
			},
			center: {
				hideTabs: true,
				titlebar: true,
				autoScroll:true
			},
			south: {
				hideTabs: true
			}
			});
		
		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( this.presTitle );
		
		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);
		}
		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.haveWMV ) {
				this.pvl.setContent(this.videoElCreate(this.liveURL));
			} else if( this.liveURL && !this.haveWMV )	{
				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.haveWMV ) {
				this.pvd.setContent(this.videoElCreate(this.demandURL));
			} else if( this.demandURL && !this.haveWMV )	{
				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( )
	{
		this.pdtls.region.showPanel('list');
	},
	
	detailsShow: function( id )
	{
		if( !this.demandVideo[id] ) {
			alert("Details does not exist!");
			return;
		}
		
		$('dtls-desc').innerHTML = this.demandVideo[id].desc;
		
		if($('dtls-debate')) $('dtls-debate').firstChild.href = this.demandDebateURL.replace('_ID_', id);
		
		this.demandLoad( this.demandVideo[id].urlMedia || 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 );
		
		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) {
		data = '';
		data += '<object id="player_object" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" width="'+this.videoWidth+'" height="'+this.videoHeight+'" type="application/x-oleobject">';
		data += '<param name="url" value="'+url+'">';
		data += '<param name="autostart" value="true">';
		data += '<param name="autosize" value="true">';
		data += '<param name="stretchtofit" value="true">';
		data += '<param name="windowlessvideo" value="false">';
		data += '<param name="uimode" value="none">';
		data += '<param name="showcontrols" value="false">';
		data += '<param name="transparentstart" value="true">';
		data += '<param name="enablefullscreencontrols" value="true">';
		data += '<param name="enablecontextmenu" value="true">';
		data += '<param name="showstatusbar" value="false">';
		data += '<param name="showdisplay" value="false">';
		data += '<param name="autorewind" value="true">';
		data += '<embed id="player_embed" width="'+this.videoWidth+'" height="'+this.videoHeight+'" type="video/x-ms-wmv"';
		data += '	src="'+url+'"';
		data += '	autostart="true"';
		data += '	autosize="true"';
		data += '	stretchToFit="true"';
		data += '	windowlessVideo="false"';
		data += '	uimode="none"';
		data += '	showcontrols="false"';
		data += '	transparentstart="true"';
		data += '	enablefullscreencontrols="true"';
		data += '	enablecontextmenu="true"';
		data += '	showstatusbar="false"';
		data += '	showdisplay="false"';
		data += '	autorewind="true">';
		data += '</embed>';
		data += '</object>';
		
		return data;
	}
	
};
