new function() {
    var minWideWidth = 1225;
    var maxNarrowWidth = 1240;
    
    __layout = {};
    
    /*@cc_on
        @if (@_jscript_version == 5.6)
            document.getElementsByTagName("html")[0].className = "l-ie6";
       @end
    @*/
    
    tilt.attachEvent(document, "contentreceived", function() {
        /* debugging        try { */
        var form = document.getElementById("form");
        if (form) {
            __layout.createShell(form, [
                "l-top", 
                {"l-columns" : [
                    "l-col-1", 
                    {"l-outer-1": [ 
                        {"l-outer-4" : [ "l-col-2", "l-col-3", "l-bottom-2"]},
                        "l-col-4",
                        "l-bottom-4"
                    ]},
                    "l-bottom-1"
                ]},
                "l-bottom-hook", 
                {"l-bottom" : [ "l-bottom-top" ]}
            ]);
            __layout.appendNode("footer", "l-bottom");
            __layout.appendNode("l-footer-bottom", "footer");

            document.getElementById("l-top").innerHTML = "<div class=\"l-left\"><div class=\"l-right\"></div></div>";
            
            var feeds = document.getElementById("feeds");
            if (feeds) {
                __layout.feeds = readFeeds(feeds, ["latest", "crew", "tags", "web2", "links", "tools", "content", "last-post", "recent-posts"]);
                __layout.appendNode(__layout.feeds["tools"], "l-top");
            }
            
            createAdminUI();
            
       }
        
        /* debugging       } catch(e) { alert(e);}*/  
    });
    
    function createAdminUI() {
        var form = document.getElementById("form");
        var authoring = document.body.insertBefore(document.createElement("form"), document.body.firstChild);
        authoring.method = form.method;
        authoring.action = form.action;
        authoring.id = "l-authoring";
	    var trigger = authoring.appendChild(document.createElement("a"));
	    trigger.href = "admin";
	    trigger.className = "l-trigger";
	    tilt.attachEvent(trigger, "click", function(a) {
		    var currentClassName = a.parentNode.className;
		    if (currentClassName.indexOf("open") >= 0) {
			    a.innerHTML = "Authoring";
			    a.parentNode.className = "l-closed";
			    document.cookie = "keep-authoring-open=false";
		    }
		    else {
			    a.innerHTML = "Close";
			    a.parentNode.className = "l-open";
			    document.cookie = "keep-authoring-open=true";
		    }
		    return false;
	    });
	    var properties = document.getElementById("properties");
	    if (properties) {
	        authoring.appendChild(properties);
		    foreach(properties.getElementsByTagName("input"), function(input) {
		        if (input.type == "text") {
		            tilt.attachEvent(input, "keydown", function(node, e) {
		                if (e.keyCode == 13) {
	                        window.setTimeout("document.getElementById(window.__authoringPropertiesApplyId).click()", 0);
	                        return false;
		                }
		                return true;
		            });
		        }
		        else {
		            window.__authoringPropertiesApplyId = input.id;
		        }
		    });
		    var pid;
		    foreach(properties.getElementsByTagName("a"), function(a) {
		        if (a.className == "admin") {
		            var href = a.href;
		            var pos = href.indexOf("durki=");
		            if (pos >= 0) {
		                pid = href.substring(pos + 6);
		            }
		        }
		    });
		    if (pid) {
		        var div = properties.appendChild(document.createElement("div")); 
		        div.className = "l-pid";
		        div.innerHTML = "page id: " + pid;
		    }
	    }
	    var isOpen = document.cookie.indexOf("keep-authoring-open=true") >= 0 || properties;
	    if (isOpen) {
	        authoring.className = "l-open";
		    trigger.innerHTML = "Close";
	    }
	    else {
	        authoring.className = "l-closed";
		    trigger.innerHTML = "Authoring";
	    }
	    var login = authoring.appendChild(document.getElementById("login"));
	    foreach(login.getElementsByTagName("input"), function(input) {
	        if (input.type == "text") {
	            tilt.attachEvent(input, "keydown", function(node, e) {
	                if (e.keyCode == 13) {
                        window.setTimeout("document.getElementById(window.__authoringLoginId).click()", 0);
                        return false;
	                }
	                return true;
	            });
	        }
	        else {
	            window.__authoringLoginId = input.id;
	        }
	    });
	    foreach(authoring.getElementsByTagName("legend"), function(legend) {
	        var value = legend.innerHTML;
	        var parent = legend.parentNode;
	        var div = parent.insertBefore(document.createElement("div"), legend);
	        div.className = "l-legend";
	        div.innerHTML = value;
	        parent.removeChild(legend);
	    });
	    foreach(authoring.getElementsByTagName("fieldset"), function(fieldset) {
	        var parent = fieldset.parentNode;
	        var div = parent.insertBefore(document.createElement("div"), fieldset);
	        div.innerHTML = fieldset.innerHTML;
	        parent.removeChild(fieldset);
	    });
    }
    
	function objectize(arrayOfIds)	{
		var result = {};
		foreach(arrayOfIds, function(item) {
			var o = document.getElementById(item);
			if (o) {
				result[item] = o;
			}
			else {
				result.complete = true;
			}
		})
		result.complete = !result.complete;
		result.contains = function(matches) {
			return foreach(matches, function(match) {
				if (result[match]) {
					return true;
				}
			});
		}
		return result;
	}
	
	function readFeeds(col, classNames) {
		var result = {};
		foreach(col, function(node) {
			push(getClassName());
			
			function push(className) {
			    if (className) {
				    var id = node.id;
				    if (!id) {
				        id = __layout.createUniqueId();
				        node.id = id;
				    }
				    result[className] = id;
			    }
			}
			
			function getClassName() {
				var result = null;
				foreach(classNames, function(className) {
					if (foreach(node.className.split(" "), function(nodeClassName) {
						return nodeClassName == className;
					})) {
						result = className;
						return true;
					}
				});
				return result;
			}

		});
		return result;
	}
	
	
	__layout.addLinkaroo = function(node) {
	    var links = node.appendChild(document.createElement("div"));
	    var title = encodeURIComponent("UAB in Antarctica : " + document.title);
	    var url = encodeURIComponent(window.location.href);
	    links.className = "l-linkaroo";
	    links.innerHTML = "<ul><li><a class=\"digg\" href=\"" + createDiggLink() + "\">Digg This Story</a></li>" + 
	        "<li><a class=\"newsvine\" onclick=\"" + createNewsvineOnClick() + "\" href=\"" + createNewsvineLink() + "\">Seed Newsvine</a></li>" +
	        "<li><a class=\"delicious\" onclick=\"" + createDeliciousOnClick() + "\" href=\"" + createDeliciousLink() + "\">Save to del.icio.us</a></li>" +
	        "<li><a class=\"permalink\" href=\"" + createPermalink() + "\">Permalink</a></li></ul>";
	        
	    function createDiggLink() {
	        return "http://digg.com/submit?phase=2&url=" + url + "&title=" + title;
	    }
	    
	    function createNewsvineOnClick() {
	        return "window.open('http://www.newsvine.com/_wine/save?popoff=1&u=" + url + "','newsvine','toolbar=no,width=590,height=600,resizable=yes,scrollbars=yes'); return false;";
	    }
	    
	    function createNewsvineLink() {
	        return "http://www.newsvine.com/_tools/seed&save?popoff=0&u=" + url;
	    }
	    
	    function createDeliciousOnClick() {
	        return "window.open('http://del.icio.us/post?v=4&noui&jump=close&url=" + url + "&title=" + title + "', 'delicious','toolbar=no,width=700,height=400'); return false;";
	    }
	    
	    function createDeliciousLink() {
	        return "http://del.icio.us/post";
	    }
	    
	    function createPermalink() {
	        return window.location.href;
	    }
	}
	
	__layout.prettifyTitling = function(titling, parentId) {
        var img = first(titling.getElementsByTagName("img"));
        if (img) {
            var a = img.parentNode;
            var className = (a.nodeName == "A" && a.className) || img.className;
            if (className.indexOf("avatar") >= 0) {
                var avatar = __layout.appendNode(document.createElement("div"), parentId);
                avatar.style.backgroundImage = "url(" + img.src + ")";
                avatar.className = "l-avatar";
                avatar.title = img.alt;
                if (a.nodeName == "A") {
                    avatar.innerHTML = "<a href=\"" + a.href + "\">" + img.alt + "</a>";
                }
                else {
                    avatar.innerHTML = "<div class=\"l-frame\"></div>"
                }
                img.parentNode.removeChild(img);
            }
        }
	}
	
	__layout.finalize = function(className) {
	
	    var form = document.getElementById("form");
	    if (form) {
	        form.className = className;
	    }
        
        var feeds = document.getElementById("feeds");
        if (feeds) {
            feeds.parentNode.removeChild(feeds);
        }

        rearrangeByWidth(true);
        
        tilt.attachEvent(window, "resize", function() {
            rearrangeByWidth(false);
            return true;
        });
        
        if (window.urchinTracker) {
            window._uacct = "UA-1371008-1";
            window.urchinTracker();
        }
        
        function rearrangeByWidth(firstTime) {
            var width = window.innerWidth || document.body.offsetWidth;
            var lastWidth = window.__lastWidth;
            var checkInProgress = window.__checkInProgress;
            if (checkInProgress || width == lastWidth) {
                return;
            }
            window.__checkInProgress = true;
            var isNarrow = document.body.className == "l-narrow";
            if (isNarrow) {
                if (width > maxNarrowWidth) {
                    document.body.className = "l-wide";
                    if (__layout.onwide) {
                        __layout.onwide();
                    }
                }
            }
            else {
                if (width < minWideWidth) {
                    document.body.className = "l-narrow";
                    if (__layout.onnarrow) {
                        __layout.onnarrow();
                    }
                }
                else if (firstTime) {
                   document.body.className = "l-wide";
                    if (__layout.onwide) {
                        __layout.onwide();
                    }
                }
            }
            window.__checkInProgress = false;
            window.__lastWidth = width;
        }

	}
	
	__layout.getFeed = function(className) {
	    var id = __layout.feeds[className];
	    return id ? document.getElementById(id) : null;
	}
	
	__layout.getRelativeDate = function(now, s) {
        var date = Date.parse(s);
        if (!date) {
			var parts = s.split(" ");
			if (parts.length == 2) {
				var dateParts = parts[0].split("-");
				if (dateParts.length == 3) {
					var timeParts = parts[1].split(":");
					if (timeParts.length == 3) {
						date = new Date(dateParts[0], dateParts[1]-1, dateParts[2], timeParts[0], timeParts[1], timeParts[2]);
					}
				}
			}
		}
		if (date) {
			var diff = (now - date) / 60000;
			if (diff < 1) {
				return "a few seconds ago";
			}
			else if (diff < 2) {
				return "about a minute ago";
			}
			else if (diff < 60) {
				return parseInt(diff, 10) + " minutes ago";
			}
			else {
				diff = diff / 60;
				if (diff < 2) {
					return "an hour ago";
				}
				else if (diff < 24) {
					return parseInt(diff, 10) + " hours ago";
				}
				else {
					diff = diff / 24;
					if (diff < 2) {
						return "a day ago";
					}
					else if (diff < 30) {
						return parseInt(diff, 10) + " days ago";
					}
					else {
						diff = diff / 30;
						if (diff < 2) {
							return "about a month ago";
						}
						else if (diff < 12) {
							return parseInt(diff, 10) + " months ago";
						}
						else {
							diff = diff / 12;
							if (diff < 2) {
								return "about a year ago";
							}
							return parseInt(diff, 10) + " years ago";
						}
					}
				}
			}
		}
        return s;        
	}

    __layout.relativizeDates = function(node, className) {
        var current = new Date();
        foreach(map(node.getElementsByTagName("abbr")), function(abbr) {
            if (abbr.className == className) {
                var relativeDate = __layout.getRelativeDate(current, abbr.title);
                var parent = abbr.parentNode;
                var previousSibling = abbr.previousSibling;
                if (previousSibling && previousSibling.nodeType == 3 && previousSibling.nodeValue.indexOf("on") >= 0) {
					previousSibling.nodeValue = "";
                }
                var div = parent.insertBefore(document.createElement("div"), abbr);
                parent.removeChild(abbr);
                div.className = "l-relative-date";
                div.innerHTML = relativeDate;
            }
        });
    }

    __layout.initFlickrGroups = function(node, count, verbose) {
        var id;
        var user;
        var group;
        var tag;
        var success;
        if (node)
        {
            if (!count) {
                count = 6;
            }
            if (node.className == "flickr") {
                readParams(node);
                node = node.parentNode;
            }
            else {
                foreach(node.getElementsByTagName("div"), readParams);
            }
            if (success) {
                var target = node.appendChild(document.createElement("div"));
                id = __layout.createUniqueId();
                target.className = verbose ? "l-verbose-flickr-group" : "l-flickr-group";
                target.id = id;
                target.innerHTML = "<div class=\"l-loading\"></div>";
                flickr.groups.pools.getPhotosHtml(group, user, tag, count, id, verbose);
            }
        }
        
        function readParams(div) {
            if (div.className == "flickr") {
                foreach(node.getElementsByTagName("abbr"), function(abbr) {
                    if (abbr.className == "user") {
                        user = abbr.title;
                    }
                    if (abbr.className == "group") {
                        group = abbr.title;
                        success = true;
                    }
                    if (abbr.className == "tag") {
                        tag = abbr.title;
                    }
                });
                foreach(node.getElementsByTagName("a"), function(a) {
                    if (a.className == "tag") {
                        tag = a.innerHTML;
                    }
                });
                return true;
            }
        }
    }

    __layout.trim = function(node, top, height) {
        if (node) {
            if (!top) {
                top = node;
            }
            if (top.offsetHeight <= height) {
                return;
            }
            trimChildren(node);
        }

        function trimChildren(node) {
            var cursor = node.lastChild;
            var buffer;
            while(cursor) {
                buffer = cursor.previousSibling;
                cursor = node.removeChild(cursor);
                if (top.offsetHeight <= height) {
                    cursor = node.appendChild(cursor);
                    if (cursor.nodeType == 1) {
                        trimChildren(cursor);
                    }
                    else if (cursor.nodeType == 3) {
                        trimText(cursor);
                    }
                    return;
                }
                cursor = buffer;
            }
        }
        
        function trimText(textNode) {
            var index;
            var half;
            var buffer;
            var lastLength = textNode.length;
            half = textNode.length / 2;
            while(textNode.length > 0) {
                index = textNode.data.indexOf(" ", half);
                if (index < 0) {
                    textNode.data += " ...";
                    return;
                }
                buffer = textNode.data;
                textNode.deleteData(index, textNode.length - index);
                if (top.offsetHeight <= height) {
                    textNode.data = buffer;
                    half = half * 1.5;
                }
                else {
                    half = textNode.length / 2;
                }
                lastLength = textNode.length;
            }
        }

    }

    __layout.appendNode = function(node, hookId) {
        if (node) {
            if (!hookId) {
                hookId = node;
                node = __layout.createUniqueId();
            }
            var hook = hookId.nodeType ? hookId : document.getElementById(hookId);
            if (hook) {
                if (typeof(node) == "string") {
                    var id = node;
                    node = document.getElementById(node);
                    if (node) {
                        node = hook.appendChild(node);
                    }
                    else {
                        node = hook.appendChild(document.createElement("div"));
                        node.id = id;
                    }
                }
                else if (node.constructor === Array) {
                    node = node[0];
                    hook.appendChild(node);
                }
                else {
                    hook.appendChild(node);
                }
            }
        }
        return node;        
    }
    
    __layout.createShell = function(node, spec) {
        processSpec(node, spec);
        
        function processSpec(node, spec) {
            var firstChild = node.firstChild;
            foreach(spec, function(nodeSpec) {
                var nodeSpecType = typeof(nodeSpec);
                if (nodeSpecType == "string") {
                    insertNode(nodeSpec, firstChild);
                }
                else if (nodeSpecType == "object") {
                    for(var name in nodeSpec) {
                        processSpec(insertNode(name, firstChild), nodeSpec[name]);
                    }
                }
            })
            
            function insertNode(id, firstChild) {
                var newNode = node.insertBefore(document.createElement("div"), firstChild);
                newNode.id = id;
                return newNode;
            }
        }
    }

	__layout.createUniqueId = function() {
	    var newId = document.uniqueID;
	    if (newId) {
	        return newId;
	    }
	    if (!window.__currentUniqueId) {
    	    window.__currentUniqueId = 0;
	    }
        return "id" + (++window.__currentUniqueId);
	}

	__layout.distribute = function(col, classNames, defaultClassName) {
		var result = {};
		foreach(col, function(node) {
			push(getClassName());
			
			function push(className) {
				var items = result[className];
				if (!items) {
					items = [];
					result[className] = items;
				}
				items.push(node);
			}
			
			function getClassName() {
				var result = defaultClassName;
				foreach(classNames, function(className) {
					if (foreach(node.className.split(" "), function(nodeClassName) {
						return nodeClassName == className;
					})) {
						result = className;
						return true;
					}
				});
				return result;
			}

		});
		return result;
	}


};