jQuery.easing.def = "easeOutExpo";
var bar_width = 352;
var min_bar_width = 10;
var t = '';

$(document).ready(function(){
     // need to dynamically write the iframe, prevent the browser from caching and regenerating the process upon revisiting this page
//          rand = Math.floor(Math.random()*9999+1);
//          $('#progress_iframe').html( '<iframe src="blank.php?rand=' + rand + '" name="progress_' + rand + '" id="progress" marginwidth="0" marginheight="0" frameborder="0" ></iframe>' );
//          $('#scraper_form').attr( 'target', 'progress_' + rand );
     $('#scraper_form').bind( 'submit', scraper_init );
     $('#email_link').bind( 'click', mail_click );
     $('#questions').bind( 'click', q_slide );
     $('#account').bind( 'blur', check_account );
} );


function check_account() {
     var brokenstring = $('#account').val().split('/');
     if( brokenstring.length > 1 )
          $('#account').val( brokenstring[4] );
}


function mail_click() {
     var link = 'mailto:' + $('#email_link').html();
     link += '?subject=DDDDOWNLOAD!'
     if( $('#account').val() != '' )
          link += '&body=account:%20' + $('#account').val() + "%0D%0D%0D";
     document.location = link;
     return false;
} // end function mail_send()


function scraper_init() {
     $('#error').slideUp( 1000 );
     pattern = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9]+)+$/;
     if( $('#account').val() == '' ) {
          $('#account')[0].focus();
          error_show( '<p><strong>error:</strong> Please enter a FFFFOUND! account name</p>' );
          return false;
     } 
     else if( !pattern.test( $('#email').val() ) ) {
          error_show( '<p><strong>error:</strong> Please enter a valid email address.</p>' );
          $('#email')[0].focus();
          return false;
     }
     // get ajaxy
          var vars = $("#scraper_form").serialize();
          $.ajax( {
               type: 'POST',
               url: 'queue.php',
               data: vars,
               success:function( response ) {
                    $('#response').html( response );
                    if( $('#account_found').val() == 'false' ) {
                         $('#account')[0].focus();
                         error_show( response );
                    }
                    else if( $('#account_found').val() == 'true' ) {
                         // disable form
                         $('#btn_submit')[0].blur();
                         $('#btn_submit').css( 'opacity', .5 );
                         $('#response').slideDown( 3000 );
                         setTimeout( function() { $('#scraper_form input').attr( 'disabled', true ); }, 2000 );
                         $('#scraper_form input.text').css( 'borderColor', '#ccc' );
                    // $('#scraper_form').submit( function() {return false; } );
                    }
               }
          } );
     return false;
} // function scraper_init();


function error_show( msg ) {
     $('#response').slideUp( 
          1000, 
          function() {
               $('#account').val();
               $('#account')[0].focus();
               $('#response').html( msg );
               $('#response').slideDown( 1000 );
               $('#btn_submit').css( 'opacity', 1 );
               $('#scraper_form input').attr( 'disabled', false );
               $('#scraper_form input.text').css( 'borderColor', '#000000' );
               $('#scraper_form').submit( function() {return true; } );
          } 
     );
     // enable form
} // end function error_show()


function q_slide() {
     $('#questions').unbind( 'click', q_slide );
     if( $('#col_q').css( 'left' ) != '-291px' ) { // slide left
          pos_x = '-291px';
          label = 'Learn More';
          delay = 40;
     }
     else { // slide right
          pos_x = '250px';
          label = 'Close';
          delay = 110;
     }
     setTimeout( function() { $('#questions').html( label ); }, delay );
     $("#col_q").animate( 
          { left: pos_x }, 
          1200, 
          'swing', 
          function() { $('#questions').bind( 'click', q_slide ); }
     );
} // end function q_slide()

