// -------------------------------------------------------------------
// gAjax RSS Feeds Displayer- By Dynamic Drive, available at: http://www.dynamicdrive.com
// Created: July 17th, 2007 Updated: n/a
// -------------------------------------------------------------------
var maxfeedlimit = 25;
var title = "";
var gfeedfetcher_loading_image="images/indicator.gif" 	//Full URL to "loading" image. No need to config after this line!!

google.load("feeds", "1") //Load Google Ajax Feed API (version 1)

function gfeedfetcher(divid, divClass, linktarget){
	this.linktarget=linktarget || "" //link target of RSS entries
	this.feedlabels=[] //array holding lables for each RSS feed
	this.feedurls=[]
	this.feeds=[] //array holding combined RSS feeds' entries from Feed API (result.feed.entries)
	this.feedsfetched=0 //number of feeds fetched
	this.feedlimit=5
	this.showoptions="" //Optional components of RSS entry to show (none by default)
	this.sortstring="date" //sort by "date" by default
	this.title=title;
	this.feedcontainer=document.getElementById(divid);
	document.getElementById(divid).setAttribute("title", title);

	this.itemcontainer="<li>" //default element wrapping around each RSS entry item
}

gfeedfetcher.prototype.addFeed=function(label, url){
	this.feedlabels[this.feedlabels.length]=label
	this.feedurls[this.feedurls.length]=url
}

gfeedfetcher.prototype.filterfeed=function(feedlimit, sortstr){
	this.feedlimit=feedlimit
	if (typeof sortstr!="undefined")
	this.sortstring=sortstr
}

gfeedfetcher.prototype.displayoptions=function(parts){
	this.showoptions=parts //set RSS entry options to show ("date, datetime, time, snippet, label, description")
}

gfeedfetcher.prototype.setentrycontainer=function(containerstr){  //set element that should wrap around each RSS entry item
this.itemcontainer="<"+containerstr.toLowerCase()+">"
}

gfeedfetcher.prototype.init=function(){
	this.feedsfetched=0 //reset number of feeds fetched to 0 (in case init() is called more than once)
	this.feeds=[] //reset feeds[] array to empty (in case init() is called more than once)
	this.feedcontainer.innerHTML='<p><img src="'+gfeedfetcher_loading_image+'" /> Retrieving RSS feed(s)</p>'
	var displayer=this
	for (var i=0; i<this.feedurls.length; i++){ //loop through the specified RSS feeds' URLs
		var feedpointer=new google.feeds.Feed(this.feedurls[i]) //create new instance of Google Ajax Feed API
		var items_to_show=(this.feedlimit<=this.feedurls.length)? 1 : Math.floor(this.feedlimit/this.feedurls.length) //Calculate # of entries to show for each RSS feed
		if (this.feedlimit%this.feedurls.length>0 && this.feedlimit>this.feedurls.length && i==this.feedurls.length-1) //If this is the last RSS feed, and feedlimit/feedurls.length yields a remainder
			items_to_show+=(this.feedlimit%this.feedurls.length) //Add that remainder to the number of entries to show for last RSS feed
		feedpointer.setNumEntries(items_to_show) //set number of items to display
		feedpointer.load(function(r){displayer._fetch_data_as_array(r)}) //call Feed.load() to retrieve and output RSS feed
	}
}


gfeedfetcher._formatdate=function(datestr, showoptions){
	if (datestr) {
		var itemdate=new Date(datestr)
		var parseddate=(showoptions.indexOf("datetime")!=-1)? itemdate.toLocaleString() : (showoptions.indexOf("date")!=-1)? itemdate.toLocaleDateString() : (showoptions.indexOf("time")!=-1)? itemdate.toLocaleTimeString() : ""
		return "<p>"+parseddate+"</p>"
	} else {
		return "";
	}
}

gfeedfetcher._sortarray=function(arr, sortstr){
	var sortstr=(sortstr=="label")? "ddlabel" : sortstr //change "label" string (if entered) to "ddlabel" instead, for internal use
	if (sortstr=="title" || sortstr=="ddlabel"){ //sort array by "title" or "ddlabel" property of RSS feed entries[]
		arr.sort(function(a,b){
		var fielda=a[sortstr].toLowerCase()
		var fieldb=b[sortstr].toLowerCase()
		return (fielda<fieldb)? -1 : (fielda>fieldb)? 1 : 0
		})
	}
	else{ //else, sort by "publishedDate" property (using error handling, as "publishedDate" may not be a valid date str if an error has occured while getting feed
		try{
			arr.sort(function(a,b){return new Date(b.publishedDate)-new Date(a.publishedDate)})
		}
		catch(err){}
	}
}

gfeedfetcher.prototype._fetch_data_as_array=function(result){
	var thisfeed=(!result.error)? result.feed.entries : "" //get all feed entries as a JSON array or "" if failed
	if (thisfeed=="") //if error has occured fetching feed
		alert("Google Feed API Error: "+result.error.message)
	for (var i=0; i<thisfeed.length; i++) //For each entry within feed
		result.feed.entries[i].ddlabel=this.feedlabels[this.feedsfetched] //extend it with a "ddlabel" property
	this.feeds=this.feeds.concat(thisfeed) //add entry to array holding all feed entries
	this._signaldownloadcomplete() //signal the retrieval of this feed as complete (and move on to next one if defined)
}

gfeedfetcher.prototype._signaldownloadcomplete=function(){
	this.feedsfetched+=1
	if (this.feedsfetched==this.feedurls.length) //if all feeds fetched
		this._displayresult(this.feeds) //display results
}


