/*****************************************************************************
 * Copyright(C) 2006-2007 The E-Book Systems Pte Ltd.  All rights reserved.  *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the E-Book Systems Pte Ltd. *
 * FlipViewer BV Version 1.1.6                                               *
 *****************************************************************************/

var flip_left_button_off = "./System/Skins/FlipLOffMed.jpg";
var flip_left_button_on = "./System/Skins/FlipLOnMed.jpg";

var flip_right_button_off = "./System/Skins/FlipROffMed.jpg";
var flip_right_button_on = "./System/Skins/FlipROnMed.jpg";

var flip_to_end_button_off = "./System/Skins/FlipToEndOffMed.jpg";
var flip_to_end_button_on = "./System/Skins/FlipToEndOnMed.jpg";

var flip_to_start_button_off = "./System/Skins/FlipToStartOffMed.jpg";
var flip_to_start_button_on = "./System/Skins/FlipToStartOnMed.jpg";

var zoom_left_button_off = "./System/Skins/ZoomLOffMed.jpg";
var zoom_left_button_on = "./System/Skins/ZoomLOnMed.jpg";

var zoom_right_button_off = "./System/Skins/ZoomROffMed.jpg";
var zoom_right_button_on = "./System/Skins/ZoomROnMed.jpg";

var print_left_button_off = "./System/Skins/PrintLOffMed.jpg";
var print_left_button_on = "./System/Skins/PrintLOnMed.jpg";

var print_right_button_off = "./System/Skins/PrintROffMed.jpg";
var print_right_button_on = "./System/Skins/PrintROnMed.jpg";

var find_keyword = null;

var client_width = 0;
var client_height = 0;
var left_client_left = 0;
var right_client_left = 0;
var client_top = 0;

var toolbar_height = 35;
var status_bar_height = 78;
var book_thickness_height = 4 + 3 + 2 + 2 + 2 + 2 + 2;

var page_background_color = "";

// Reserved only for when image shelters are used.
var during_loading_html_pages = false;

// For a Good First Impression
var cover_supplement_list = [];



var have_loaded_EBook_No_Download_Viewer_OneA = false;
var have_loaded_EBook_No_Download_Viewer_OneB = false;
var have_loaded_EBook_No_Download_Viewer_Two = false;
var have_loaded_EBook_No_Download_Viewer_Three = false;

var have_generated_opf_meta_data = false;
var have_generated_opf_meta_data_pre = false;
var authenticate = -1;
var have_generated_opf_meta_data_post = false;



// Utilities

function withinSpreads(item_i)
{
   if ((item_i >= 0) && (item_i <= (item_total + (item_total % 2) - 1))) return true;
   else return false;
}

function safariHideFlipTooltip()
{
   if (macintosh_browser && safari)
   {
      if (item_current == -3) hideFlipRightTooltip();
      else if (item_current == -4) hideFlipLeftTooltip();
      else
      {
         hideFlipRightTooltip();
         hideFlipLeftTooltip();
      }
   }
}

// Prestreaming achieved efficiently through repetitively loading "id" for period of "how_long".
// This can also be used to request the Internet Explorer to add an entry into its "personal" indexing data file, for tracing an "overseas" address,
// as a preloaded correspondence in the local cache.
function urgeRetrieval(id, how_long)
{
   var date1 = new Date();
   while (1)
   {
      getPageElement(id).src = getPageElement(id).src;

      var date2 = new Date();
      var difference = date2 - date1;
      if (difference > how_long) break;
   }
}

// We are prestreaming an image by using the Java class Image.  We need to use a list of class Image elements so that there is an element for each caching.
// If not, Javascript will act strangely that not all images will be cached, sometimes it will be only 3, sometimes 4 and mostly the last few images within the loop of caching.

var caching_image_list = [];
var caching_image_list_index = 0;
var cacheAnImageOnLoad_flag;

function cacheAnImageOnLoad()
{
   cacheAnImageOnLoad_flag = false;
}

