function hoverLoginBox()
{
    document.getElementById('loginbox').className = 'semiactiveloginbox';
}

function unhoverLoginBox()
{
    document.getElementById('loginbox').className = '';
}

function toggleStickyLoginBox()
{
    var lb = document.getElementById('loginbox'); 
    if (lb.className == 'activeloginbox') 
    {
        lb.className = '';
    } 
    else 
    {
        lb.className = 'activeloginbox';
    } 
    return false;
}

// Telegrams
function ToggleAll(b)
{
    var tl = document.telegramlist;
    var len = tl.elements.length;
    for (var i = 0; i < len; i++)
    {
	var e = tl.elements[i];
	if (e.name.slice(0,15) == "delete_message_")
	{
	    e.checked = b.checked;
	}
    }
}

function createNationUpdateHistoryText(sel)
{
   var choice =  sel.options[sel.selectedIndex].value;
   var txt;
   if (choice == 'pioneer')
    txt = 'Brave, decreasingly well-financed explorers traveled across the globe in search of virgin lands; upon finding them, declined to notify homeland.';
   else if (choice == 'tribe')
    txt = 'Lived contentedly in self-contained universe until discovering actual universe larger than ten by twelve miles.';
   else if (choice == 'sacker')
    txt = 'Simple-minded barbarians met advanced, scholarly empire; barbarians sacked empire for shiny things.';
   else if (choice == 'isolationist')
    txt = 'Disparate peoples drawn together by common urge to escape homeland oppression/debts/outstanding warrants.';
   else if (choice == 'segregationalist')
    txt = 'Clamored for right to secede from unrepresentative majority using petitions, speeches, letter campaign, automatic weapons.';
   else if (choice == 'refugee')
    txt = 'Fled bloodthirsty regime bent on extermination; now shaken and stirred.';
   else if (choice == 'wrangler')
    txt = 'Made persuasive case for world superpower to annex other nation on their behalf.';
   else if (choice == 'survivor')
    txt = 'Slightly stunned remnants of nation-shattering internal conflict over issues which remain unclear.';
   else if (choice == 'pilgrim')
    txt = 'Departed godless homeland to found new spiritual paradise-on-Earth; some decades lost after following wild-eyed man into desert.';
   document.getElementById('historytext').innerHTML = txt;
}

// submit_issue, create_nation2
function submitForm(s, txt)
{
    s.disabled = true;
    s.value = (txt || " Submitting... ");
    return false;
}
// create_nation2
function checkLegal(v)
{
    if (!v)
    {
        alert("You must agree to the site Terms & Conditions " +
                "before continuing.");
        return false;
    }
    return true;
}
function popup(mylink, windowname)
{
   if (! window.focus) return true;
   var href;
   if (typeof(mylink) == 'string')
      href=mylink;
   else
      href=mylink.href;
   if (windowname == 'check_name')
      href += '?name=' + window.form.name.value;
   window.open(href, windowname,
        'width=600,height=450,resizable=yes,scrollbars=yes');
   return false;
}
// create_region
function changePasswordStatus(v)
{
    if (v) { document.getElementById("PasswordLabel").className="unprotected"; }
    else { document.getElementById("PasswordLabel").className="protected"; }
    document.getElementById("rpassword").disabled = !v;
    return false;
}

