﻿new function() {
    tilt.attachEvent(document, "contentreceived", function() {
        /* debugging */        try {
        var content = document.getElementById("content");
        
        __layout.appendNode("l-social", "l-bottom-hook");
    
        attachPreviewHandlers(__layout.getFeed("latest"), "l-latest", true);
        attachPreviewHandlers(__layout.getFeed("crew"), "l-crew", false);
        attachPreviewHandlers(__layout.getFeed("tags"), "l-tags", false);
        __layout.appendNode(__layout.getFeed("web2"), "l-col-3");

        if (content) {
            __layout.appendNode(content, "l-social");
            __layout.initFlickrGroups(content, 9);
        }
        
        __layout.onwide = function() {
            __layout.appendNode("l-social", "l-col-1");
        }
        
        __layout.onnarrow = function() {
            __layout.appendNode("l-social", "l-bottom-hook");
        }
        
        __layout.finalize("l-home");
        /* debugging */        } catch(e) { alert(e);}
    });

    function attachPreviewHandlers(feed, className, preload) {
        if (feed) {
            if (!window.__previews) {
                __previews = {};
            }
            __layout.appendNode(feed, "l-col-3");
            feed.className += " " + className;
            foreach(feed.getElementsByTagName("a"), function(a) {
                var id = __layout.createUniqueId();
                var previewId = __layout.createUniqueId();
                var lastId = __layout.createUniqueId();
                var photosId = __layout.createUniqueId();
                var recentId = __layout.createUniqueId();
                __previews[id] = {
                    preview: previewId,
                    last: lastId,
                    photos: photosId,
                    recent: recentId
                };
                var preview = __layout.appendNode(previewId, "l-col-2");
                preview.className = "l-hidden";
                preview.innerHTML = "<div id=\"" + lastId + "\" class=\"l-content\"></div><div class=\"l-right\"><div id=\"" + photosId + "\" class=\"l-photos\"></div><div id=\"" + recentId + "\" class=\"l-recent\"></div><div class=\"l-more-link\"><a href=\"" + a.href + "\">more</a></div></div><div class=\"l-bottom\"></div>";
                a.id = id;
                if (preload) {
                    __previews.preload = id;
                    window.setTimeout(function() {
                        setPreview();
                    }, 100);
                }
                tilt.attachEvent(a, "click", function(a) {
                    setPreview(a.id);
                    return false;
                });
            });
        }
    }
        
    function setPreview(id) {
        if (!id) {
            id = __previews.preload;
        }
        if (id) {
            var a = document.getElementById(id);
            if (!a) {
                return;
            }
            var currentId = __previews.current;
            if (currentId) {
                if (currentId == id) {
                    return;
                }
                document.getElementById(currentId).parentNode.className = "";
                document.getElementById(__previews[currentId]).className = "l-hidden";
            }
            var ids = __previews[id];
            if (typeof(ids) == "string") {
                a.parentNode.className = "l-current";
                document.getElementById(ids).className = "l-visible";
                __previews.current = id;
            }
            else {
                a.parentNode.className = "l-loading";
                var preview = document.getElementById(ids.preview);
                preview.className = "l-loading";
                ajah(a.href, {
                    titling: function(titling) {
                        __layout.prettifyTitling(titling, ids.last);
                        var h1 = first(titling.getElementsByTagName("h1"));
                        if (h1) {
                            __layout.appendNode(h1, ids.last);
                        }
                    },
                    content: function(content) {
                        var c = __layout.distribute(content, ["description", "flickr"], "junk");
                        var last = document.getElementById(ids.last);
                        if (last) {
                            var description = c.description && c.description[0];
                            if (description) {
                                last.appendChild(description);
                            }
                            last.appendChild(document.createElement("div")).className = "l-title-bottom";
                        }
                        __layout.initFlickrGroups(__layout.appendNode(c.flickr, ids.photos), 6);
                        foreach(c.junk, function(junk) {
                            junk.parentNode.removeChild(junk);
                        });
                    },
                    feeds: function(feeds) {
                        var c = __layout.distribute(feeds, ["last-post", "recent-posts"], "junk");
                        var last = c["last-post"];
                        last = last && last[0];
                        if (last) {
                            var link = first(last.getElementsByTagName("a"));
                            if (link) {
                                link = link.href;
                            }
                            var top = document.getElementById("l-outer-1");
                            var height = top.offsetHeight;
                            __layout.appendNode(last, ids.last);
                            if (link) {
                                var more = __layout.appendNode(ids.last);
                                more.className = "l-more-link";
                                more.innerHTML = "<a href=\"" + link + "\">read more</a>";
                            }
                            __layout.trim(last, top, height);
                        }
                        __layout.appendNode(c["recent-posts"], ids.recent);
                        foreach(c.junk, function(junk) {
                            junk.parentNode.removeChild(junk);
                        });
                    }
                }, function() {
                        var preview = document.getElementById(ids.preview);
                        var a = document.getElementById(id);
                        preview.className = "l-visible";
                        a.parentNode.className = "l-current";
                        __previews[id] = ids.preview;
                        __previews.current = id;
                        __layout.relativizeDates(preview, "posted");
               });
           }
        }
    }

	function ajah(url, callbacks, finalizer) {
	    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : null;
	    if (xhr) {
		    var prefix = __layout.createUniqueId();
	        xhr.onreadystatechange = function() {
		        if (xhr.readyState == 4 && xhr.status == 200) {
		            var html = xhr.responseText;
		            var startPos = html.indexOf("body>");
		            if (startPos >= 0) {
		                startPos += 5;
		                var endPos = html.indexOf("</body", startPos);
		                if (endPos >= 0) {
		                    html = html.substring(startPos, endPos).replace(/id(\s)*=(\s)*"/g, "id=\"" + prefix + "-");
		                    var store = document.body.appendChild(document.createElement("div"));
		                    store.style.cssText = "position:absolute;left:-1972px;width:10px;height:10px;overflow:hidden;";
		                    store.innerHTML = html;
		                    for(var id in callbacks) {
		                        var node = document.getElementById(prefix + "-" + id);
		                        if (node) {
		                            callbacks[id](node);
		                        }
		                    }
		                    store.parentNode.removeChild(store);
		                    if (finalizer) {
		                        finalizer();
		                    }
		                }
		            }
		        }
			}	
	        xhr.open("GET", url, true);
	        xhr.send(null);
	    }
	}

}