function cacheAnImage(the_source, activate)
{
   caching_image_list[caching_image_list_index] = new Image();
   cacheAnImageOnLoad_flag = true;
   caching_image_list[caching_image_list_index].onLoad = cacheAnImageOnLoad();

   do {
      caching_image_list[caching_image_list_index].src = the_source;
   } while (cacheAnImageOnLoad_flag);

   // Ask the Internet Explorer to build the indexing data for reference.
   getPageElement("picture_deluded").src = the_source;

   if (activate == "light.reserved") urgeRetrieval("picture_deluded", 100);
   else if (activate == "medium.reserved") urgeRetrieval("picture_deluded", 200);
   else if (activate == "heavy.reserved") urgeRetrieval("picture_deluded", 300);
   // else... Do no activation.

   caching_image_list_index++;
   // We should concern on the overloading of the memory.  200 pointers should be enough to keep all the requests. 
   if (caching_image_list_index > 200)
   {
      // Javascript delete operator is not the same as the C++ delete.  It simply undefines a variable or property, and does not actually delete or destroy or free up
      // the memory associated with an object created with new.  delete simply sets its operand to null.  You could obviously do this with an assignment statement.
      // The reason that a C++-style delete is not necessary is that JavaScript provides automatic "garbage collection" when objects and other values are no longer being used.
      // Then, the memory associated with them is automatically reclaimed by the system.  You do not have to worry about deleting objects or freeing or releasing memory that
      // is no longer in use.
      var i;
      for (i = 0; i < caching_image_list_index; i++) caching_image_list[i] = null;
      // We should do a clean-up to free the unused memory.
      caching_image_list = [];
      caching_image_list_index = 0;
   }
}



// Prestreaming

function prestreamCoverShelterOne()
{
   if (flip_dir == "standard")
   {
      if (cover_supplement_list[0] != "") cacheAnImage(cover_supplement_list[0], "heavy.reserved");
   }
   else
   // Should be (flip_dir == "non-standard")
   {
      if (cover_supplement_list[1] != "") cacheAnImage(cover_supplement_list[1], "heavy.reserved");
   }
}

function prestreamCoverShelterTwo()
{
   if (flip_dir == "standard")
   {
      if (cover_supplement_list[1] != "") cacheAnImage(cover_supplement_list[1], "heavy.reserved");
   }
   else
   // Should be (flip_dir == "non-standard")
   {
      if (cover_supplement_list[0] != "") cacheAnImage(cover_supplement_list[0], "heavy.reserved");
   }
}



// Initialization

function prepareBookBackground()
{
   if (have_loaded_EBook_No_Download_Viewer_Three) prepareBookBackgroundThree();
   else if (have_loaded_EBook_No_Download_Viewer_Two) prepareBookBackgroundTwo();
}

function approachingCore(division)
{
   if (have_loaded_EBook_No_Download_Viewer_Three) approachingCoreThree(division);
   else if (have_loaded_EBook_No_Download_Viewer_Two) approachingCoreTwo(division);
   else approachingCoreOne(division);
}

function approachingCoreOne(division)
{
   var progress_percent = 0;

   if (image_flipping_through)
   {
      if (division == 1) progress_percent = 4;
      else if (division == 2) progress_percent = 18;
      else if (division == 3) progress_percent = 22;
      else if (division == 4) progress_percent = 35;
      else if (division == 5) progress_percent = 42;
      else if (division == 1000) progress_percent = 100;
   }
   else
   {
      if (division == 1) progress_percent = 8;
      else if (division == 2) progress_percent = 36;
      else if (division == 3) progress_percent = 44;
      else if (division == 4) progress_percent = 70;
      else if (division == 1000) progress_percent = 100;
   }

   getPageElement("approaching_measure").innerHTML = progress_percent + "%";
   getPageElement("progress_bar").style.width = Math.floor(357 * progress_percent / 100);
}

function prepareItems()
{
   if (have_loaded_EBook_No_Download_Viewer_Three) prepareItemsThree();
   else if (have_loaded_EBook_No_Download_Viewer_Two) prepareItemsTwo();
   else prepareItemsOne();
}

