//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 onePageEnable = {
	_obj 			: "1pageImg",
	src 			: "images/1page.gif",
	alt 			: "Single Page View"
}

var onePageDisable = {
	_obj 			: "1pageImg",
	src 			: "images/1page_dis.gif",
	alt 			: ""
}

var twoPageEnable = {
	_obj 			: "2pageImg",
	src 			: "images/2page.gif",
	alt 			: "Two Page View"
}

var twoPageDisable = {
	_obj 			: "2pageImg",
	src 			: "images/2page_dis.gif",
	alt 			: ""
}

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 nextEnable = {
	_obj 			: "next",
	src 			: "images/next.gif",
	alt 			: "Next page"
}

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

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

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(nextDisable);
	    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();
	recreateShowShow();

	//set customer logo link
	if(typeof CustomerHref != "undefined"){
		document.getElementById('CustomerHref').href = "http://" + CustomerHref;
	}

	// check for RSS and wipe out if not needed.
	try {
		if ( dijit.byId('rssObj').rssFeeds.length == 0 ) {
			document.getElementById('rssObj').style.display = "none";
		}
	} catch (e) {
		document.getElementById('rssObj').style.display = "none";
	}



	eengine.total_load                   = 1;
}
eengineConfig.subscribe( "onEngineIdle()", "eengine.postProcess.engineidle");

// this is a workaround until the full themes system can be implemented.
function injectStyle(widget, mySession) {
	if ( mySession.event == -900 ) {
		/* add stylesheet code here like so:
			var css = '.rssCrumbBaseMiddle { background-color: #CCC; }';
		*/
		var css = '';
		css += '.rssWidgetContainer { width:  150px; height: 350px;	}';

		// css strings may be entered
		if ( css != '' ) {
			//dojo.html.insertCssText(css);
		}
	}
}
eengineConfig.subscribe( "injectStyle", "rssCrumb" );

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;

		// adjust zoom first
		switch (eengine.zoomLvl) {
			case 1:
				uiElements.processImg(zoomInEnable);
				document.getElementById('_zoomInLink').style.display = "";

				uiElements.processImg(zoomOutDisable);
				document.getElementById('_zoomOutLink').style.display = "none";

				// enable / disable the one / two page buttons
				uiElements.processImg(onePageDisable);
				uiElements.processImg(twoPageEnable);
				break;
			case 2:
				uiElements.processImg(zoomInDisable);
				document.getElementById('_zoomInLink').style.display = "none";

				// swap zoom out for zoom in on ui
				uiElements.processImg(zoomOutEnable);
				document.getElementById('_zoomOutLink').style.display = "";
				break;
			default:
				// zoomLvl 0
				uiElements.processImg(zoomInDisable);
				document.getElementById('_zoomInLink').style.display = "";

				uiElements.processImg(zoomOutDisable);
				document.getElementById('_zoomOutLink').style.display = "none";

				// enable / disable the one / two page buttons
				uiElements.processImg(onePageEnable);
				uiElements.processImg(twoPageDisable);
				break;
		}

		if (isAtKitStart()) {
			uiElements.processImg(firstDisable);
			uiElements.processImg(previousDisable);
			uiElements.processImg(nextEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable)
			} else {
				_alt = "Next eBook - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt)
			}
		} else if (isAtKitEnd()) {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(nextDisable);
			uiElements.processImg(nextDocDisable);
		} else if (isAtDocStart()) {
			_alt = "Previous eBook - " + binder.kitData.documents[binder.kitData.current.doc-1].title;
			uiElements.processImg(firstEnable, _alt);

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

			uiElements.processImg(nextEnable);

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

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

			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable)
			} else {
				_alt = "Next eBook - " + binder.kitData.documents[binder.kitData.current.doc+1].title;
				uiElements.processImg(nextDocEnable, _alt)
			}
		} else {
			uiElements.processImg(firstEnable);
			uiElements.processImg(previousEnable);
			uiElements.processImg(nextEnable);
			if (isLastDoc()) {
				uiElements.processImg(nextDocDisable)
			} else {
				_alt = "Next eBook - " + 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();
	}
}
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() {
}


/**
 *  showGlobal code
 */

