// File: init-pages.js
// Contains: Initialization Code 


function adjustLayout()
{
	// get natural heights from elements that can be read.
	var booklist_table_1 = xHeight("booklist_table_1");	
	var booklist_table_2 = xHeight("booklist_table_2");	
	var b1_content_table = xHeight("b1_content_table");	
	
	var maxHeight = Math.max(booklist_table_1, booklist_table_2 ) + 70; // get heights of booklist plus the vertical offset
 	maxHeight = Math.max(maxHeight, b1_content_table );
	
	var minHeight = 380; // minimum acceptable height for bl_frame
	maxHeight = Math.max(maxHeight, minHeight );
	
	maxHeight += 250; // add the fixed height of "bl_header" + "bl_tabbar" plus a little extra margin.	
	
	xHeight("bl_frame", maxHeight);
}

// This function is used for non-AJAX display events (e.g. entry PHP pages)
function activateBookListMgr()
{
  var preloadImageNames = "";
  var elem = $('booklist_image_list');
  if(elem != null)
  {
//    debugAlert("activateBookListMgr 111");  
    var preloadImageNames = $F('booklist_image_list');
    
    if(preloadImageNames != "")
    {
//    debugAlert("activateBookListMgr 222");      
      var booklistMgr = new booklistManager();            
      booklistMgr.init(preloadImageNames, "userfiles/image/", "PHP");
    }
  }
//  else
//  {
//    setTimeout('activateBookListMgr()', 500);  
//  }
}

// This function is used for non-AJAX display events (e.g. entry PHP pages)
function activateSectionImageMgr()
{
  var preloadImageNames = $F('section_image_list');
  if (preloadImageNames != null)
  {
    if(preloadImageNames != "")
    {
      sectionImageMgr.init(preloadImageNames, "userfiles/image/", "PHP");
    }
  }
  else
  {
    setTimeout('activateSectionImageMgr()', 500);  
  }
}


function clearSubNavButtons()
{
	var selectedBtns = $$('div#bl_subnav_menu .menu_item_selected ');
	for (var i=0; i<selectedBtns.length; i++)
	{
		selectedBtns[i].className = "menu_item";
	}
	selectedBtns = $$('div#bl_subnav_menu .menu_btn_frame_selected ');
	for (var i=0; i<selectedBtns.length; i++)
	{
		selectedBtns[i].className = "menu_btn_frame";
	}
}



function showData_ContentItem(ajaxRequest)
{
	try
	{
		var response = eval("(" + ajaxRequest.responseText + ")");
		if(response.error)
			alert("Eval Error:" + response.error);
		else
		{
			var target = null;
			if(response.contentData.mainHTML != null)
			{
				target = $('bl_content_display_area');
				target.innerHTML = decodeAJAX(response.contentData.mainHTML);
			}

			if(response.contentData.booklistHTML != null)
			{
				target = $('bl_book_list_container');
        if (target != null)				
					target.innerHTML = decodeAJAX(response.contentData.booklistHTML);
			}
      else
      {
        target = $('bl_book_list_container');
        if (target != null)
	        target.innerHTML = "&nbsp;";
      }
      
      // hide any sublink containers
      if($('bl_sublink_container') != null)
				$('bl_sublink_container').className = 'hiddenBlock';

			setTimeout('init_ibox()', 0);       
			
			showHeaderTitle(decodeAJAX(response.contentData.headerTitle));	
			setTimeout('adjustLayout()', 0);
		}
	}
	catch(e)
	{
		if (e instanceof Error) 
			alert('showData_ContentItem:\n' + e.name + ': ' + e.message + '\n' + ajaxRequest.responseText);
	}
}



// moved showData_ItemDetail to respective modules (Features and Event)

// TBD: Is this used?
/*
function showData_EventItem(ajaxRequest)
{
	var target = $('bl_content_display_area');
	target.innerHTML = ajaxRequest.responseText;

	initBookListSelectors();
}


function selectSubLink(activeLink)
{
	var selectedBtns = $$('div#bl_sublink_container .sub_link_selected ');
	for (var i=0; i<selectedBtns.length; i++)
	{
		selectedBtns[i].className = "sub_link_unselected";
	}
	var target = $(activeLink)
	if(target != null)
		target.className = "sub_link_selected";
	
}

*/


function showBookList(index)
{
	if(index == 1)
	{
		$('booklist_table_1').className = "visibleItem_vis";	
		$('booklist_table_2').className = "hiddenItem_vis";			
		
		$('showBookList1').className = "activeLink";
		$('showBookList2').className = "adjacentLink";		
	}
	else
	{
		$('booklist_table_1').className = "hiddenItem_vis";			
		$('booklist_table_2').className = "visibleItem_vis";	
		
		$('showBookList1').className = "adjacentLink";
		$('showBookList2').className = "activeLink";		
	}
}


function showHeaderTitle(title)
{
	var hdr = $('bl_content_box_hdr_title');
	if (hdr)
	{
    hdr.innerHTML = "<p>" + title + "</p>";
	}
}

// Not sure about these functions. We need to add exception handling. 
// They don't actually seem to be used. Stub them out later.
function showData_PickItem(ajaxRequest)
{
	var target = $('bl_content_display_area');
	target.innerHTML = decodeAJAX(ajaxRequest.responseText);
}

function showData_ArtWindowItem(ajaxRequest)
{
	var target = $('bl_content_display_area');
	target.innerHTML = decodeAJAX(ajaxRequest.responseText);
}

function showData_ShopItem(ajaxRequest)
{
	var target = $('bl_content_display_area');
	target.innerHTML = decodeAJAX(ajaxRequest.responseText);
}


function showData_LoginPage(ajaxRequest)
{
  var target = $('bl_feedback_area');
  target.innerHTML = decodeAJAX(ajaxRequest.responseText);
}


function decodeAJAX(str)
{
	var workingString = str;
	workingString = workingString.replace(/\+/g, " ");
	workingString = decodeURIComponent(workingString);
	return workingString;
}


function onCancel()
{
	hideIbox();
  setTimeout('init_ibox()', 0);
	return;
}

function debugAlert(str)
{
//  alert("DEBUG: " + str);
}
