$(document).ready(function() {

  try {
    twttr.anywhere(function(twitter) {
      $(".tbox .upgrade_browser").remove();  
      var tweetLabel = "<span style='color:#3D5971;'>Send a Tweet</span>";

      if (twitter.isConnected()) {
        tweetLabel += " from " + twitter.currentUser.data('screen_name');
      }

      twitter(".tbox").tweetBox({
        height: 40,
        width: 430,
        counter: false,
        label: tweetLabel
      });
      $(".tboxTwitter blockquote").remove();
      twitter(".tboxTwitter").tweetBox({
        height: 40,
        width: 500,
        counter: false,
        defaultContent: '@Twitter UseQwitter.com is very sick! Help @UseQwitter please!',
        label: ''
      });
      twitter.hovercards({
        username: function(node) {
          return node.getAttribute('name');
        }
      });
    });
  } catch(err) {
  }

  if ($(".followers").length > 0) {
    var uri = "/recent_followers/" + $("#handles option:selected").attr('id');
    $.get(uri, function(data) {
      if (data != "ok") {
        $(".followers .content").html(data).show();
        $(".followers .loading").hide();
      }
    });
  }

  // Help IE understand last-child selector
  $(".item3 .container table tr:last-child").addClass("last");

  if ($("#seed_iterations option:enabled").length > 0) {
    $.get($("#seed_iterations").val(), function(data) {
      $(".qwitters .content").html(data).linkify().show(); 
      $(".qwitters .loading").hide();
      $(".qwitters .content").html(data).linkify().show(); 
      $(".qwitters .loading").hide();
    });
  }

  $("#seed_iterations").change(function(e) {
    $(".qwitters .loading").show();
    $(".qwitters .content").hide();
    $.get($(this).val(), function(data) {
      $(".qwitters .content").html(data).linkify().show(); 
      $(".qwitters .loading").hide();
    });
  });


  $("#handles").change(function(e) {
    window.location.href = $(this).val();
  });

  $("select[name='subscription[payment][country]']").change(function(e) {
    $.get("/regions", {country: $(this).val()}, function(regions) {
      $(":input[name='subscription[payment][region]']").replaceWith(regions)
    });
  });

  $("select[name='handle_rate']").change(function() {
    $.get($(this).val(), function(data) {
      $(this).val(data);
    });
  });


  $(".handles a.delete").click(function() {
    var href = $(this).attr('href');
    $("#q_dialog").dialog("option", {
      modal: true,
      title: "Delete Handle?",
      width: 400,
      buttons: {
        'Delete': function() {
          window.location.href = href;
        },
        'Cancel': function() {
          $("#q_dialog").dialog('close');
        }
      }
    }).html("Permanetly delete handle and data?");
    $("#q_dialog").dialog('open');
    $("button").blur();
    return false;
  })

  $("#ccv").click(function() {
    $("#q_dialog").dialog('option', {
      modal: true,
      title: "What is CCV?",
      width: 400,
      minHeight: 600,
      buttons: {
        'Close': function() { 
          $("#q_dialog").dialog('close');
        }
      }
    }).html("The Credit Card Verification (CCV) is a 3 or 4 digit code printed on the back side of Visa, MasterCard and Discover cards, and appears after and to the right of your card number. On American Express the number is located on the front of the card, and appears after and to the right of your card number. <br/><br/>UseQwitter verifies this code for your protection. <br/><img src='/img/ccv.jpg'/>");
    $("#q_dialog").dialog('open');
    $("button").blur();
    return false;
  });

  $("#cancel_subscription").click(function() {
    $("#q_dialog").dialog('option', {
      modal: true,
      title: "Cancel Qwitter Subscription?",
      width: 460,
      buttons: {
        'Stop Subscription': function() {
          $("#q_dialog").html("Paid premium services will conitnue to be available though your current billing cycle. <br/> <button onclick=\"window.location.href='/account/cancel'\">CANCEL</button>");
        },
        'Delete Account': function() {
          $("#q_dialog").html("Permenantly delete account and discontinue all services?<br/><b>This cannot be undone</b>. <br/> <button onclick=\"window.location.href='/account/delete'\">DELETE</button>");
        },
        'Just Kidding!' : function() {
          $("#q_dialog").dialog('close');
        }
      }
    }).html("Seriously? Do you really want to leave us?");
    $("#q_dialog").dialog('open');
    $("button").blur();
    return false;
  });

  $("#lost_password").click(function() {
    var email = $("input[name=email]").val();
    $("#q_dialog").dialog('option', {
      modal: true,
      width: 500,
      title: "Password Reset",
      buttons: {
        'Reset': function() {
          $("#password_reset_form").submit();
        }
      }
    }).html("Password qwit you? Enter your account email address and we will help you out!<br/><form id='password_reset_form' action='/password/reset' method='post'><div class='item'><label style='font-weight:bold;'>Email Address:</label><input type='text' name='email' value='"+email+"'/></item></form>");
    $("#q_dialog").dialog('open');
    $("button").blur();
    return false;
  });
  
  // twttr.anywhere(function(T) {
  //   T("#twitter_login").connectButton({ size: 'small'});
  // });
  // 
  // $("#twitter_login").click(function() {
  //   $("#twitter_login").hide();
  // });
  
  jQuery(function () {

    twttr.anywhere(function (T) {

      if (T.isConnected()) {

        $("#login-logout").append('<button id="signout" type="button">Sign out of Twitter</button>');

        $("#signout").bind("click", function () {
          twttr.anywhere.signOut();
        });

      }
      else {
        T("#login-logout").connectButton({ size : "small" });
      }

    });

  });
  

});

function twitter_login_help() {
  $("#q_dialog").dialog("option", {
    modal: true,
    width: 600,
    closeText: 'return',
    title: "Logging into Twitter",
  }).html($(".twitter_login_help_display").html());
  $("#q_dialog").dialog('open');
  $("button").blur();
  return false;
}

