/*
 * $Log: layout.js,v $
 * Revision 1.5  2010-04-19 11:55:29  t4w00-diedrich
 * Sortierung
 *
 * Revision 1.4  2009-02-07 20:31:25  t4w00-diedrich
 * Added search functionality
 *
 * Revision 1.3  2009-01-19 20:07:34  t4w00-diedrich
 * Aenderungen mit Tom
 *
 * Revision 1.2  2008-11-27 00:27:23  t4w00-diedrich
 * The menu is rendered using images now.
 *
 * Revision 1.1  2008-11-25 20:41:14  t4w00-diedrich
 * Browser compatibility stuff.
 *
 */

function search_key_press(input, event)
{
    if ( event.keyCode == 13 )
    {
        var params = Array();
        params["q"] = input.value;
        window.location = context_url + "/search?" + t4lib.url_params(params);
    }
}

function search_focus(input)
{
    if ( input.value == "Produktsuche" )
    {
        input.value = "";
    }
}

function correct_pngs() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
    // Cool shit from http://www.dynamicdrive.com/forums/showthread.php?t=22176
    // written by ??
    
    var arVersion = navigator.appVersion.split("MSIE");
    var version = parseFloat(arVersion[1]);
    
    if ((version >= 5.5) && (version < 7.0) && (document.body.filters)) {
        for(var i=0; i<document.images.length; i++) {
            var img = document.images[i]
            var imgName = img.src.toUpperCase();
            if (imgName.substring(imgName.length-4, imgName.length) == ".PNG") {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;
                if (img.align == "left") imgStyle = "float:left;" + imgStyle;
                if (img.align == "right") imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
                var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                    + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
                img.outerHTML = strNewHTML;
                i = i-1;
            }
        }
    }    
}

// No need...
// t4lib.onload_manager.register(correct_pngs);


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("need-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);

function reload_menu()
{
    new Ajax.Request(context_url + "/reload_menu",
                     {  
                         method: "get",
                         asynchronous: false,
                         onSuccess: function(transport) {
                             $("menu").innerHTML = transport.responseText;
                         },
                         onFailure: function(){ }
                     });
}
