if (typeof com == "undefined") com = {};
if (typeof com.atnext == "undefined") com.atnext = {};
if (typeof com.atnext.page == "undefined") com.atnext.page = {};
if (typeof com.atnext.page.utilities == "undefined") com.atnext.page.utilities = {};
com.atnext.page.utilities.LinkReplacer = {
	win: null,
	init: false,
	rootPath: "",
	fst_run: true,
	lookup: function (col) {
		for( var i=0; i<col.childNodes.length; i++ ) {
			var base = col.childNodes.item(i);
			if (base.nodeType == 3) {
				var mth = base.nodeValue.match(/(?:http[s]{0,1}\:\/\/|mailto\:|ftp\:)[A-Za-z0-9\~\!\@\#\$\%\^\&\*\(\)\_\+\-\=\`\{\}\[\]\|\;\'\:\"\,\.\/\?]*[$]*/i);
				if (mth) {
					if (this.fst_run) {
						this.fst_run = false;
						if (document.all) {
							if (document.styleSheets.length)
								var ss = document.styleSheets(0);
							else
								var ss = document.createStyleSheet();
							ss.addRule(".atnextpagelink", "font-size:9pt !important; color: blue !important; border-bottom: 1px dotted blue !important;");
							ss.addRule(".atnextpagelink img", "border: none; margin-left: 3px;");
							ss.addRule(".atnextpagelink:hover", "font-size:9pt !important; color: red !important; border-bottom: 1px dotted red !important;}");
						} else {
							document.writeln("<style type='text/css'>");
							document.writeln(".atnextpagelink {font-size:9pt !important; color: blue !important; border-bottom: 1px dotted blue !important;}");
							document.writeln(".atnextpagelink img {border: none; margin-left: 3px;}");
							document.writeln(".atnextpagelink:hover {font-size:9pt !important; color: red !important; border-bottom: 1px dotted red !important;}");
							document.writeln("</style>");
						}
					}
				
					var txt = base.nodeValue;
					var sib = base.nextSibling;
					var aNode = document.createElement("a");
					var imgNode = this.createLinkIcon();
					var link_txt = orig_link = mth[0];
					if (/^mailto\:/i.test(link_txt))
						link_txt = link_txt.substring(7,link_txt.length);
					if (link_txt.length > 62)
						link_txt = link_txt.substring(0,27) + "......" + link_txt.substring(link_txt.length-27,link_txt.length);
					aNode.href =  mth[0];
					aNode.target = "_blank";
					aNode.className = "atnextpagelink";
					aNode.title = mth[0];
					aNode.eclipse_ver = link_txt;
					aNode.orig_ver = orig_link;
					aNode.linkIcon = imgNode;
					aNode.insertBefore(document.createTextNode(link_txt), null);
					aNode.insertBefore(imgNode, null);
					aNode.link_img = imgNode;
					var tn1 = document.createTextNode(txt.substring(0,mth.index));
					var tn2 = document.createTextNode(txt.substring(mth.index+mth[0].length, txt.length));
					col.replaceChild(tn1,base);
					col.insertBefore(aNode,sib);
					col.insertBefore(tn2, sib);

					i+=1;
				}
			}
			if (base.nodeType != 3 && base.tagName && (! base.tagName.match(/^a|select|input|form|script|br$/i) || (base.tagName == "A" && base.href == "")))
				this.lookup(base);
		}
	},
	createLinkIcon: function () {
		var imgNode = document.createElement("img");
		imgNode.src = this.rootPath+"images/replacelink/url.gif";
		return imgNode;
	},
  findPath: function () {
		var tags = document.getElementsByTagName("script");
		for(var i=0; i<tags.length; i++) {
			if (tags[i].getAttribute("src")) {
				var mth = tags[i].getAttribute("src").match(/^([^\s]+\/){0,1}replacelink\.js$/i);
				if (mth)
					this.rootPath = mth[1];
			}
		}
  },
  replaceLink: function (node) {
	  	if (! this.init) this.findPath();
		//this.win = window.open("about:blank");
		//this.win.document.open();
		//this.win.document.writeln("<meta http-equiv='content-type' content='text/html; charset=big5'/>");
		this.lookup(node);
		//this.win.document.close();
  }
};
