  jQuery.fn.getFeed = function(url, options) {
    
	settings = jQuery.extend({
      name: "defaultName",
      number: 1000000,
      global: true,
	  element: this,
	  showElements: 'title,description,pubDate',
	  dateFormat: 'l j M Y',
	  noLink: false, 
	  callback: '',
	  textParser: '',
	  activateURLs: true,
	  prepend: false
      }, options);  
  
    settings.showElements = settings.showElements.split(',');
	for(var i=0; i<settings.showElements.length; i++){
	  eval('settings.show' + settings.showElements[i] + ' = ' + true);
	  }  
  
    return this.each(function(){
	  $(this).append('<li class="loadingMessage">Loading feed, please wait...</li>');
	  var theFeed = new feed();
	  theFeed.getFeed(url, settings);  
      });
    };

  function feed(){
	}
		
  feed.prototype.getFeed = function(url, options){
	var element = options.element;
	var insertCode = '';
	
    $.ajax({
	  //url: "http://www.dev.surrey.ac.uk/test/xmlproxy.php?url=" + url,
	  url: "/assets/Includes/php/xmlproxy.php?url=" + url,
      cache: false,
	  dataType: 'xml',
	  //complete: doCallback(options),
      success: function(xml, textStatus){
	    $('item', xml).each(function(i){
		  if(i < options.number){
	        if(options.showtitle){
			  if(options.noLink){
			    //$(element).append('<h3>' + $('title', $(this)).text() + '</h3>');
				//$(element).append('<li>' + activateURLs($('title', $(this)).text().replace(/^Surreysportpark: /, '')) + '</li>');
				insertCode += '<li>' + parseText($('title', $(this)).text(), options) + '</li>';
				if(options.prepend){
				  if($(element).find('li').size() > 0){
				    //$(element).find('li:first').before('<li>' + parseText($('title', $(this)).text(), options) + '</li>');
					}
				  else{
				    //$(element).prepend('<li>' + parseText($('title', $(this)).text(), options) + '</li>');
				    }
				  }
				else{
				  //$(element).append('<li>' + parseText($('title', $(this)).text(), options) + '</li>');
				  }
				}
			  else{
			    //$(element).append('<h3><a href="' + $('link', $(this)).text() + '">' + $('title', $(this)).text() + '</a></h3>');
				$(element).append('<li><a href="' + $('link', $(this)).text() + '">' + $('title', $(this)).text() + '</a></li>');
				}
			  }
		    if(options.showdescription)$(element).append('<p>' + $('description', $(this)).text() + '</p>');
			if(options.showpubDate){
			  if($('pubDate', $(this)).text() != ''){
			    var pubDate = new Date($('pubDate', $(this)).text());
			    var pubDate = pubDate.format(options.dateFormat);			    
			    $(element).append('<p class="pubDate">' + pubDate + '</p>');
				}
			  }
			}

		  $('.loadingMessage', $(element)).remove();		  
		  });
		  		  if(options.prepend){
		    $(element).prepend(insertCode);	
			}
		  else{
		    $(element).append(insertCode);
			}
		doCallback(options);
        }
      }); 
    }

	function parseText(a, options){
	  var theText = '';
      if(options.textParser != ''){
		var func = options.textParser + '("' + escape(a) + '")';
		theText = eval(func);
		theText = unescape(theText);
	    }
	  else{
	    theText = a;
		}
	  if(options.activateURLs){
	    theText = activateURLs(theText);
		}
	  return theText;
	  }

	
function doCallback(options){
		  if(options.callback != ''){
		    eval(options.callback + '()');
		    }
}


function activateURLs(input){
  //var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/g;
  //var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/g;
  //var url_match = /((mailto\:|(news|(ht|f)tp(s?))\:\/\/){1}\S+)/
  //var url_match = /(http:\/\/|www.)([a-z]|[A-Z]|[0-9]|[\/.]|[~])*/;
  //var matchedText = url_match.exec(text);
  //return text.replace(url_match, 'URL: '+matchedText);
  return input.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,'<a href="$&">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2">$2</a>');
  }
  
  
function chat_string_create_urls(input){
  return input.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim,'<a href="$&" class="my_link" target="_blank">$&</a>').replace(/([^\/])(www[\S]+(\b|$))/gim,'$1<a href="http://$2" class="my_link" target="_blank">$2</a>');
  } 