// ==UserScript==
// @name           Twitter Goggles v2
// @namespace      http://nonrational.org
// @include        http*twitter.com*
// @description    A quick, silly, and dirty implementation of 'Twitter Goggles'. Every time you tweet from the web, you'll need to answer a +,-,or * question. V2 adds time of day support (6am to 11pm is Goggle-free),  allows for smaller numbers if multiplying, and specifies the url of twitter better.
// ==/UserScript==

var ALL_THE_TIME = false;

//console = unsafeWindow['console'];
//console.log('beginning');
jQuery = unsafeWindow['jQuery'];

/*************************************************************************************
 * Confirm plugin 1.2
 *
 * Copyright (c) 2007 Nadia Alramli (http://nadiana.com/)
 * Modifications for Twitter Goggles by Alan Norton (http://nonrational.org)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 */
 
jQuery.fn.confirm = function(options) {
  options = jQuery.extend({
    msg: 'Are you sure?',
    stopAfter: 'never',
    wrapper: '<span></span>',
    eventType: 'click',
    dialogShow: 'show',
    dialogSpeed: '',
    removeOnCancel: false,
    timeout: 0,
    cancelFunc: function(){ return true; },
    okFunc: function(){ return true; }
  }, options);
  
  options.stopAfter = options.stopAfter.toLowerCase();
  
  if (!options.stopAfter in ['never', 'once', 'ok', 'cancel']) {
    options.stopAfter = 'never';
  }
  
  options.buttons = jQuery.extend({
    ok: 'Yes',
    cancel: 'No',
    wrapper:'<a href="#"></a>',
    pre: '',
    separator: '/'
  }, options.buttons);

  // Shortcut to eventType.
  var type = options.eventType;

  return this.each(function() {
    var target = this;
    var $target = jQuery(target);
    var timer;
    var saveHandlers = function() {
      var events = jQuery.data(target, 'events');
      if (!events) {
        // There are no handlers to save.
        return;
      }
      target._handlers = new Array();
      for (var i in events[type]) {
        target._handlers.push(events[type][i]);
      }
    }
    
    // Create ok button, and bind in to a click handler.
    var $ok = jQuery(options.buttons.wrapper).append(options.buttons.ok).click(function() {
      if(!options.okFunc()){
        return false;
      }
      // Check if timeout is set.
      if (options.timeout != 0) {
        clearTimeout(timer);
      }
      $target.unbind(type, handler);
      $target.show();
      $dialog.hide();
      // Rebind the saved handlers.
      if (target._handlers != undefined) {
        jQuery.each(target._handlers, function() {
          $target.click(this);
        });
      }
      // Trigger click event.
      $target.click();

      if (options.stopAfter != 'ok' && options.stopAfter != 'once') {
        $target.unbind(type);
        // Rebind the confirmation handler.
        $target.one(type, handler);
      }
      return false;
    })
    
    var $cancel = jQuery(options.buttons.wrapper).append(options.buttons.cancel).click(function() {
      // Check if timeout is set.
      if (options.timeout != 0) {
        clearTimeout(timer);
      }
      if (options.stopAfter != 'cancel' && options.stopAfter != 'once') {
        $target.one(type, handler);
      }
      $target.show();
      if(options.removeOnCancel){
        $dialog.remove();
      }else{
        $dialog.hide();
      }
      options.cancelFunc();
      
      return false;
    });

    if (options.buttons.cls) {
      $ok.addClass(options.buttons.cls);
      $cancel.addClass(options.buttons.cls);
    }

    var $dialog = jQuery(options.wrapper).append(options.msg).append($ok).append(options.buttons.separator).append($cancel);

    var handler = function() {
      jQuery(this).hide();

      // Do this check because of a jQuery bug
      if (options.dialogShow != 'show') {
        $dialog.hide();
      }

      $dialog.insertBefore(this);
      // Display the dialog.
      $dialog[options.dialogShow](options.dialogSpeed);
      if (options.timeout != 0) {
        // Set timeout
        clearTimeout(timer);
        timer = setTimeout(function() {$cancel.click(); $target.one(type, handler);}, options.timeout);
      }
      return false;
    };

    saveHandlers();
    $target.unbind(type);
    target._confirm = handler
    target._confirmEvent = type;
    $target.one(type, handler);
  });
}
/*********************************************************************************************************/

function genMsg(){
    var dt = new Date();
	var hr = dt.getHours();
	
	if((hr >= 6 && hr < 23) || ALL_THE_TIME){
		return;
	}
	
    var opi = Math.floor(Math.random()*3);
    var lhs = Math.floor(Math.random()*100)+10;
    var rhs = Math.floor(Math.random()*100)+10;
    var opact = null;
    switch (opi){
        case 0: opact='+'; ans = lhs + rhs; break;
        case 1: opact='-'; ans = lhs - rhs; break;
        case 2: opact='*'; 
			//lets make things a little easier for mulitplication
			if(lhs > 12){ lhs = Math.floor(lhs / 7); } 
			if(rhs > 12){ rhs = Math.floor(rhs / 7); }
			ans = lhs * rhs; break;
    }
    
    var mathMess = "What is "+lhs+" "+opact+" "+rhs+" ?";
    
    var _inputField="<input type='text' id='goggleanswer' size='2' style='margin-left:2px;margin-right:5px;'></input>"

    jQuery('#update-submit.round-btn').confirm({
        msg: mathMess,
        cancelFunc: genMsg,
        okFunc: ansCorrect,
        removeOnCancel: true,
        buttons: {
            pre: _inputField,
            ok: _inputField+"<span style='text-decoration:underline;'>i\'m sure!</span>",
            cancel: "<span style='text-decoration:underline;'>forget it.</span>",
            wrapper: '<a href="#" style="text-decoration: none;"></a>',
            separator:' or '
        }
    })   
    //console.log('loaded twitter goggles');
}

function ansCorrect() {
    inval = jQuery('#goggleanswer')[0].value;
    //console.log(inval + ' vs ' + ans);
    return inval == ans;
}
//TODO implement add global style
var ans = -.1;


genMsg();
//console.log('finished');