// trap in case links.js is missing.
try {
	if ( usefulLinksString == '' ) {
		usefulLinksString = "Useful links";
	}
} catch (e) {
	usefulLinksString = "Useful links";
}

try {
	if ( clickString == '' ) {
		clickString = "Click here";
	}
} catch (e) {
	clickString = "Click here";
}

try {
	for ( var i=1; i < 10; i++ ) {
		var _var = '';
		try {
			eval("_var = useful"+i+"_link");
			eval("useful"+i+"_link = escape(useful"+i+"_link);");
		} catch (e) {
			eval("useful"+i+"_link = ''");
		}
	}
} catch (e) {}

try {
	document.getElementById('useful_links').innerHTML = usefulLinksString;

	var any_useful_links = false;
	if (configureSegment('useful1') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful2') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful3') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful4') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful5') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful6') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful7') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('useful8') && ! any_useful_links) { any_useful_links = true }
	if (configureSegment('financials') && ! any_useful_links) { any_useful_links = true }

	if (any_useful_links) {
	  document.getElementById('useful_links_start').style.visibility = "visible";
	  document.getElementById('useful_links_close').style.visibility = "visible";
	} else {
	  document.getElementById('useful_links_start').innerHTML = "";
	  document.getElementById('useful_links_close').innerHTML = "";
	}

}catch (e){}

function _checkToCloseZone(any_useful_links) {
	if ( ( g_RSSWidget_rssFeeds.length == 0 ) && (! any_useful_links ) ) {
		// remove globalRCSpacer and globalRCContainer
		document.getElementById('globalRCSpacer').style.display = "none";
		document.getElementById('globalRCContainer').style.display = "none";
	}
}

function configureSegment (segment, dateConstraint) {

	try {
		var textDiv = segment + "_link_text";
		var textVariable = textDiv;
		var segmentLink = eval(segment + "_link");
		var segmentDiv = segment + "_segment";

		var textString = eval(textVariable);
		if (trim(segmentLink) != '') {

				var _content = document.getElementById(segmentDiv).innerHTML;

				var obj = /\n/ig;
				_content = _content.replace ( obj, "temporaryreplaceoflinefeeds" );

				obj = eval("/\\[\\[" + textVariable + "\\]\\]/ig");
				_content = _content.replace(obj, textString);

				obj = eval("/\\%5B\\%5B" + textVariable + "\\%5D\\%5D/ig");
				_content = _content.replace(obj, textString);

				obj = /\[\[click_text\]\]/ig;
				_content = _content.replace(obj, clickString);

				obj = /\%5B\%5Bclick_text\%5D\%5D/ig;
				_content = _content.replace(obj, clickString);

				obj = /temporaryreplaceoflinefeeds/ig;
				_content = _content.replace ( obj, "\n" );

				document.getElementById(segmentDiv).innerHTML = _content;
				document.getElementById(segmentDiv).style.visibility = "visible";

				return true;
		} else {
				document.getElementById("globalRC").removeChild(document.getElementById(segmentDiv));
				return false;
		}
	}
	catch (e) {
	}
}

function fireLink(link) {

    var url = "";

	if (link.indexOf('http') != 0 && link.indexOf('_link') > 0) {
		try {
			url = eval(link);
			if (url.toLowerCase().indexOf('.xls') > 0 && url.toLowerCase().indexOf('http') == -1)
			{
				url = 'xls/' + url;
			}
			if (url.toLowerCase().indexOf('.pdf') > 0 && url.toLowerCase().indexOf('http') == -1)
			{
				url = 'print/' + url;
			}
		}
		catch (e) {
			alert('Invalid hyperlink: ' + link);
			return 0;
		}
	} else if (link.indexOf('http') == 0 || link.indexOf('mailto') == 0) {
		url = link;
	} else {
		alert('Invalid hyperlink: ' + link + "...Links must be valid http, https, or mailto qualified hyperlinks.");
		return 0;
	}

    if (url != '')
	{
		window.open(url);
	}
}
//End showGlobal code


function trim (strText) {
    // Get rid of leading spaces
    while (strText.substring (0, 1) == ' ')
        strText = strText.substring (1, strText.length);

    // Get rid of trailing spaces
    while (strText.substring (strText.length - 1, strText.length) == ' ')
        strText = strText.substring (0, strText.length - 1);

    return strText;
}
