/*
		File: init-page-picks.js
		Description: Initialization function and command handlers for the main site Books page.
*/

window.onload= function(){
  initOnClicksPicks();

  adjustLayout_Picks();

//  activateSectionImageMgr();  
//	activateBookListMgr();
  
  // add more calls here

}

var screenHeight = 0;
var adjustmentCounter = 0;


// Add handler functions to UI buttons for the Books page
function initOnClicksPicks()
{
		// For each subnav button, assign event handler callback
		var btn = null;

		btn = $('btnBookListPicks');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch_pagespicks_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch_pagespicks_item);
		}
		btn = $('btnBookListTop100');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch_pagestop100_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch_pagestop100_item);
		}
		btn = $('btnBookListTopTens');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch__default_pagestop10s_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch__default_pagestop10s_item);
		}
    
}


function booklistHighlight(item)
{
  item.className="bookTableItem itemLink_hover";
}

function booklistNormal(item)
{
  item.className="bookTableItem itemLink_normal";
}

function onclickBookListItem(item)
{
  var t = item.getAttribute("rel");
  var params = parseQuery(t.substr(5,999));
  var url = "book_blurb.php?ct=picks&bookId=";
  url += item.id;

  var title = item.title;

  if(showIbox(url,title,params)) {
    showBG();
    window.onscroll = maintPos;
    window.onresize = maintPos;
  }
  return false;
  
}

function fetch_pagespicks_item()
{
		clearSubNavButtons();
		var btn = $('btnBookListPicks');
		if (btn.parentNode)
		{
			btn.parentNode.className = "menu_item_selected";
			btn.parentNode.parentNode.className = "menu_btn_frame_selected";
		}

		var url = 'db_scripts/db_get_content_by_type_AJAX.php';		
		var pars = 'contentType=ct_pagespicks&startIndex=0';		
		
		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_PickListItem
		});
}

function fetch_pagestop100_item()
{
		clearSubNavButtons();
		btn = $('btnBookListTop100');
		if (btn.parentNode)
		{
			btn.parentNode.className = "menu_item_selected";
			btn.parentNode.parentNode.className = "menu_btn_frame_selected";
		}

		var url = 'db_scripts/db_get_content_by_type_AJAX.php';		
		var pars = 'contentType=ct_top100&startIndex=0';				
		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_PickListItem
		});
}


function fetch__default_pagestop10s_item()
{
	// Use 213 (FICTION) section for default for now
	var sectionID = -1;
	clearSubNavButtons();
	
	btn = $('btnBookListTopTens');
	if (btn.parentNode)
	{
		btn.parentNode.className = "menu_item_selected";
		btn.parentNode.parentNode.className = "menu_btn_frame_selected";
	}

	var url = 'db_scripts/db_get_content_by_type_AJAX.php';		
	var pars = 'contentType=ct_toptens&section='+sectionID;

	var ajaxRequest = new Ajax.Request(
	url,
	{
			method: 'get',
			parameters: pars,
			onComplete: showData_PickListItem
	});
}


//function fetch_pagestop10s_item(sectionId)
//{
//  hideSectionImages( { duration: .5 } );
//
//  setTimeout('fetch_pagestop10s_item_exec(' + sectionId + ')', 500);
//}
//
//function fetch_pagestop10s_item_exec(sectionId)


function fetch_pagestop10s_item(sectionId)
{
		clearSubNavButtons();
		btn = $('btnBookListTopTens');
		if (btn.parentNode)
		{
			btn.parentNode.className = "menu_item_selected";
			btn.parentNode.parentNode.className = "menu_btn_frame_selected";
		}

		var url = 'db_scripts/db_get_content_by_type_AJAX.php';		
		var pars = 'contentType=ct_toptens&section='+sectionId;

		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_PickListItem
		});
}



function showData_PickListItem(ajaxRequest)
{
  try
  {
    var response = eval("(" + ajaxRequest.responseText + ")");
    if(response.error)
      alert("Eval Error:" + response.error);
    else
    {
    
//      if(response.contentData.sectionImages != null)
//      {
//        var sectionImageNames = decodeAJAX(response.contentData.sectionImages);
//        var sectionImageMgr = new sectionImageManager();              
//        sectionImageMgr.init(sectionImageNames, "userfiles/image/", "AJAX");
//      }
    
//      if(response.contentData.booklistImages != null)
//      {
//        var booklistImageNames = decodeAJAX(response.contentData.booklistImages);
//        var booklistMgr = new booklistManager();
//        booklistMgr.init(booklistImageNames, "userfiles/image/", "AJAX");
//      }

      
      var target = null;
      if(response.contentData.booklistHTML != null)
      {
        target = $('bl_book_list_container');
        if (target != null)        
        	target.innerHTML = decodeAJAX(response.contentData.booklistHTML);

        setTimeout('init_ibox()', 0);
      }
	    else
	    {
	      target = $('bl_book_list_container');
        if (target != null)	      
	      	target.innerHTML = "&nbsp;";
	    }
      
      if(response.contentData.mainHTML != null)
      {
        target = $('bl_content_display_area');
        target.innerHTML = decodeAJAX(response.contentData.mainHTML);
      }
      
      if(response.contentData.topTenListHTML != null)
      {      
        target = $('bl_sublink_container');
        target.innerHTML = decodeAJAX(response.contentData.topTenListHTML);
      }
      
      
      
      if (response.contentData.contentType == "ct_toptens")
      {
        $('bl_sublink_container').className = 'visibleBlock';
      }
      else
      {
        $('bl_sublink_container').className = 'hiddenBlock';
      }        
      showHeaderTitle(decodeAJAX(response.contentData.headerTitle));
      
      setTimeout('init_ibox()', 0); 
      doLayoutAdjustment();                     
    }
  }
  catch(e)
  {
    if (e instanceof Error) 
      alert('showData_PickListItem:\n' + e.name + ': ' + e.message + '\n' + ajaxRequest.responseText);
  }
}


function doLayoutAdjustment()
{
	// reset the global var 
	screenHeight = 0;
	adjustmentCounter = 0;	
  setTimeout('dynamicAdjustLayout_Picks()', 0);

}


function dynamicAdjustLayout_Picks()
{
  var newHeight = adjustLayout_Picks();
  if (newHeight != screenHeight || adjustmentCounter < 50)    // iteration count of 100 allows for up 10 secs of image load time before we abandon adjustments
  {
    adjustmentCounter++;
    setTimeout('dynamicAdjustLayout_Picks()', 100);
  }
  
  screenHeight = newHeight;  
}

function adjustLayout_Picks()
{
  // 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 table_archive_links = xHeight("table_archive_links") + 180;  
  
  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 );
  maxHeight = Math.max(maxHeight, table_archive_links );

  
  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);
  
  return maxHeight;
}


