// a la http://www.codingforums.com/archive/index.php/t-59339.html
function get(name) {
	var q = unescape(location.search.substring(1)).split(/[=&]/);
	for (var j=0; j<q.length; j+=2) {
		if (q[j] == name) {	return q[j+1]; }
	}
	return null;
}

// http://docs.jquery.com/Tutorials:Getting_Started_with_jQuery
 jQuery.fn.check = function(mode) {
   // if mode is undefined, use 'on' as default
   var mode = mode || 'on';
   
   return this.each(function() {
     switch(mode) {
       case 'on':
         this.checked = true;
         break;
       case 'off':
         this.checked = false;
         break;
       case 'toggle':
         this.checked = !this.checked;
         break;
     }
   });
 };

function arrange() {
	if ($(window).width() <= 620 ) {
		$("#big").addClass("compact");	
	} else {
		$("#big").removeClass("compact");	
	};
};

var resizeTimer = null;
$(window).bind('resize', function() {
	if (resizeTimer) clearTimeout(resizeTimer);
	resizeTimer = setTimeout(arrange, 50);
});

$(document).ready(function(){
	arrange();
	$(":text:visible:enabled:first").focus();
	//hide window checkbox if it's already a new window
	if (window.name == "floyd_window") { $("#p_newwindow").hide(); }
	// pull in variables from url, if any
	varvar = get("u"); if (varvar !== null && varvar !== "") {$("#username").val(varvar);}
	varvar = get("p"); if (varvar !== null && varvar !== "") {$("#password").val(varvar);}
	$("#login").submit(function(){
		$("#ohno_login").fadeOut();
		$.post("/ajax/login.php", {username: $("#username").val(), password: $("#password").val(), rememberme: $("#rememberme").val(), newwindow: $("#newwindow").val()}, 
		function(data){
			if(data == "1") {
				if ($("#newwindow").attr("checked") == true) {
					window.location = "newwindow.php";
				} else {
					window.location.reload();
				}
			} else {
				$("#ohno_login").fadeIn();
			}
		});
		return false;
	});
	$(".checkboxes span").click(function(event) {
		$("#"+$(this).attr("rel")).check('toggle');
	});
});
