/*
 * Feed reader example
 *
 * Copyright (c) 2009 Luis Montero
 * BSD Revised License
 *
 * Date: 2009-10-17
 */

jQuery(document).ready(function($)  {
    // Hook to click event for ajax-feed-trigger links
   // $('a.ajax-feed-trigger').click(function(e) {
        // Prevent default click action
      //  e.preventDefault();
        
        // Store reference to container object in local var
        var container = $('#NewsFeed');
        
        // Empty container div and temporarily add "loading" style
        container.empty().addClass('loading');

        // Get links href attribute
       // var href  = $(this).attr('href');
		
		var href = 'http://www.environmentalharmony.ca/blog/?feed=rss';
        
        // Get feed using jGFeed
        $.jGFeed(
            href,
            function(feeds) {
                // Check for errors
                if (!feeds) {
                    // there was an error
                    container.append('Error fetching feed.');
                    return false;
                }

                container.hide();
                container.removeClass('loading');
                 

                // Process feed entries
				//$total = feeds.entries.length;
                for (var i=0; i<5; i++) {
                    var entry = feeds.entries[i];
                    //console.log(entry);
                   
                    // Build HTML string for entry
					var html = '';
					 
					if(i == 4)
					{ 
					html += '<li class="NewsItem last">'; 
					}	
					else { 
					html += '<li class="NewsItem">';
					}				
                    
					
					/* Truncate the content of the P, then go back to the end of the
       				previous word to ensure that we don't truncate in the middle of
      				 a word */
					var my_title = entry.title;
 					if(my_title.length > 40)
					{ 
					my_title = my_title.substring(0,38); 
					my_title = my_title.replace(/\w+$/, '');
					my_title +=  "..."; 
					}
					
                    html += '<a class="sm" href="'+entry.link+'">';
                    html += my_title + '</a>';
                    //html += '<div class="ajax-feed-date">';
                    //html += entry.publishedDate + '</div>';
                    //html += '<div class="ajax-feed-author"> Posted by ';
                    //html += entry.author + '</div>';
                    //html += '<div class="ajax-feed-content-snippet">';
                    //html +=  entry.contentSnippet + '</div>';
                    //html += '<div id="ajax-feed-content-'+i;
                    //html += '" class="ajax-feed-content" ';
                    //html += 'style="height:0px; overflow:hidden;">';
                    //html +=  entry.content + '</div>';
                    //html += '<div><a class="ajax-feed-readmore" ';
                    //html += 'href="' + i + '">read more</a></div>';
                    html += '</li>';
					html += '<div class="clear"></div>';

                    container.append(html);
                }
				
				 
				

                // Hook to click event for ajax-feed-trigger links
                $('a.ajax-feed-readmore').click(function(e) {
                    // Prevent default click action
                    e.preventDefault();

                    var content_id  = $(this).attr('href');
                    var div_id      = 'ajax-feed-content-' + content_id;
                    var content_div = $('#' + div_id);
                    var content_txt = content_div.html();
                    var snippet_div = content_div.prev();
                    var snippet_txt = snippet_div.html();

                    // Swap text content between divs
                    content_div.html(snippet_txt);
                    snippet_div.html(content_txt);
                    
                    if ($(this).html() === 'hide') {
                        $(this).html('read more');
                    } else {
                        $(this).html('hide');
                    }
                });

                container.show('slow');
            },
           100
        ); // End jGFeed
		
		
  //  }); // End a.ajax-feed-trigger click event
});

