// --------------------------------------------------------------------
// From mashimonator to Metaphase with love.
// --------------------------------------------------------------------

/*@cc_on 
var doc = document;
eval('var document = doc');
try{document.execCommand('BackgroundImageCache', false, true);}catch(e){}
@*/
var mashimonator = {
	constant : {
		cmd : '|38|38|40|40|37|39|37|39|66|65|',
		targetTag : [ 'IMG', 'A', 'SPAN' ],
		vibrateInterval : 20,
		fallInterval : 25,
		bgColor : 'transparent',
		url1 : 'http://works.metaphase.co.jp/',
		url2 : 'http://labs.metaphase.co.jp/blog/coder/',
		url3 : 'http://www.metaphase.co.jp/service/flashbook/'
	},
	storage : {
		cmd : '|',
		count : 0
	},
	init : function() {
		if ( window.onkeydown ) {
			mashimonator.addEvent( window, 'keydown', mashimonator.konamiCmd );
		} else {
			mashimonator.addEvent( document, 'keydown', mashimonator.konamiCmd );
		}
	},
	konamiCmd : function( event ) {
		if ( !event ) {
			event = window.event;
		}
		if ( !mashimonator.constant.cmd.match('\|' + event.keyCode + '\|') ) {
			mashimonator.storage.cmd = '|';
		} else {
			mashimonator.storage.cmd += event.keyCode + '|';
			if ( mashimonator.constant.cmd == mashimonator.storage.cmd ) {
				mashimonator.storage.cmd = '|';
				mashimonator.prank();
			}
		}
	},
	prank : function() {
		var len = mashimonator.constant.targetTag.length;
		for ( var x = 0; x < len; x++ ) {
			if ( mashimonator.constant.targetTag[x].match(/img/i) ) {
				var img = document.getElementsByTagName(mashimonator.constant.targetTag[x]);
				var len1 = img.length;
				for ( var i = 0; i < len1; i++ ) {
					var layer = mashimonator.createLayer4Image(img[i]);
					img[i].style.visibility = 'hidden';
					layer.id = 'mashimonator' + mashimonator.storage.count + 'Img';
					var count = ((Math.random()+1)*5) * 3;
					mashimonator.vibrate(layer.id, count);
					mashimonator.storage.count += 1;
				}
			} else {
				var elm = document.getElementsByTagName(mashimonator.constant.targetTag[x]);
				var len2 = elm.length;
				for ( var i = 0; i < len2; i++ ) {
					var layer = mashimonator.createLayer(elm[i]);
					elm[i].style.visibility = 'hidden';
					layer.id = 'mashimonator' + mashimonator.storage.count + 'Block';
					var count = ((Math.random()+1)*5) * 3;
					mashimonator.vibrate(layer.id, count);
					mashimonator.storage.count += 1;
				}
			}
		}
	},
	vibrate : function( id, count ) {
		var element = document.getElementById(id);
		var pos = mashimonator.getElementPosition(element);
		var left = pos.left + mashimonator.getVibrateValue();
		var top = pos.top + mashimonator.getVibrateValue();
		var scSize = mashimonator.getScreenSize();
		mashimonator.setPosition( element, top, left );
		count -= 1;
		if ( count > 0 ) {
			window.setTimeout('mashimonator.vibrate(\''+ id +'\','+count+')', mashimonator.constant.vibrateInterval);
		} else {
			mashimonator.fall(id);
		}
	},
	getVibrateValue : function() {
		var sign = parseInt(Math.random()*10) % 2;
		var val = parseInt(Math.random()*3)+1;
		switch( sign ) {
			case 0:
				sign = 1;
				break;
			case 1:
				sign = -1;
				break;
		}
		return (val * sign);
	},
	fall : function( id ) {
		var element = document.getElementById(id);
		var scSize = mashimonator.getScreenSize();
		var pos = mashimonator.getElementPosition(element);
		pos.top = pos.top + (document.documentElement.scrollTop || document.body.scrollTop);
		if ( scSize.height > pos.top ) {
			element.style.top = (pos.top + 5) + 'px';
			window.setTimeout('mashimonator.fall(\''+ id +'\')', mashimonator.constant.fallInterval);
		} else {
			element.style.display = 'none';
			mashimonator.end( element );
		}
	},
	forceRerendering : function( element ){
		try {
			var n = document.createTextNode(' ');
			element.appendChild(n);
			element.removeChild(n);
		} catch(e) {}
	},
	end : function( element ) {
		mashimonator.storage.count -= 1;
		if ( mashimonator.storage.count > 0 ) {
			mashimonator.forceRerendering( document );
			window.setTimeout('mashimonator.reload()', 8000);
		}
	},
	reload : function() {
		var ran = parseInt(Math.random()*3);
		switch ( ran ) {
			case 0:
				location.href = mashimonator.constant.url1;
				break;
			case 1:
				location.href = mashimonator.constant.url3;
				break;
			default:
				location.href = mashimonator.constant.url2;
				break;
		}
	},
	createLayer4Image : function( image ) {
		var size = mashimonator.getElementSize(image);
		var pos = mashimonator.getElementPosition(image);
		var div = document.createElement('div');
		div.style.zIndex = '200';
		div.style.margin = '0px';
		div.style.padding = '0px';
		div.style.position = 'absolute';
		div.style.top = pos.top + 'px';
		div.style.left = pos.left + 'px';
		div.style.width = size.width + 'px';
		div.style.height = (size.height) + 'px';
		div.style.backgroundColor = mashimonator.constant.bgColor;
		var img = document.createElement('img');
		div.appendChild(img);
		img.style.margin = '0px';
		img.style.padding = '0px';
		img.width = '';
		img.height = '';
		img.width = size.width;
		img.height = size.height;
		img.setAttribute('width', size.width);
		img.setAttribute('height',size.height);
		img.src = image.src;
		document.body.appendChild(div);
		return div;
	},
	createLayer : function( element ) {
		var size = mashimonator.getElementSize(element);
		var pos = mashimonator.getElementPosition(element);
		var style = element.currentStyle || document.defaultView.getComputedStyle(element, '');
		var div = document.createElement('div');
		div.style.zIndex = '200';
		div.style.margin = '0px';
		div.style.padding = '0px';
		div.style.position = 'absolute';
		div.style.top = pos.top + 'px';
		div.style.left = pos.left + 'px';
		div.style.width = size.width + 'px';
		div.style.height = (size.height) + 'px';
		div.style.background = style.background;
		//div.style.backgroundColor = mashimonator.constant.bgColor;
		div.innerHTML = element.parentNode.innerHTML;
		document.body.appendChild(div);
		return div;
	},
	setPosition : function( element, top, left ) {
		element.style.top = top + 'px';
		element.style.left = left + 'px';
	},
	getElementPosition : function( element ) {
		var offsetTrail = (typeof element == 'string') ? document.getElementById(element): element;
		var x = 0;
		var y = 0;
		while (offsetTrail) {
			x += offsetTrail.offsetLeft;
			y += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if ( navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined' ) {
			x += document.body.leftMargin;
			y += document.body.topMargin;
		}
		return ({ 'left': x, 'top': y });
	},
	getElementSize : function( element ) {
		var w = element.offsetWidth;
		var h = element.offsetHeight;
		return ({ 'width': w, 'height': h });
	},
	getScreenSize : function() {
		var w = 0;
		var h = 0;
		if ( window.innerWidth ) {
			w = window.innerWidth;
		} else if ( document.documentElement && document.documentElement.clientWidth != 0 ) {
			w = document.documentElement.clientWidth;
		} else if ( document.body ) {
			w = document.body.clientWidth;
		}
		w = (document.documentElement.scrollLeft || document.body.scrollLeft) + w;
		if ( window.innerHeight ) {
			h = window.innerHeight;
		} else if ( document.documentElement && document.documentElement.clientHeight != 0 ) {
			h = document.documentElement.clientHeight;
		} else if ( document.body ) {
			h = document.body.clientHeight;
		}
		h = (document.documentElement.scrollTop || document.body.scrollTop) + h;
		return ({ 'width': w, 'height': h });
	},
	getTargetElements : function( tag, cls ) {
		var elements = new Array();
		var targetElements = document.getElementsByTagName(tag.toUpperCase());
		var len = targetElements.length;
		for ( var i = 0; i < len; i++ ) {
			if ( targetElements[i].className.match(cls) ) {
				elements.push(targetElements[i]);
			}
		}
		return elements;
	},
	addEvent : function( target, event, func ) {
		try {
			target.addEventListener(event, func, false);
		} catch (e) {
			target.attachEvent('on' + event, (function(el){return function(){func.call(el);};})(target));
		}
	}
};
mashimonator.addEvent( window, 'load', mashimonator.init );