// ==UserScript==
// @name           AutoReloader
// @namespace      http://log.sncr.jp/11/
// @description    Auto reloader for Wonderfl
// @include        http://wonderfl.net/code/*
// ==/UserScript==

(function(){
	
	var containerDiv = document.createElement('div');
    containerDiv.setAttribute('style', 'padding:2px;position:fixed;'
                                     + 'bottom:-1px;right:-1px;font-size:10px;'
                                     + 'background:#fff;color:#000;border:1px solid #ccc;'
                                     + 'z-index:256;text-align:left;font-weight:normal;'
                                     + 'line-height:120%;font-family:verdana;'
	                                 + 'width:175px;height:14px;')
	
	var title = document.createElement('p');
	title.setAttribute('style', 'float:left; margin:0 5px 0 3px');
	title.innerHTML = 'AutoReloader';
	
	var COLOR = ['#0f0', '#ccc', '#f00'];
	
	var colorIcon = document.createElement('span');
	
    function setColor(num) {
		colorIcon.setAttribute('style', 'float:left;width:4px;height:12px;margin-left:2px;'
                                     + 'border:1px solid #ccc;border-right:none;'
                                     + 'background-color:' + COLOR[num] + ';');
	}
	
	var delayInput = document.createElement('input');
    delayInput.setAttribute('type', 'text');
    delayInput.setAttribute('value', '10');
    delayInput.setAttribute('size', '2');
	delayInput.setAttribute('style', 'margin:0 3px 3px 0;border:1px solid #ccc;float:left;');
	
	var sec = document.createElement('p');
	sec.setAttribute('style', 'float:left; margin:0 2px 0 0');
	sec.innerHTML = 'sec';
	
	var toggleButton = document.createElement('a');
	toggleButton.setAttribute('style', 'float:left;background-color:#eee;'
	                                 + 'border:1px solid #ccc;text-decoration:none;'
	                                 + 'width:35px;text-align:center;outline:none;');
	toggleButton.href = 'javascript:void(0)';
	
	var timer;
	
	// 実行
	function executeAutoReload()
	{
		// 数字かどうかチェック
		if (delayInput.value.match(/[^0-9]+/)) return; 
		delayInput.setAttribute('disabled', 'disabled');
		
		timer = setInterval(autoReload, delayInput.value * 1000);
		autoReload();
		setColor(2);
		toggleButton.removeEventListener('click', executeAutoReload, false)
		toggleButton.addEventListener('click', interruptAutoReload, false)
		toggleButton.innerHTML = 'Stop'
	}
	
	// 中断
	function interruptAutoReload()
	{
		clearInterval(timer);
		setColor(0);
		delayInput.removeAttribute('disabled', 'disabled')
		toggleButton.removeEventListener('click', interruptAutoReload, false)
		toggleButton.addEventListener('click', executeAutoReload, false)
		toggleButton.innerHTML = 'Start'
	}

	function autoReload()
	{
		var elem = document.getElementById("reload_button");
		var e = document.createEvent( "MouseEvents" );
		e.initEvent( "click", false, true );
		elem.dispatchEvent( e );
	}
	
	function init()
	{
		document.body.appendChild(containerDiv);
		
		containerDiv.appendChild(title)
		containerDiv.appendChild(delayInput)
		containerDiv.appendChild(sec)
		containerDiv.appendChild(colorIcon)
		containerDiv.appendChild(toggleButton)
		
		interruptAutoReload();
	}
	init();
	
})();
