$(document).ready(function () {
	
	$('div#navigation').corners('2px transparent');
	$('h1.orangeheader').corners('2px transparent');
	
	$.news_center.init();
	
	
	if ($('body').attr('id') == 'news_center') {
		$.news_center.list_view_init();
	};	
	
	
	
	if ($('body').attr('id') == 'news_detail') {
		// Round corners for the bottom of the feed window
		$('#feed_detail').corners('8px transparent bottom');
		
		$.news_center.detail_view_init();
	};
	
	$('#date_string').html($.news_center.get_current_date());
	
});


(function($) {
	$.news_center = {
		config: {
			
			// Feed Config
			feeds: {
				news: [
						{
						  id: "noswhitman",
						name: 'Hoofdpunten',
				      	from: 'NOS',
				       image: "url('img/rss/list_item_img_newspaper.jpg')",
				detail_image: "url('img/rss/rss_detail_img_newspaper.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=noswhitman'  // Feed url.Needs to use a proxy if its crossdomain.
						},
						{
						 id: "nunl",
						name: 'Hoofdpunten',
				      	from: 'NU.nl Algemeen Nieuws',
				       image: "url('img/rss/list_item_img_newspaper.jpg')",
				detail_image: "url('img/rss/rss_detail_img_newspaper.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=nunl' 
						},
						{
					      id: "rtl",
						name: 'Hoofdpunten',
				      	from: 'RTL Nieuws',
				       image: "url('img/rss/list_item_img_newspaper.jpg')",
				detail_image: "url('img/rss/rss_detail_img_newspaper.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=rtl' 
						},
						{
						  id: "telegraff",
						name: 'Hoofdpunten',
				      	from: 'Telegraaf.nl',
				       image: "url('img/rss/list_item_img_newspaper.jpg')",
				detail_image: "url('img/rss/rss_detail_img_newspaper.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=telegraff' 
						}
				],

				weather: [
						{
						  id: "knmi",
						name: 'Weer',
				      	from: 'KNMI',
				       image: "url('img/rss/list_item_img_sky.jpg')",
				detail_image: "url('img/rss/rss_detail_img_sky.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=knmi' 				
						},
						{
						  id: "bulenradar",
						name: 'Weer',
				      	from: 'Buienradar.nl',
				       image: "url('img/rss/list_item_img_sky.jpg')",
				detail_image: "url('img/rss/rss_detail_img_sky.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=bulenradar' 				
						},
						{
						  id: "weer",
						name: 'Weer',
				      	from: 'Weer.nl',
				       image: "url('img/rss/list_item_img_sky.jpg')",
				detail_image: "url('img/rss/rss_detail_img_sky.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=weer' 				
						},
						{
						  id: "weeronline",
						name: 'Weer',
				      	from: 'WeerOnline.nl',
				       image: "url('img/rss/list_item_img_sky.jpg')",
				detail_image: "url('img/rss/rss_detail_img_sky.jpg')",
				     h_color: "#000000",
				    sh_color: "#000000",
				       	 url: 'rss_proxy.php?feed=weeronline' 				
						}
				],

				traffic: [
						{
					      id: "fileindex",
						name: 'Verkeer',
				      	from: 'Fileindex.nl',
				       image: "url('img/rss/list_item_img_traffic.jpg')",
				detail_image: "url('img/rss/rss_detail_img_traffic.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=fileindex' 								
						},
						{
						  id: "vananaarbeter",
						name: 'Verkeer',
				      	from: 'Vananaarbeter.nl',
				       image: "url('img/rss/list_item_img_traffic.jpg')",
				detail_image: "url('img/rss/rss_detail_img_traffic.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=vananaarbeter' 								
						}
				],

				sport: [
						{
						  id: "nossport",
						name: 'Sport',
				      	from: 'NOS Sport Journaal',
				       image: "url('img/rss/list_item_img_wistle.jpg')",
				detail_image: "url('img/rss/rss_detail_img_wistle.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=nossport' 												
						},
						{
						  id: "nunlsport",
						name: 'Sport',
				      	from: 'NU.nl Sport',
				       image: "url('img/rss/list_item_img_wistle.jpg')",
				detail_image: "url('img/rss/rss_detail_img_wistle.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=nunlsport' 												
						},
						{
						  id: "niewsnlsport",
						name: 'Sport',
				      	from: 'Nieuws.nl Sport',
				       image: "url('img/rss/list_item_img_wistle.jpg')",
				detail_image: "url('img/rss/rss_detail_img_wistle.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=niewsnlsport' 												
						},
						{
						  id: "sportweek",
						name: 'Sportweek',
				      	from: 'Metro Entertainment',
				       image: "url('img/rss/list_item_img_wistle.jpg')",
				detail_image: "url('img/rss/rss_detail_img_wistle.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=sportweek' 												
						},
						{
						  id: "sportwereld",
						name: 'Sport',
				      	from: 'Sportwereld',
				       image: "url('img/rss/list_item_img_wistle.jpg')",
				detail_image: "url('img/rss/rss_detail_img_wistle.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=sportwereld' 												
						}
				],

				entertainment: [
						{
						  id: "telegraafprive",
						name: 'Vrije tijd',
				      	from: 'Telegraaf Prive',
				       image: "url('img/rss/list_item_img_croud.jpg')",
				detail_image: "url('img/rss/rss_detail_img_crowd.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=telegraafprive' 												
						},
						{
						  id: "rtlboulevard",
						name: 'Vrije tijd',
				      	from: 'RTL Boulevard',
				       image: "url('img/rss/list_item_img_croud.jpg')",
				detail_image: "url('img/rss/rss_detail_img_crowd.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=rtlboulevard' 												
						},
						{
						  id: "msnentertainment",
						name: 'Vrije tijd',
				      	from: 'MSN Entertainment',
				       image: "url('img/rss/list_item_img_croud.jpg')",
				detail_image: "url('img/rss/rss_detail_img_crowd.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=msnentertainment' 												
						},
						{
						  id: "metroentertainment",
						name: 'Vrije tijd',
				      	from: 'Metro Entertainment',
				       image: "url('img/rss/list_item_img_croud.jpg')",
				detail_image: "url('img/rss/rss_detail_img_crowd.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=metroentertainment' 												
						}
				],

				auto: [
						{
						  id: "autoweek",
						name: 'Auto/Motor',
				      	from: 'Autoweek.nl',
				       image: "url('img/rss/list_item_img_car.jpg')",
				detail_image: "url('img/rss/rss_detail_img_car.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=autoweek' 																
						},
						{
						  id: "autovandaag",
						name: 'Auto/Motor',
				      	from: 'Autovandaag.nl',
				       image: "url('img/rss/list_item_img_car.jpg')",
				detail_image: "url('img/rss/rss_detail_img_car.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=autovandaag' 																
						},
						{
						  id: "nunlauto",
						name: 'Auto/Motor',
				      	from: 'NU.nl Auto',
				       image: "url('img/rss/list_item_img_car.jpg')",
				detail_image: "url('img/rss/rss_detail_img_car.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=nunlauto' 																
						},
						{
						  id: "autowereld",
						name: 'Auto/Motor',
				      	from: 'Autowereld.com',
				       image: "url('img/rss/list_item_img_car.jpg')",
				detail_image: "url('img/rss/rss_detail_img_car.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=autowereld' 																
						}
				]

			},
			// Nr of news por channel in list view
			list_view_nr_news: 3,
			// html element to clone for list view
			list_view_channel_template: '.feedlist_item',
			// Nr of news to show in detail view
			detail_view_nr_news: 6,
			// Container for the feed news in detail view
			detail_view_feed_container: '#feed_detail_container',
			
			// Text labels used
			LABELS: {
				enablecookiesupport: '<p>Het toevoegen van RSS feeds maakt gebruik van cookies. Zorg er voor de uw computer instellingen cookies accepteren van deze site.</p>',
				config_panel_choose_news: 'Kies uw nieuws',
				config_panel_build_your: 'Voeg uw eigen RSS feeds toe',
				config_panel_build_txt: 'Kopieer hieronder de URL van uw persoonlijke RSS feed',
				config_panel_change_your: "Verander 'Mijn Kanaal'-feed",
				config_panel_change_txt: "Om uw kanaal te veranderen, kopieert u hieronder de URL van de gewenste RSS",
				config_panel_change_txt_news: "Om uw hoofdpunten te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_change_txt_weather: "Om uw weernieuws te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_change_txt_traffic: "Om uw verkeersnieuws te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_change_txt_sport: "Om uw sportnieuws te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_change_txt_entertainment: "Om uw vrijetijdsnieuws te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_change_txt_auto: "Om uw Auto/Motornieuws te veranderen selecteert u een andere feed via het drop-downmenu",
				config_panel_blocked_feed: 'De door u gekozen URL is geblokkeerd. Probeer een andere URL',
				config_panel_invalid_feed: 'De URL die u gekozen heeft is geen geldige RSS feed. Probeer opnieuw.',
				detail_view_feed_item_laoding: '<h4><span class="ajax-loading"><img src="img/rss/ajax-loader.gif" width="32" height="32" /></span></h4><p></p><p class="date"></p>'
			}
		},
		
		_user_preferences: {},
		
		init: function() {
			var nc = this;

			// Load user preferences
			this.load_user_preferences();
			
			// Bind event to create custom channel bt
			$('#custom_channel_bt').click( function (event) {
				$.post('custom_channel.php', { custom: $('#custom_channel').val() }, function (data) {
					if (data == 'true' || $('#custom_channel').val() == '') {
						
						// Reload the page with the new mychannel
						window.location.reload();
						
					} else {
						
						// set error message to display
						var error_msg = 
							(data == 'blocked') ?
								nc.config.LABELS.config_panel_blocked_feed :
								nc.config.LABELS.config_panel_invalid_feed;
							
						// Show invalid feed msg
						$('#custom_channel_error_msg').remove();
						$('<p></p>')
							.attr('id','custom_channel_error_msg')
							.html(error_msg)
							.insertAfter('#config_your_channel p:first')
							.css('backgroundColor', '#FF3213')
							.animate({'backgroundColor': '#FFBEB5'}, 'slow');
					};
				});
			});
			
			// Hide MyChannel link if custom channel not set
			if (!this.config.feeds.mychannel) {
				$('#mychannel_link').hide();
			};
			
			
			if ($.cookie('news_center_mychannels_names')) {
				this.init_mychannels_list();
			};
			
			
		},
		
		init_mychannels_list: function () {
			// Custom feeds list and behavior
			var nc = this;
			
			$('#config_your_channel').append('<div id="mychannels_list"><h5>Your Feeds</h5><span class="right">Remove</span></div><br style="clear:both />"');
			
			var my_channels = unescape($.cookie('news_center_mychannels_names'));
			my_channels = my_channels.replace(/\+/g,' ');
			my_channels = my_channels.split(';');
			$.each(my_channels, function(i, val) {
				$('#mychannels_list').append('<div class="custom_mychannel"><a class="custom_mychannel_link" href="'+ i +'">'+ val +'</a> <a class="custom_mychannel_remove" href="'+ i +'">-</a></div>');
			});
			$('.custom_mychannel_link').click(function (event) {
				event.preventDefault();
				var custom_mychannel_index = $(this).attr('href');
				$.post('custom_channel.php', { select: custom_mychannel_index }, function (data) {
					// window.location.reload();

					if($('#feed_detail').length > 0) {
						nc.update_channel_detail('mychannel');						
					} else {
						nc.update_channel('mychannel', nc.config.feeds.mychannel[0]);						
					}
					
				});
			});
			$('.custom_mychannel_remove').click(function (event) {
				event.preventDefault();
				var custom_mychannel_index = $(this).attr('href');
				$.post('custom_channel.php', { remove: custom_mychannel_index }, function (data) {
					window.location.reload();
				});
			});
		},
		
		// Use cookies to get the user preferences
		load_user_preferences: function () {
			var nc = this;
			
			
			// Load channel feed selection from cookies
			$.each(this.config.feeds, function(key, value) {
				nc._user_preferences[key] = $.cookie('news_center_'+ key) || 0;
			});
			
			// If mychannel is set, add it to the feeds
			if ($.cookie('news_center_mychannels')) {
				
				// We have to make a temp object so that the added 
				// mychannel feed goes to the top of the list.
				var temp_feeds = {
					mychannel: [
						{
						  id: "mychannel",
						name: 'Mijn Kanaal',
				      	from: '',
				       image: "url('img/rss/list_item_img_antena.jpg')",
				detail_image: "url('img/rss/rss_detail_img_antena.jpg')",
				     h_color: "#ffffff",
				    sh_color: "#ffffff",
				       	 url: 'rss_proxy.php?feed=mychannel'
						}
					]
				};
				
				$.extend(temp_feeds, nc.config.feeds );
				nc.config.feeds = temp_feeds;
				// nc.config.feeds.mychannel = [
				// 	{
				// 	  id: "mychannel",
				// 	name: 'MyChannel',
				// 			      	from: '',
				// 			       image: "url('img/rss/list_item_img_newspaper.jpg')",
				// 			       	 url: 'rss_proxy.php?feed=mychannel'
				// 	}
				// ];
				nc._user_preferences['mychannel'] = 0;
			};
		},
		
		// Populate the list view
		list_view_init: function () {
			var nc = this;
			
			
			// Populate Config panel
			this._update_feed_config_panel('list');
			
			
			// Get template to clone and remove it
			var channel_template = $(this.config.list_view_channel_template).eq(0);
			channel_template.remove();



			var current_item;
			$.each(this.config.feeds, function (channel_name, channel_feeds) {
				
				// Feed to use in the channel
				var feed_config = channel_feeds[nc._user_preferences[channel_name]];

				// Clone the template add a proper id and append it to the list container
				current_item = $(channel_template)
					.clone()
					.attr('id','channel_'+ channel_name)
					.appendTo('#centralContentArea');

				// Change the image for the channel
				$('.feedlist_item_inner', current_item).css('background-image', feed_config.image);

				// Change the title
				$('h3', current_item)
					.html(feed_config.name)
					.append(' <span>van '+ feed_config.from +'</span>');
				
				// Populate the Channel with news	
				nc.update_channel(channel_name, feed_config);
				
				// Bind reload event
				$('a.reload_bt', current_item).click(function (event) {
					event.preventDefault();
					$('#channel_'+ channel_name +' ul').html('<li><span>--:--</span><img src="img/rss/ajax-loader-small.gif" width="16" height="16" /></li>');
					nc.update_channel(channel_name, feed_config);
				});
				$('a.more_bt', current_item).attr('href', 'news-detail.html#'+ channel_name);
			});
		},
		
		// Update channel
		update_channel: function(channel_name, feed_config) {
			var nc = this;

			var channel_container = $('#channel_'+ channel_name);

			$.getFeed({
				url: feed_config.url,
				success: function(feed) {

					var html_string, current_li;
					for (var i=0; i < nc.config.list_view_nr_news; i++) {
						// If no more news stop
						if( feed.items[i] == undefined ) { break; } 
						
						// If no row for the news item yet, create one
						if ($('li',channel_container).length < i+1) {
							$('li:first', channel_container).clone().appendTo($('ul', channel_container));
						};
						
						// Choose correspondent row
						current_li = $('li', channel_container).eq(i);
						
						// Populate row with news
						current_li.html($('<a></a>')
							.attr('target','_blank')
							.attr('href', feed.items[i].link)
							.html(nc._truncate(feed.items[i].title, 40)));
							
						// Add time
						current_li.prepend($('<span></span>')
							.attr('title', feed.items[i].updated)
							.html(nc._parse_time(feed.items[i].updated)));
						
						// Set news tooltip
						current_li.attr('title', feed.items[i].title);						
					};
					
					// In the case of mychannel also update the "from" field
					if (channel_name == 'mychannel') {
						$('#channel_mychannel h3 span')
							.html('van '+ nc._truncate(feed.title, 30))
							.attr('title', feed.title);
					};
				}
			});
		},
		
		detail_view_init: function() {
			var nc = this;
						
			// Bind events to the left side links
			$('#leftContentArea li a').click(function (event) {
				event.preventDefault();
				window.location.href= $(this).attr('href');
				nc.update_channel_detail(nc._get_channel_name());
				nc._update_feed_config_panel(nc._get_channel_name());
			});

			var channel_name = this._get_channel_name();
			
			this.update_channel_detail(channel_name);

			// Bind reload event
			$('#feed_detail a.reload_bt').click(function (event) {
				event.preventDefault();
				nc.update_channel_detail(nc._get_channel_name());
			});

			this._update_feed_config_panel(channel_name);

		},
		
		update_channel_detail: function(channel_name) {
			var nc = this;
			
			nc._update_highlighted_link(channel_name);
	
			// Feed to use in channel
			var feed_config = this.config.feeds[channel_name][this._user_preferences[channel_name]];
			
			// Populate header
			$('#feed_detail h3')
				.css('color', feed_config.h_color)
				.html(feed_config.name);
			$('#feed_detail .subtitle')
				.css('color', feed_config.sh_color)
				.html('Van '+ feed_config.from);
			$('#feed_detail_header').css('backgroundImage', feed_config.detail_image);
			$('#feed_detail_header .reload_bt').css('color', feed_config.h_color);
			
			var feed_container = $(this.config.detail_view_feed_container);

			// Clear all feeds
			$('.feed_item:first ~ .feed_item', feed_container).remove();
			$('.feed_item', feed_container).html(this.config.LABELS.detail_view_feed_item_laoding);
			

			$.getFeed({
				url: feed_config.url,
				success: function(feed) {
					
					var html_string, current_news;
					for (var i=0; i < nc.config.detail_view_nr_news; i++) {
						
						// If no more new in feed, stop.
						if (feed.items[i] == undefined) { break; };
						
						// If no row for the news item create one
						if ($('.feed_item', feed_container).eq(i).length == 0) {
							$('.feed_item:first', feed_container).clone().appendTo(feed_container);
						};
						
						current_news = $('.feed_item', feed_container).eq(i);
						
						// Populate new item row
						$('h4', current_news)
							.html(feed.items[i].title)
							.wrapInner('<a href="'+ feed.items[i].link +'" target="_blank"></a>');		
						$('p:first', current_news).html(nc._sanitize_feed(feed.items[i].description));
						$('p.date', current_news).html(nc._get_short_date(feed.items[i].updated));
					};
					
					// In the case of mychannel also update the "from" field
					if (channel_name == 'mychannel') {
						$('#feed_detail_header p.subtitle')
							.html('Van '+ nc._truncate(feed.title, 30))
							.attr('title', feed.title);
					};
				}
			});
			
		},
		
		_update_feed_config_panel: function(channel_name) {
			var nc = this;
			
			switch(channel_name) {
				
				// Default list view
				case 'list':
					
					// Change config panel title and text
					$('#rightContentArea h1 span').html(this.config.LABELS.config_panel_build_your);
					$('#rightContentArea #config_txt').html(this.config.LABELS.config_panel_build_txt);
					
					// If we have a mychannel continue down and change the config panel
					if(!nc.config.feeds.mychannel) {
						break;
					}
					
					
				// MyChannel detail. Option to change the custom feed
				case 'mychannel':
					$('#rightContentArea h1 span').html(this.config.LABELS.config_panel_change_your);
					$('#rightContentArea #config_txt').html(this.config.LABELS.config_panel_change_txt);
					$('#config_your_channel').show();
					$('#feed_config_panel').hide();
					
					$('#custom_channel').val($.cookie('news_center_mychannel'));
					break;
				
				// News Detail with selected channel
				default:
				
					// Hide mychannel config section and show pulldown
					$('#config_your_channel').hide();
					$('#feed_config_panel').show();
				
					// Change config panel title
					$('#rightContentArea h1 span').html(this.config.LABELS.config_panel_choose_news);
					$('#rightContentArea #feed_config_txt').html(this.config.LABELS['config_panel_change_txt_'+ channel_name]);
			
					// Populate feed selection pulldown
					$('#feed_select').empty();
					$.each(this.config.feeds[channel_name], function(option_index) {
						$('#feed_select').append('<option value="'+ option_index +'">'+ this.from +'</option>');
					});
					// Select current user preference
					$('#feed_select').val(this._user_preferences[channel_name]);
			
			}
			
			// Bind feed select pulldown event
			$('#feed_select').unbind('change');
			$('#feed_select').change(function (event) {

				// Set user preference in cookie
				$.cookie('news_center_'+ channel_name, this.value, { expires: 30 });
		
				// Test if cookie did get set. If not probably cookie support is disabled
				// show cokkie enable message
				if( $.cookie('news_center_'+ channel_name) != this.value) {
					$('#feed_config_panel').html(nc.config.LABELS.enablecookiesupport);
					return false;
				}
				
				// Load user preferences
				nc.load_user_preferences();
				
				nc.update_channel_detail(channel_name);
			});
		},
		
		
		get_current_date: function() {
			var d = new Date();
			return this._getFormatedDate(d);
		},
		
		_get_short_date: function(date_string) {
			var d = new Date(date_string);
			
			// If can't parse date just send the original string
			if (isNaN(d.getDate())) {
				return date_string;
			};
			
			return this._getFormatedDate(d, true);
		},
		
		_getFormatedDate: function(d, abr) {
			var d_names = new Array("Zondag", "Maandag", "Dinsdag",
				"Woensdag", "Donderdag", "Vrijdag", "Zaterdag");
			var d_names_abr = new Array("Zon", "Ma", "Di",
				"Wo", "Do", "Vr", "Za");

			var m_names = new Array("januari", "februari", "maart", 
				"april", "mei", "juni", "juli", "augustus", "september", 
				"oktober", "november", "december");
			var m_names_abr = new Array("Jan", "Feb", "Mrt", 
				"Apr", "Mei", "Jun", "Jul", "Aug", "Sep", 
				"Okt", "Nov", "Dec");

			
			var curr_day = d.getDay();
			var curr_date = d.getDate();
			var curr_month = d.getMonth();

			var date_string;
			if (abr) {
				date_string = d_names_abr[curr_day] + " " + curr_date + " " + m_names_abr[curr_month] +" "+ this._parse_time(d);
			} else {
				var date_string = d_names[curr_day] + ", " + curr_date + " " + m_names[curr_month] +" "+ d.getFullYear();				
			};
			
			return date_string;
		},
		
		_get_channel_name: function() {
			// Get requested channel name from url anchor
			var channel_name = window.location.hash.substr(1);

			// If no channel_selected or invalid use default
			if (!this.config.feeds[channel_name]) {
				channel_name = 'news';
			}
			
			return channel_name;
		},
		
		_update_highlighted_link: function(channel_name) {
			$('#leftContentArea li a').removeClass('currentsidenav');
			$("#leftContentArea li a[href='news-detail.html#"+ channel_name +"']").addClass('currentsidenav');
		},
		
		_truncate: function(text, max_len) {
			if (text.length > max_len) {
				text = text.substring(0, max_len);
				text = text.replace(/\w+$/, '');
				text += '...';
			};
			return text;
		},
		
		_sanitize_feed: function(text) {
			return $('<div></div>').html(text).text();
		},
		
		_parse_time: function(time_string) {
			// var time = Date.parse(time_string);
			var time = new Date(Date.parse(time_string));
			if(isNaN(time)) {
				return '--:--';
			}
			
			return this._make_double_digit(time.getHours()) +':'+ this._make_double_digit(time.getMinutes());
		},
		
		_make_double_digit: function(time) {
			return (time < 10) ? '0'+ time : time;
		}
	};
})(jQuery);


