//Portions are Copyright (c) 2000-2005, Mobular Technologies&#174;, Inc.  All Rights Reserved.
//Mobular and Mobular Technologies are Registered Trademarks of Mobular
//Technologies, Inc.
//All rights reserved.  Unauthorized reproduction prohibited.
//http://www.mobular.com/
//

var zoomInEnable = {
	_obj 			: "zoomInImg",
	src 			: "images/zoom_in.gif",
	alt 			: "Zoom in"
}

var zoomInDisable = {
	_obj 			: "zoomInImg",
	src 			: "images/zoom_in_dis.gif",
	alt 			: ""
}

var zoomOutEnable = {
	_obj 			: "zoomOutImg",
	src 			: "images/zoom_out.gif",
	alt 			: "Zoom out"
}

var zoomOutDisable = {
	_obj 			: "zoomOutImg",
	src 			: "images/zoom_out_dis.gif",
	alt 			: ""
}

var firstEnable = {
	_obj 			: "first",
	src 			: "images/rewind.gif",
	alt 			: "First page"
}

var firstDisable = {
	_obj 			: "first",
	src 			: "images/rewind_dis.gif",
	alt 			: ""
}

var previousEnable = {
	_obj 			: "previous",
	src 			: "images/previous.gif",
	alt 			: "Previous page"
}

var previousDisable = {
	_obj 			: "previous",
	src 			: "images/previous_dis.gif",
	alt 			: ""
}

var previousDetailEnable = {
	_obj 			: "previous_detail",
	src 			: "images/previous2.gif",
	alt 			: "Next page"
}

var previousDetailDisable = {
	_obj 			: "previous_detail",
	src 			: "images/previous2_dis.gif",
	alt 			: ""
}

var nextEnable = {
	_obj 			: "next",
	src 			: "images/next.gif",
	alt 			: "Next page"
}

var nextDisable = {
	_obj 			: "next",
	src 			: "images/next_dis.gif",
	alt 			: ""
}

var nextDetailEnable = {
	_obj 			: "next_detail",
	src 			: "images/next2.gif",
	alt 			: "Next page"
}

var nextDetailDisable = {
	_obj 			: "next_detail",
	src 			: "images/next2_dis.gif",
	alt 			: ""
}

var nextDocEnable = {
	_obj 			: "nextdoc",
	src 			: "images/fastforw.gif",
	alt 			: "Next document"
}

var nextDocDisable = {
	_obj 			: "nextdoc",
	src 			: "images/fastforw_dis.gif",
	alt 			: ""
}

var uiElements = {

	imgSettings : [
					"_obj",
					"src",
					"alt"
				  ],
	processImg: function(settings, alt_override) {

		var collection = uiElements.imgSettings;
		var _obj = settings[collection[0]];
		var obj = document.getElementById(_obj);
		if (obj) {
			for ( var n=1; n < collection.length; n++ ) {
				var setting = collection[n];
				try {
					var _setting = settings[setting];
					if (alt_override && setting == "alt") { _setting = alt_override }
					obj.attributes[setting].value = _setting;
				} catch (e) { }
			}
		}
	},
	_disableAll: function() {
		// anything else is probably not in the book.
    	this.processImg(zoomInDisable);
	    this.processImg(zoomOutDisable);
	    this.processImg(firstDisable);
	    this.processImg(previousDisable);
		this.processImg(previousDetailDisable);
	    this.processImg(nextDisable);
		this.processImg(nextDetailDisable);
	    this.processImg(nextDocDisable);
	}
};

// Event model listeners

function onEngineIdle() {
	if ( document.getElementById('toc_placeholder') ) {
		var _div = document.getElementById('toc_placeholder');
		var _title = binder.kitData.description;
		var _param = {
			buttonText: ''+_title,
			id: 'toc_placeholder',
			buttonTemplate: "<span class=\"dijit dijitReset dijitLeft dijitInline\" dojoAttachEvent=\"onmouseenter:_onMouse,onmouseleave:_onMouse,onmousedown:_onMouse,onclick:_onDropDownClick,onkeydown:_onDropDownKeydown,onblur:_onDropDownBlur,onkeypress:_onKey\" waiRole=\"presentation\"><div class='dijitReset dijitRight' waiRole=\"presentation\"><button class=\"dijitReset dijitStretch dijitButtonNode dijitButtonContents\" type=\"${type}\"dojoAttachPoint=\"focusNode,titleNode\" waiRole=\"button\" waiState=\"haspopup-true,labelledby-${id}_label\"><div class=\"dijitReset dijitInline dijitButtonText\"  dojoAttachPoint=\"containerNode,popupStateNode\" waiRole=\"presentation\"id=\"${id}_label\">${label}</div></button></div></span>"
		};
		new display.dropDownTOC.dropDownTOC(_param, _div);
	}

	document.getElementById('active_page_count').innerHTML = "";
	dijit.byId('tocDDbutton').containerNode.innerHTML = '[+] Click to select';

	uiElements._disableAll();
	
	try {		
		var _link = document.getElementById('logo').parentNode.href;		
		document.getElementById('footer_customer_link').href = _link;
	} catch (e) {}

	recreateShowShow();
}
eengineConfig.subscribe( "onEngineIdle()", "eengine.postProcess.engineidle");

function onDisplayS2F() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayS2F()", "eengine.postProcess.s2fUI" );

function onDisplayS2D() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayS2D()", "eengine.postProcess.s2dUI" );

function onDisplayPrint() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayPrint()", "eengine.postProcess.displayprintUI" );

function onDisplayHelp() {
	uiElements._disableAll();
}
eengineConfig.subscribe( "onDisplayHelp()", "eengine.postProcess.helpUI" );

