// -----------------------------------------
//
// common.js
// Common functions used within PaintbrushJS
//
// -----------------------------------------
//
// These are adapted from code examples written by other people, 
// and are excluded from the PaintbrushJS license.
//
// Please see respective URLs for usage and licensing restrictions.
//


// addLoadEvent function, to attach events to page load
// by Simon Willison
// http://simonwillison.net/2004/May/26/addLoadEvent/

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}



//	getElementsByClassName
//	Developed by Robert Nyman, http://www.robertnyman.com
//	Code/licensing: http://code.google.com/p/getelementsbyclassname/
//	Documentation: http://robertnyman.com/2008/05/27/the-ultimate-getelementsbyclassname-anno-2008/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};



// simple check to see whether canvas is supported in this browser
// copied and pasted from http://diveintohtml5.org/detect.html#canvas
function supports_canvas() {
	return !!document.createElement('canvas').getContext;
}



// calculate gaussian blur
// adapted from http://pvnick.blogspot.com/2010/01/im-currently-porting-image-segmentation.html
function gaussianBlur(img, pixels, amount) {

	var width = img.width;
	var width4 = width << 2;
	var height = img.height;
	
	var data = pixels.data;
	
	// compute coefficients as a function of amount
	var q;
	if (amount < 0.0) {
		amount = 0.0;
	}
	if (amount >= 2.5) {
		q = 0.98711 * amount - 0.96330; 
	} else if (amount >= 0.5) {
		q = 3.97156 - 4.14554 * Math.sqrt(1.0 - 0.26891 * amount);
	} else {
		q = 2 * amount * (3.97156 - 4.14554 * Math.sqrt(1.0 - 0.26891 * 0.5));
	}
	
	//compute b0, b1, b2, and b3
	var qq = q * q;
	var qqq = qq * q;
	var b0 = 1.57825 + (2.44413 * q) + (1.4281 * qq ) + (0.422205 * qqq);
	var b1 = ((2.44413 * q) + (2.85619 * qq) + (1.26661 * qqq)) / b0;
	var b2 = (-((1.4281 * qq) + (1.26661 * qqq))) / b0;
	var b3 = (0.422205 * qqq) / b0; 
	var bigB = 1.0 - (b1 + b2 + b3); 
	
	// horizontal
	for (var c = 0; c < 3; c++) {
		for (var y = 0; y < height; y++) {
			// forward 
			var index = y * width4 + c;
			var indexLast = y * width4 + ((width - 1) << 2) + c;
			var pixel = data[index];
			var ppixel = pixel;
			var pppixel = ppixel;
			var ppppixel = pppixel;
			for (; index <= indexLast; index += 4) {
				pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
				data[index] = pixel; 
				ppppixel = pppixel;
				pppixel = ppixel;
				ppixel = pixel;
			}
			// backward
			index = y * width4 + ((width - 1) << 2) + c;
			indexLast = y * width4 + c;
			pixel = data[index];
			ppixel = pixel;
			pppixel = ppixel;
			ppppixel = pppixel;
			for (; index >= indexLast; index -= 4) {
				pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
				data[index] = pixel;
				ppppixel = pppixel;
				pppixel = ppixel;
				ppixel = pixel;
			}
		}
	}
	
	// vertical
	for (var c = 0; c < 3; c++) {
		for (var x = 0; x < width; x++) {
			// forward 
			var index = (x << 2) + c;
			var indexLast = (height - 1) * width4 + (x << 2) + c;
			var pixel = data[index];
			var ppixel = pixel;
			var pppixel = ppixel;
			var ppppixel = pppixel;
			for (; index <= indexLast; index += width4) {
				pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
				data[index] = pixel;
				ppppixel = pppixel;
				pppixel = ppixel;
				ppixel = pixel;
			} 
			// backward
			index = (height - 1) * width4 + (x << 2) + c;
			indexLast = (x << 2) + c;
			pixel = data[index];
			ppixel = pixel;
			pppixel = ppixel;
			ppppixel = pppixel;
			for (; index >= indexLast; index -= width4) {
				pixel = bigB * data[index] + b1 * ppixel + b2 * pppixel + b3 * ppppixel;
				data[index] = pixel;
				ppppixel = pppixel;
				pppixel = ppixel;
				ppixel = pixel;
			}
		}
	} 
	
	return(pixels);
}