function prepareItemsOne()
{
   cover_supplement_list[0] = "";
   if ((cover_list[0] != "") && (cover_list[0].lastIndexOf('.') != -1)) cover_supplement_list[0] = cover_list[0].substring(0, cover_list[0].lastIndexOf('.')) + ".jpg";

   cover_supplement_list[1] = "";
   if ((cover_list[1] != "") && (cover_list[1].lastIndexOf('.') != -1)) cover_supplement_list[1] = cover_list[1].substring(0, cover_list[1].lastIndexOf('.')) + ".jpg";

   if (flip_dir == "non-standard")
   {
      if (cover_list.length == 1)
      {
         cover_list[1] = cover_list[0];
         cover_list[0] = "";

         cover_supplement_list[1] = cover_supplement_list[0];
         cover_supplement_list[0] = "";
      }
      else if (cover_list.length == 2)
      {
         var temporary = cover_list[1];
         cover_list[1] = cover_list[0];
         cover_list[0] = temporary;

         temporary = cover_supplement_list[1];
         cover_supplement_list[1] = cover_supplement_list[0];
         cover_supplement_list[0] = temporary;
      }
   }
}

function showFrontCover()
{
   if (have_loaded_EBook_No_Download_Viewer_Three) showFrontCoverThree();
   else if (have_loaded_EBook_No_Download_Viewer_Two) showFrontCoverTwo();
   else showFrontCoverOne();
}

function showFrontCoverOne()
{
   if ((cover_list.length > 0) && (cover_list[0] != ""))
   // There is front cover picture.
   {
      if (cover_supplement_list[0] != "") 
      {
         during_loading_html_pages = true;

         var covers_deluded = getPageElement("covers_deluded");
         covers_deluded.style.left = right_client_left - 1;
         covers_deluded.style.top = client_top - 1;
         covers_deluded.style.width = page_content_width;
         covers_deluded.style.height = page_content_height;
         covers_deluded.src = cover_supplement_list[0];
         covers_deluded.style.visibility = "visible";
         urgeRetrieval("covers_deluded", 900);
         return;
      }
   }

   // Front Hard Cover
   var front_cover = getPageElement("front_cover");
   front_cover.style.left = right_client_left - 2;
   front_cover.style.top = client_top - 2;
   front_cover.style.width = page_content_width + 4;
   front_cover.style.height = page_content_height + 4;
   front_cover.style.visibility = "visible";
}

function showBackCover()
{
   if (have_loaded_EBook_No_Download_Viewer_Three) showBackCoverThree();
   else if (have_loaded_EBook_No_Download_Viewer_Two) showBackCoverTwo();
   else showBackCoverOne();
}

function showBackCoverOne()
{
   if ((cover_list.length > 1) && (cover_list[1] != ""))
   // There is back cover picture.
   {
      if (cover_supplement_list[1] != "") 
      {
         during_loading_html_pages = true;

         var covers_deluded = getPageElement("covers_deluded");
         covers_deluded.style.left = left_client_left - 1;
         covers_deluded.style.top = client_top - 1;
         covers_deluded.style.width = page_content_width;
         covers_deluded.style.height = page_content_height;
         covers_deluded.src = cover_supplement_list[1];
         covers_deluded.style.visibility = "visible";
         urgeRetrieval("covers_deluded", 900);
         return;
      }
   }

   // Back Hard Cover
   var back_cover = getPageElement("back_cover");
   back_cover.style.left = left_client_left - 2;
   back_cover.style.top = client_top - 2;
   back_cover.style.width = page_content_width + 4;
   back_cover.style.height = page_content_height + 4;
   back_cover.style.visibility = "visible";
}



// Dummies

function staticImageMouseMove(event)
{
   if (have_loaded_EBook_No_Download_Viewer_Two) staticImageMouseMoveTwo(event);
}

function staticImageMouseOut(event)
{
   if (have_loaded_EBook_No_Download_Viewer_Two) staticImageMouseOutTwo(event);
}

function staticImageMouseDown(event)
{
   if (have_loaded_EBook_No_Download_Viewer_Two) staticImageMouseDownTwo(event);
}



var ready_to_browse_book = "unready.reserved";
var avoid_JSP_transform = false;

function prepareBookLoading()
{
   var progress_bar_bg = getPageElement("progress_bar_bg");
   progress_bar_bg.style.left = Math.floor((client_width - 357) / 2);
   progress_bar_bg.style.top = Math.floor((client_height - 287 - 10 - 70) / 2) + 287 + 10 + 41;
   progress_bar_bg.style.visibility = "visible";

   var progress_bar = getPageElement("progress_bar");
   progress_bar.style.left = Math.floor((client_width - 357) / 2);
   progress_bar.style.top = Math.floor((client_height - 287 - 10 - 70) / 2) + 287 + 10 + 41;
   progress_bar.style.visibility = "visible";

   var approaching = getPageElement("approaching");
   approaching.style.width = 96;
   approaching.style.height = 24;
   approaching.style.top = Math.floor((client_height - 287 - 10 - 70) / 2) + 287 + 10;
   approaching.style.left = Math.floor((client_width - 96) / 2);
   approaching.style.visibility = "visible";
}

