(function($){

	$.fn.iconize = function(options) {
	
		var icons = [
					//Extensions
					
					 {'icon':'icon_doc.gif','files':['.doc$','.docx$','.rtf$']}
					,{'icon':'icon_infopath2.gif','files':'.xsn$'}
					,{'icon':'icon_xls.gif','files':'.xls$'}
					,{'icon':'icon_pps.gif','files':'.pps$'}

					,{'icon':'icon_txt.gif','files':'.txt$'}
					,{'icon':'icon_pdf.gif','files':'.pdf$'}
					,{'icon':'icon_plugin.gif','files':'.xpi$'}

					,{'icon':'icon_feed.gif','files':['.rss$', '.atom$']}
					,{'icon':'icon_opml.gif','files':'.opml$'}

					,{'icon':'icon_vcard.gif','files':'.vcard$'}
					,{'icon':'icon_ical.gif','files':'.ical$'}

					,{'icon':'icon_exe.gif','files':'.exe$'}
					,{'icon':'icon_dmg.gif','files':['.dmg$','.app$']}

					,{'icon':'icon_pic.gif','files':['.gif$','.jpg$','.jpeg$','.png$','.bmp$','.svg$','.eps$']}
					,{'icon':'icon_flash.gif','files':['.fla$','.swf$']}

					,{'icon':'icon_music.gif','files':['.mp3$','.wav$','.ogg$','.wma$','.m4a$']}
					,{'icon':'icon_archive.gif','files':['.zip$','.rar$','.gzip$','.bzip$']}
					,{'icon':'icon_film.gif','files':['.mov$','.wmv$','.mp4$','.avi$','.mpg$']}

					,{'icon':'icon_css.gif','files':'.css$'}
					,{'icon':'icon_ttf.gif','files':'.ttf$'}

					,{'icon':'icon_phps.gif','files':'.phps$'}
					,{'icon':'icon_torrent.gif','files':'.torrent$'}
				
					// URI schemes
					
					,{'icon':'icon_icq.gif','files':'icq.com'}
					,{'icon':'icon_yim.gif','files':'edit.yahoo.com/config/send_webmesg?'}
					,{'icon':'icon_mailto.gif','files':'mailto:'}
					,{'icon':'icon_call.gif','files':['tel:','callto:']}
					,{'icon':'icon_aim.gif','files':'aim:'}
					,{'icon':'icon_msn.gif','files':'msnim:'}
					,{'icon':'icon_jabber.gif','files':'xmpp:'}
					,{'icon':'icon_skype.gif','files':'skype:'}
					,{'icon':'icon_gadugadu.gif','files':'gg:'}
					
					
					//Video Sites
					,{'icon':'icon_film.gif','files':['youtube.com/watch', 'sevenload.com/videos/', 'metacafe.com/watch/']}
					
					//Photo Sites
					,{'icon':'icon_pic.gif','files':["flickr.com/photos/" ,"zooomr.com", "imageshack.us", "bubbleshare.com", "sevenload.com/bilder/"]}

					//forms
					,{'icon':'icon_form.gif','files':['web=FORMS', 'ekfrm']}
					
					//favicon
					
					,{'icon':'favicon','files':'facebook.com'}
					,{'icon':'favicon','files':'yahoo.com'}
					,{'icon':'favicon','files':'google.com'}
					
				];
	
		var filesize = new Array('.pdf', '.zip', '.rar', '.gzip', '.bzip', '.mov', '.wmv', '.mp4', '.avi', '.mpg', '.wav', '.ogg', '.wma', '.m4a');

		host = parseUri(window.location).host;
		if (host.split('.').length > 2){
			host = host.replace(host.split('.')[0]+'.','');
		}				

		
		this.each(function() {
		  	var $this = $(this);
			var url = this.href;
			if($this.is('a') && $this.attr('href') != '') {
				if (!$this.hasClass('imageLink')){
					//off domain
					
					if 	(!find_uri_new(url, icons) && url.match('http:') ){
						var re = new RegExp(host);
						if (!parseUri(url).host.match(re)) {
							a = '/css/icons/icon_external.gif';
							$this
								.wrap('<span></span>')
								.after('<a href="'+url+'" style="margin:0;padding:0;border:none;background:none;height:16px;width:16px;"><img src="'+a+'" style="vertical-align:text-bottom;padding:0 0 0 4px;margin:0;border:none;background:none;"/></a>')
						}
					}
				
					//Icons 
					a = find_uri_new(url, icons);
					
					if (a){
						if (a == 'favicon') 
							a = "http://" + parseUri(url).host + "/favicon.ico";
						else 
							a = '/css/icons/' + a;
						
						$this
							.wrap('<span></span>')
							.after('<a href="'+url+'" style="margin:0;padding:0;border:none;background:none;"><img src="'+a+'" style="vertical-align:text-bottom;padding:0 0 0 4px;margin:0;border:none;background:none;height:16px;width:16px;"/></a>')
					}
					
					//file size
					/*
					if (find_uri(url, filesize) ){
						file = parseUri(url).relative;
						if (parseUri(window.location).file != parseUri(url).file){
							$.get('/javascript/iconize/filesize.php?file='+file, function(data){
								if (data > 1000000){
									$this.parent().after('<span style="padding:0 3px;font-style: italic;">(' + sizeto(data)+ ')</span>');
								}
							});
						}
					}
					*/
				}
			}
		  });
		  // returns the jQuery object to allow for chainability.
		  return this;
		  
		function find_uri(elem, items){
			for (i=0; i < items.length; i++){
				reg = items[i].replace('.', '\\.');
				var re = new RegExp(reg);
				if (elem.match(re)) {
					return items[i];
				}
			}
			return false;
		}
		

		function isArray(obj) {
			if (obj.constructor.toString().indexOf("Array") == -1)
				return false;
			else
				return true;
		}
		
		function find_uri_new(elem, items){
			for (z=0; z < items.length; z++){
				if (isArray(items[z].files)){
					for (y=0; y < items[z].files.length; y++){
						reg = items[z].files[y].replace('.', '\\.')
						if (elem.match(reg)) {
							return items[z].icon;
						}
					}
				} else {
					reg = items[z].files.replace('.', '\\.')
					var re = new RegExp(reg);
					if (elem.match(re)) {
						return items[z].icon;
					}
				}
			}
			return false;
		}
		
		
		function inserticon(t,i){
			t.append('<span></span>');
		}
		function sizeto (s){
			if (s < 1000000){
				return Math.round(s/100)/10 +' KB'
			}else if(s > 1000000 && s < 1000000000){
				return Math.round(s/100000)/10 +' MB'
			}else if(s > 1000000000 && s < 1000000000000){
				return Math.round(s/100000000)/10 +' GB'
			}else if(s > 1000000000000){
				return Math.round(s/100000000000)/10 +' TB'
			}
		}
		
	}
	
})(jQuery);


/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

$(document).ready(function () {
	ca = $('#Content a');
	if (ca.length > 0){
		ca.iconize();
	}
});
