/**
  * Javascript toolkit.
  * Lots of usefull tools for your front-end.
  * Configure at the bottom of this page, in the run() function
  *
  * LICENCE:	GPL
  *
  * @copyright	2007 iSource B.V.
  * @Author		Gabor de Mooij, Guus van de Garde, Kasper Vrind
  * @version	1
  */

JsToolkit = function(){

	/* ========================================================== */
	// Main method
	/* ========================================================== */
	
	/**
	  * Add a tool to a page node
	  * Sets tool configuration and runs its method
	  *
	  * Example config object (see @param):
	  *
	  * {
			toolName:'fontSmaller',
			nodeId:'toolkit1',
			contentNodes:'kolom1,kolom2,kolom3,searchresult,titel,projecten,submenu',
			'html':'<img id="lettergrootte" src="/img/fontresize_smaller.gif" alt="Lettergrootte" />'
		}
	  *
	  * @param	obj	tool	An object with tool configs (toolname, node id to place tool in, node id's to affect with the tool, html to display in the tool node)
	  *
	  */
	  

	
	
	this.getNode = function(rawNode)
	{
		return rawNode.replace(/<!-- toolkitnodeSTART -->.*?<!-- toolkitnodeEND -->/,'');	
	}
	
	this.addTool = function(tool)
	{
		
		// Set the name of the tool (= name of tool method)
		this.toolName			= tool.toolName;
		
		// Set the id of the node to put the tool in
		var iconBoxId	 		= tool.nodeId;
		
		// Set nodes to affect
		var contentBoxString 	= tool.contentNodes;			// id's of nodes to affect with the tool
		var contentBoxes 		= contentBoxString.split(",");		// split into array of node id's
		

		if (typeof(this.nodes) != 'undefined'){
			this.toolNumber			= this.nodes.length;
		} else {
			this.nodes			= [];								// array of nodes to affect	
			this.toolNumber		= 0;
		}
		this.nodes[this.toolNumber]		= [];
		
		

		for (var j in contentBoxes)
		{
			if (document.getElementById(contentBoxes[j])){ // only the elements showed op the site				
		   		this.nodes[this.toolNumber].push(document.getElementById(contentBoxes[j]));
			}
		}
		
		// Set the HTML to display for the tool
		this.toolHTML 			= tool.html;

		// Set some extra params for the tool
		this.toolParams			= tool.params;
						
		// Get current HTML from toolbar node
		if (document.getElementById(iconBoxId)){
			this.iconBoxNode = document.getElementById(iconBoxId);
			this.currentHTML = this.iconBoxNode.innerHTML;

		} else if (jQuery("." + iconBoxId)){ // not fully tested
			this.iconBoxNode = jQuery("." + iconBoxId);
			
			this.currentHTML = this.iconBoxNode.innerHTML;
		} else {
			return false;	
		}
		
		// Execute method
		this[this.toolName]();
	}
	
	
	/* ========================================================== */
	// Tool methods
	/* ========================================================== */
	
	/**
	  * Add a custom (this site only) tool to the toolkit
	  * 
	  * @param	str			toolname	The name of the tool
	  * @param	function 	func		The function for the tool
	  */
	this.addNewTool = function(toolName,func)
	{
		this[toolName] = func;
	}
	
	
	/**
	  * Adds a downsize fonts tool
	  *
	  */
	this.fontSmaller = function()
	{
		// IE/FF take their computed font-size differently
		// set method params in different sizes
		this.px	= this.toolParams.px;
		this.pt	= this.toolParams.pt;
		this.em	= this.toolParams.em;
		
		var toolIcon = '<a class="'+this.toolName+'" href="javascript:jsToolkit.resize(-1 , ' + this.toolNumber + ');">'+this.toolHTML+'</a>';
		this.iconBoxNode.innerHTML = this.currentHTML + toolIcon;

	}
	
	this.printPage = function()
	{
		if(this.toolParams.logo){
			this.logo = this.toolParams.logo;
		}
		
		var toolIcon = '<a class="'+this.toolName+'" href="javascript:jsToolkit.printPopPage(' + this.toolParams.newWindow +',' + this.toolParams.withTools +',\'' + this.toolParams.styleSheet + '\', ' + this.toolNumber + ');">'+this.toolHTML+'</a>';
		this.iconBoxNode.innerHTML = this.currentHTML + toolIcon;
	 
	}
	
	/**
	  * Adds ads upsize fonts tool
	  *
	  */
	this.fontLarger = function()

	{
		// IE/FF take their computed font-size differently
		// set method params in different sizes
		this.px	= this.toolParams.px;
		this.pt	= this.toolParams.pt;
		this.em	= this.toolParams.em;		
	
		var toolIcon = '<a class="'+this.toolName+'" href="javascript:jsToolkit.resize(1 , ' + this.toolNumber + ');">'+this.toolHTML+'</a>';
	
		this.iconBoxNode.innerHTML = this.currentHTML + toolIcon;
	 
	}




	/* ========================================================== */
	// Helper methods
	/* ========================================================== */
	
	/**
	  * Resize fonts in configured nodes.
	  *
	  * @required	Required to run addToolbar method
	  * @param		str size The number of steps (in pixels) to resize, positive to upsize or negative to downsize
	  *
	  */
	this.resize = function(upOrDown , toolNumber )
	{
		var upOrDown = parseInt(upOrDown, 10);
		
		// Get the current font size for configured nodes

		for (var i in this.nodes[toolNumber])
		{

			resizeNode(this.nodes[toolNumber][i] , upOrDown , this.px , this.em , this.pt);
		}
	}
	

	
	/**
	  * Print all the nodes in popup
	  *
	  * @required	Required to run addToolbar method
	  * @param		str size The number of steps (in pixels) to resize, positive to upsize or negative to downsize
	  *
	  */
	this.printPopPage = function(newWindow , toolbars, styleSheet , toolNumber){
		if (newWindow === true){
			
			win = window.open("", 'pop' , 'status=0,location=00,scrollbars=1,width=' + (screen.width-300) + ',height=' + (screen.height-300) + ', top=150, left=150');

			var html = "<html>\n<head>\n<title>\n</title>\n";

			//html += "<script type=\"text/javascript\" language=\"javascript\" src=\"/js/jstoolkit.js\"></script>";
			html += "<link href=\""+ styleSheet +"\" rel=\"stylesheet\" type=\"text/css\" />\n";
									
			html 	+= "</head>\n<body>\n";
			
			if (this.logo){
				html += "<img id=\"logo\" src=\"" + this.logo + "\" alt=\"\" />";	
			}

			for (var i in this.nodes)
			{
				
				if(this.nodes[toolNumber][i] && i >= 0)
				{
					html += '<div id="' + this.nodes[toolNumber][i].id + '">' + this.getNode(this.nodes[toolNumber][i].innerHTML) + '</div>';
				}
			}
			html += '</body></html>';
			win.document.write(html);
			html='';
			
			win.document.close();
			win.resizeTo((screen.width-300), (screen.height-300));
			win.focus();
						

						
			setTimeout(
				function(){
				win.print();
			} , 2000);
		} else {
			window.print();
		}
	}
}

