/**
 * $Log: layout.js,v $
 * Revision 1.5  2009-03-26 10:18:21  t4w00-diedrich
 * Use filter: for MSIE6. Why am I doing this??
 *
 * Revision 1.4  2009-03-04 23:34:25  t4w00-diedrich
 * Kristinas eMail
 *
 * Revision 1.3  2009-02-17 14:06:00  t4w00-diedrich
 * Center and add newspaper clippings
 *
 * Revision 1.2  2008-12-19 23:50:11  t4w00-diedrich
 * Added boxes and some minor modifications.
 *
 * Revision 1.1.1.1  2008-12-04 00:11:15  t4w00-diedrich
 *
 *
 * Revision 1.1  2008-09-18 15:54:54  t4w00-diedrich
 * Initial commit (brilletheater)
 *
 *
 */

/*
function set_body_background()
{
    var body = document.getElementsByTagName("body")[0];
    console.log(body);
    body.style.backgroundImage = 'bg.png';
    body.style.backgroundColor = "red";
}

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

function resize_event(body)
{
    var body = document.getElementsByTagName("body")[0];
    var plumet = document.getElementById("plumet");
    var container = document.getElementById("container");

    var cls = "";
    
    if ( container.offsetWidth < body.offsetWidth )
    {
        cls = "center";
    }
    else
    {
        cls = "left";
    }

    container.className = cls;
    plumet.className = cls;
;
};

t4lib.onload_manager.register(resize_event);

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

    if ( content.offsetTop + content.offsetHeight <
         menu.offsetTop + menu.offsetHeight )
    {
        var new_height = content.offsetHeight + (
            menu.offsetTop + menu.offsetHeight ) - (
                content.offsetTop + content.offsetHeight ) + 8;
        content.style.height = new_height + "px";
    }
}

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

t4lib.onload_manager.register(recalc_sizes);


/* 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;
            
            if ( navigator.appVersion.indexOf("MSIE 6") != -1
                 && url.indexOf("png") != -1 )
            {
                var filter =
                    "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +
                    url + "', sizingMethod='scale');";

                img.style.filter = filter;
                img.src = "space.gif";
            }
		}
    }
}

t4lib.onload_manager.register(windows_image_improver, 500);

function move_facebook()
{
    var menu = document.getElementById("menu");
    var facebook = document.getElementById("facebook");
    var y = menu.offsetTop + menu.offsetHeight - 10;
    facebook.style.top = y + "px";
    facebook.style.display = "block";
}

t4lib.onload_manager.register(move_facebook);


