/**
 * $Log: layout.js,v $
 * Revision 1.1  2008-09-18 15:54:54  t4w00-diedrich
 * Initial commit (uwelal)
 *
 *
 */

/* If an active menu item exists in the (left) main menu, enlarge the
   <div> inside to span the pircure column */
function active_menu_design()
{
    if ( typeof disable_translucent_bar != "undefined" )
    {
        return;
    }
    else
    {        
        var menu_div = document.getElementById("menu");
        var lis = menu_div.getElementsByTagName("li");
        
        for( var a = 0; a < lis.length; a++)
        {
            var li = lis[a];
            var cls = li.className;
            
            if ( cls.indexOf("active") != -1 )
            {
                /* Find the content <div> */
                var content_div = document.getElementById("main");
                
                var menu_item_div = li.childNodes[0];
                var em = menu_item_div.offsetWidth / 9.0;
                var nw = em * 17.1;
                menu_item_div.style.width = nw + "px";
                
                break;
            }
        }
    }
}

t4lib.onload_manager.register(active_menu_design);

/* Hehehe... the Wedges! */
function add_wedges()
{
    // Let's figure out a frame for our wedges.
    var picture_column = document.getElementById("picture-column");
    // The frame covers the whole of the picture column and 1/4th of
    // its size into the main column

    var x = picture_column.offsetLeft;
    var y = picture_column.offsetTop;
    var w = picture_column.offsetWidth * 1.75;
    var h = picture_column.offsetHeight;
    var b = picture_column.offsetWidth;

    var wedges_div = document.getElementById("wedges");
    
    // The wedges <div> is located in the upper left hand corner
    // with a 0x0 px size so it doesn't interfere with site usage.
    wedges_div.style.left   = "0px";
    wedges_div.style.top    = "0px";
    wedges_div.style.width  = "0px";
    wedges_div.style.height = "0px";

    // Number of wedges
    var number = 40 + 10 * Math.random();
    // number = 2;

    // Create the wedges and position them in the frame
    for ( var a = 0; a < number; a++ )
    {
        var new_image = new Image();
        
        var i;
        do
        {
            i = Math.round(Math.random() * wedges.length+0.0);
        } while (i >= wedges.length);
        
        new_image.src = wedges[i];
        
        new_image.className = "wedge";

        var img_x = x + 1.25*b + (w-b)*Math.log(Math.random()*10)/(-2.3);

        if ( img_x > x + (b*1.25) )
        {
            img_x = img_x - w;
        }
        
        var img_y = y + Math.random() * h;
        var size = 0.25 + 0.5 * Math.random();

        new_image.style.left = img_x + "px";
        new_image.style.top  = img_y + "px";
        new_image.style.width = size + "em";
        new_image.style.height = size + "em";

        wedges_div.appendChild(new_image);
    }
}

// The add_wedges() function is registered with the onload mechanism
// by the view PageTemplate(s)

/* Walk through the DOM tree and mark all documents whose urls do not start
 * with the portal_url as external. Also make them open in a new window. */ 
function mark_external_links()
{
    // Get all the images
    var links = document.getElementsByTagName("a");

    // Traverse the images and find those with class= need-improvement
    for(var a = 0; a < links.length; a++)
    {
		var link = links[a];
		var url = t4lib.get_attribute(link, "href");

		if ( url &&
             url.substr(0, 4) == "http" &&
			 url.substr(0, portal_url.length) != portal_url ) {
			link.className = link.className + " external";
			link.target = "_new";
			link.alt = "In einem neuen Fenster " + link.alt;
		}
	}			
}

t4lib.onload_manager.register(mark_external_links);

/* Reload all images with the src= set to a value that contains the current
 * physical extends in pixels. If it's an RImage the server will provide a
 * nicely scaled version of the image. */

function windows_image_improver()
{
    // Get all the images
    var images = document.getElementsByTagName("img");

    // Traverse the images and find those with class= need-improvement
    for(var a = 0; a < images.length; a++)
    {
        var img = images[a];
        var cls = img.className;

        if ( cls.indexOf("no-improvement") == -1 ) {
			var scaleto = img.width + "x" + img.height;
			
			var url = img.src;
			var parts = t4lib.split_url(url);    
			var base = parts[0];
			var param_array = t4lib.param_array_from_url(parts[1]);
			
			param_array = t4lib.set_param_in_array(param_array, "size", null);
			param_array = t4lib.set_param_in_array(param_array,
                                                   "preview", null);
			param_array = t4lib.set_param_in_array(param_array,
                                                   "scaleto", scaleto);

            // Images that are .png files or smaller than 200x200 pixels
            // in terms of area, are requested in .png format.
			if ( img.src.indexOf(".png") != -1 ||
                        img.width * img.height <= 200*200 ) {
				param_array = t4lib.set_param_in_array(param_array, "png", 1);
			}
			
			url = t4lib.url_from_param_array(base, param_array);
		
			img.src = url;
		}
    }
}

t4lib.onload_manager.register(windows_image_improver);


/*
t4lib.onload_manager.register(recalc_sizes, 500);

function recalc_sizes() {
    var main = document.getElementById("main");
    var menu = document.getElementById("menu");
    var margin = document.getElementById("margin");

	if ( !margin)
	{
		return;
	}
	else
	{
		var head = document.getElementById("logo");
		var body = document.getElementById("body");
		var container = document.getElementById("container");
		
		var main_height = main.offsetHeight;
		var menu_height = menu.offsetHeight;
		var margin_height = margin.offsetHeight;
		
		var mx = max(main_height, max(menu_height, margin_height));
		
		if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
			//		mx = mx * 2;
		}
		
		body.style.height = mx + "px";
		margin.style.height = mx + "px";
	}
}

function max(a, b) {
    if (a > b) {
		return a;
    }
    else {
		return b;
    }
}

*/
