var COUNTDOWN_TIMER = 5000;
var PAGE_TURNED     = false;
$(document).ready(function(){
    
    //attach listeners to login box
    $('#login-button').live('click', function(){
        if($('#overlay-window').css("display")=="none")
        {
            open_login();
        }
        else
        {
            close_login();
        }
    });
    $('.close_overlay > a ').live('click', function(){
        close_login();
    });
        
    $('.modify-profile-image-link').live('click', function(){
        $.post("/ajax/load_plugin/edit_profile_image", function(results) {
	    var json = eval('('+results+')');
            $('.upload-form-area').parent().html(json.payload);
        
        });
    });
     
    
    $('#overlay-mask').live('click', function(){
        close_login();
    });
    
 
    $('a.sign-up').live('click', function(){
        open_registration();
    });
    $('a.login').live('click', function(){
        open_login();
    });
	$('a.fb-login').live('click', function(){
		open_fb_login();
	});
    
    $('a.logout').live('click', function(){
        //DO AJAX LOGOUT SCRIPT HERE!
        executeJSONP('log_out');
        //THEN CHECK LOGIN STATUS
    });

	//logs a fb user out that reached edit_profile inappropriately (bug 1460)
	$('a.fb_discon_logout').live('click', function(){
		$("#overlay-window").hide();
		executeJSONP('log_out');
		open_fb_login();
	});

    $('#edit-profile-popup').live('click',function(){
        var obj = $(this).parent().parent().parent();    
        $.post('/ajax/load_plugin/edit_profile', function(response){
	    var json = eval('('+response+')');
            $('#overlay-window').show();
            $('#register').hide();
            $('#login').hide();
        $('.modal-target').html(json.payload);
            
        });
    });
    $('.member-popup-close').live('click', function(){
        $('.member-popup').remove();
    });
    $('.super-login').live('click', function(){
        superLogin();
    });
   
});

function refresh_edit_profile() //made for fb connect (bug 1407)
{
	//$('#overlay-window').hide();
	//$('.edit-profile-form').remove();
	
	var obj = $(this).parent().parent().parent();    
    $.post('/ajax/load_plugin/edit_profile', function(response){
        //$('#overlay-window').show();
	var json = eval('('+response+')');
    	$('.modal-target').html(json.payload);        
    });
	
}

function logoutCallback()
{
    $.post('/ajax/unset_user');
    $('.nnn-form + .edit-profile-form').remove();
    $('#comment-form').remove(); // remove comment form if it happens to be open so that new instance must be hit
    checkLoginStatus();
}

function open_login()
{
    $.post('/ajax/load_plugin/popup_login', function(response) {
	var json = eval('('+response+')');
        $('#nnn-overlay').addClass('login-form').removeClass('signup-form').removeClass('fb-login-form');
        $('#overlay-window').show();
        $('.modal-target').html(json.payload);
        $('#comment-form').remove(); // remove comment form if it happens to be open so that new instance must be hit

    });
}

function close_login()
{
    $('#overlay-window').hide();
    $('.form-login-window').remove();
    restore_ie_elements();

}


//facebook connect login (bug 1407)
function open_fb_login()
{
	$.post('/ajax/load_plugin/popup_fb_login', function(response) {
	var json = eval('('+response+')');
        $('#nnn-overlay').addClass('fb-login-form').removeClass('login-form');
        $('#overlay-window').show();
        $('.modal-target').html(json.payload);
        $('#comment-form').remove();
    });
}

function close_fb_login()
{
    $('#overlay-window').hide();
    $('.form-login-window').remove();
    restore_ie_elements();
}

function refresh_fb_login()
{
	$('#overlay-window').hide();
    $('.form-login-window').remove();

	open_fb_login();
}

//This function was written to solve an issue between refreshing
//the popup fb login and grabbing data off the form during refresh (bug 1440)
function fb_login_reconnect()
{
	$.post('/ajax/reconnect_fb_user', {"uid":$('#fb-login-form-nnnuid').val()}, refresh_fb_login());
}

function open_registration()
{
     $.post('/ajax/load_plugin/popup_register', function(response) {
	var json = eval('('+response+')');
        $('#nnn-overlay').addClass('signup-form').removeClass('login-form').removeClass('fb-login-form');
        $('#overlay-window').show();
        $('.modal-target').html(json.payload);
        $('#comment-form').remove(); // remove comment form if it happens to be open so that new instance must be hit

    });
}

function superFBLogin()
{
    var json = {"fbuid":$('#fb-login-form-fbuid').val(), "nnnuid":$('#fb-login-form-nnnuid').val()};
    executeJSONP("super_fb_login", json);
}

function superFBLoginCallback(data)
{
	if(data.failed) {
		if(data.response == 'banned') {
			 $('#fb-login-form span.errormsg').html("This account has been banned.").show();
		} else if(data.response == 'disconnected') {
			 var message = "This account has been disconnected from Facebook. " +
						   "Please reconnect to Facebook to reclaim your account here.";
			 $('#fb-login-form span.errormsg').html(message).show();
             reloadButton();
		} else {
			$('#fb-login-form span.errormsg').html("Sorry, there was an error with your login.").show();
		}
	} else {
		checkLoginStatus();
        close_fb_login();
	}
}

function close_registration()
{
    $('#overlay-window').hide();
    $('.form-register-window').remove();
    restore_ie_elements();
}

//this function logs into the main domain via a JSONP request, which will then allow cookies/etc. 
function superLogin()
{
    var json = {"u":$('#login-email').val(), "p":$('#login-pw').val()};
    executeJSONP("super_login", json);
}
function superLoginCallback(data)
{
    if (data.failed)
    {
        $('#login-form span.errormsg').html("Incorrect login").show();
        
    }
    else
    {
        checkLoginStatus();
        close_login();
    }

}
//this function is called because certain elements get masked for IE when the overlay pops up
//must be called to show elements after modal windows are closed
function restore_ie_elements() 
{
    $('#content').show();
    $('#titlebar').show();
    $('#footer-video-slider').show();
    $('#login-box-nbot').show();
    $('#nav').show();
    
    
}
function start_countdown()
{
    setInterval('tick()', 1000);//once per second
}

function tick()
{
    if (!PAGE_TURNED)
    {
        if (COUNTDOWN_TIMER <= 0)
        {
            PAGE_TURNED = true;
            window.location = "/";
        }
        else
        {
            $('span.countdown').html(COUNTDOWN_TIMER / 1000);
        
        }
    }
    COUNTDOWN_TIMER = COUNTDOWN_TIMER - 1000;
    
}

