  var urlValue = 'your-username';
    var emailValue = 'Enter your email here.';
    $(document).ready(function() {
    $('#custom_url').blur(function(){$(this).keyup()});
        $('#custom_url').keyup(function() {
            checkUrl($(this).val());
        }).focus(function() {
            if($(this).val() == urlValue) $(this).val('');
            checkUrl($(this).val());
        }).blur(function() {
            if($(this).val() == '') $(this).val( urlValue );
            checkUrl($(this).val());
        });
    $('#email').blur(function(){$(this).keyup()});
        $('#email').keyup(function() {
            checkEmail($(this).val());
        }).focus(function() {
            if($(this).val() == emailValue ) $(this).val('');
            checkEmail($(this).val());
        }).blur(function() {
            if($(this).val() == '') $(this).val( emailValue );
            checkEmail($(this).val());
        });
    });
    
    function checkURLBasic (url){
   		 if(url != urlValue && url != ''){
   		 	$('#url_error').fadeOut('fast');
   		 	 $('#url_invalid').fadeOut('fast');   		 	
   		 	return true;
   		 } else {
   		 	 $('#url_invalid').fadeIn('fast');
   		 	$('#url_error').html('A valid username is required..');
   		 	$('#url_error').fadeIn('fast');
   		 	return false;
   		 	
   		 }
    }
    
    function showAlert(title, str){
    	$('#alert-box').html('<div id="alert-title">'+title+'</div><div id="alert-body">'+str+'</div><a href="#" id="alert-button" onClick="hideAlert(); return false;">&nbsp;</a>');
		$('#alert-box').fadeIn('fast');
    }
    
    function hideAlert(){
    	$('#alert-box').fadeOut('fast');
    }
    
    function showFixedAlert(){
    	$('#fixed-alert').html('<div style="width:800px;position:absolute;left:50%;margin-left:-400px;"><span style="font-weight:bold;">FOLIO IS COMING SOON</span>, UNTIL THEN, FIND OUT WTF YOU ARE & SPREAD THE WORD FOR <span style="font-weight:bold;">BETA ACCESS</span></div>');
		$('#fixed-alert').fadeIn('slow');
    }
    
    function getTwitterData(dat){
    	checkTwitterID(dat);
    }
    
    function checkUser (id, email, nextStr){
    	
    	var bool;
    if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
        	$.post('/ajax.php',
                    {check_id: id, check_email: email},
                    function(data){
                     var obj = JSON.parse(data);
                     // Check if ID is already in use - display error
                     if((obj.id > 0) || (obj.email > 0)){
                     showAlert('Ouch!', '<span>That Facebook profile or it\'s email is already registered. You\'ve gotta log out and use a different one!</span>');
                     
                     	var button = document.getElementById('signin-fb');
					  	//button.innerHTML = 'Logout';
					  	button.style.backgroundPosition = "0 100%";
						button.onclick = function() {
							FB.logout(function(response) {
								var userInfo = document.getElementById('user-info');
								userInfo.innerHTML="";
							});
						};
                     } else {
                     	if (nextStr == 'submitForm'){                     	
                     		submitFB(id, email);
                     	}
                     }                                   	
                    });
        }); 
    	
    }
    
    function checkUrl(url){
        if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
            if(url != urlValue && url != ''){
                $.post('/ajax.php',
                    {custom_url: url},
                    function(data){
                        if(data == 0){
                            $('#url_invalid').fadeOut( function(){
                                $('#url_valid').fadeIn('fast');
                                $('#url_error').fadeOut('fast');
                                $('#url_error').html('');
                            });
                        } else {
                            $('#url_valid').fadeOut( function(){
                                $('#url_invalid').fadeIn('fast');
                                if(url != '' && url != 'custom-url') {
                                    if(data == 999){
                                        $('#url_error').html('Your username must be at least 3 characters long.');
                                    } else if(data == 998){
                                        $('#url_error').html('Your username can only contain A-Z, 0-9, and - (hyphen).');
                                    } else if(data == 996){
                                        $('#url_error').html('A username is required..');
                                    } else if(data < 900 && data != 0){
                                        $('#url_error').html('Unfortunately, that username is already taken.');
                                    }
                                    $('#url_error').fadeIn('fast');
                                } else {
                                    $('#url_error').fadeOut('fast');
                                }
                            });
                        }
                    })
            } else {
                $('#url_valid').fadeOut('fast');
                $('#url_invalid').fadeOut('fast');
               
            }
        }, 100);
    };
    function checkEmail(email){
        if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
            if(email != emailValue && email != ''){
                $.post('/ajax.php',
                    {email: email},
                    function(data){
                        if(data == 0){
                        $('#email_valid').fadeIn('fast');
                            $('#email_invalid').fadeOut( function(){
                                $('#email_error').fadeOut('fast');
                                $('#email_error').html('');
                            });
                        } else {
                            $('#email_invalid').fadeIn('fast');
                            $('#email_valid').fadeOut( function(){
                                if(email != '' && email != 'Enter your email here.' && email.length > 4) {
                                    if(data == 999){
                                        $('#email_error').html('Please enter a valid email address.');
                                    } else if(data == 900){
                                        $('#email_error').html('An email address is required.');
                                    } else if(data < 900){
                                        $('#email_error').html('That email address has already been used.');
                                    }
                                    $('#email_error').fadeIn('fast');
                                } else {
                                    $('#email_error').fadeOut('fast');
                                }
                            });
                        }

                })
            } else {
                $('#email_valid').fadeOut('fast');
                $('#email_invalid').fadeOut('fast');
            }
        }, 100);
    };

	function checkTwitterID(tw){
	    twuid = tw;    
	    $.twitid = twuid;
		if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
    		 $.post('/ajax.php',
                    {tw: twuid},
                    function(data){
                    	if(data > 0){
                    		showAlert('Sorry!', '<span>That Twitter account is already registered, sorry please use a different one!</span>');
                    	} else if (data == 0){
                    		requestEmail('twitter');
                    	}})
        }, 100)
    };
    
    function requestEmail(type){
    	// Hide Social & Show Email
    	$('#signup-options').fadeOut('fast');
    	$('#email_error').html('Just add an email address and you\'re done!');
    	$('#email_error').fadeIn('fast');
    	if(type == 'twitter'){
	    	$('#email-button').html('<a href="#" class="submit" id="submit" onClick="submitTW(); return false;">&nbsp;</a>');
	    } else if(type == 'form'){
		    $('#email-button').html('<a href="#" class="submit" id="submit" onClick="submitForm(); return false;">&nbsp;</a>');
	    }
    	$('#email-holder').fadeIn('fast');   				            
    }
    
    function showThanks(title, str){
    	setupQuiz();
    	$('#complete-box').html('<div id="complete-title">'+title+'</div><div id="complete-body">'+str+'</div><a href="#" id="complete-button" onClick="hideThanks(); return false;">&nbsp;</a><div id="complete-arrow"></div>');
		$('#complete-box').fadeIn('fast');
		$(document).click(function() {
		   hideThanks();
		});		
		}
		
 function hideThanks(){
 	$('#complete-box').fadeOut('fast');
	showFixedAlert();
 }

    function submitForm(){
		$('#container-content').fadeOut('fast');
		if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
	  		url = $('#custom_url').val();
		    email = $('#email').val();

    		 $.post('/ajax.php',
                    {sub_url: url, sub_mail: email, form: true},
                    function(data){
                    	if(data == 1001){
							$('#form-confirmation').html("Sorry, there's been a weird error. We're embarrassed, but could you please try again?");
                    		$('#form-confirmation').fadeIn('fast');
                    	} else if (data == 1){
								showThanks('FOLIO IS COMING SOON', 'UNTIL THEN FIND OUT <span style="font-weight:bold;">WHAT TYPE OF FILE</span> YOU ARE <br>& SPREAD THE WORD TO BE <span style="font-weight:bold;">FIRST</span> WITH <span style="font-weight:bold;">BETA ACCESS!</span>');
                    	} else if (data == 5){
                    		$('#container-content').fadeIn('fast');
                    		showAlert('Whoops!', '<span>Sorry but you need to complete the username and password field correctly!</span>');
                    	}
                    	
				})
        }, 100)
    };


    function submitFB(fb, em){
  		url = $('#custom_url').val();
	    email = em;
	    fbuid = fb;    
		$('#container-content').fadeOut('fast');
		if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
    		 $.post('/ajax.php',
                    {sub_url: url, sub_mail: email, fb: fbuid, facebook: true},
                    function(data){
                    	if(data == 1001){
                    		$('#form-confirmation').html("Sorry, there's been a weird error. We're embarrassed, but could you please try again?");
                    		$('#form-confirmation').fadeIn('fast');
                    	} else if (data == 1){
	                    	showThanks('FOLIO IS COMING SOON', 'UNTIL THEN FIND OUT <span style="font-weight:bold;">WHAT TYPE OF FILE</span> YOU ARE <br>& SPREAD THE WORD TO BE <span style="font-weight:bold;">FIRST</span> WITH <span style="font-weight:bold;">BETA ACCESS!</span>');
                    	}})
        }, 100)
    };
    
    
  function submitTW(){
        $('#container-content').fadeOut('fast');
  		url = $('#custom_url').val();
	    email = $('#email').val();
	    twuid = $.twitid;    
		if (this.timer){
            clearTimeout(this.timer);
        }
        this.timer = setTimeout(function () {
    		 $.post('/ajax.php',
                    {sub_url: url, sub_mail: email, tw: twuid, twitter: true},
                    function(data){
	                    
                    	if(data == 1001){
                    		$('#form-confirmation').html("Sorry, there's been a weird error. We're embarrassed, but could you please try again?");
                    		$('#form-confirmation').fadeIn('fast');
                    	} else if (data == 1){
	                    	showThanks('FOLIO IS COMING SOON', 'UNTIL THEN FIND OUT <span style="font-weight:bold;">WHAT TYPE OF FILE</span> YOU ARE <br>& SPREAD THE WORD TO BE <span style="font-weight:bold;">FIRST</span> WITH <span style="font-weight:bold;">BETA ACCESS!</span>');
                    	}})
        }, 100)
    };


      $(document).ready(function () {
        $('#shell img').plaxify()
       // $('#shell div#form_box').plaxify() // To disable moving reg box
        $.plax.enable();
        
            $('#signin-tw').click(function(e){
		if(checkURLBasic(document.getElementById('custom_url').value)){     
	        $.oauthpopup({
	            path: 'tw.php',
	            callback: function(dat){
	               //window.location.reload();
	            }
	        });
	    }
	    e.preventDefault();
    });
    
    $('#signin-em').click(function(e){
		requestEmail('form')
	    e.preventDefault();
    });
        
      })
      
      
