var _PAGE_CONTENT = 'page.php';
var _SECTION_CONTENT = 'section.php';
var _PAGE_ATTR = 'page-attr.php';

$(document).ready(function(){
	
    // DIALOG BOX
    $("#dialog").dialog({
		height:		500,
		width:		300,
		modal:		true,
		autoOpen:	false
	});
    
	// LEFT COLUMN HIDING
	var autohide = $("#autohide").attr('value');
	var permahide = $("#permahide").attr('value');
	var genhide = (autohide == 1 || permahide == 1) ? 1 : 0;
	
	if (genhide == 1) {
		if (permahide == 0) {
			$("#hideBar").toggle(showLeftCol, hideLeftCol);
		}
		$("#content").css('background', 'url(\'images/bg-r.png\')');
		$("#hideBar").css('border-left', '1px dashed #AAA');
		$("#hideBar").css('left', '0px');
		$("#hideBar").css('width', '3px');
		$("#leftCol").css('display', 'none');
		$("#middleCol").css('width', '705px');
	} else {
		$("#hideBar").toggle(hideLeftCol, showLeftCol);
		$("#content").css('background', 'url(\'images/bg.png\')');
	}
	
	// EDITING
    if ($("#editing").attr('value') == 1) {
	    startEditing();
    }
    
    // ADDENDA
    initAddenda();
    
	
    // NAV LINK ANIMATION
	$(".navLink").hover(function() {
		$(this).toggleClass("buttonOn");
		$(this).toggleClass("buttonOff");
	}, function() {
		$(this).toggleClass("buttonOn");
		$(this).toggleClass("buttonOff");
	});
	
	// LOGIN QUESTION MARK
	$(".loggedOut #forgottenHover").hover(function() {
		$("#forgottenText").show('fast');
	}, function() {
		$("#forgottenText").hide('slow');
	});
	
	// LOGIN FUNCTIONS
	$("#username, #password").focus(function() {
		getSeed();
	});
	
	$("#username, #password, #signin").blur(function() {
		$("#errorText").hide('fast');
	});
	
	$("#username, #password").keypress(function() {
		$("#errorText").hide('fast');
	});
	
	$("#loginForm").submit(function() {
		validateLogin();
    });
    
    $(".logoutLink").click(function() {
		$.post("logout.php", {}, function(text) {
			$("#userBox").html("?");
		});
    });
    
    // SEARCH FUNCTIONS
    $(".navSearchButton").click(siteSearch);
    $(".searchBar").keypress(function(e) {
	    if (e.keyCode == 13 || e.which == 13) {
		    siteSearch();
	    }
    });
});

function siteSearch() {
	var searchQuery = $(".searchBar").attr("value");
	searchQuery = searchQuery.replace(/ /g, '_');
	var edit = $("#editing").attr('value');
	window.location = "?s=28&p=140" + ((edit == 1) ? '&e=1' : '') + "&sq=" + searchQuery;
}

function hideLeftCol() {
	$("#leftCol").animate( {opacity: 0}, 500, 'swing', function() {
		$("#content").css('background', 'url(\'images/bg-r.png\')');
		$("#hideBar").css('border-left', '1px dashed #AAA');
		$("#hideBar").animate( { left: '0px', width: '3px'}, 1500 );
		$("#leftCol").css('display', 'none');
		$("#middleCol").animate( { width: '705px'}, 1000 );
		$("#leftcolhidden").attr("value", "1");
	});
}

function showLeftCol() {
	$("#hideBar").css('border-left', '0px');
	$("#hideBar").animate( { left: '247px'}, 1500 );
	$("#middleCol").animate( { width: '460px'}, 1000, function() {
		$("#content").css('background', 'url(\'images/bg.png\')');
		$("#leftCol").css('display', 'block');
		$("#leftCol").animate( { width: '249px', opacity: 1}, 500, 'swing', function() {
			$("#leftcolhidden").attr("value", "0");
		});
	} );
}

function searchFor() {
	var search = document.getElementById('search').value.toLowerCase();
	if (search == "daire mcnab" || search == "dáire mcnab" || search == "daisy mcnab") {
		document.getElementById("banner").src = 'images/dairebanner.jpg';
	}	
}