/**
 * rssFeedReader.js
 * Author: Jerry Smith
 * Dependencies: base-web.js
 */
Ext.namespace("web.rss");

// need a panel that can load the appropriate rss feed into
// the content panel for the website.
// The RSS Panel consists of a tab layout.  The tabs will present
// the media inside them.  There will be "Audio", "Video" tabs because
// there shouldn't be multiple audio/video playing.  However,
// there can be multiple Article tabs.
web.rss.rssfeeds = function(){
	mainViewport.addContent({
			title: 'Rss Feeds',
			id: 'rssFeedsLinks',
			autoLoad: 'rssLinksContent.html',
			closable: true
		});
}
web.rss.NumberRenderer = function(value){
	if(value)
		return Ext.util.Format.fileSize(value);
	else
		return "";
};

web.rss.articles = function(){
	var articlesStore = new web.base.data.Store({
		url: '/northpointcoc/rss/RssArticles.coc?responseType=json',
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{root: 'rows', totalProperty: 'recordcount'},
			[   {name:'title'},
				{name:'author'},
				{name:'publisheddate'},
				{name:'description'},
				{name:'size'}
			])	
	});

	// add Articles tab
	// The articles are simply a grid of downloadable articles:
	mainViewport.addContent({
		helpId: 'articlesMenuItem',
		title: "Articles",
		xtype:'panel',
		layout: 'border',
		items:[{
			xtype: 'grid', // a grid panel
			closable: true,
			id: 'articlesGrid',
			frame: true,
			title: 'Article Selections (Click to download)',
			region: 'center',
			listeners: {
				'rowclick': {
					fn: function(grid, rowIndex, evnt){
						var rec = grid.getStore().getAt(rowIndex);
						var link;
						if(rec.json){
							link = rec.json.link;
							web.base.openWindow(link, 'Unable to open article.  Please make sure all popup blockers are turned off and try again.');
						}
					},
					scope: this,
					delay: 500
				}
			},
			columns: [
				{header: 'Title', dataIndex: 'title', width: 250, sortable: true},
				{header: 'Author', dataIndex: 'author', width: 100, sortable: true},
				{header: 'Published', dataIndex: 'publisheddate', sortable: true},
				{header: 'Size', dataIndex: 'size', sortable: true, renderer: web.rss.NumberRenderer},
				{header: 'Description', dataIndex: 'description', width: 275, hidden: true}
			],
			store: articlesStore,
			bbar: new Ext.PagingToolbar({
				pageSize: 50,
				displayInfo: true,
				store: articlesStore
			})
		}]
	}); 
	articlesStore.load();
}

