/*
 * AppLib.js
 * Defines constants, resources, etc
 * Dependencies: 
 *	jQuery (http://jquery.org)
 *	http://static.chadlindstrom.ca/scripts/applib.js
 */	


// initialize namespace
var chadlindstrom = {
	"resources" 	: {},
	"tools"		: {},
	"about" 	: {},
	"delicious"  : {}
};

Object.extend = function(d, s) {
   for (p in s) {
      d[p] = s[p];
   }
   return d;
}
var Class = {
   create: function() {
      return function() {
         this.initialize.apply(this, arguments);
      }
   }
}

Object.extend(chadlindstrom.delicious,{
	"GetMax" : function(deliciousObj)
	{
		var max = 0;
		for (var k in deliciousObj.tags) {
			var count = deliciousObj.tags[k];
			if(count>max)
			{
				max = count;
			}
		}
		return max;
	},
	"GetTagMap" : function(deliciousObj)
	{
		var max = this.GetMax(deliciousObj);
		var para = document.createElement('p');
		
		for (var k in deliciousObj.tags) {
			var count = deliciousObj.tags[k];
			var relative_rank = Math.exp(count/max);
	
			var a = document.createElement('a');
			a.setAttribute('href', 'http://del.icio.us/chad.lindstrom/'+k);
			a.setAttribute('style', 'font-size:'+relative_rank+'em');
			a.setAttribute('title', count);
			a.appendChild(document.createTextNode(k));
			para.appendChild(a);
			para.appendChild(document.createTextNode(' '));
		}
		return para;
	},
	"GetItemList" : function(deliciousObj,isSorted)
	{
		var ul = document.createElement('ul');
		
		var posts = deliciousObj.posts;
		
		if(isSorted)
		{
			posts.sort(this.SortLinks);
		}
		
		for (var i=0, post; post = posts[i]; i++) {
			 var li = document.createElement('li');
			 var a = document.createElement('a');

			 a.setAttribute('href', post.u);
			 a.appendChild(document.createTextNode(post.d));
			 li.appendChild(a);
			 ul.appendChild(li);
		}
		return ul;
	},
	"SortLinks" : function(a,b)
	{
		return (b.d < a.d) - (a.d < b.d);
	}
});

/* spread some goodness */
Object.extend(chadlindstrom.resources,{
	"ads" : [
		{
			"url" 	: "http://www.spreadfirefox.com/?q=affiliates&id=0&t=86",
			"img" 	: "http://sfx-images.mozilla.org/affiliates/Buttons/125x50/takebacktheweb_125x50.png",
			"title"	: "Get Firefox!"
		},
		{
			"url" 	: "http://www.spreadfirefox.com/?q=affiliates&id=0&t=177",
			"img" 	: "http://sfx-images.mozilla.org/affiliates/thunderbird/reclaimyourinbox_small.png",
			"title"	: "Get Thunderbird!"
		},
		{
			"url" 	: "http://www.spreadfirefox.com/?q=affiliates&id=0&t=68",
			"img" 	: "http://sfx-images.mozilla.org/affiliates/Buttons/88x31/take.gif",
			"title"	: "Get Firefox!"
		}
	]	
});

Object.extend(chadlindstrom.tools, {
	"ParseLinks" : function(){
		// should use jquery and target head/foot/content links accordingly
		var linkTags = document.getElementsByTagName("a");

		for( var i = 0; i < linkTags.length; i++ ){
		
			if( linkTags[i].className.length > 0 ){
				if( linkTags[i].className.match("bookmarkItem") ){
					linkTags[i].target="newWin";
				}
				if( linkTags[i].className.match("myGallery") ){
					linkTags[i].target="myGalleryWin";
				}
				if( linkTags[i].className.match("myBlog") ){
					linkTags[i].target="myBlogWin";
				}
				if( linkTags[i].className.match("myResume") ){
					linkTags[i].target="myResumeWin";
				}
				if( linkTags[i].className.match("newWindow") ){
					linkTags[i].target="externalWin";
				}
				if( linkTags[i].className.match("external") ){
					linkTags[i].target="externalWin";
				}
				if( linkTags[i].className.match("topLink") ){
					linkTags[i].href = "#";
				}
			} // end if( length(linkTags[i].className) > 0 )
		}// end for
	},
	
	// it is what it is, show some goodness
	"ShowGoodness" : function (){
		var index = Math.floor(Math.random() * chadlindstrom.resources.ads.length);
		var a = chadlindstrom.resources.ads[index];
		
		$("#goodness").html('<a href="'+ a.url +'"><img border="0" alt="'+ a.title +'" title="'+ a.title +'" src="'+ a.img +'"/></a>');
	}
});