window.ellsoft = {};
window.ellsoft_test = {};
(function($) {
	
	var loadContent = function(type, options) {
		// build main options before element iteration	
		var opts = $.extend({}, loadContent.defaults, options);
		try {
			var content = ContentFactory(type,options);
			if (content) {
				content.show();
			}
		} catch (e) { //if (e instanceof TypeError) 
			alert("loadContent error: " + e.message);
		}
	
	};
	loadContent.defaults = {divTitle:'Feed'};

	var ContentFactory = function(type,options) {
		switch (type) {
			case 'loadfeed':
				ff = new FeedFetch();
				df = new DisplayFeed({callback:options.callback,desc:options.divTitle});
				return new ContentFetch(options,ff,df);
				break;
			case 'loadcounterpunch':
				ff = new RemoteFetch();
				df = new DisplayHTML({
					callback:options.callback,
					parse: function(data) {
						var matches = data.match(/Today\'s(\n|.)*?(<P>(\n|.)*?)\/TD>/i);
						data = matches[0];
						data = data.replace(/<font(\n|.)*?>/ig, '');
						data = data.replace(/<\/font>/ig, '');
						data = data.replace(/<a href="(?!(http:\/\/))/ig, '<a href="http://www.counterpunch.org/');
						return data;
					},
					desc:options.divTitle});
				return new ContentFetch(options,ff,df);
				break;
			case 'loadwundermap':
				ff = new RemoteFetch();
				df = new DisplayHTML({
					callback:options.callback,
					parse: function(data) {
						data = $("#map",data).wrap("<div></div>").html();
						//data += $(".stationscript",data).wrap("<div></div>").html();
						
						return data;
						
						var matches = data.match(/Today\'s(\n|.)*?(<P>(\n|.)*?)\/TD>/i);
						data = matches[0];
						data = data.replace(/<font(\n|.)*?>/ig, '');
						data = data.replace(/<\/font>/ig, '');
						data = data.replace(/<a href="(?!(http:\/\/))/ig, '<a href="http://www.counterpunch.org/');
						return data;
					},
					desc:options.divTitle});
				return new ContentFetch(options,ff,df);
				break;
		}
	};
	
	var ContentFetch = function(options,fetchObj,displayObj) {
		var result;
		this.show = function() {
			var data = LocalCache.get(options.url);
			if (data) {
				dataReturned(data);
			}
			else {
				fetchObj.get(options.url,dataReturned);
			}
		}
		var dataReturned = function(data) {
			result = data;
			LocalCache.set(options.url,result)
			completed();
		}
		var completed = function() {
			displayObj.show(result);
		}
	};
	
	var DisplayHTML = function(options) {
	
		this.show = function(data) {
			var html = data;			
			html = options.parse(html);
			var func = options.callback;
			func(html);	
		};
		
	};

	var DisplayFeed = function(options) {

		this.show = function(data) {
			var html = ParseFeedHTML(data);
			var func = options.callback;
			func(html);
		};
	};

 /*	var ParseFeedMarkup = function(feed) {
	parses 'feed' object returned by jFeed plugin
		var html = '!!' + '[[' + feed.title + '|' + feed.link + ']]' + '';
		
		for(var i = 0; i < feed.items.length; i++) {
		
				var item = feed.items[i];
				
				html += '!!!!' + '[[' + item.title + '|' + item.link + ']]' + '';
				html += '' + item.updated + '';
				html += '//' + item.description + '//';
		}    
		return html;
	};
*/	
	var ParseFeedHTML = function(feed) {
	// parses 'feed' object returned by jFeed plugin
		var html = '<h2>' + '<a href="' + feed.link + '">' + feed.title + '</a>' + '</h2>';
		
		for(var i = 0; i < feed.items.length; i++) {		
			var item = feed.items[i];				
			html += '<h3>' + '<a href="' + item.link + '">' + item.title + '</a>' + '</h3>';			
			html += '<div class="updated">' + item.updated + '</div>';		
			html += '<div>' + item.description + '</div>';
		}    
		return html;
	}; 
	
	var LocalCache = (function() {
		var cache = [];
		var cacheTime = [];
		//var defaults = {durationMinutes:15};
		function cacheHasData(url) {
			var now = new Date();  
			if (cache[url] && (cacheTime[url] <= (now + defaults.durationMinutes))) {
				return true;
			} 
			else {
				return false;
			}	
		}
		this.set = function(url,data) {
			var now = new Date();  
			cacheTime[url] = [now];
			cache[url] = data;
		}
		this.get = function(url) {
			if (cacheHasData(url)) {
				return cache[url];
			} 
			else {
				return null;
			}
		}
		this.clear = function(url) {
			var now = new Date();  
			cacheTime[url] = null;
			cache[url] = null;
		}
		this.clearAll = function() {
			cache = [];
			cacheTime = [];
		}
		return this;
	})();
	LocalCache.defaults = {durationMinutes:15};
	
	var IFrameFetch = function() {
		this.get = function(url,success) {
			$("<iframe>")
				.load(function(){
					success($("#iframefetch").contents().find('body'));
					//success('dude');
				})
				.css({display: "inline",height:'5px',width:'5px'})
				.attr({id: "iframefetch",src: url})
				.appendTo("body");
		};
	};

	var RemoteFetch = function() {
		this.get = function(url,success) {
			$.ajax({
				url : url,
				type : 'GET',
				dataType : 'html',
				success : function (data, textStatus) {
					// data could be xmlDoc, jsonObj, html, text, etc...
					success(data);
				},
				error : function(XMLHttpRequest, textStatus, errorThrown) {
					alert(textStatus + ' ' + errorThrown);
				}
			});
		};
	};
	
	var FeedFetch = function() {
		this.get = function(url,success) {
			$.ajaxSetup({dataType:'xml'});
			//jFeed plugin
			$.getFeed({url: url,
				success: function(feed) {success(feed);}});
		};
	};
	
	var loadingMsg = {
		display : function() {
			$('#message_area')
					.css( {
						display : 'none'
					})	
					.html(
							'<img src="images/loading.gif" ><span class="message_text">Loading...</span>')
					.show();
		},

		remove : function() {
			$('#message_area').css( {
				display : 'none'
			});
		}
		
	};
	
	if (window.ellsoft) {
		var es = window.ellsoft;
		es.loadContent = loadContent;
	}
	if (window.ellsoft_test) {
		var es = window.ellsoft_test;
		es.loadContent = loadContent;
		es.RemoteFetch = RemoteFetch;
		es.IFrameFetch = IFrameFetch;
		es.FeedFetch = FeedFetch;
		es.LocalCache = LocalCache;
		es.ContentFetch = ContentFetch;
		es.DisplayFeed = DisplayFeed;
		es.DisplayHTML = DisplayHTML;
	}	
	//this below is the jFeed addin, put here because of reference problems
	eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('3.X=a(h){h=3.J({v:D,C:D,u:D},h);k(h.v){$.W({t:\'V\',v:h.v,C:h.C,U:\'4\',u:a(4){d f=j B(4);k(3.T(h.u))h.u(f)}})}};a B(4){k(4)2.K(4)};B.r={t:\'\',l:\'\',c:\'\',b:\'\',g:\'\',K:a(4){k(3(\'8\',4).y==1){2.t=\'x\';d s=j z(4)}H k(3(\'f\',4).y==1){2.t=\'S\';d s=j A(4)}k(s)3.J(2,s)}};a o(){};o.r={c:\'\',b:\'\',g:\'\',i:\'\',n:\'\'};a A(4){2.q(4)};A.r={q:a(4){d 8=3(\'f\',4).9(0);2.l=\'1.0\';2.c=3(8).5(\'c:e\').6();2.b=3(8).5(\'b:e\').p(\'I\');2.g=3(8).5(\'R:e\').6();2.w=3(8).p(\'4:Q\');2.i=3(8).5(\'i:e\').6();2.m=j G();d f=2;3(\'P\',4).F(a(){d 7=j o();7.c=3(2).5(\'c\').9(0).6();7.b=3(2).5(\'b\').9(0).p(\'I\');7.g=3(2).5(\'O\').9(0).6();7.i=3(2).5(\'i\').9(0).6();7.n=3(2).5(\'n\').9(0).6();f.m.E(7)})}};a z(4){2.q(4)};z.r={q:a(4){k(3(\'x\',4).y==0)2.l=\'1.0\';H 2.l=3(\'x\',4).9(0).p(\'l\');d 8=3(\'8\',4).9(0);2.c=3(8).5(\'c:e\').6();2.b=3(8).5(\'b:e\').6();2.g=3(8).5(\'g:e\').6();2.w=3(8).5(\'w:e\').6();2.i=3(8).5(\'N:e\').6();2.m=j G();d f=2;3(\'7\',4).F(a(){d 7=j o();7.c=3(2).5(\'c\').9(0).6();7.b=3(2).5(\'b\').9(0).6();7.g=3(2).5(\'g\').9(0).6();7.i=3(2).5(\'M\').9(0).6();7.n=3(2).5(\'L\').9(0).6();f.m.E(7)})}};',60,60,'||this|jQuery|xml|find|text|item|channel|eq|function|link|title|var|first|feed|description|options|updated|new|if|version|items|id|JFeedItem|attr|_parse|prototype|feedClass|type|success|url|language|rss|length|JRss|JAtom|JFeed|data|null|push|each|Array|else|href|extend|parse|guid|pubDate|lastBuildDate|content|entry|lang|subtitle|atom|isFunction|dataType|GET|ajax|getFeed'.split('|')))

})(jQuery);