/* options:
  base (required): base url to tell what's internal vs. external
  cb (optional): callback to determine whether to apply icons + sizes (return false)
  targetClass (optional): replace matching elements with icon rather than adding to the end
*/

jQuery.fn.linkHints = function(o) {
	return this.find('a[@href]').each(function(){
		
		if(this.href.substr(0,4) != 'http') return;
		
		var iconClass = '';
		var description = '';
		var addSize = false;
		var addIcon = false;
		var newWindow = false;
		if(this.href.match(/\.(pdf|doc|ppt|xls)($|\?|\#)/i)) {
			addIcon = true;
			iconClass = 'document linkhint_icon';
			description = 'This link will open a document.';
			addSize = true;
			}
		else if(this.href.match(/\.zip($|\?|\#)/i) || this.href.match(/\/download\//i)) {
			addIcon = true;
			iconClass = 'download linkhint_icon';
			description = 'This link will download a file.';
			var addSize = true;
			}
		else if(this.href.substr(0,o.base.length) != o.base) {
			addIcon = true;
			iconClass = 'external linkhint_icon';
			description = 'This link will open a page that is part of another web site.';
			newWindow = true;
			}
		if(!addIcon && !addSize) return;
		
		// add hover description
		if(description && !this.title) this.title = description;
		
		// open in new window if no target or onclick
		if(newWindow && !jQuery(this).attr('target') && !this.onclick) {
			var new_window_id = "window_"+String(Math.random()).substr(2,12);
			jQuery(this).attr('target', new_window_id);
			var self = this;			
			jQuery(this).click(function() { open(jQuery(self).attr('href'), new_window_id, 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=800,height=560'); return false });
			}

		// skip icon and size if the link text is blank (usually images) or the callback is false
		if(jQuery(this).text().match(/^\s*$/)) return;
		if(o.cb && !o.cb.apply(this)) return;

		var target;
		// if the link has children and is not display:inline, add to the first child, rather than at the end
		var children = jQuery('*', this);
		var displayStyle = jQuery(this).css('display') || 'inline';

		if (o.targetClass && children && children.length && children.is(o.targetClass)) {
			target = children.filter(o.targetClass);
		} else if (children && children.length && displayStyle != 'inline') {
			target = children[0];
		}
		else {
			target = this;
		}

		if(addIcon) {
			// add class to anchor
			jQuery(this).addClass("linkhint");
			var span = jQuery('<span>&nbsp;</span>');
			span.attr('class', iconClass);
			span.attr('title', description);
			span.appendTo(target);
			}
		
		if(addSize) {
			try {
				jQuery.ajax({
					type: "HEAD",
					url: this.href,
					complete: function(xhr, success) {
						if(success != 'success') return;
						var size = xhr.getResponseHeader('Content-Length');
						if(!size) return;
						if(size > 1024*1024) {
							size = Math.round(size/1024/102.4)/10+'&nbsp;MB';
							}
						else if(size > 1024) {
							size = Math.round(size/1024)+'&nbsp;KB';
							}
						else {
							size = size + '&nbsp;bytes';
							}
						
						var span = jQuery('<span class="document_size"></span>');
						span.html('('+size+')');
						jQuery(target).append('&#32;', span);
						}
					});
				}
			catch(e) {
				if(!String(e).match(/^permission denied/i)) throw e;
				}
			}

		});
	};


/* Recommended CSS

.linkhint_icon {
	background: transparent center no-repeat;
	padding: 0 11px 0 0;
	margin: 0 0 0 2px
}
	.external {background-image: url(../images/link_external.gif);}
	.document {background-image: url(../images/link_document.gif);}
	.download {background-image: url(../images/link_download.gif);}

*/

