//////////////////////////////////////////////////////////////////////////
////////// NETRASOFT JAVASCRIPT LIBRARY FUNCTIONS (IMAGE) ////////////////
//////////////////////////////////////////////////////////////////////////
//
// (c) Netrasoft 2005
// Web:		http://www.netrasoft.co.uk
// Email:	info@netrasoft.co.uk
//
// File Info:		Image Functions
// Version: 		1.02
// Release Date: 	28/04/06
//
///////////////////////////////////////////////////////////////////////////


// function: 	p_to_image (string class_name, string image_script)
// purpose:		replaces the content of a paragraph with an image containing the same text
// parameters:	[1] class_name (string) -> the name of the class to search for
//				[2] image_script (string) -> the path to the image script that will replace the text
// returns:		nothing
// NOTE:		requires php library nslib_image.php
function p_to_image (class_name, image_script) {
	// get all the paragraph elements into an array
	var doc = document.getElementsByTagName('p');
	// loop through all of the elements
	for (var i = 0; i < doc.length; i++){
		// if the classname matches the specified one
		if(doc[i].className == class_name) {
			// replace all xhtml line breaks with html 4.0 ones
			p_lines = doc[i].innerHTML.replace('<br />', '<br>');
			// replace all space charachters wit real spaces
			p_lines = p_lines.replace('&nbsp;', ' ');
			// remove all problem charachters
			p_lines = p_lines.replace('#','');
			p_lines = p_lines.replace('&','');
			p_lines = p_lines.replace('?','');
			// convert to lower case
			p_lines = p_lines.toLowerCase();
			// split the elements up by line breaks
			p_lines = p_lines.split('<br>');
			// initialise an array to store the final image names
			p_lines_img = new Array;
			// looup though all the lines
			for (x = 0; x < p_lines.length; x++) {
				// if the text is not blank
				if (p_lines[x] != '' && p_lines[x] != ' ') {
					// calculate the images script string based on each string
					p_lines_img[x] = '<img src="'+image_script+'?text='+p_lines[x]+'" />';
				}
			}
			// join the lines together with line breaks and swap the paragraph text with the images
			doc[i].innerHTML = p_lines_img.join('<br>');
		}
	}
}


// function: 	p_to_image2 (string class_name, string image_script_path)
// purpose:		replaces the content of a paragraph with an image containing the same text
//				using mutiple classnames
// parameters:	[1] class_names (string) -> the names of the classes to search for
//				[2] image_script_path (string) -> the path to the image scripts that will replace the text
// returns:		nothing
// NOTE:		requires php library nslib_image.php
function p_to_image2 (class_names, image_script_path) {
	// remove commas from classname string
	class_names = class_names.replace(',','');
	// get all the paragraph elements into an array
	var doc = document.getElementsByTagName('p');
	// loop through all of the elements
	for (i in doc) {
		// if the paragraph has a class
		if (typeof(doc[i].className) != 'undefined') {
			// if the class is in the list
			if (class_names.indexOf(doc[i].className) > -1 && doc[i].className != '') {
				for (j in doc[i].childNodes) {
					nodeobj = doc[i].childNodes[j];
					if(nodeobj.nodeName == '#text') {
						textval = nodeobj.nodeValue;
						if (textval != ' ') {
							doc[i].innerHTML = doc[i].innerHTML.replace(textval, '<img src="'+image_script_path+doc[i].className+'.php?text='+textval+'" />');
						}
					}
					for (k in nodeobj.childNodes) {
						if(nodeobj.childNodes[k].nodeName == '#text') {
							textval2 = nodeobj.childNodes[k].nodeValue;
							if (textval2 != ' ') {
								doc[i].innerHTML = doc[i].innerHTML.replace(textval2, '<img src="'+image_script_path+doc[i].className+'.php?text='+textval2+'" />');
							}
						}
					}
				}
			}
		}
	}
}


// function: 	resize_window_to_image (object img_object)
// purpose:		resizes a popup window created by the window.open method to the size of the
//				specified image within that document
// parameters:	[1] img_object (object) -> the image object to resize to
// returns:		nothing
// NOTE:		call this function using onload="resize_window_to_image(this);" within the
//				image tag
// NOTE:		do not include a doctype in the popup window as XTHML Transitional & Strict
//				causes the script not to work in IE
function resize_window_to_image (img_object) {
	// get the current objects dimensions
	imageWidth = img_object.width;
	imageHeight = img_object.height;
	// if the browser can use the innerWidth method
	if (window.innerWidth) {
		// get the window dimensions that way
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else {
		// otherwise use the clientWidth method
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// calculate the difference between the image and the window
	diffWidth = imageWidth - windowWidth;
	diffHeight = imageHeight - windowHeight;
	// resize the window by that amount
	window.resizeBy(diffWidth, diffHeight);
}
