window.addEvent("domready", function() {
    // the DCI largerbox: an Apple.com style slimbox

    largerBox.init();

    largerBox.init({
        selector: "a.newimage",
        useModal: true,
        useShadow: true,
        colourModal: "#fff",
        styleBorder: "1px solid #000"
    });

    labels.init();

    (function() {
        $$(".dismiss").fade(0);
    }).delay(10000);

    (function() {
        $$(".dismiss").dispose();
        labels.moveLabels();
    }).delay(10500);

    // tooltips on tags
    $$("strong.tag").addEvents({
        mouseenter: function() {
            this.tooltip("google " + this.get("text"), {opacity: .8});
        },
        click: function() {
            window.open("http://www.google.com/search?q="+ this.get("text"));
        }
    }).addClass("cur");


    // hidden emails
    $$("a.mailLink").each(function(el) {
        var mailProperties = JSON.decode(el.get("title"));
        el.set({
            "href": "mailto:" + mailProperties.user + "@" + mailProperties.domain + "?subject="+ mailProperties.subject,
            "title": "Send a mail to "+ mailProperties.user + "@" + mailProperties.domain
        });
    });


    $$(".box-right li").addEvents({
        mouseenter: function() {
            var linked = this.getFirst();

            this.addClass("box-right-on").fade(1);
            linked.store("color", linked.getStyle("color")).setStyle("color", "#000");
        },
        mouseleave: function() {
            var linked = this.getFirst();
            this.removeClass("box-right-on").fade(.5);
            linked.setStyle("color", linked.retrieve("color"));
        }
    }).setOpacity(.5);

    var startAndFold = (function() {
        $("search-submit").set("tween", {
            duration: "long"
        }).tween("left", -27);
    }).delay(2000);


    $("search-submit").set({
        styles: {
            opacity: 1
        },
        events: {
           mouseenter: function() {
                $clear(startAndFold);
                this.tween("left", 0);
            },
            mouseleave: function() {
                this.tween("left", -27);
            },
            click: function() {

            }
        }
    });

    var focused = false;
    $("search").addEvents({
        focus: function() {
            focused = true;
            $("search-submit").fireEvent("mouseenter");
            if (this.get("value") == "Enter text here")
                this.set("value", "");
        },
        blur: function() {
            focused = false;
        },
        mouseenter: function() {
            $("search-submit").fireEvent("mouseenter");
        },
        mouseleave: function() {
            if (!focused)
            $("search-submit").fireEvent("mouseleave");
        }

    });

    // Cookie.write("leftmenu", "");

    new Sortables($("content-right"), {
        handle: 'h4',
        clone: true,
        constrain: true,
        revert: true,
		onStart: function() {

        },
        onComplete: function() {
            var ustring = '';
            var neworder = this.serialize(2, function(el, i) {
                ustring += el.get("data-id") + ",";
            });
            Cookie.write("leftmenu", ustring, {
                duration: 31
            });
        }
    });





    // current page -> mouseover
    var curElement = $E(".current_page_item");
    if (!curElement)
        curElement = $E(".page_item");
    var coords = curElement.getPosition();
    var leaver;

    var mp = new Element("div", {
        id: "mypointer",
        styles: {
            position: "absolute",
            width: 112,
            height: 9,
            left: coords.x,
            top: coords.y-9,
            background: "url(/images/targetter.gif)"
        }
    }).inject(document.body).setOpacity(.8).set("tween", {
        duration: "long"
    });

    var showCurrent =  function(what) {
        var coords = what.getPosition();
            mp.tween("left", coords.x);
    }

    $$(".page_item").addEvents({
        mouseenter: function() {
            $clear(leaver);
            showCurrent(this);
        },
        mouseout: function() {
            leaver = (function() {
                showCurrent(curElement);
            }).delay(2000);
        }
    });

    window.addEvent("resize", function() {
        showCurrent(curElement);
    });

    $$(".submit").addEvents({
        click: function() {
            if ($("message").get("value").trim().length == 0) {
                this.Bubble("Sorry, cannot submit an empty comment");
                return false;
            }
        }
    });

    // google analytics via mootools / dom.
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    new Asset.javascript(gaJsHost + "google-analytics.com/ga.js", {
        onload: function() {
            var pageTracker = _gat._getTracker("UA-1199722-1");
            pageTracker._initData();
            pageTracker._trackPageview();
        }
    });

    $$(".myimage").each(function(el) {
        el.set({
            "data-title": el.get("title"),
            title: ""
        }).addEvents({
            mouseenter: function() {
                this.tooltip(this.get("data-title"), {opacity: .8});
            }
        })
    });

    var socialBookmarks = [
        Digg = {
            sName: "Digg",
            sURL: "http://digg.com/submit?phase=2&url="+encodeURIComponent(window.location.href)+"&bodytext="+encodeURIComponent(document.title),
            sImage: "/images/digg.gif"
        },
        StumbleUpon = {
            sName: "Stumble Upon",
            sURL: "http://www.stumbleupon.com/submit?url="+encodeURIComponent(window.location.href)+"&newcomment="+encodeURIComponent(document.title),
            sImage: "/images/stumble.gif"
        },
        Delicious = {
            sName: "Delicious",
            sURL: "http://del.icio.us/post?v=2&url="+encodeURIComponent(window.location.href)+"&tags=fragged&title="+encodeURIComponent(document.title),
            sImage: "/images/delicious.gif"
        },
        Mixx = {
            sName: "Mixx",
            sURL: "http://www.mixx.com/submit?page_url="+encodeURIComponent(window.location.href),
            sImage: "/images/mixx.gif"
        },
        Google = {
            sName: "Google",
            sURL: "http://www.google.com/bookmarks/mark?op=add&bkmk="+encodeURIComponent(window.location.href)+"&title="+encodeURIComponent(document.title),
            sImage: "/images/goog.png"
        },
        Wong = {
            sName: "Mister Wong",
            sURL: "http://www.mister-wong.com/index.php?action=addurl&bm_url="+encodeURIComponent(window.location.href)+"&bm_description="+encodeURIComponent(document.title),
            sImage: "/images/mister-wong.gif"
        }
    ];

    // console.log(socialBookmarks);
    socialBookmarks.each(function(bookmark) {
        new Asset.image(bookmark.sImage, {
            onload: function() {
                this.set({
                    "class": "cur",
                    styles: {
                        "margin-right": 1
                    },
                    events: {
                        click: function() {
                            window.open(bookmark.sURL);
                        },
                        mouseenter: function() {
                            this.tooltip("At " + bookmark.sName, {opacity: .8}).fade(1);
                        },
                        mouseleave: function() {
                            this.fade(.5);
                        }
                    },
                    opacity: .5
                }).inject($("social_bookmarks"));
            }
        });
    });

    $("social_what").addEvent("click", function() {
        $("social_bookmarks").Bubble("Social bookmarks are just internet favourites");
        this.fade(0);
    });

    $("tag_cloud").getElements("a").each(function(TAG) {
        TAG.set({
            "data-title": TAG.get("title"),
            title: ""
        }).addEvents({
            mouseenter: function() {
                this.tooltip(this.get("data-title"));
            }
        });
    });

    // adsense move/*

    var positionAdwords = function() {
        var adsize = $("as").getCoordinates();
        var coords = $("content").getCoordinates();
        var win = window.getScroll();
        var winSize = window.getScrollSize();

        var topOffset = (coords.top - 110 + win.y + adsize.height > winSize.y)
                ? winSize.y - adsize.height - 14
                : coords.top - 110 + win.y;

        $("as").set({
           styles: {
                position: "absolute",
                left: coords.left + coords.width + 20,
                top: topOffset
            }
        });
    }

    positionAdwords();

    window.addEvent("scroll", function() {
        var win = window.getScroll();
        positionAdwords();
        $("as").fireEvent("mouseleave");
    });

    $("as").addEvents({
        mouseenter: function() {
            this.tooltip("help us pay for hosting", {opacity: .8});
        }
    });

    // autohide.
    $$("div.autohide").each(function(el) {
        var _this = $(el.get("data-trigger"));
        el.setStyle("display", "none");
        _this.addEvents({
            mouseenter: function() {
                el.setStyle("display", "block").fade(0,1);
            },
            mouseleave: function() {
                el.fade(1,0);
                (function() {
                    el.setStyle("display", "none");
                }).delay(600);
            }
        }).addClass("cur");
    });

    if ($("facebOOk"))
    $("facebOOk").addEvents({
        mouseenter: function() {
            this.tooltip("here is a tip, click below!");
        }
    });


    return false;
    // google chrome frame
    CFInstall.check({
        node: "page",
        destination: "http://www.waikiki.com"

    });

});
