var http;

//login/logout related stuff
//--------------------------------------------------------------------------------
function showLogin() {
	var customCallback = function() {
		document.getElementById('username').focus();
	}

	addPhpToDiv(document.getElementById('content'), '../includes/loginPage.php', true, customCallback);
}

function loginSubmit(form) {
	document.getElementById('errorDiv').innerHTML = "";
	form = document.getElementById('loginForm');
	form.username.disabled = true;
	form.password.disabled = true;

	var query = "../includes/submits/loginPageSubmit.php";

	var poststr = "username=" + encodeURI( form.username.value )+"&password=" + encodeURI( form.password.value );
	var successFunction = function() {
		drawAdminOptions(true); //add all the administration buttons
		showHome(new Object()); //goto home page
	};
	submitToPhp(query, document.getElementById('errorDiv'), successFunction, poststr);
	form.username.disabled = false;
	form.password.disabled = false;
}

function logoutSubmit() {
	var successFunction = function() {
		showHome(new Object()); //goto home page
		drawAdminOptions(false); //remove the administration button

	};

	submitToPhp("../includes/submits/logoutSubmit.php", document.getElementById('errorDiv'), successFunction, "");
}


function contactSubmit() {
	//Ajax submit of contact form...
	var email = document.getElementById('emailTextbox');
	var comments = document.getElementById('commentsTextbox');
	var securityImage = document.getElementById('securityTextbox');

	//Errors go to 'errorDiv'
	if (email != null && comments != null && securityImage != null) {
		var query = "../includes/submits/contactSubmit.php";

		var poststr = "email=" + encodeURI( email.value )+"&message=" + encodeURI( comments.value )+"&contactSecurity=" + encodeURI( securityImage.value );
		var successFunction = function() {
			showContactSuccess(new Object());
		};
		submitToPhp(query, document.getElementById('errorDiv'), successFunction, poststr);
	}
	else {
		document.getElementById('errorDiv').innerHTML = "";
	}

}


//add all the administration buttons to the adminDiv on the left (logout, and ideas, or just admin...)
function drawAdminOptions(isLoggedIn) {
	var adminDiv = clearDiv('adminDiv');

  	if (isLoggedIn) {
		var logoutButton = createAnchor("javascript:logoutSubmit()", "");
	    logoutButton.appendChild(createImage("images/logoutButton.jpg", ""));
		var ideaButton = createAnchor("javascript:showPostIdea()", "");
		ideaButton.appendChild(createImage("images/postIdeaButton.jpg", ""));

		adminDiv.appendChild(logoutButton);
		adminDiv.appendChild(ideaButton);
	}
	else { //not logged in, so just draw the top menu item
	    var adminButton = createAnchor("javascript:showLogin()", "");
	    adminButton.appendChild(createImage("images/adminButton.jpg", ""));
	    adminDiv.appendChild(adminButton);
	}
}
//--------------------------------------------------------------------------------


//right menu item commands
//--------------------------------------------------------------------------------
function showHome(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/homePage.php', true);
}
function showPhotos(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/photosPage.php', true);

}
function showCode(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../code/index.php', true);
}
function showArchive(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/archivePage.php', true);
}
function showLinks(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/linksPage.php', true);
}
function showContact(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/contactPage.php', true);
}
function showContactSuccess(event) {
	if (event != undefined) //this fixes mozillas 'dblclick' on single click problem..
		addPhpToDiv(document.getElementById('content'), '../includes/contactSuccess.php', true);
}
//--------------------------------------------------------------------------------


//content grabbing functions
//--------------------------------------------------------------------------------

//get a news item and add it to a div. If the div has the tag "gotContent" set, then just grab the title...
//closedContentType is the state of the news item in its collapsed (closed) state, and can be one of 'title', 'truncated', or ''
function getNewsItem(id, divId, closedContentType) {
	var div = document.getElementById(divId);
	var contentType = 'truncated';
	var currentContentType = '';
	if (div.getAttribute('gotContent') == null) {
		div.setAttribute('gotContent', 'true');
		contentType = 'full';
	}
	else {
		div.removeAttribute('gotContent');
	}

	saveScrollPosition();
	div.innerHTML = "<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Loading...";
	addPhpToDiv(div, '../includes/submits/getNewsItem.php?id=' + id + '&content=' + contentType + '&closedContentType=' + closedContentType, true, scrollToSavedPosition);

}

var scrollPositionX, scrollPositionY;
function saveScrollPosition() {
	if (document.all) {
		if (!document.documentElement.scrollLeft)
			scrollPositionX = document.body.scrollLeft;
		else
			scrollPositionX = document.documentElement.scrollLeft;

		if (!document.documentElement.scrollTop)
			scrollPositionY = document.body.scrollTop;
        else
            scrollPositionY = document.documentElement.scrollTop;
	}
	else {
		scrollPositionX = window.pageXOffset;
        scrollPositionY = window.pageYOffset;
    }

}