function initOneA()
{
   ready_to_browse_book = "unready.reserved";

   detectBrowser();

   // Disable the context menu.
   document.oncontextmenu = contextMenuDisabled;

   // Set up viewing environment.
   //window.resizeTo(window.screen.availWidth, window.screen.availHeight);
   window.resizeTo(1024, 768);
   window.focus();

   // Set Client Dimensions
   //if (macintosh_browser) client_width = window.screen.availWidth;
   //else client_width = window.screen.availWidth - 12;
   //client_height = window.screen.availHeight;
   if (macintosh_browser) client_width = 1024;
   else client_width = 1024 - 12;
   client_height = 768;
   

   if (protocall[0].substring(protocall[0].length - 3) == ".js")
   // Pre-created Opf Meta Information
   {
      var opf_meta_data_js = document.createElement("script");
      opf_meta_data_js.src = protocall[0];
      opf_meta_data_js.language = "Javascript";
      document.getElementsByTagName("head")[0].appendChild(opf_meta_data_js);

      avoid_JSP_transform = true;
   }
   else
   {
      avoid_JSP_transform = false;

      var opf_meta_data_pre = document.createElement("script");
      opf_meta_data_pre.src = protocall[0];
      opf_meta_data_pre.language = "Javascript";
      document.getElementsByTagName("head")[0].appendChild(opf_meta_data_pre);
   }

   setTimeout("initOneB();", 250);
}

function initOneB()
{
   if (avoid_JSP_transform)
   {
      if (!have_generated_opf_meta_data)
      // Baseline Assuring OPF_Meta_Data.js Available
      {
         setTimeout("initOneB();", 250);
         return;
      }
   }
   else
   {
      if (!have_generated_opf_meta_data_pre)
      // Baseline Assuring OPF_Meta_Data_Pre.js Available
      {
         setTimeout("initOneB();", 250);
         return;
      }
   }

   // Provide default color for paper-flipping-through if required.
   if (page_background_color == "") page_background_color = "#D8D4D3";   // We use #D8D4D3 as the default color.

   // Set Client Dimensions
   left_client_left = Math.floor((client_width - (page_content_width * 2)) / 2);
   right_client_left = Math.floor(client_width / 2) + 1;   // 1: Middle Border
   client_top = Math.floor((client_height - toolbar_height - status_bar_height - page_content_height - book_thickness_height) / 2) + toolbar_height;

   // Dynamically set up everything based on variables in OPF_Meta_Data.js.
   prepareItems();

   prestreamCoverShelterOne();
   if (flip_dir == "non-standard")
   {
      showBackCover();
      item_current = -4;
   }
   else
   // Should be (flip_dir == "standard")
   {
      showFrontCover();
      item_current = -3;
   }

   if (image_flipping_through) setTimeout("activateEBook_No_Download_Viewer_OneA();", 1500);
   else activateEBook_No_Download_Viewer_OneA();
}

function activateEBook_No_Download_Viewer_OneA()
{
   // Comfort readers with percentage progress.
   prepareBookLoading();
   approachingCore(1);

   // The only way works for all browsers.
   var EBook_No_Download_Viewer_OneA_js = document.createElement("script");

   if (image_flipping_through) EBook_No_Download_Viewer_OneA_js.src = selfURLDirectory() + "System/E-Book_No_Download_Viewer_OneAii.js";
   else EBook_No_Download_Viewer_OneA_js.src = selfURLDirectory() + "System/E-Book_No_Download_Viewer_OneAi.js";

   EBook_No_Download_Viewer_OneA_js.language = "Javascript";
   document.getElementsByTagName("head")[0].appendChild(EBook_No_Download_Viewer_OneA_js);

   setTimeout("activateEBook_No_Download_Viewer_OneAA();", 250);
}

var authenticate_timer = 0;