web.rss.audio = function(){
	var audioStore = new web.base.data.Store({
		url: '/northpointcoc/rss/RssAudio.coc?responseType=json',
		remoteSort: true,
		reader: new Ext.data.JsonReader(
		{root: 'rows', totalProperty: 'recordcount'},
			[   {name:'title'},
				{name: 'author'},
				{name:'publisheddate'},
				{name:'size'},
				{name:'description'}
			])	
	});
	
	// add audio tab: a panel with a north panel and a center GridPanel
	// The audio tab should have a collapsable North panel in which
	// the audio player is shown collapsed on default.
	// The Center panel contains the GridPanel, where is displayed
	// the audio rss feed selections.  onClick, the item should be
	// loaded into the audio player.
	mainViewport.addContent({
		title: 'Audio Sermons',
		xtype: 'panel',
		closable: true,
		layout: 'border',
		helpId: 'audioMenuItem',		
		items: [{
			height: 300,
			id: 'audioPlayerPanel',
			layout: 'fit',
			region: 'north',
			xtype: 'panel'
		},
		{
			xtype: 'grid',
			region: 'center',
			id: 'audioGrid',
			title: 'Audio Selections (Click to listen)',
			columns: [
				{header: 'Title', dataIndex: 'title', width: 250, sortable: true},
				{header: 'Author', dataIndex: 'author', width: 100, sortable: true},
				{header: 'Published', dataIndex: 'publisheddate', sortable: true},
				{header: 'Size', dataIndex: 'size', sortable: true, renderer: web.rss.NumberRenderer},
				{header: 'Description', dataIndex: 'description', width: 275, hidden: true}
			],
			store: audioStore,
			bbar: new Ext.PagingToolbar({
				pageSize: 50,
				displayInfo: true,
				store: audioStore
			}),
			createPlaylist: function(){
				var store = this.getStore();
				var count = store.getCount(); // how many on the page
				var playlistCount = (count < 50) ? count : 50;
				
				// build the xml:
				var playListXML = "";
				for(var index = 0; index < playlistCount; index = index + 1){
					var rec = store.getAt(index);
					var creator = (rec.json.author !== "" && rec.json.author !== null && rec.json.author !== undefined) ? rec.json.author : rec.json.description;
					playListXML += "<track><location>" + rec.json.link + "</location><creator>" +  creator + "</creator><title>" + rec.json.title + "</title></track>";
				}
				
				// if the player has not been rendered, then render it with the playlist:
				var player = Ext.getCmp('audioPlayer');
				if(!player){
					var playerConfig = {
						collapsible: false,
						flashPlayer: {
							location: '/northpointcoc/audioplayer/ep_player.swf',
							height: 226,
							width: 269
						},
						id: 'audioPlayer',
						region: 'north',
						split: true,
						title: 'Audio Player',
						variables: {
							playlistxml: playListXML,
							skin: '/northpointcoc/audioplayer/skins/nobius_platinum/skin.xml'
						},
						xtype: 'audio.ephonic'
					};
				
					// add the player to the panel:
					var playerPanel = Ext.getCmp('audioPlayerPanel');
					playerPanel.add(playerConfig);
					playerPanel.doLayout();
				}
				else{
					// otherwise update with the new playlist:
					player.setPlayListXML(playListXML);
				}
			},			
			listeners: {
				'render': function(){
					this.store.addListener('load', this.createPlaylist, this);
					// this.createPlaylist();
				},
				'rowclick': {
					fn: function(grid, rowIndex, evnt){
						var rec = grid.getStore().getAt(rowIndex);
						var link;
						if(rec.json){
							link = rec.json.link;
							// play audio:
			                var player = Ext.getCmp("audioPlayer");// document.getElementById("player");
			                // var ieplayer = document.getElementById("ieplayer");
							/*
			                if(document.player)
			                    player = document.player;
							*/
							
			                if(player)
			                {
								player.playIndex(rowIndex + 1);
			                	
			                	// player.loadMP3(link, (rec.json.author) ? rec.json.author : rec.json.description, rec.json.title);
			                	// player.play();
			                	
			                    // player.SetURL(link);
			                    //player.Play();
			                }
						}
					},
					scope: this,
					delay: 500
				}
			}					
		}]
	});
	audioStore.load();
}

web.rss.video = function(){
	var videoStore = new web.base.data.Store({
		url: '/northpointcoc/rss/RssVideo.coc?responseType=json',
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{root: 'rows', totalProperty: 'recordcount'},
			[   {name:'title'},
				{name:'author'},
				{name:'publisheddate'},
				{name:'description'},
				{name:'size'}
			])	
	});
	
	mainViewport.addContent({
		helpId: 'videosermonsMenuItem',
		title: 'Video Sermons',
		xtype: 'panel',
		closable: true,
		layout: 'border',
		items: [{
			xtype: 'grid',
			region: 'center',
			id: 'videoGrid',
			height: 200,
			collapsible: true,
			autoScroll: true,
			title: 'Video Selections (Click To Download)',
			split: true,
			columns: [
				{header: 'Title', dataIndex: 'title', width: 250, sortable: true},
				{header: 'Author', dataIndex: 'author', width: 100, sortable: true},
				{header: 'Published', dataIndex: 'publisheddate', sortable: true},
				{header: 'Size', dataIndex: 'size', sortable: true, renderer: web.rss.NumberRenderer},
				{header: 'Description', dataIndex: 'description', width: 275, hidden: true}
			],
			store: videoStore,
			bbar: new Ext.PagingToolbar({
				pageSize: 50,
				store: videoStore,
				displayInfo: true
			}),
			listeners: {
				'rowclick': function(grid, rowIndex, evnt){
					
					var rec = grid.getStore().getAt(rowIndex);
					var link;
					if(rec.json){
						link = rec.json.link;
						window.open("/northpointcoc/downloadURLFile.coc?size=" + rec.json.size + "&file=" + rec.json.link, "_blank");
					}					
				}
			}			
		}]
	});
	videoStore.load();	
};

