/* This file sets up things and handles workarounds etc. The idea is to keep
these necessary evils separate from the content */
code_evaluator_loaded=0;

adjustWidth = 0;
page_loaded = 0;
init_funcs = []; // holds function names assigned in other included js files. Functions are then called in init() below
showImage_init_arg = 'imageDisplay';
is_harvesting = 0;


//setTimeout('document.location.href="http://scriptorium/home/page.php"',1000);

function init_css()
{
	var preset_height = 500;
	
	if(arguments.length && (typeof(arguments[0] == 'number')) )
	{
		preset_height = arguments[0];
	}  

	var el_ids = ['content','ancillary1','ancillary2'];
	var els = [];
	var colHeights = []; 
	var tallestCol = 0; 
	
	for( var i=0; i<el_ids.length; i++ )
	{
		var curr_el = theObjs[el_ids[i]];
		var el_height = 0;
		
		if( curr_el )
		{
			els[els.length] = curr_el;
			
			el_height = curr_el.getHeight();
			
			if( el_height )
			{
				colHeights[colHeights.length] = el_height;
			}
		}
	}
	
	for(var i = 0; i <colHeights.length; i++)
	{
		tallestCol = Math.max(tallestCol, colHeights[i]);
	}
	
	if( preset_height > tallestCol )
	{
		// hide "Back To Top" link if page is not taller than screen
		if( getEl('toTop') ) 
		{
			getEl('toTop').style.visibility = 'hidden';
		}
	}
	
	tallestCol = Math.max(tallestCol,preset_height);
	
	for( var i=0; i<els.length; i++)
	{
		els[i].setHeight(tallestCol);
	}
}

includes = [];

function init()
{
	
	//changeAllLinksWithClassToTargetBlanks();	
	//generate_TOC('toc');
	
	// Functions loaded in other js files for specific pages. If these functions found execute them 
	
	var head = document.getElementsByTagName('head')[0];
	script_tags = head.getElementsByTagName('script');
	/*
		var re  = /(\[\d{2}\])/g; //returns [num] followed by all charecters up to next match - IE6 does not return the split terms!
		
		//alert('cb paras pre =' + clipboard_paragraphs)
		pages = clipboard_paragraphs.split(re);
		matches = clipboard_paragraphs.match(re);
	
	*/
	var re = /(\w+)\.js$/;
	
	for(var i=0;i< script_tags.length;i++)
	{
		
		if( ! (js_src = script_tags[i].src) )
		{
			continue;
		} 
		
		if( !(js_file_match = js_src.match(re)) )
		{
			continue;
		}
		
		if( !(js_file = js_file_match[1]) )
		{
			continue;
		}
	
		includes[includes.length] = js_file;
		
		init_func = 'init_' + js_file;
		
		var o;
		var func_data;
		
		if( typeof(eval('window.' + js_file + '_data')) != 'undefined')
		{
			func_data = eval('window.' + js_file + '_data');
		}
		
		if(func_data)
		{
			//alert(func_data.src_files);
		}
		
		if(window[init_func]) 
		{ 
			if(func_data)
			{
				eval(init_func + '(func_data)');
			}
			else
			{
				eval(init_func + '()');
			}
		}
	}
	
	// document.body.offsetHeight
	//init_css()
	
	if( typeof(images_list)  != 'undefined' )
	{
		for(i=0,j=images_list.length; i<j; i++)
		{
			if(document.images && image_list[i]) newImage(image_list[i]);
		}
	}

	if( getEl('toc' ) )
	{
		generate_TOC('toc');
	}
		
	alter_links();	
	
}
	
function page_loaded_flag()
{	
	page_loaded = 1;
}


function get_tallest_col(col_list,preset_height)
{
	/* 
		Find height of each div in array cols. Set height of each to tallest column.
		If preset_height specified set all columns to preset_height if preset_height is taller 
		than all columns 
	*/
	
	var cols = col_list.split(',');
	var colHeights = []; 
	var tallestCol = 0; 
	
	if(!cols.length) return;
	
	for(var i = 0; i<cols.length;i++)
	{
		if( theObjs[cols[i]] ) 
		{
			colHeights[colHeights.length] = theObjs[cols[i]].getHeight();
		}
	}
	
	for(var i = 0; i <colHeights.length;i++)
	{
		tallestCol = Math.max(tallestCol, colHeights[i]);
	}
	
	return Math.max(tallestCol,preset_height);
	
	
}

EventManager.Add(window,'load',init,false);

	
