function logRead() {
	new Ajax.Request('ajax/logRead', {parameters: {story: currentstory , chapter: currentchapter, page: pages.activeLink.toString()}});
}

function np() {
	pages.next();
	takenAction = false;
	logRead();
}

function pp() {
	pages.previous();
	takenAction = false;
	logRead();
}

var takenAction = false;

//IE&Gecko Code
function IEGeckoKeyPress(oEvent) {
	if (!oEvent) var oEvent = window.event;
	if (oEvent.keyCode) myKeyCode = oEvent.keyCode;
	else if (oEvent.which) myKeyCode = oEvent.which;
	if (oEvent.repeat || takenAction) {	return;	}
	if (myKeyCode >= 16 && myKeyCode <= 18) { return; }
	if (oEvent.shiftKey) { myKeyCode += 1000; }
	if (oEvent.ctrlKey)  { myKeyCode += 2000; }
	if (oEvent.altKey)   { myKeyCode += 4000; }
	//alert(oEvent.type + "=" + myKeyCode);
	myKeyPress(myKeyCode);
}

function myKeyPress(myKeyCode) {
	switch (myKeyCode) {
		case 39:					// RIGHT arrow
		case 107:					// NUM +
			takenAction = true;
			np();
			break;
		case 37:					// LEFT arrow
		case 109:					// NUM -
			takenAction = true; 
			pp();
			break;
		}
}

function initKeyboard() {
	document.onkeydown = IEGeckoKeyPress;
}


var Client = {
  viewportWidth: function() {
    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
  },

  viewportHeight: function() {
    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
  },

  viewportSize: function() {
    return { width: this.viewportWidth(), height: this.viewportHeight() };
  }
};

function startUpdates() {
	Event.observe(window, 'load', function() {window.setInterval("new Ajax.Updater('statusupdates', 'ajax/ping');", 120000);});
}

function showExplanation(container, id) {
	new Ajax.Updater(container, 'ajax/displayExplanation', {parameters: {explanation: id}});
}

function openLayer(link) {
	if ($('lightbox')==undefined) addLightboxMarkup();

	$$('div.tooltip').each(function(elem) {elem.hide();});	
	$$('div.inlineCommentPopup').each(function(elem) {elem.hide();});	

	var a = Builder.node('a', {href: link}, ' ');
	var lb = new lightbox(a); 
	lb.activate();

	if (link.match('ajax')) {
		self.location.href=self.location.href.split('#')[0]+'#/layer'+link.split('ajax')[1];
		window.opennewlayer = false;
		}
	
	pageTracker._trackPageview("/layer/"+link);

//	new Draggable('lightbox', {handle: 'logonBox', starteffect: '', endeffect: '', zindex: 5100});
//	positionBox();
}

function closeLayer() {
	var a = Builder.node('a', {href: '#'}, ' ');
	var lb = new lightbox(a); 
	lb.deactivate();
}

function showAlert(x, y) {
	if ($('overlay')) {
		if (y!=undefined) openLayer('ajax/displayAlert/'+x+'/'+y);
		else openLayer('ajax/displayAlert/'+x);
	} else {
		if (y!=undefined) window.setTimeout("openLayer('ajax/displayAlert/"+x+"/"+y+"');", 2000);
		else window.setTimeout("openLayer('ajax/displayAlert/"+x+"');", 2000);
	}
}

function showFAQ(x) {
	if (self.location.href.match(/\/faq/) && !self.location.href.match(/layer\/faq/)) {
		if ($('selectSection').style.display='none') {
			$('selectSectionMore').hide(); 
			$('selectSection').show(); 
			$$('#section_header').each(function(el) {el.show();}); 
			$$('ul.searchResultsList').each(function(el) {el.show();}); 
		}
		Effect.ScrollTo('question_'+x, {duration:0.3, offset: -20, afterFinish: function() { new Effect.BlindDown('answer_'+x, {duration: 0.3});}});
	} else openLayer('ajax/faq/'+x);
}

function noModifierPressed(event) {
	if (!event) event = window.event; 
	return (!(event.altKey || event.ctrlKey || event.shiftKey || event.metaKey));
}

function setPageHeight() {
	if ($$('.textbox').length>1) {
		$$('.page').each(function(el){ el.show(); });

		var pagesMaxHeight = 0;
		$$('.singlepage').each(function(el){
			if (el.getHeight()>pagesMaxHeight) pagesMaxHeight = el.getHeight();
		});

		$$('.textbox').each(function(el){
			el.style.height = pagesMaxHeight+'px';
		});

		
		if (window.pages) {
			$$('.page').each(function(el){ el.hide(); });
			window.pages.activeContainer.show();
		}
	}
	
	if ($('text').style.height=='auto') $('text').style.height='100%';
	else $('text').style.height='auto';
}

/*
   FUNCTION:
   GetSelText()
   Grabs the text that is currently selected.
   Jeff Anderson (9/1/2006)
   http://www.codetoad.com/javascript_get_selected_text.asp
*/


function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;
	return txt;
}


function api(type, id, method, args, callbacks) {
	if (typeof(callbacks)!=='object') callbacks = new Object();
	var obj = new Object();
	obj['object'] = new Object();
	obj['object']['type'] = type;
	obj['object']['id'] = id;
	obj['method'] = method;
	obj['params'] = args;
	
	callbacks["parameters"] = new Object();
	callbacks["parameters"]["json"] = Object.toJSON(obj);
	new Ajax.Request('api/call', callbacks);
}

function saveFormElement(el, type, id, method, arg) {
	if (!$(el).next().hasClassName('invalid')) {
		$(el).disabled = true;
		$(el).next().update('SAVING');
		api(type, id, method, [arg, $(el).value], {onSuccess: function() {$(el).next().update('SAVED'); $(el).disabled = false; window.setTimeout("$('"+$(el).next().identify()+"').update(''); ", 3000);}});
	}
}

function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);
}

function trim(t) {
	if (t[0]==' ') t = t.substr(1, t.length);
	if (t[t.length]==' ') t = t.substr(0, t.length-1);
	if (t[0]==' ' || t[t.length]==' ') return trim(t);
	else return t;
}



var opennewlayer = true;
