var ComicNavigator =
{
	_nextId: null,
	_prevId: null,

	Initialize: function( nextId, prevId )
	{
		this._nextId = nextId;
		this._prevId = prevId;

		if ( window.navigator.userAgent.toLowerCase().indexOf("msie") != -1 )
		{
			document.body.onkeydown = this._handleOnKeyDownEvent;
		}
		else
		{
			window.onkeydown = this._handleOnKeyDownEvent;
		}
	},

	NextComic: function()
	{
		this._followLink( this._nextId );
	},

	PrevComic: function()
	{
		this._followLink( this._prevId );
	},

	_handleOnKeyDownEvent: function(e)
	{
		if ( !ComicNavigator._isTargetValid(e) )
		{
			return;
		}

		switch ( ComicNavigator._getEventKeyCode(e) )
		{
		case 37:
			ComicNavigator.PrevComic();
			break;
		case 39:
			ComicNavigator.NextComic();
			break;
		}
	},

	_isTargetValid: function(e)
	{
		var target = null;
		if ( e && e.target )
		{
			target = e.target;
		}
		else if ( window.event && window.event.srcElement )
		{
			target = window.event.srcElement;
		}
		else
		{
			return true;
		}

		if ( target.nodeType == 3 )
		{
			target = target.parentNode;
		}

		switch ( target.tagName.toLowerCase() )
		{
			case "textarea":
			case "input":
			case "select":
				return false;
			default:
				return true;
		}
	},

	_getEventKeyCode: function(e)
	{
		if ( window.event )
		{
			return window.event.keyCode;
		}
		else if ( e.which )
		{
			return e.which;
		}
		return 0;
	},

	_followLink: function(id)
	{
		var link = document.getElementById(id);
		if ( link )
		{
			window.open(link.href, "_self");
		}
	}
};


ComicNavigator.Initialize( "comic-nav-next-link", "comic-nav-prev-link" );


