window.onload = init;

var clickCount = 0;
var clickedAll;

function init() {

	var photoWrapper = document.getElementById('photo_wrapper');
	
	if (photoWrapper) {
	
		photoWrapper.hasAttribute ? photoWrapper.setAttribute('class','active') : photoWrapper.className = 'active';
	};
	
	addListener(photoWrapper, 'click', fadeBlock, false);
};

function fadeBlock(event) {
	setEventTargets(event);
	if (eTarget.hasAttribute) {
		if (eTarget.getAttribute('class') == 'box') {
			eTarget.setAttribute('class','');
			clickCount++;
		};
		
	} else {
		if (eTarget.className == 'box') {
			eTarget.className = '';
			clickCount++;
		};
	};
	
	if (clickedAll == 'yes') {
		clickCount++;
	};
	
	if (clickCount == 2) {
		alert('Continue to click the blocks to reveal the photo below...');
	} else if (clickCount == 12) {
		alert('There are quite a few blocks left, keep clicking.');
	} else if (clickCount == 21) {
		alert('Keep going, you are doing a great job.');
	} else if (clickCount == 32) {
		alert('Don\'t give up.');
	} else if (clickCount == 43) {
		alert('Wonderfully clicked.');
	} else if (clickCount == 49) {
		alert('Motivation comes from within.');
	} else if (clickCount == 57) {
		alert('Getting tired?');
	} else if (clickCount == 70) {
		alert('Click...');
	} else if (clickCount == 71) {
		alert('Click, click...');
	} else if (clickCount == 72) {
		alert('Click, click, click...');
	} else if (clickCount == 81) {
		alert('You can do it.');
	} else if (clickCount == 100) {
		alert('What\s that clicking sound...');
	} else if (clickCount == 101) {
		alert('...oh yeah, nevermind.');
	} else if (clickCount == 157) {
		alert('Phew, this is hard.');
	} else if (clickCount == 177) {
		alert('You are just about there!');
	} else if (clickCount == 180) {
		alert('Great job, you did it!');
		clickedAll = 'yes';
	} else if (clickCount == 181) {
		alert('There are no more boxes to click.');
	} else if (clickCount == 182) {
		alert('You have completed the challenge.');
	} else if (clickCount > 182) {
		alert('Please stop clicking me.');
	};
};

function addListener(obj, triggerEvent, callFunction, capture) {
	obj.addEventListener ? obj.addEventListener(triggerEvent, callFunction, capture) : obj.attachEvent('on' + triggerEvent, callFunction);
};

function setEventTargets(event) {
	event.target ? eTarget = event.target : eTarget = event.srcElement;
	event.relatedTarget ? eRelatedTarget = event.relatedTarget : eRelatedTarget = event.toElement;
	eTargetParent = eTarget.parentNode;
};