/*
		File: init-page-features.js
		Description: Performs initialization for the main site Features page.
*/

window.onload= function(){
  initOnClicksFeatures();
  adjustLayout_Features();

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


var screenHeight = 0;
var adjustmentCounter = 0;

//var booklistMgr = new booklistManager();
//var sectionImageMgr = new sectionImageManager();


function initOnClicksFeatures()
{
    // make sure the sublinks show up for the initial page state
    var subLinks = $('bl_sublink_container');
    if (subLinks != null)
      subLinks.className = 'visibleBlock'; 

		// For each subnav button, assign event handler callback
		var btn = null;

		btn = $('btnArticles');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch_article_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch_article_item);
		}
    btn = $('btnFilmArticles');
    if (btn)
    {
    if(btn.addEventListener)
        btn.addEventListener('click', fetch_film_article_item, false);
    else if(btn.attachEvent)
        btn.attachEvent('onclick', fetch_film_article_item);
    }
		btn = $('btnInterviews');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch_interview_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch_interview_item);
		}
		btn = $('btnMagazines');
		if (btn)
		{
		if(btn.addEventListener)
				btn.addEventListener('click', fetch_magazine_item, false);
		else if(btn.attachEvent)
				btn.attachEvent('onclick', fetch_magazine_item);
		}
    btn = $('btnFullContent');
    if (btn)
    {
    if(btn.addEventListener)
        btn.addEventListener('click', fetch_feature_details, false);
    else if(btn.attachEvent)
        btn.attachEvent('onclick', fetch_feature_details);
    }

	
		// TBD: Add call to init booklist items? Or do we embed the JavaScript call in the <A> element directly?
		//init_BookListItems();
}


function fetch_article_item()
{
		clearSubNavButtons();
		var btn = $('btnArticles');
		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_article_overview';
		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_FeatureOverview
		});

}

function fetch_film_article_item()
{
    clearSubNavButtons();
    var btn = $('btnFilmArticles');
    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_film_article_overview';
    var ajaxRequest = new Ajax.Request(
    url,
    {
        method: 'get',
        parameters: pars,
        onComplete: showData_FeatureOverview
    });

}

function fetch_interview_item()
{
		clearSubNavButtons();
		btn = $('btnInterviews');
		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_interview_overview';
		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_FeatureOverview
		});
}

function fetch_magazine_item()
{
		clearSubNavButtons();
		btn = $('btnMagazines');
		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_magspot_overview';
		var ajaxRequest = new Ajax.Request(
		url,
		{
				method: 'get',
				parameters: pars,
				onComplete: showData_FeatureOverview
		});

}

function fetch_feature_details()
{
    clearSubNavButtons();
    var btn = $('btnFullContent');
    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_feature_details&s=current';
    var ajaxRequest = new Ajax.Request(
    url,
    {
        method: 'get',
        parameters: pars,
        onComplete: showData_ItemDetail_Features
    });
}


function fetch_features_for_period(idx)
{
	var type = $F('content_type'); 
	var year = $F('year_'+ idx);
	var url = 'db_scripts/db_get_content_by_type_AJAX.php';		
	var pars = 'contentType='+ type;
	pars += '&y='+year;

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


}

function showData_FeatureOverview(ajaxRequest)    
{
  try
  {
    var response = eval("(" + ajaxRequest.responseText + ")");
    if(response.error)
      alert("Eval Error:" + response.error);
    else
    {
    
      var target = $('bl_content_display_area');
      target.innerHTML = decodeAJAX(response.contentData.mainHTML);
      // We don't show a book list for the event season schedule

      showHeaderTitle(decodeAJAX(response.contentData.headerTitle));
      
      target = $('bl_sublink_container');
      target.innerHTML = decodeAJAX(response.contentData.archiveListHTML);
      
      target = $('bl_book_list_container');
      if (target != null)      
	      target.innerHTML = "&nbsp;";

      if (response.contentData.contentType == "ct_article_overview" || 
          response.contentData.contentType == "ct_film_article_overview" || 
          response.contentData.contentType == "ct_interview_overview" || 
          response.contentData.contentType == "ct_magspot_overview")
      {
        $('bl_sublink_container').className = 'visibleBlock';   // TBD: This could be faded in and out.
				setTimeout('init_ibox()', 0);        
      }
      else
      {
        $('bl_sublink_container').className = 'hiddenBlock';
      }        
      
//      setTimeout('adjustLayout_Features()', 0);      
      doLayoutAdjustment();        
    }      
  }
  catch(e)
  {
    if (e instanceof Error) 
      alert('showData_EventSchedule:\n' + e.name + ': ' + e.message + '\n' + ajaxRequest.responseText);
  }
}

function showData_ItemDetail_Features(ajaxRequest)    
{
  try
  {
    var response = eval("(" + ajaxRequest.responseText + ")");
    if(response.error)
      alert("Eval Error:" + response.error);
    else
    {
//      if(response.contentData.booklistImages != null)
//      {
//        var preloadImageNames = decodeAJAX(response.contentData.booklistImages);
//        var booklistMgr = new booklistManager();        
//        booklistMgr.init(preloadImageNames, "userfiles/image/", "AJAX");
//      }
//      else
//      debugAlert("showData_ItemDetail_Features - No booklistImages");
    
    
      var target = $('bl_content_display_area');
      if (target == null)
        debugAlert("Software Error: showData_ItemDetail_Features - Cannot find Target")
      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);

        setTimeout('init_ibox()', 0);
      }
      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';
        
      showHeaderTitle(decodeAJAX(response.contentData.headerTitle));
//      setTimeout('adjustLayout()', 0);            
      doLayoutAdjustment();        
    }
  }
  catch(e)
  {
    if (e instanceof Error) 
      alert('showData_ItemDetail_Features:\n' + e.name + ': ' + e.message + '\n' + ajaxRequest.responseText);
  }
}


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

//  if (booklistMgr != null)
//    doAppearSequence(0);

}

function dynamicAdjustLayout_Features()
{
  var newHeight = adjustLayout_Features();
  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_Features()', 100);
  }
  
  screenHeight = newHeight;  
}

function adjustLayout_Features()
{
  // 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);
}

