﻿var feeds = {
    feedItem : [],
	cur_item: '',
	itemsToShow: '',
	
	read : function(feedID,feedURL,feedFilter,argItemsToShow) {
	
	    // Set default value for argItemsToShow
		argItemsToShow = argItemsToShow || 1000;
	    this.itemsToShow = argItemsToShow;
	    
        var feed = new Ajax(feedURL, {method: 'get' , onComplete: function(text, xml) { this.parseXML(text, xml, {fID: feedID,fFilter:feedFilter}) }.bind(this) }).request();
    },
    
	parseXML : function(text, xml, params) {
	    var myFeedXMLDoc = xml;
	    feedID = params.fID;
	    feedFilter = params.fFilter;
	    
	    var channel = myFeedXMLDoc.getElementsByTagName('channel')[0];
	    var items = channel.getElementsByTagName('item');
	    
	    /*LOOP THRU FEED AND MAKE ARRAY*/
        for(i=0; items.length > i; i++){
	        var nodeCount = items[i].childNodes.length;
	        this.feedItem["item" + i] = new Array();
	        this.feedItem["item" + i]["subjects"] = new Array();
	        for(n=0; nodeCount > n; n++){
                if(items[i].childNodes[n].nodeType == 1 && items[i].childNodes[n].firstChild){
                    if(items[i].childNodes[n].nodeName == "dc:subject"){
                        this.feedItem["item" + i]["subjects"].push(items[i].childNodes[n].firstChild.nodeValue);
                    }else{
                        this.feedItem["item" + i][items[i].childNodes[n].nodeName] = items[i].childNodes[n].firstChild.nodeValue;
                    }
                }
	        }
	    }
	    global.setFilter(feedFilter);
	    this.find(feedID);
	},
	
	find : function(argEle) {
	     var newItemLayout = '';
	     var totalItems = 0;
	     
	     for(i in this.feedItem){
                if(typeof(this.feedItem[i]) == 'object'){
                    var curItem = this.feedItem[i];
                    totalItems++;   
                if(global.filtersArray != '' && global.filtersArray != 'undefined' && global.filtersArray != null){
                    //Loop thru subjects and find a match to any of the filters.
                    for(i=0; curItem['subjects'].length > i; i++){
                        for(j=0; global.filtersArray.length > j; j++){
                            if(curItem['subjects'][i] == global.filtersArray[j]){
                                if(totalItems <= this.itemsToShow){
                                    newItemLayout += this.build(curItem,argEle);
                                 }
                            }
                        }
                    }
                }else{
                    if(totalItems <= this.itemsToShow){
                        newItemLayout += this.build(curItem,argEle);
                    }
                }
            }
        } 
        
        $(argEle).getLast().setHTML(newItemLayout);
        global.initAccordion('feed_accordions','div.'+argEle+'_accordion','div.'+argEle+'_toggler', true);
        global.scrollBar(argEle);
	// Not sure if we need this yet, but Tracking looks like its working fine with out it.

        

        $$('.news_item').each(function(f){

                $(f).onclick = function(){

                     var linkName = $(f).getFirst().innerHTML;

                      _hbSet('lid', linkName + ' Story Link');

                      _hbSet('lpos', 'News Feed Nav');

                      _hbSend();

                }

          });


	},
	
	build : function(argItem,argFeed) {
	    var itemLayout = '';
	    var objRegExp = /^(\s*)$/;
	    var toggleClass= argFeed + "_toggler";
	    var accordionClass = argFeed + "_accordion";
	    
	    var description = argItem['description'];
	    
	    var catchUnderScore = ' ';
	    var lposId = argFeed.replace(/_/g, catchUnderScore);
		
		/*var orgDate = argItem['pubDate'];
		var DateSplit = orgDate.split(" ");
	    var Month = DateSplit[2];
		switch(Month){
		case "Jan":
			Month="1";
			break;
		case "Feb":
			Month="2";
			break;
		case "Mar":
			Month="3";
			break;
		case "Apr":
			Month="4";
			break;
		case "May":
			Month="5";
			break;		
		case "Jun":
			Month="6";
			break;
		case "Jul":
			Month="7";
			break;		
		case "Aug":
			Month="8";
			break;
		case "Sep":
			Month="9";
			break;
		case "Oct":
			Month="10";
			break;		
		case "Nov":
			Month="11";
			break;
		case "Dec":
			Month="12";
			break;
		default :
			Month="00";
		}*/
		
		    itemLayout += '<a class="news_item" name="&lid=' + argItem['title'] + '&lpos='+lposId+' nav" href="' + argItem['link'] + '&reader=portal" target="_new">';
			itemLayout += '<span class="sub_headline">' + argItem['title'] + ' <img src="/shared/autoshow/images/arrow.jpg"></span></a><br />';
			itemLayout += '<span class="sub_date">' + argItem['pubDate'] + ' &bull; </span>';
            itemLayout += '<span class="source">' + argItem['source'] + '</span><br /><br />';
           
 
 
	    return itemLayout;
	},
	
	update : function(argItem,argTarget) {
	    global.setFilter(argItem);
	    this.find(argTarget);
	}
} 