/*jslint eqeqeq: true, browser: true */
/*global window, $, jQuery */ 
/*JP 17/07/09*/
/*  JS Linted - All problems Solved before Minifying aparton from a "used before it was defined error" on line 6 as the function it sets, calls the variable!*/
/*JP END*/
var FadePeeps = new FadeCutOutPeeps();

function FadeCutOutPeeps() {

	var that = {};

	function changeOpac(opacity, i) {
		var object = FadePeeps.CutOutPeeps[i].obj.style;
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";

		if (SupportsAlpha(object)) {
			object.filters.alpha.opacity = FadePeeps.CutOutPeeps[i].iOpacity;
		}
	}

	function SupportsAlpha(obj) {
		if (obj.filters) {
			if (obj.filters.alpha) {
				return true;
			}
		}

		return false;
	}

	function GetElementsByClassName(className) {
		var results = [];
		walkTheDOM(document.body, function (node) {
			var a, c = node.className,
			i;
			if (c) {
				a = c.split(' ');
				for (i = 0; i < a.length; i += 1) {
					if (a[i] === className) {
						results.push(node);
						break;
					}
				}
			}
		});
		return results;
	}

	function walkTheDOM(node, func) {
		func(node);
		node = node.firstChild;
		while (node) {
			walkTheDOM(node, func);
			node = node.nextSibling;
		}
	}

	/* Public functions */

	that.CutOutPeeps = [];

	that.Hide = function () {
		var AllPeeps = GetElementsByClassName('cutout');

		for (i = 0; i < AllPeeps.length; i++) {
			that.CutOutPeeps[i] = {};
			that.CutOutPeeps[i].obj = AllPeeps[i];
			that.CutOutPeeps[i].iOpacity = 0;
			changeOpac(that.CutOutPeeps[i].iOpacity, i);
		}
	};

	that.Show = function () {
		window.setTimeout(FadePeeps.ShowNoPause, 1750);
	};

	that.ShowNoPause = function () {
		var AllPeeps = GetElementsByClassName('peeps');

		for (i = 0; i < AllPeeps.length; i++) {
			that.CutOutPeeps[i] = {};
			that.CutOutPeeps[i].obj = AllPeeps[i];
			that.CutOutPeeps[i].iOpacity = 0;
			that.CutOutPeeps[i].fadeInterval = setInterval("FadePeeps.FadeIn('" + i + "')", 100);
		}
	};

	that.FadeIn = function (i) {
		if (FadePeeps.CutOutPeeps[i].iOpacity >= 100) {
			FadePeeps.CutOutPeeps[i].fadeInterval = clearInterval(FadePeeps.CutOutPeeps[i].fadeInterval);
		}

		FadePeeps.CutOutPeeps[i].iOpacity += 10;
		changeOpac(FadePeeps.CutOutPeeps[i].iOpacity, i);
	};

	that.MixEmUp = function () {
		//document.getElementById('peepsBox1').className = 'blob-new-skills-0' + (Math.floor(Math.random()*6) + 1);
		//document.getElementById('peepsBox2').className = 'blob-new-job-0' + (Math.floor(Math.random()*6) + 1);
		//document.getElementById('peepsBox3').className = 'blob-new-people-0' + (Math.floor(Math.random()*6) + 1);
		//document.getElementById('peepsBox4').className = 'blob-just-looking-0' + (Math.floor(Math.random()*6) + 1);
		document.getElementById('peepsBox1').className = 'blob-new-skills-05';
		document.getElementById('peepsBox2').className = 'blob-new-job-01';
		document.getElementById('peepsBox3').className = 'blob-new-people-06';
		document.getElementById('peepsBox4').className = 'blob-just-looking-06';

		document.getElementById('blobs').style.visibility = 'visible';
	}

	return that;
}

if (window.addEventListener) {
	window.addEventListener('load', FadePeeps.Show, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', FadePeeps.Show);
}
// for testing
//javascript:FadePeeps.Show();
//javascript:FadePeeps.Hide();