web.rss.stickamvideo = function(){
	var videoStore = new web.base.data.Store({
		url: '/northpointcoc/rss/RssArchiveVideo.coc?responseType=json',
		remoteSort: true,
		reader: new Ext.data.JsonReader(
			{root: 'rows', totalProperty: 'recordcount'},
			[   {name:'title'},
				{name:'author'},
				{name:'publisheddate'},
				{name:'description'},
				{name:'size'}
			])	
	});

	// add video tab:
	mainViewport.addContent({
		helpId: 'stickamvideosermonsMenuItem',
		title: 'Stickam Sermons',
		xtype: 'panel',
		closable: true,
		layout: 'border',
		items: [{
			xtype: 'panel',
			region: 'center',
			id: 'stickamVideoPlayerPanel',
			// html: '<embed id="player" name="player" src="http://www.northpointcoc.com/audio/SPACER_2SECONDS.wav" width="320" height="50" autoplay="true" loop="false" controller="true" pluginspage="http://www.apple.com/quicktime/" EnableJavaScript="true"></embed>',
			autoLoad: '/northpointcoc/videoPlayer.html',
			autoScroll: true,
			title: 'Stickam Video Player',
			collapsible: false
		},{
			xtype: 'grid',
			region: 'south',
			id: 'stickamVideoGrid',
			height: 200,
			collapsible: true,
			autoScroll: true,
			title: 'Stickam Video Selections (Click to watch)',
			split: true,
			columns: [
				{header: 'Title', dataIndex: 'title', width: 250, sortable: true},
				{header: 'Author', dataIndex: 'author', width: 100, sortable: true, hidden: true},
				{header: 'Published', dataIndex: 'publisheddate', sortable: true},
				{header: 'Size', dataIndex: 'size', sortable: true, renderer: web.rss.NumberRenderer, hidden: true},
				{header: 'Description', dataIndex: 'description', width: 275, hidden: true}
			],
			store: videoStore,
			bbar: new Ext.PagingToolbar({
				pageSize: 50,
				store: videoStore,
				displayInfo: true
			}),
			listeners: {
				'rowclick': {
					fn: function(grid, rowIndex, evnt){
						var createVideoPlayerLink = function(link){
							// create video player based on link:
							var urlParts = link.split("mId=");
							var playerLink = "http://player.stickam.com/flashVarMediaPlayer/" + urlParts[1];
							return playerLink;
						};
					
						var rec = grid.getStore().getAt(rowIndex);
						var link;
						if(rec.json){
							link = createVideoPlayerLink(rec.json.link);
							// play audio:
							var videoDom = document.getElementById("stickamvideo");
							videoDom.innerHTML = "<embed id=\"stickamVideoPlayer\" src=\"" + link + "\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"480\" height=\"480\" scale=\"noscale\" allowScriptAccess=\"always\" allowFullScreen=\"true\"></embed>";
		                    mainViewport.findById("stickamVideoGrid").collapse(true);
						}
					},
					scope: this,
					delay: 500
				}
			}
		}]
	});
	videoStore.load();

}
