var INDUSTRIALNATION = (function(){

	return {
		init: function() {
			//console.log('INDUSTRIALNATION.init()');
		},
		delicious: {
			init: function() {
				this.getTags();
			},
			getTags: function() {
				var that = this;
				$.ajax({
					url: "http://feeds.delicious.com/v2/json/tags/philpill",
					context: that,
					dataType: "jsonp",
					success: this.getTagsSuccess,
					error: this.getTagsError
				});
			},
			getTagsError: function(jqXHR, textStatus, errorThrown) {
				//get tags from localStorage
				console.log(jqXHR, textStatus, errorThrown);
			},
			getTagsSuccess: function(data) {
				//this.cacheTags(data);
				for(tag in data) {
					this.createList(tag)
					this.getBookmarks(tag);
				}
			},
			createList: function(tag) {
				var bookmarkSection = document.getElementById('Bookmarks');
				var newArticle = document.createElement('article');
				var newHeader = document.createElement('h3');
				var newHeaderText = document.createTextNode(tag.toLowerCase());
				var newList = document.createElement('ul');
				
				newArticle.setAttribute('id', tag);			
				bookmarkSection.appendChild(newArticle);
				newHeader.appendChild(newHeaderText);
				newArticle.appendChild(newHeader);
				newArticle.appendChild(newList);
			},
			cacheTags: function(tags) {
				if (Modernizr.localstorage) {
					localStorage.setItem('tags', JSON.stringify(tags));
				}
			},
			cacheBookmarks: function(tag, data) {
				if (Modernizr.localstorage) {
					localStorage.setItem(tag, JSON.stringify(data));
				}
			},
			getBookmarks: function(tag) {
				$.ajax({
					url: "http://feeds.delicious.com/v2/json/philpill/" + tag + "?count=100",
					context: this,
					data: { 'tag': tag },
					dataType: "jsonp",
					success: this.getBookmarksSuccess,
					error: this.getBookmarksError
				});
			},
			getBookmarksSuccess: function(data) {
				var truncatedBookmark, count = 0;
				for(count = 0; count < data.length; count++) {	
					truncatedBookmark = {};
					truncatedBookmark.tag = data[count].t[0];
					truncatedBookmark.title = data[count].d;
					truncatedBookmark.url = data[count].u;
					this.appendBookmarkToList(truncatedBookmark);
				}
				//this.cacheBookmarks(data[0].t[0], data);
			},
			getBookmarksError: function(jqXHR, textStatus, errorThrown) {
				//get bookmarks from localStorage
				console.log(jqXHR, textStatus, errorThrown);
			},
			appendBookmarkToList: function(bookmark) {
			
				var sanitisedTag = bookmark.tag.trim();
				var article = document.getElementById(sanitisedTag);				
				var list = article.getElementsByTagName('ul')[0];
				var newBookmark = document.createElement('li');
				var newBookmarkAnchor = document.createElement('a');
				var newBookmarkAnchorText = document.createTextNode(bookmark.title.toLowerCase());
				
				newBookmarkAnchor.setAttribute('href', bookmark.url);
				newBookmarkAnchor.appendChild(newBookmarkAnchorText);
				newBookmark.appendChild(newBookmarkAnchor);
				list.appendChild(newBookmark);
			}
		}
	};
})();

INDUSTRIALNATION.init();

INDUSTRIALNATION.delicious.init();



















