jQuery.extend({
    is_valid: false,
    valid_video_link: function() {
        is_valid = false;
        
        $('span.status').html('Validating link');
        // Link validation
        $.ajax
        ({
          type: "POST",
          url: 'ajax/valid',
          cache: false,
          data: ({ tomp3_net_link: $('input#tomp3_link').val() }),
          success: function(tomp3)
          {
            if(tomp3 == 'true')
            {
              document.location.reload(true);
            }
            else
            {
              $.video_is_online();
            }
          }
        });
        
        return true;
    },
    
    video_is_online: function() {
        $('span.status').html('Checking the availability of video');
        
        $.ajax
        ({
          type: "POST",
          url: 'ajax/file_checker',
          cache: false,
          dataType: 'json',
          data: ({ tomp3_net_link: $('input#tomp3_link').val() }),
          success: function(tomp3)
          {
            if(tomp3.error == 'true')
            {
              document.location.reload(true);
            }
            else
            {
              $('input[name=video_size]').attr('value', tomp3.filesize);
              $.fetch_video(tomp3.filename, tomp3.filesize);
            }
          }
        });
    },
    
    fetch_video: function(video_name, size) {
        $('span.status').html('Fetching video <span id="percent">0</span>% <br/><small>'+video_name+'</small>');
        
        $.ajax
        ({
          url: 'ajax/file_generator',
          cache: false,
          success: function(html)
          {
            $('input[name=download_file_hash]').attr('value', html);
            
            $.download_progress();
          }
        });

    },
    
    download_progress: function()
    {
        var times = 0;
        
        $.post_to_download();
        $(document).everyTime(2000, "download_stater", function(i)
        {
          $.ajax
          ({
            url: 'ajax/get_percent/'+$('input[name=download_file_hash]').val()+'/'+$('input[name=video_size]').val(),
            cache: false,
            success: function(html)
            {
              times = html;
            }
          });
          
          if(times == '100')
          {
            $('span.status').html('Converting to MP3 - please wait.');
            $(this).stopTime();
            $.is_converted();
          }
          
          $('span#percent').text(times);
        }, times);
    },
    
    post_to_download: function()
    {
       $.ajax
        ({
          type: "POST",
          dataType: 'json',
          url: 'download/downloading_file/'+$('input[name=download_file_hash]').val(),
          cache: false,
          data: ({ tomp3_net_link: $('input#tomp3_link').val() })
          });
    },
    
    is_converted: function()
    {
        var is_completed = false;
        $(document).everyTime(5000, "download_stater_2", function(i)
        {
          $.ajax
          ({
              type: "POST",
              url: 'download/is_converted/'+$('input[name=download_file_hash]').val(),
              cache: false,
              dataType: 'json',
              success: function(tomp3)
              {
                if(tomp3.error == 'false')
                {
                  is_completed = true;
                  var download_link = 'http://www.tomp3.net/download/mp3/'+tomp3.hash+'/'+tomp3.url_title;
                  $('span.status').html('File converted!<br />Click <a href="'+download_link+'">here</a> to download.');
                  
                  $(this).stopTime();
                }
              }
            });
            
            if (is_completed == true)
            {
              $(this).stopTime();
            }
            
        }, is_completed);
    }
});

$(document).ready
(
  function()
  {
    $('form').submit
    (
      function()
      {
        $('input#tomp3_link').attr('disabled', 'disabled'); // Block input
        $('button').fadeOut();
        $('#conversion_status').fadeIn('slow');
        
        $.valid_video_link();
        
        return false;
      }
    );
  
  var counter_pass = 0;
  $(document).everyTime(10000, "recent", function(i) {
    $.ajax
    ({
        url: 'ajax/recent',
        cache: false,
        dataType: 'json',
        success: function(html)
        {
          $('span#recent').html('<a href="'+html.url+'">'+html.title+'</a>');
        }
    });
  }, counter_pass);
  }
);