(function($){
    rmb = function(region) {
	$.fn.replaceParentRmbRow = function(action) { 
	    var row = $(this).parents('.rmbrow');
            var postid = $(row).attr('class').split('post-')[1];
            var done = false;
            row.fadeOut(1000, function() {
                if (!done) {
                    row.hide().html('<p class="spinner"><img src="/images/loading1.gif">').show('slow');
                }
            });
            $.get('/page=ajax/a=' + action + '/region=' + region + '/postid=' + postid, function(data) {
                row.hide().replaceWith(data);
                $('.post-' + postid).hide().show(200);
                done = true;
            });
	};
	$('#olderrmb').click(function(event){
	    event.preventDefault();
	    $('.rmbolder .notloading').hide();
	    $('.rmbolder .loading').show();
	    rmboffset += 10;
	    $.get('/page=ajax/a=rmb/region=' + region + '/offset=' + rmboffset, function(data) {
		if (data.length > 1) {
		    $(data).insertBefore('.rmbrow:first').hide().show('slow');
		} else {
		    $('.rmbolder').text("At Earliest Message");
		}
		$('.rmbolder .loading').hide();
		$('.rmbolder .notloading').show();
	    });
        });
	$('a.rmbsuppress,a.rmbunsuppress').live('click', function(event) {
	    event.preventDefault();
	    var which = $(this).attr('class').match(/\w+suppress/);
	    $(this).replaceParentRmbRow(which);
	});
	$('a.rmbshow:not(.noa)').live('click', function(event) {
	    event.preventDefault();
	    var postid = $(this).parents('.rmbrow').attr('class').split('post-')[1];
	    $('.suppressedbody-' + postid).toggle('slow');
	});
	$('a.rmbdelete').live('click', function(event) {
	    event.preventDefault();
	    if (confirm("Are you sure you want to delete your post?")) {
	        $(this).replaceParentRmbRow('rmbdelete');
	    }
	});
	$('a.rmbmod-delete').live('click', function(event) {
	    event.preventDefault();
	    if (confirm("You are about to permanently mod-delete an RMB post. Are you sure you want do this?")) {
                $(this).replaceParentRmbRow('rmbmoddelete');
	    }
	});
	$('#embassymore').click(function(event) {
	    event.preventDefault();
	    $(this).hide();
	    $('#embassyothers').show(100);
	}); 
	$('#tagmore').click(function(event) {
	    event.preventDefault();
	    $(this).hide();
	    $('#tagothers').show(100);
	}); 
   };
    tooltips = function() {
	$('a.ttq').click(function(event) {
	    event.preventDefault();
	    var tt = $(this).attr('id');
	    $('p#' + tt + 'a').toggle(200);
	});
    };
    createregion = function() {
	$('input#founder_control').click(function(event) {
	    if ($(this).prop('checked')) {
		$('input#delegate_control').attr('disabled', false);
		$('p#p_delegate_control').removeClass('protected');
	    } else {
		$('input#delegate_control').attr('checked', true).attr('disabled', true);
		$('p#p_delegate_control').addClass('protected');
	    }
	});
    };
    createnation = function() {
	$('table').bind('checknationname', function(event) {
	    if ($('a#checknamelink').hasClass('disabled'))
		return false;
	    $('a#checknamelink').addClass('disabled');
	    var row = $('td#namecheckresult');
	    row.html('<p class="info" style="background-color:#F0F0F0; border-color: #CCC;"><img src="/images/loading1.gif" style="vertical-align:middle"> Checking name availability...').show('slow');
	    $.get('/page=ajax2/a=checknationname/name=' + $('input#name').val(), function(data) {
		$('a#checknamelink').removeClass('disabled');
		$('td#namecheckresult').html(data).show(100);
	    });
	});
	$('a#checknamelink').click(function(event) {
	    event.preventDefault();
	    $('table').trigger('checknationname');
	});
	$('input#name').change(function(event) {
	    $('table').trigger('checknationname');
	});
        $('table').bind('writeofficialnationname', function(event) {
	    if ($('input#name').val()) {
		$('h2#officialname').fadeOut(800, function() {
		    $(this).html('The ' + $('select#type option:selected').text() + ' of ' + $('input#name').val()).fadeIn(200);
		});
	    }
	});
	$('table').trigger('writeofficialnationname'); // Do this on page load in case we're redirecting here
	$('input#name').change(function(event) {
	    $('table').trigger('writeofficialnationname');
	});
	$('select#type').change(function(event) {
	    $('table').trigger('writeofficialnationname');
	});
    };
    settings = function() {
	$('select#flag').change(function(event) {
	    $('img#flag_image').hide().attr('src', '/images/flags/' + $('select#flag option:selected').val()).bind('load', function() {
		$(this).fadeIn(400);
	    });
	});
	$('a#settingsShowOpenIDBoxes').click(function(event) {
	    event.preventDefault();
	    $('.openid').toggle(200);
	});
    };
    preview = function() {
        $('input[name="preview"]:not(.noa)').click(function(event) {
                event.preventDefault();
                var button = $(this);
                $('fieldset.preview').hide('slow');
                button.prop('disabled', true);
                button.val("Loading preview...");
                $.post('/page=ajax/a=rmbpreview', { message: $('textarea[name="message"]').val() }, function(data) {
                    $('#previewcontent').html(data);
                    $('fieldset.preview').show('slow', function(event) {
                        button.prop('disabled', false);
                        button.val("Preview");
                    });
                    button.val("Preview");
                    $(window.opera?'html':'html, body').animate({ scrollTop: $('#previewcontent').offset().top }, 800);
                });
        });
    };
})(jQuery);

