var option = {};

$(function(){

  var debug = function (){};

  var getCurrentStep = function ()
  {
    var step = location.href.split("#")[1];
    return (step != undefined ? step : "_step1" );
  }

  var getNextStep = function (elem_button)
  {
    var nextStep = $(elem_button).next().attr("value");
    return parseInt(nextStep.split("step")[1])-1;
  } 


  var reloadTwitterWind = function(){ // {{{
    var t = null;
    return function () {
      if( null != t )
      {
        clearTimeout(t);
        t = null;
      }
      if( getCurrentStep() == "_step1")
      {
        return;
      }

      if( option['username'] == '' || option['password'] == '' )
      {
        return;
      }
      
      if(undefined != option['twitterwind_frame_border'] &&
         'on' == option['twitterwind_frame_border'] )
      {
        var border_color = option['twitterwind_frame_border_color']
        $('#twitterWindIframe').css('border','solid 1px');
        $('#twitterWindIframe').css('border-color', '#'+border_color);
      }else
      {
        $('#twitterWindIframe').css('border','none');
      }

      if(option['twitterwind_frame_width']  != 0 &&
         option['twitterwind_frame_width']  > 0 &&
         option['twitterwind_frame_height'] != 0 &&
         option['twitterwind_frame_height'] > 0
         )
      {
        $("#twitterWindIframe").animate( { width: option['twitterwind_frame_width'], 
                                           height:option['twitterwind_frame_height']} );
      }else
      {
        debug(option['twitterwind_frame_width'] + ":" + option['twitterwind_frame_height']);
      }
      
      $.ajaxSetup({cache: false});

      t = setTimeout( function ()
        {
          $('#loading').show();
          debug($.toJSON(option));
          $("#twitterWindIframe").hide('fast');
          var url = "twitterwind.php?option="+encodeURIComponent($.toJSON(option));
          $('#twitterWindIframe').attr('src',url).load(function(){ $('#loading').hide(); $(this).show('fast'); });
        },
        1000 );
    }
  }(); // }}}
    
  // input tag -> save option
  var changed = function (elem)
  {
    if($(this).attr('id') == 'source_code')
    {
      return;
    }

    if($(this).attr('name') == 'twitterwind_max_length')
    {
      try {
        if($(this).val() > 140)
        {
          $(this).val(140);
        }
      } catch(e)
      {
      }
    }
    
    if($(this).val != undefined && $(this).attr('name') != undefined)
    {
      option[$(this).attr('name')] = $(this).val();
    }
    reloadTwitterWind();
  };

  var changed_with_frame = function (elem)
  {
    changed(elem);
  }

  $(":text").blur( changed );
  $(":text").keyup( changed );
  $(":text").change( changed );

  $(":hidden").blur( changed );
  $(":hidden").keyup( changed );
  $(":hidden").change( changed );

  $(":password").blur( changed );
  $(":password").keyup( changed );

  $(":radio").click( changed );

  $("#twitterwind_frame_height").change();
  $("#twitterwind_frame_width").change();
  $("#twitterwind_frame_border_color").change();

  $(".submit button").each(function (){
    $(this).click(function () {
      reloadTwitterWind()
    });
  });

  $("form").submit( function() {
    return false;
  } );

  $(".step").each(function(){
    $(this).find("button").click(function(){
      var stepNum = getNextStep($(this));
      if(stepNum == 3)
      {
        var elem = $('#source_code');
        elem.html('登録中....');
        var url = $.toJSON(option);
        var result = $.ajax({
          async:false,
          timeout:10000,
          cache:false,
          type:"POST",
          url: "./index.php",
          data: "act=register&option=" + encodeURIComponent($.toJSON(option)),
          error: function () { 
                    alert('通信エラーが発生しました');
          }
          }).responseText;
        elem.text(result);
      }

      });
  });
});

function openOAuth(url)
{
  window.location.href = url;
}


  

