// Javascript //

var pagePosition = 0;
var nextPage = 0;
var scrollPage = 0;

// We only need to load comments once per page
// after that, simply redisplay the comment div
var commentsLoaded = 0;

function el(id) {
	return document.getElementById(id);
}

function commentToggle() {
	var ele = el('commentdiv');
	if(parseInt(ele.style.height) > 50)
		ele.style.height = 50+'px';
	else
		ele.style.height = 250+'px';
	
}

function pageInit(id, adjust) {
	pagePosition += adjust;
	
	if(pagePosition < 0)
		pagePosition = 0;
	
	// New page; reset comments
	commentsLoaded = 0;
	el('commentBox').innerHTML = "<span class='commentfloatbox'><a href='#' onClick=\"el('commentBox').style.display='none';\">&lt; close window &gt;</a></span>";
	
	el('forwardButton').disabled = true;
	el('backwardButton').disabled = true;
	el('pageJump').selectedIndex = pagePosition;
	el('pageJump').disabled = true;
	xajax_pageChange(id,pagePosition);
}

// Original function courtesy of http://wiki.xajaxproject.org
var OnKeyRequestBuffer = 
    {
        bufferText: false,
        bufferTime: 750,
        
        modified : function(strId)
        {
        	el('title_error').innerHTML = '';
        	if(xajax.$(strId).value == "")
        		return;
                setTimeout('OnKeyRequestBuffer.compareBuffer("'+strId+'","'+xajax.$(strId).value+'");', this.bufferTime);
                
        },
        
        compareBuffer : function(strId, strText)
        {
            if (strText == xajax.$(strId).value && strText != this.bufferText)
            {
                this.bufferText = strText;
                OnKeyRequestBuffer.makeRequest(strId);
            }
        },
        
        makeRequest : function(strId)
        {
            xajax_checkExistingTitle(xajax.$(strId).value);
        }
    }

function sidebar_flip() {
	var ele = document.getElementById('main');
	if(el('subnav').style.display != 'none') {
		el('subnav').style.display = 'none';
		// Apply some effects to simulate an expanded sidebar
		ele.style.backgroundColor = '#F7F7F7';
		ele.style.width = '838px';
	}
	else {
		el('subnav').style.display = '';
		ele.style.backgroundColor = 'transparent';
		ele.style.width = '680px';
	}
}

function deleteStoryPrompt(story) {
	if(confirm("Are you sure you wish to delete this story?"))
		return true;
	return false;
}

function scrollPageBar(id) {
	el("num_"+id+"_1").scrollIntoView(false);
}

function sideFocus(field, val) {
	field.value = "";
	field.style.color = "#000";
	if(val == 2)
		field.type = 'password';
}

function sideBlur(field, val) {
	if(field.value != "")
		return;
	field.type = 'text';
	field.value = (val==1?"Username":"Password");
	field.style.color = "#444";
}

function JumpToPage(id, item) {
	pageInit(id, item.value-pagePosition-1);
}
