var init_global = null, global_data = null; //global.js fires warning in strict mode debuging if this is not defined

/* This file sets up things and handles workarounds etc. The idea is to keep
these necessary evils separate from the content */

//setTimeout('document.location.href="http://scriptorium/home/page.php"',1000);

function check_js_file(el)
{
		if( el && el.src )
		{
			var re = /(\w+)\.js$/;
			
			var js_file_match = el.src.match(re);
			
			if( js_file_match && js_file_match[1] )
			{
				return js_file_match[1];
			}
		}
		
		return null;
}

function init()
{
	// Functions loaded in other js files for specific pages. If these functions found execute them 
	var head = document.getElementsByTagName('head')[0];
	var script_tags = head.getElementsByTagName('script');
		
	for(var i=0;i< script_tags.length;i++)
	{
		var js_file = check_js_file(script_tags[i]);
		if( js_file && window['init_' + js_file] ) 
		{	
			if( typeof(window['init_' + js_file]) == 'function' )
			{
				var init_func = window['init_' + js_file];
				
				if( window[js_file + '_data'] )
				{
					init_func(window[js_file + '_data']);
				}
				else
				{
					init_func();
				}
			}
		}
	}
		
	alter_links();	
	
	if( image_list && image_list.length )
	{	// might be set in conf.js
		preload_images(image_list);
	}
	
	adjust_size_for_less_than_1024_screen_width()
}

function preload_images(image_list)
{
	if( typeof(image_list) != 'undefined' && image_list.length)
	{
		for(var i=0,j=image_list.length; i<j; i++)
		{
			if(document.images && image_list[i])
			{	
				new_image(image_list[i]); // defined in common.js
				alert(image_list[i]);
			}
		}
	}
}

function get_tallest_col(col_list, preset_height)
{
	/* 
		Find height of each div in array col_list. 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 preset_h = preset_height || 0;
	var cols = col_list.split(',');
	
	if(!cols.length)
	{
		return null;
	}
	
	var tallestCol = 0; 
	
	for(var i = 0; i<cols.length; i++)
	{
		if( theObjs[cols[i]] ) 
		{			
			tallest_col = Math.max(tallestCol, theObjs[cols[i]].getHeight());
		}
	}
	
	return Math.max(tallestCol,preset_h);
}

function adjust_size_for_less_than_1024_screen_width()
{
    if( screen.width > 800 )
    {
        return;
    }
    var menu_global = getEl('fred');
    
    if( menu_global )
    {
        menu_global.style.marginLeft = '0px';
        var menu_els = menu_global.getElementsByTagName('li');
        
        if( !menu_els )
        {
            return;
        }
        
        for(var i=0; i<menu_els.length; i++)
        {
           
            var links = menu_els[i].getElementsByTagName('a');
            if( links )
            {
                for( var j=0; j<links.length; j++ )
                {
                    links[j].style.fontSize = '90%';
                }
            }
           
        }
       
    }
}

EventManager.Add(window,'load',init,false);

	
