var dirty = false;
jQuery(document).ready(function($) {
        $('a[rel*=facebox]').facebox();
        $('div.submit').click(function(){$(this).parents("form").submit();});
        $('input[type=text].labelify').labelify();
        $('input[type=textarea].labelify').labelify();
        $('div.btn').corner("5px");
        /*
        $(".btn").hover(function(){$(this).css({'color':'#FFF495'});},
                        function(){$(this).css({'color':'#FFFFFF'});});
        */
        $('#id_login_password').live('blur', function(){
            if($(this).val() == '') $('#id_login_password').Text2Pass({text: "Password"});
        });
        if($("#id_login_password").val() == '') //Account for password manager auto-filling password
            $('#id_login_password').Text2Pass({text: "Password"});

        if($.browser.msie) {
            $('input[type=password]').live('keydown', function(e){
                if (e.keyCode == 13) {
                    $(this).parents('form').submit();
                    return false;
                }
            });
        }
        
    });

function popup( position ) {
	var display = $("#login_popup").css('display');
	if( display == "none" ) {
                if( position == "bottom" ) {
                    $("#login_popup").css('top', "625px");
                    $("#login_popup").css('left',"200px");
                } else {
                    $("#login_popup").css('top', "18px");
                    $("#login_popup").css('left', "765px");
                }
                $("#login_popup").css('display', "block");
	} else {
		$("#login_popup").css('display', "none");
	}
}

function clearInput( thisfield, defaulttext ) {
	if ( thisfield.value == defaulttext ) {
		thisfield.value = "";
	}
}

function recallInput(thisfield, defaulttext) {
	if ( thisfield.value == "" ) {
		thisfield.value = defaulttext;
		thisfield.style.color = "#737373";
	}
}

function changeStyle( thisfield ) {
	thisfield.style.color = "#000";
}

function submenu_click(link){
    return function(){
        if(dirty){
            $.facebox($("#confirm").html());
            $("#redirect").val(link.attr('href'));
        }
        else{
            // Go to submenu link to get #frag in url 
            window.location.href=link.attr('href');
            // Set form action so we remember current page we're on
            $("#frag").val(link.attr('href'));
            //  call click event manually
            link.click();
            // rebuild prev/continue buttons
            checklist_menu();
        }
    }
}

function main_menu_click(link){
    return function(){
        if(dirty){
            jQuery.facebox($("#confirm").html());
            $("#redirect").val(link.attr('href'));
            return false;
        }
        else
            window.location.href=link.attr('href');
    }

}    
function checklist_menu(){
    // Setup prev/next buttons
    cur = $("div.active");
    var cur_sub = cur.siblings(".submenu.highlight");
    var prev = cur.prev();
    var prev_link = prev.find('a');
    var next = cur.nextAll('.chkmenu');
    if(next.length > 0)
        next = $(next[0]);
    var next_link = next.find('a');
    // all sections besides getting started have submenu items
    if(cur_sub){
        //Should next just go to submenu link?
        if(cur_sub.next().hasClass("submenu")){
            next_link = ''; //Don't go to next menu link
            var sub_next = cur_sub.next().find('a');
            $("#next_button").unbind('click');
            $("#next_button").click(submenu_click(sub_next));
        }
        //Should prev just go to submenu link?
        if(cur_sub.prev().hasClass("submenu")){
            prev_link = ''; //Don't go to next menu link
            var sub_prev = cur_sub.prev().find('a');
            $("#prev_button").unbind('click');
            $("#prev_button").click(submenu_click(sub_prev));
        }
    }
    if(prev_link.length){
        $("#prev_button").unbind('click');
        $("#prev_button").click(main_menu_click(prev_link));
    }
    if(next_link.length){
        $("#next_button").unbind('click');
        $("#next_button").click(main_menu_click(next_link));
    }
    // If there is an additional/required measures section that's hidden
    if($(".additional:visible").length == 1){
        //Unbind other handlers
        $("#next_button").unbind('click');
        $("#next_button").click(function(){
                $(".additional:visible").click();
                checklist_menu();
                return false;
            });
    }
    if($(".required:visible").length == 1){
        //Unbind other handlers
        $("#prev_button").unbind('click');
        $("#prev_button").click(function(){
                $(".required:visible").click();
                checklist_menu();
                return false;
            });
    }
    
}

