/* jFeed : jQuery feed parser plugin
 * Copyright (C) 2007 Jean-François Hovinne - http://www.hovinne.com/
 * Dual licensed under the MIT (MIT-license.txt)
 * and GPL (GPL-license.txt) licenses.
 */

jQuery.getFeed = function(options) {

    options = jQuery.extend({
    
        url: null,
        data: null,
        success: null
        
    }, options);

    if(options.url) {

        $.ajax({
            type: 'GET',
            url: options.url,
            data: options.data,
            dataType: 'xml',
            success: function(xml) {
                var feed = new JFeed(xml);
                if(jQuery.isFunction(options.success)) options.success(feed);
            }
        });
    }
};

function JFeed(xml) {
    if(xml) this.parse(xml);
};

JFeed.prototype = {

    type: '',
    version: '',
    title: '',
    link: '',
    description: '',
    parse: function(xml) {
        
        if(jQuery('channel', xml).length == 1) {
        
            this.type = 'rss';
            var feedClass = new JRss(xml);

        } else if(jQuery('feed', xml).length == 1) {
        
            this.type = 'atom';
            var feedClass = new JAtom(xml);
        }
        
        if(feedClass) jQuery.extend(this, feedClass);
    }
};

function JFeedItem() {};

JFeedItem.prototype = {

    title: '',
    link: '',
    description: '',
	published: '',
    updated: '',
	author: '',
	authorName: '',
	authorURI: '',
	authorEmail: '',
    id: '',
	dcDate: '',
	dateToText: '',
	replyCount: '',
	objectType	: ''
};

function JAtom(xml) {
    this._parse(xml);
};

JAtom.prototype = {
    
    _parse: function(xml) {
    
        var channel = jQuery('feed', xml).eq(0);

        this.version = '1.0';
        this.title = jQuery(channel).find('title:first').text();
        this.link = jQuery(channel).find('link:first').attr('href');
        this.description = jQuery(channel).find('subtitle:first').text();
        this.language = jQuery(channel).attr('xml:lang');
        this.updated = jQuery(channel).find('updated:first').text();
        
        this.items = new Array();
        
        var feed = this;
        
        jQuery('entry', xml).each( function() {
        
            var item = populateItem(this);
            
            feed.items.push(item);
        });
    }
};

function JRss(xml) {
    this._parse(xml);
};

JRss.prototype  = {
    
    _parse: function(xml) {
    
        if(jQuery('rss', xml).length == 0) this.version = '1.0';
        else this.version = jQuery('rss', xml).eq(0).attr('version');

        var channel = jQuery('channel', xml).eq(0);
    
        this.title = jQuery(channel).find('title:first').text();
        this.link = jQuery(channel).find('link:first').text();
        this.description = jQuery(channel).find('description:first').text();
        this.language = jQuery(channel).find('language:first').text();
        this.updated = jQuery(channel).find('lastBuildDate:first').text();
    
        this.items = new Array();
        
        var feed = this;
        
        jQuery('item', xml).each( function() {
        
            var item = populateItem(this);
            
            feed.items.push(item);
        });
    }
};

populateItem = function(obj){
	var item = new JFeedItem();
	var isIE = $.browser.msie;
	
	for (var i = 0 ; i < obj.childNodes.length ; i++){
		switch (obj.childNodes[i].nodeName){
			case "title":
				item.title = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "link":
				item.link = obj.childNodes[i].text;
				if (item.link == null || item.link.length == 0){
					for (var j = 0 ; j < obj.childNodes[i].attributes.length ; j++){
						switch (obj.childNodes[i].attributes[j].nodeName){
							case "href":
								item.link = obj.childNodes[i].attributes[j].value;
								break;
						}
					}					
				}
				break;
			case "summary":
			case "description":
				item.description = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "published":
				item.published = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "updated":
			case "pubDate":
				item.updated = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "author":
				if (obj.childNodes[i].childNodes != null){
					for (var j = 0 ; j < obj.childNodes[i].childNodes.length ; j++){
						switch (obj.childNodes[i].childNodes[j].nodeName){
							case "name":
								item.authorName = isIE ? obj.childNodes[i].childNodes[j].text : obj.childNodes[i].childNodes[j].textContent;
								break;
							case "uri":
								item.authorURI = isIE ? obj.childNodes[i].childNodes[j].text : obj.childNodes[i].childNodes[j].textContent;
								break;
							case "email":
								item.authorEmail = isIE ? obj.childNodes[i].childNodes[j].text : obj.childNodes[i].childNodes[j].textContent;
								break;
						}
					}
				}
				else{
					item.author = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				}
				break;
			case "guid":
				item.id = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "dc:date":
				item.dcDate = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "clearspace:dateToText":
				item.dateToText = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "clearspace:replyCount":
				item.replyCount = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
			case "clearspace:objectType":
				item.objectType = isIE ? obj.childNodes[i].text : obj.childNodes[i].textContent;
				break;
		}
	}	
	
	return item;
}

displayRSS = function(sender, URI, num) {
	if (num == null)
		num = 10;
	
    $.getFeed({
        url: URI,
        success: function(feed) {
            
			var html = "";
			
            for(var i = 0; i < feed.items.length; i++) {
            
                var item = feed.items[i];
                var textcss = 'csText';
                
                if (sender == "#feedItemsRight")
                	textcss = 'csText-side-bar';
                
            	html += '<div class="communitySocialWrap clearfix">';
				html += '<div class="csIcon"><img src="images/icon-community-1.gif" /></div>';
				html += '<div class="'+textcss+'">';
//				html += '<p class="csComment" style="cursor:pointer;" onclick="window.open(\''+item.link+'\',\'name\');">'+item.title+'</p>';
				html += '<p class="csComment" style="cursor:pointer;" onclick="goToRSSFeedEntry(\''+item.link+'\',\'name\');">'+item.title+'</p>';
				html += '<p class="csTime"><span class="greenTxt">'+item.dateToText+'</span></p>';
				
				if (item.author.length == 0){
					html += '<p class="csName"><img src="http://community.telustalksbusiness.com/'+item.authorURI+'/avatar/32.png" align="left" />by ';
					html += item.authorName;
					html += '</p>';
				}
				else{
					html += '<p class="csName"><img src="images/community-avatar.jpg" align="left" />by '+item.author+'</p>';
				}
				html += '</div>';
				html += '</div>';
				
				
				
				if (i >= num - 1)
					break;
			}
			$(sender).html(html);
			$('#feedItemsRight .communitySocialWrap:last').addClass('no-cs-border');
        }  
		
    });
}

goToRSSFeedEntry = function(URI, title){
	// remember the old url so it can be reverted after submitting form
	var oldCommunityFormAction = $("#communityForm").attr("action");
	
	// set action and target and submit form
	// URI = 'http://community.telustalksbusiness.com/blogs/talk_business/2010/05/13/the-new-work-revolution;' // live link on community feed for testing
	// URI = 'http://community.telustalksbusiness.com/thread/1041'; // live link on recent discussions feed for testing
	$("#communityForm").attr({"action" : URI, "target":"_blank"});
	document.getElementById("communityForm").submit();
	
	// revert url
	$("#communityForm").attr({"action" : oldCommunityFormAction}).removeAttr("target");
}