//Weston Ruter, 2007-10-17

window.intervalID = 0;
window.useOpacityProperty = !(!document.addEventListener && document.attachEvent);

window.lastHash = window.location.hash;
window.lastImage = null;
window.lastOpacity = null;



function toggleOverlay(image, opacity){
	if(!image)
		return;
	
	window.clearInterval(intervalID);
	var img;
	var matches;
	if(img = document.getElementById('designOverlay')){
		//if(window.console && console.info)console.info('toggleOverlay on');
		if(!(img.src.indexOf(image) >= 0)){
			img.src = image;
		}
		else {
			window.intervalID = window.setInterval(function(){
				var newOpacity = 0;
				if(useOpacityProperty)
					newOpacity = parseFloat(img.style.opacity);
				else if(matches = String(img.style.filter).match(/opacity=(\d+)/))
					newOpacity = parseInt(matches[1])/100;
					
				if(!newOpacity)
					newOpacity = opacity;
				newOpacity -= 0.1;
				if(useOpacityProperty)
					img.style.opacity = newOpacity;
				else
					img.style.filter = "alpha(opacity=" + (newOpacity*100) + ");";
					
				if(newOpacity <= 0.0){
					window.clearInterval(intervalID);
					img.parentNode.removeChild(img);
				}
			}, 10);
		}
	}
	else {
		//if(window.console && console.info)console.info('toggleOverlay off');
		
		img = document.createElement('img');
		img.src = image;
		img.style.opacity = 0.0;
		img.style.filter = "alpha(opacity=0)";
		img.style.position = 'absolute';
		img.style.top = "0px";
		img.style.left = "0px";
		img.style.zIndex = 100;
		img.id = "designOverlay";
		document.body.appendChild(img);
		
		window.intervalID = window.setInterval(function(){
			var newOpacity = 0;
			if(useOpacityProperty)
				newOpacity = parseFloat(img.style.opacity);
			else if(matches = String(img.style.filter).match(/opacity=(\d+)/))
				newOpacity = parseInt(matches[1])/100;
			
			if(!newOpacity)
				newOpacity = 0;
			newOpacity += 0.1;
			if(useOpacityProperty)
				img.style.opacity = newOpacity;
			else
				img.style.filter = "alpha(opacity=" + (newOpacity*100) + ");";
			if(newOpacity >= opacity)
				window.clearInterval(intervalID);
		}, 10);
	}
};


function keypressHandler(e){
	e = e || window.event;
	var key = e.keyCode || e.which;
	if(key == 32){
		if(window.lastImage){
			toggleOverlay(window.lastImage, window.lastOpacity);
			if(e.preventDefault)
				e.preventDefault();
			return false;
		}
	}
	//console.info(key)
	//return key;
}

//document.onkeypress = onkeypress;

if(document.addEventListener)
	document.addEventListener('keypress', keypressHandler, false);
else if(document.attachEvent)
	document.attachEvent('onkeypress', keypressHandler);


	

function getParams(){
	var matches;
	if(matches = window.location.hash.match(/overlayOpacity=(\d+(\.\d+)?)/))
		window.lastOpacity = parseFloat(matches[1]);
	else if(matches = window.location.search.match(/overlayOpacity=(\d+(\.\d+)?)/))
		window.lastOpacity = parseFloat(matches[1]);
	
	if(matches = window.location.hash.match(/overlayImage=(.+?)(?:$|&)/))
		window.lastImage = matches[1];
	else if(matches = window.location.search.match(/overlayImage=(.+?)(?:$|&)/))
		window.lastImage = matches[1];
}
getParams();

window.setInterval(function(){
	if(window.lastHash != window.location.hash){
		getParams();
		if(window.lastOpacity && window.lastOpacity)
			toggleOverlay(window.lastImage, window.lastOpacity);
		window.lastHash = window.location.hash;
	}
}, 100);

