using ('IBSYS.features.storyTools');

IBSYS.features.storyTools = function() {
	// Calling IBSYS.storyTools() a second time simply calls resizeTools()
		
	function createTools (tools) {
		function createTool(tool) { // Only createTools needs createTool, so I've restricted createTool to its scope
			var t = document.createElement('div');
			t.title = tool.label;
			t.rel = tool.name;
			t.className = tool.primary ? 'primary' : 'secondary' ;

			var anchor = document.createElement('a');
			anchor.rel = tool.name;
			tool.url = tool.url.replace(/\[story title\]/, document.title.split(' - ')[0]);
			tool.url = tool.url.replace(/\[story coid\]/, pageProps.coid);
			tool.url = tool.url.replace(/\[site url\]/, location.href);
			tool.url = tool.url.replace(/\[site name\]/, siteinfo.sitename.lc);
			tool.url = tool.url.replace(/\[site key\]/, siteinfo.sitekey.lc);
			anchor.href = tool.url;
			anchor.id = "st_a_" + tool.name;
			anchor.setAttribute('target', '_blank');
			if (tool.popup) {
				YAHOO.util.Event.on(anchor, 'click', newWindow);
			}

			if (tool.icon) {
				var img = document.createElement('img');
				// The following URL will obviously need to be changed when things are for reals yo.
				var imageRoot = location.hostname.substr(0,3) == 'dev' ? 'http://images.ibsys.com/' + siteinfo.sitekey.lc + 'v3-structure' : '' ;
				img.src = imageRoot + "/images/structures/misc/" + tool.name + "_icon_grey.gif";
				var highlightImage = new Image(20,20);
				highlightImage.src = imageRoot + "/images/structures/misc/" + tool.name + "_icon.gif";
				img.id = "st_i_" + tool.name;
				YAHOO.util.Event.on(anchor, 'mouseover', rollover, img);
				YAHOO.util.Event.on(anchor, 'mouseout', rollover, img);
				anchor.appendChild(img);
			}

			var toolText = tool.label + (tool.primary ? ' \u203A\u203A' : '');
			textNode = document.createTextNode(toolText);

			anchor.appendChild(textNode);
			t.appendChild(anchor);
			return t;
		}

		//console.log('createTools');
		var toolbox = document.createElement('div');
		toolbox.id = 'toolbox';
		for (var i=0; i < tools.length; i++) {
			toolbox.appendChild(createTool(tools[i]));
		};
		//YAHOO.util.Event.on(toolbox, 'click', processClick);
		displayTools(toolbox);
	}
	
	function rollover(e,el) {
		var img = el;
		var grey = /icon_grey/;
		var highlight = /icon/;
		if (grey.test(img.src)) {
			img.src = img.src.replace(grey, "icon");
		}else{
			img.src = img.src.replace(highlight, "icon_grey");
		}
	}
	
	function newWindow(e) {
		YAHOO.util.Event.preventDefault(e);
		var anchor=e.target||e.srcElement, i=0, t=IBSYS.features.storyTools.config, args='';
		for (i=0; i<t.length; i++) {
			if (anchor.rel == t[i].name) {
				args = t[i].popupArgs;
				break;
			}
		}
		popUp(anchor.href, args);
		return false;
	}

	function displayTools(toolbox) {
		// This function may not be necessary.
		document.getElementById('StoryTools').appendChild(toolbox);
	}

	return function(tools) {
		YAHOO.util.Event.onDOMReady(function() {
			if (!document.getElementById('toolbox')) {
				if (!tools) {
					tools = [
						{
							name:"email",
							label:"Email",
							url:"http://cf." + siteinfo.sitename.display + "/" + siteinfo.sitekey.lc + "/sh/toafriend/index.cfm?page=[site url]",
							popup:true,
							popupArgs:"width=450,height=250",
							primary:true,
							icon:false,
							url:""
						},
						{
							name:"print",
							label:"Print",
							url:"/print/15288241/detail.html",
							popup:true,
							popupArgs:"width=460,height=400,scrollbars",
							primary:true,
							icon:false,
							url:""
						}
					];
				}
				createTools(tools);
			};
		});
	}
}();
