// ==UserScript==
// @name           auto bank-in
// @namespace      hk.orz
// @include        http://apps.facebook.com/street-wars/*
// @description    by neo@orz.hk, please visit http://orz.hk
// Version 1.2
// ==/UserScript==

// config
var check_interval = 60;

var last_check = new Date();

// setup a count down box
var htmlElement = document.createElement('div');
htmlElement.id = 'timer';
htmlElement.style.color = 'white';
htmlElement.style.position = 'fixed';
htmlElement.style.width = '150px';
htmlElement.style.height = '80px';
htmlElement.style.paddingTop = '4px';
htmlElement.style.paddingLeft = '2px';
htmlElement.style.zIndex = 100;
window.parent.document.body.appendChild(htmlElement);

// start up check
window.setTimeout(function() { tick() }, 1);


function tick() {
  current = new Date();
  seconds = (current - last_check) / 1000;

  // click the deposit button if there is money to save
  if (document.getElementsByName("amount").length > 1)
    if (parseInt(document.getElementsByName("amount")[1].value) > 100) {
	GM_setValue('reload', 1);
	document.getElementsByName("deposit")[0].click();
  }

  // display 
  htmlElement.innerHTML = '古惑自動存錢機 v1.2<br />by neo@orz.hk<br />http://orz.hk' 
	+ '<br />上次檢查:' + last_check.getHours() + ':' 
	+ last_check.getMinutes() + ':' 
	+ last_check.getSeconds() +
	'<br />下次檢查尚餘:' + (check_interval - Math.round(seconds)) + '秒';

  // we only refresh in first/last 5 minutes, as the revenue period
  if (current.getMinutes() < 5 || current.getMinutes() > 55) {
     htmlElement.style.background = 'red';    
     htmlElement.innerHTML += '<br />監察中';
     // check every 60 seconds
     if (seconds > check_interval) {

        // check if is in error page
        if (document.getElementById('error_message') != null) {
          location.href='http://apps.facebook.com/street-wars/job.php';
          return;
        }

	if (GM_getValue('reload') == 1) {
	        location.href='http://apps.facebook.com/street-wars/bank.php';
		GM_setValue('reload', 0);
	} else {
		location.reload();
	}
     }
  } else {
     htmlElement.style.background = 'green';
     htmlElement.innerHTML += '<br />靜止中';
  }

  // come back again every seconds to update
  window.setTimeout(function() { tick()}, 1000);
}