function onDisplayPage(widget, mySession){
	var action = mySession.event;

	if (action == 902) {
		var _page_count = binder.kitData.documents[binder.kitData.current.doc].pages.length
		document.getElementById('active_page_count').innerHTML = "of " + _page_count;

		// check to see if we turn on the 'return to search' link
		try {
			if ( eengine.dbSearch._results.length > 0) {
				document.getElementById('redisplaySearch').style.visibility = "visible";
			}
		} catch (e) {}

		// adjust zoom first
		switch (eengine.zoomLvl) {
			case 1:
				uiElements.processImg(zoomInEnable);

				// last check buildSettings... if _startingLayout is 0, then don't let user drop below single page mode.
				if ( eengineConfig.buildSettings._startingLayout == "0" ) {
					// only display if they are allowed to return to two page mode.
					uiElements.processImg(zoomOutDisable);
				} else {
					uiElements.processImg(zoomOutEnable);
				}

				break;
			case 2:
				uiElements.processImg(zoomInDisable);
				uiElements.processImg(zoomOutEnable);
				break;
			default:
				// zoomLvl 0
				uiElements.processImg(zoomInEnable);
				uiElements.processImg(zoomOutDisable);
				break;
		}

		// by default pageStatus is enabled
		document.getElementById('pageStatus').disabled=false;
		document.getElementById('pageStatus').enabled =true;
		if (isAtKitStart()) {
			uiElements.processImg(firstDisable);
			uiElements.processImg(previousDisable);
			uiElements.processImg(previousDetailDisable);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt)
			}
			document.getElementById('pageStatus').disabled=true;
			document.getElementById('pageStatus').enabled =false;
		} else if (isAtKitEnd()) {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);
			uiElements.processImg(nextDisable);
			uiElements.processImg(nextDetailDisable);
			uiElements.processImg(nextDocDisable);
		} else if (isAtDocStart()) {
			_alt = "Previous document - " + binder.kitData.documents[binder.kitData.current.doc-1].title;
			uiElements.processImg(firstEnable, _alt);

			_alt = "Previous document - " + binder.kitData.documents[binder.kitData.current.doc-1].title + " (last page)";
			uiElements.processImg(previousEnable, _alt);
			uiElements.processImg(previousDetailEnable, _alt);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);

			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		} else if (isAtDocEnd()) {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);

			_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
			uiElements.processImg(nextEnable, _alt);
			uiElements.processImg(nextDetailEnable, _alt);

			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		} else {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(previousDetailEnable);

			uiElements.processImg(nextEnable);
			uiElements.processImg(nextDetailEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable);
			} else {
				_alt = "Next document - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt);
			}
		}
	}
}
eengineConfig.subscribe( "onDisplayPage", "display.book.book");

function onDislayCover() {

	var event = eengine.reporting.getLatest();
	var action = parseInt( event.action, 10 );
	if ( action == -95 ) {
		document.getElementById('active_page_count').innerHTML = "";
		uiElements._disableAll();
	}
	// on displaying the cover, disable pageStatus element
	document.getElementById('pageStatus').disabled=true;
	document.getElementById('pageStatus').enabled =false;
}
eengineConfig.subscribe( "onDislayCover()", "eengine.postProcess.coverUI" );

function isAtDocStart() {
	var _current = binder.kitData.current;
	if ((_current.layout == 1 && _current.page <= 1) || (_current.layout == 0 && _current.page == 0)) {
		return true;
	} else {
		return false;
	}
}

function isAtKitStart() {
	var _current = binder.kitData.current;
	if (isAtDocStart() && _current.doc == 1) {
		return true;
	} else {
		return false;
	}
}

function isAtDocEnd() {
	var _kit = binder.kitData;
	var _current = _kit.current;
	if ((_current.layout == 1 && _current.page >= _kit.documents[_current.doc].pages.length - 1) || (_current.layout == 0 && _current.page == _kit.documents[_current.doc].pages.length - 1)) {
		return true;
	} else {
		return false;
	}
}

function isAtKitEnd() {
	var _kit = binder.kitData;
	var _current = _kit.current;
	if (isAtDocEnd() && _current.doc == _kit.documents.length - 1) {
		return true;
	} else {
		return false;
	}
}

function isLastDoc() {
	var _doc = binder.kitData.current.doc;
	if (_doc == binder.kitData.documents.length - 1) {
		return true;
	} else {
		return false;
	}
}

function zoomIn() {
	// First part of the if statement is a work around, I should just be able to call eengine.getZoom(0) to
	// zoom out.  Once all zoom issues are worked out the getZoom calls should go directly into the eesearch.html
	// template file and the zoomIn and zoomOut functions can be deleted.
	if (binder.kitData.current.layout == 1) {
		eengine.switchLayout();
	} else if (binder.kitData.current.layout == 0) {
		eengine.getZoom(0);
	}
}

function zoomOut() {
	eengine.getZoom(1);
}

function goFirst() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocStart() && !isAtKitStart()) {
		_doc--;
	}
	if (_doc > 0) {
		eengine.writePage(_doc, 0);
	}
}

function goPrevious() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocStart() && !isAtKitStart()) {
		_doc--;
		var _page = binder.kitData.documents[_doc].pages.length - 1;
		eengine.writePage(_doc, _page);
	} else {
		eengine.writePage(-65536);
	}
}

function goNext() {
	var _doc = binder.kitData.current.doc;
	if (isAtDocEnd() && !isAtKitEnd()) {
		_doc++;
		eengine.writePage(_doc, 0);
	} else {
		eengine.writePage(65536);
	}
}

function goNextDoc() {
	var _doc = binder.kitData.current.doc;
	_doc++;
	if (_doc < binder.kitData.documents.length) {
		eengine.writePage(_doc, 0);
	}
}

function recreateShowShow() {
}
