$(document).ready(function() {
    $('.signinResponse').hide();
    setupHomepage();
    setupLightbox();
    setupSignIn();
    if ($('#AlertMessageText').html() != "") {
        showAlert($('#AlertMessageText').html());
    }
    $('.gs-suggest').click(function(event) {
        $('#fdbk_tab').click();
        event.preventDefault();
    });
}); 

/*------------- Set Up Homepage -----------------------------*/

function setupHomepage() {

$('.teaser-wrap').append('<span class="stem"></span>');


//Homepage Take a Tour Cycle
if($('h2').hasClass('home-title')) {

$('body').addClass('bodyhome');

//Recent Remarks
$('.cycle').cycle({
    fx:	'scrollLeft',
    speed:     '600', 
    timeout:   10000, 
    pause:     1
});

//Romance Quotes
$('.quotes').cycle({
    fx:	'fade',
    delay:		1000,
    speed:		'1000', 
    timeout:	10000, 
    pause:		1
});

//Take a Tour
$('.tour-teaser').cycle({ 
    fx:    'scrollLeft', 
    speed:     '1000', 
    timeout:   10000, 
    pause:     1
});

}

//FadeIn/FadeOut Columns on Scroll
$(window).scroll(function(){
	var scroll_timer;
	var $window = $(window);
	var top = $(document.body).children(0).position().top;
	window.clearTimeout(scroll_timer);
	scroll_timer = window.setTimeout(function () {
		if($window.scrollTop() <= top){
			$('.column').animate({opacity: .70}, 600);
		}
		if($window.scrollTop() > top){
			$('.column').animate({opacity: 1.00}, 600);
		}
	}, 100);
});
}


/* Setup Lightbox Trigger Links */
function setupLightbox() {
    $('.fadein').click(function(event) {
        var el_show = $(this).attr("href");
        $(el_show).fadeIn('slow');
        event.preventDefault();
        popDiv();
    });
    $('.user-message, .summary-text, .extended-list, .user-notice').wrapInner('<div class="inner-wrap"></div>');
}

/* Position and Event Handlers for Lightboxes */
function popDiv() {
   // $('#overlay').show();    THIS BREAKS IN IE 8. Makes the screen go blank. TODO: FIX OR DELETE
   // $('.tranny').removeClass('tranny');
    
    getHeight();
 
    if ($.browser.msie) {
        $(".lightbox").css("top", document.documentElement.scrollTop + 70 + 'px');
        closeAction();
    }
    else if ($.browser.opera) {
        $('.lightbox').css('top', document.body.scrollTop + 70 + 'px');
        closeAction();
    }
    else {
        $(".lightbox").css("top", window.pageYOffset + 70 + 'px');
        closeAction();
    }
    $(".lightbox .close, .close-message").click(function(event) {
        closePopDiv();
        event.preventDefault();

    });

    getHeight();
}

function closePopDiv() {
    $(".lightbox").fadeOut('slow');
   // $("#overlay").hide();   SEE ABOVE


    return false;
}

function closeAction() {
    $(".close, .close-message").click(function() {
        closePopDiv();
        return false;
    });
}

/* Overlay Set Up */
function getHeight() {
    if (parseInt(navigator.appVersion) > 3) {
        if ($.browser.msie) {
            wind = document.body.offsetWidth;
            wind = document.body.offsetHeight;
        }
        else {
            wind = window.innerWidth;
            wind = window.innerHeight;
        }
    }
    var divh = document.getElementsByTagName('body')[0].offsetHeight;
    var lock = document.getElementById('overlay');
    if (divh >= wind) {
        lock.style.height = divh + "px";
    }
    else { lock.style.height = wind + "px"; }
}

function showAlert(message) {
    var el_show = $("#alert");
    $(".user-message .inner-wrap div").html(message);
    $(el_show).fadeIn('slow');
    popDiv();
    return false;
}

function setupSignIn() {
    $('.signin').click(function(event) {
        $(this).parent().hide();
        var el_show = $(this).attr("href");
        $(el_show).show('slow');
        event.preventDefault();
    });
    $('#username2').focus(function() {
        if (this.value == "Username") {
            this.value = "";
        }
    });
    $('#username2').blur(function() {
        if (this.value == "") {
            this.value = "Username";
        }
    });
    $('#password2').focus(function() {
        if (this.value == "Password") {
            $(this).replaceWith('<input id="password2" name="password2" type="password" value="" />');
            $('#password2').focus();
            passwordBlur()
        }
    });
    $('.signin-close').click(function(event) {
        $(this).parent().parent().hide().prev('p').fadeIn('slow');
    });
    $('#SubmitResetPassword').click(function(event) {
        performResetPassword($('#ResetPasswordUsername').val(), $('#ResetPasswordEmail').val());
        event.preventDefault();
    });
    
    
    $("#SignInButton").not('.disabled').click(function(event) {
        performSignin(event, $("#username2").attr('value'), $("#password2").attr('value'), $('#signin').find('label').hasClass('checked'));
        event.preventDefault();
    });

    $("#RegisterButton").not('.disabled').click(function(event) {
    performRegister(event, $("#username").attr('value'), $("#email").attr('value'), "", $("#password").attr('value'), $("#confirmPassword").attr('value')/*, $("#registrationCode").attr('value')*/);
        event.preventDefault();
    });

    /* These are used to swap input type 'text' with 'password' on focus */
    function passwordFocus() {
        $('#password2').focus(function() {
            if (this.value == "Password") {
                $(this).replaceWith('<input id="password2" name="password2" type="password" value="" />');
                $('#password2').focus();
                passwordBlur();
            }
        });
    }
    function passwordBlur() {
        $('#password2').blur(function() {
            if (this.value == "") {
                $(this).replaceWith('<input id="password2" name="password2" type="text" value="Password" />');
            }
            passwordFocus();
        });
    }
    //Checkbox form Elements
    $('.checkbox label').click(function() {
        $(this).toggleClass('checked').next('input[type=checkbox]').click();
    });
}

/* Do Signin Event */
function performSignin(event, username, password, rememberMe) {
    var request = new Object();
    request.username = username;
    request.password = password;
    request.rememberMe = rememberMe;
    $('.signinResponse').hide();
    $.post($('#loginActionUrl').attr('value'), request, function(result) {
        $('.signinResponse').html(result);
        // If signed out
        if ($('.user-info').length == 0) {
            showAlert(result);

        }
        else {
            window.location.reload(true);
        }

    });
}


/* Do Register Event */
function performRegister(event, username, email, identification, password, repassword/*, registrationCode*/) {
    var request = new Object();
   // request.registrationCode = registrationCode;
    request.username = username;
    request.email = email;
    request.identification = identification;
    request.password = password;
    request.confirmPassword = repassword;

    $('.signinResponse').hide();
    $.post($('#registerActionUrl').attr('value'), request, function(result) {
        $('.signinResponse').html(result);
        closePopDiv();
        //event.preventDefault();


        // If signed out
        if ($('.user-info').length == 0) {
            showAlert(result);
        }
        else {
            window.location.reload(true);
        }
    });
}

function performResetPassword(username, email) {
    var request = new Object();
    request.username = username;
    request.accountEmail = email;

    closePopDiv();
    
    $.post($('#resetPasswordUrl').attr('value'), request, function(result) {
        showAlert(result);

    });
}

/*------------- /Set Up Homepage -----------------------------*/