gfeedfetcher.prototype._displayresult=function(feeds){
//gjm customized - content contains the picture
	gfeedfetcher._sortarray(feeds, this.sortstring)
	var div = document.getElementById("data");
	if (div.firstChild) {
    	div.removeChild(div.firstChild); 
    	div.innerHTML = "";
    }
    var strReplaceColor;
	var ul = document.createElement("div");


    // cj code
/*
    var liAd1 = document.createElement("li");
    liAd1.innerHTML = "<script type='text/javascript'>
	    <!--window.googleAfmcRequest = {
  		client: 'ca-mb-pub-5095396929201049',
  		ad_type: 'text',
  		output: 'html',
  		channel: '3748660198',
  		format: '320x50_mb',
  		oe: 'utf8',
  		color_border: '000000',
  		color_bg: '000000',
  		color_link: 'E1771E',
  		color_text: 'E1771E',
  		color_url: '999999',
		};
		//--></script>
		<script type='text/javascript' src='http://pagead2.googlesyndication.com/pagead/show_afmc_ads.js'></script>";
	liAd1.setAttribute("class", "ads");
	this.feedcontainer.appendChild(liAd1);
*/

	for (var i = 0; i < feeds.length; i++) {
		var entry = feeds[i];
		var li = document.createElement("li");
		li.setAttribute("class", "lipix");
		
		var innerHTML;
		
// data with images
//		var temp = new Array();
//		temp = entry.content.split('>');
//		entry.content = temp[0] + ' height="50" width="50" align="left">';	
//		innerHTML = "<a href='" + entry.link + "' target='_blank'>" + entry.content;			   
//		innerHTML = innerHTML +  entry.title + "</a><br />";

//data without images
		innerHTML = "<a href='" + entry.link + "' target='_blank'>" + entry.title + "</a><br />";
		strReplaceColor = entry.content.replace(/333/g, 'FFFFFF');			   
		//innerHTML = innerHTML +  entry.content;		
		innerHTML = innerHTML + strReplaceColor;
		
		li.innerHTML = innerHTML;


		// add to the container
		this.feedcontainer.appendChild(li);
	}		

}



function loadDataFeed(title, url) {

var cssfeed=new gfeedfetcher("data", "lipix", "_blank")
cssfeed.addFeed(title, url); //Specify "label" plus URL to RSS feed./
cssfeed.displayoptions("content datetime label snippet"); 			//show the specified additional fields
cssfeed.setentrycontainer("li"); 		//Display each entry as a list (li element)
cssfeed.filterfeed(maxfeedlimit, "datetime"); 	//Show 50 entries, sort by date
cssfeed.init(); 						//Always call this last

}


function loadTopNews() {
    title = "TopNews";
    feedurl="http://news.google.com/news?ned=us&topic=h&output=rss";
    loadDataFeed(title, feedurl);    
}
function loadWorldNews() {    
    title="World";
    feedurl="http://news.google.com/news?ned=us&topic=w&output=rss"    
    loadDataFeed(title, feedurl);
}
function loadUSNews() {
    title="US News";
    feedurl="http://news.google.com/news?ned=us&topic=n&output=rss"    
    loadDataFeed(title, feedurl);
}
function loadUKNews() {
    title="UK News";
    feedurl="http://news.google.com/news?ned=uk&topic=n&output=rss"     
    loadDataFeed(title, feedurl);
}
function loadFranceNews() {
    title="France Actualitˇs";
    feedurl="http://news.google.fr/nwshp?hl=fr&tab=wn&output=atom"     
    loadDataFeed(title, feedurl);
}
function loadTechNews() {
    title="Tech News";
    feedurl="http://news.google.com/news?ned=us&topic=t&output=rss"
    loadDataFeed(title, feedurl);
}

function loadMedNews() {
    title="Med News";
    feedurl="http://news.google.com/news?ned=us&topic=m&output=rss"
    loadDataFeed(title, feedurl);
}

function loadSportsNews() {
    title="Sports";
    feedurl="http://news.google.com/news?ned=us&topic=s&output=rss"
    loadDataFeed(title, feedurl);
}

function loadEntNews() {
    title="Entertainment";
    feedurl="http://news.google.com/news?ned=us&topic=e&output=rss"
    loadDataFeed(title, feedurl);
}

function loadiPhoneNews() {
    title="iPhone News";
    feedurl="http://news.google.com/news?q=iphone&output=rss"
    loadDataFeed(title, feedurl);
}
function loadSearchNews() {
    var txtSearch = document.searchForm.searchText
    title="myNews";
    feedurl="http://news.google.com/news?q=" 
    feedurl += txtSearch 
    feedurl += "&output=rss"
    loadDataFeed(title, feedurl);
}

function loadiPhoneApps() {
    title="iPhoneApps";
    //feedurl="http://feeds.feedburner.com/iphoneapplicationlist"
    feedurl="http://www.apple.com/webapps/feeds/recent.rss"
    loadDataFeed(title, feedurl);    
}
function loadiPhoneBlogs() {
    title="iPhoneBlogs";
    feedurl="http://blogsearch.google.com/blogsearch_feeds?hl=en&q=iphone&ie=utf-8&num=10&output=rss"
    loadDataFeed(title, feedurl);    
}


function loadiPhoneSDK() {
    title="iPhone SDK";
    feedurl="http://developer.apple.com/rss/com.apple.adc.documentation.AppleiPhone.atom"
    loadDataFeed(title, feedurl);    
}

function loadYouTube1() {
    incimage = 1  
    title="Pop YouTube";
    feedurl="http://youtube.com/rss/global/top_viewed_week.rss"
    loadDataFeed(title, feedurl);
    incimage = 0
}

function loadYouTube2() {
    incimage = 1  
    title="Fav YouTube";
    feedurl="http://youtube.com/rss/global/top_favorites.rss";    
    loadDataFeed(title, feedurl);
    incimage = 0    
    
}