function activateEBook_No_Download_Viewer_OneAA()
{
   if (!have_loaded_EBook_No_Download_Viewer_OneA)
   // Baseline Assuring E-Book_No_Download_Viewer_OneA.js Available
   {
      setTimeout("activateEBook_No_Download_Viewer_OneAA();", 250);
      return;
   }

   if (image_flipping_through) prestreamCloseBookThicknesses();

   prepareButtons();

   if (image_flipping_through)
   {
      if (flip_dir == "non-standard") setBookThicknessOnLeft(item_total, false, 0);
      // Should be (flip_dir == "standard")
      else setBookThicknessOnRight(item_total, false, 0);
   }

   approachingCore(2);

   if (avoid_JSP_transform || (protocall[1] == ""))
   {
      // The only way works for all browsers.
      var EBook_No_Download_Viewer_OneB_js = document.createElement("script");
      EBook_No_Download_Viewer_OneB_js.src = selfURLDirectory() + "System/E-Book_No_Download_Viewer_OneB.js";
      EBook_No_Download_Viewer_OneB_js.language = "Javascript";
      document.getElementsByTagName("head")[0].appendChild(EBook_No_Download_Viewer_OneB_js);

      setTimeout("activateEBook_No_Download_Viewer_OneB();", 250);
   }
   else
   // Authentication
   {
      var authenticate_js = document.createElement("script");
      authenticate_js.src = protocall[1];
      authenticate_js.language = "Javascript";
      document.getElementsByTagName("head")[0].appendChild(authenticate_js);
      setTimeout("authenticating();", 250);
   }
}

function authenticating()
{
   if (authenticate == -1)
   // Baseline Assuring Authenticate.js Available
   {
      if (authenticate_timer > 5000)
      {
         // The only way works for all browsers.
         var EBook_No_Download_Viewer_OneB_js = document.createElement("script");
         EBook_No_Download_Viewer_OneB_js.src = selfURLDirectory() + "System/E-Book_No_Download_Viewer_OneB.js";
         EBook_No_Download_Viewer_OneB_js.language = "Javascript";
         document.getElementsByTagName("head")[0].appendChild(EBook_No_Download_Viewer_OneB_js);

         setTimeout("activateEBook_No_Download_Viewer_OneB();", 250);
         return;
      }

      authenticate_timer += 250;
      setTimeout("authenticating();", 250);
      return;
   }

   if (authenticate > 0)
   {
      alert("There is an error in opening this FlipBook. Please contact your FlipBook publisher.");
      window.close();
      return;
   }

   // The only way works for all browsers.
   var EBook_No_Download_Viewer_OneB_js = document.createElement("script");
   EBook_No_Download_Viewer_OneB_js.src = selfURLDirectory() + "System/E-Book_No_Download_Viewer_OneB.js";
   EBook_No_Download_Viewer_OneB_js.language = "Javascript";
   document.getElementsByTagName("head")[0].appendChild(EBook_No_Download_Viewer_OneB_js);

   setTimeout("activateEBook_No_Download_Viewer_OneB();", 250);
}

function activateEBook_No_Download_Viewer_OneB()
{
   if (!have_loaded_EBook_No_Download_Viewer_OneB)
   // Baseline Assuring E-Book_No_Download_Viewer_OneB.js Available
   {
      setTimeout("activateEBook_No_Download_Viewer_OneB();", 250);
      return;
   }

   // Insert the body background iframe here so that there will be time for HTML element installation, to be ready for the coming usage just after EBook_No_Download_Viewer_Two.js is loaded.
   if (image_flipping_through)
   {
      getPageElement("body_background").innerHTML = "<iframe id=\'body_background_html_content\' name=\'body_background_html_content_frame\' hspace=\'0\' vspace=\'0\' framespacing=\'0\' marginwidth=\'0\' marginheight=\'0\' frameborder=\'0\' scrolling=\'no\' allowtransparency=\'true\' style=\'position:absolute; background-color:transparent; left:0; top:0; width:0; height:0;\' src=\'./System/BlankTransparentPage.html\'></iframe>";
   }

   approachingCore(3);

   if (image_flipping_through) loadEBook_No_Download_Viewer_TwoJs();
   else loadEBook_No_Download_Viewer_ThreeJs();
}