function resizeNode(node , upOrDown , px , em , pt){
	if(typeof(node) != 'undefined' && node.toString().indexOf('function') == -1)
	{

	//	if (typeof(node.childNodes) == 'undefined' && node.childNodes.length > 0){
		for (var i in node.childNodes)
		{			
			resizeNode(node.childNodes[i] , upOrDown , px , em , pt);
		} 
	//	}

		if (typeof(node.style) == 'undefined'){
			// all the elements are copied so the font will increse with them
			return false;	
		}
		
		if (node.style.fontSize){
			var currentFontSize = node.style.fontSize;
		} else if (node.currentStyle)
		{ 
			//	IE 
			var currentFontSize = node.currentStyle['fontSize'];	
		}
		else if (window.getComputedStyle)
		{ 
			//	FF, Opera 
			var currentFontSize = document.defaultView.getComputedStyle(node,null).getPropertyValue('font-size');
		} 			

		if(currentFontSize.indexOf('em', currentFontSize) >= 1)
		{
			currentFontSize = parseFloat(currentFontSize.replace('em',''));
			
			var newSize = currentFontSize + (em * upOrDown);
			newSize = Math.round(newSize*100)/100;
			newSize = newSize+'em';
		}
		else if(currentFontSize.indexOf('pt', currentFontSize) >= 1)
		{
			currentFontSize = parseFloat(currentFontSize.replace('pt',''));
			
			var newSize = currentFontSize + (pt * upOrDown);
			newSize = newSize+'pt';
		}
		else if(currentFontSize.indexOf('px', currentFontSize) >= 1)
		{
			currentFontSize = parseFloat(currentFontSize.replace('px',''));
			
			var newSize = currentFontSize + (px * upOrDown);
			newSize = newSize+'px';
		}

		node.style.fontSize = newSize;
	}	
	
}



/*
 * Load toolkit init, called in contentmanager
 */
run = function()
{
	jsToolkit = new JsToolkit();
		jsToolkit.addTool({
							 
					toolName:'fontSmaller',					
				 				
							 
					nodeId:'toolkit1',					
				 				
							 
					contentNodes:'two,three',					
				 				
							 
					html:'A',					
				 				
												params:{'px':2,'em':0.2,'pt':2}					
				 				
				});
		jsToolkit.addTool({
							 
					toolName:'fontLarger',					
				 				
							 
					nodeId:'toolkit2',					
				 				
							 
					contentNodes:'two,three',					
				 				
							 
					html:'A',					
				 				
												params:{'px':2,'em':0.2,'pt':2}					
				 				
				});
		jsToolkit.addTool({
							 
					toolName:'printPage',					
				 				
							 
					nodeId:'toolkit3',					
				 				
							 
					contentNodes:'two',					
				 				
							 
					html:'<img src="/img/layout/print.gif" alt="Print" />',					
				 				
												params:{'newWindow':true , 'withTools':true , 'styleSheet':'/css/document_print.css' , 'logo':'/files/img/Briefpapier.png'}					
				 				
				});
	}
setTimeout(function(){
	run();
},1000);