function scrollToSavedPosition() {
	window.scrollTo(scrollPositionX, scrollPositionY);
}

//--------------------------------------------------------------------------------




//generic element functions
//--------------------------------------------------------------------------------
function createImage(src, alt) {
    var img = document.createElement('IMG');
    img.src = src;
    img.alt = alt;
    img.border = "0";
    return img;
}
function createAnchor(href, onMouseDown) {
    var anchor = document.createElement('A');
    anchor.href = href;
    anchor.onmousedown = onMouseDown;
    return anchor
}
function createErrorDiv(msg) {
	div = document.createElement('DIV');
	div.setAttribute('class', 'errorDiv');
	div.setAttribute('className', 'errorDiv');
	div.appendChild(document.createTextNode(msg));
	return div;
}
function createBR() {
	br = document.createElement('BR');
	return br;
}
function createPopupDiv(left, top, width, height) {
	var newDiv = document.createElement('div');
	newDiv.style.position = 'absolute';
	newDiv.style.width = width+'px';
	newDiv.style.height = height+'px';
	newDiv.style.left = left + 'px';
	newDiv.style.top = top + 'px';
	newDiv.style.zIndex = '1000';
	newDiv.id = 'popupDiv';

	newDiv.style.backgroundColor = '#ffffff';
	newDiv.style.border = 'solid 1px #000000';
	newDiv.style.padding = '10px';

	return newDiv;
}
function createBlankTable(){
	var table = document.createElement("table");
	table.cellPadding = 2;
	table.cellSpacing = 0;
	return table;
}

//clears a div by name and returns the div...
function clearDiv(divName) {
	var div = document.getElementById(divName);
	//clear existing nodes
	while (div.childNodes.length > 0)
		div.removeChild(div.childNodes[0]);
	return div;
}

//--------------------------------------------------------------------------------


//get administration pages
//--------------------------------------------------------------------------------

//if newsId is specified, then we are editing and existing item
function showPostNews(newsId) {
	var customCallback = function() {
		document.getElementById('title').focus();
	}

	var query = "";
	if (newsId != undefined)
		query = "?id=" + newsId;
	addPhpToDiv(document.getElementById('content'), '../includes/postNewsPage.php' + query, true, customCallback);
}

function showPostPhoto() {
	addPhpToDiv(document.getElementById('content'), '../includes/postPhotoPage.php', true);
}

function showPostMovie() {
	addPhpToDiv(document.getElementById('content'), '../includes/postMoviePage.php', true);
}

function showPostIdea() {
	addPhpToDiv(document.getElementById('content'), '../includes/postIdeaPage.php', true);
	ideaMaker = new ideasPoster();
}

function showPostLink() {
	alert('to be implemented');
}


function deleteNews(newsId) {
	if (confirm('Are you sure you want to delete this news?')) {
		var query = "../includes/submits/newsDelete.php";

		var poststr =  "id=" + encodeURI(newsId);
		var successFunction = function() {
			showHome(new Object());
			scrollToSavedPosition();
		};
		saveScrollPosition();
		submitToPhp(query, document.getElementById('postErrorDiv'), successFunction, poststr);
	}
}

function deleteComment(commentId) {
	if (confirm('Are you sure you want to delete this comment?')) {
		var query = "../includes/submits/commentDelete.php";

		var poststr =  "id=" + encodeURI(commentId);
		var successFunction = function() {
			showHome(new Object());
			scrollToSavedPosition();
		};
		saveScrollPosition();
		submitToPhp(query, document.getElementById('postErrorDiv'), successFunction, poststr);
	}
}

//--------------------------------------------------------------------------------



//random functions
//--------------------------------------------------------------------------------
function fadePageOn() {
   	var div = document.getElementById('fadediv');
   	var madeNewDiv = (div == null);

   	if (madeNewDiv)
		div = document.createElement('DIV');

	div.style.position = 'absolute';
	div.style.width = document.body.offsetWidth+'px';
	div.style.height = document.body.offsetHeight+'px';	
	div.style.left = '0px';
	div.style.top = '0px';
	div.style.zIndex = '999';
	div.style.background = '#555555';
	div.style.opacity = '.40';
	div.style.filter = 'alpha(opacity=40)';

	div.id = 'fadediv';
   	if (madeNewDiv)
		document.body.appendChild(div);
	else
		div.style.display = '';
}

function fadePageOff() {
	div = document.getElementById('fadediv');
	if (div != null)
		div.style.display = 'none';
}

function toggleShowHide(id) {
	div = document.getElementById(id);
	if (div != null) {
		if (div.style.display == '')
			div.style.display = 'none';
		else
			div.style.display = '';
	}
}
//--------------------------------------------------------------------------------
























//------------------------------------------------------------------------------------------------------