/**
 * @package danbettles_net
 * @copyright Copyright (c) 2009, Dan Bettles 
 * @author Dan Bettles <danbettles@yahoo.co.uk>
 */

var danbettles = {

    //@todo Private
    originalTitle: '',

    setOriginalTitle: function (p_title) {
        this.originalTitle = p_title;
    },

    getOriginalTitle: function () {
        return this.originalTitle;
    },

    getTitleEl: function () {
        return jQuery('h1').eq(0);
    },

    setTitle: function (p_title) {
        this.getTitleEl().html(p_title);
    },

    getTitle: function () {
        return this.getTitleEl().html();
    },

    decodeURI: function (p_string) {
        ///%[0-9a-fA-F]{2}/
        return decodeURI(p_string).replace('%40', '@');
    },

    select: function (p_oTitleEl) {
        var oPosition = p_oTitleEl.position(),
            oAnchorEl = p_oTitleEl.children('a'),
            href = oAnchorEl.attr('href'),
            aUrlPartMatch,
            aThisHostnameMatch,
            urlHostname;

        p_oTitleEl
            .next('dd')
                .css({ left: oPosition.left, top: oPosition.top })
                .addClass('selected');

        oAnchorEl.addClass('selected');

        //Ignore "#" or "proto://domain/path/#"
        if (/(^#|\/#$)/.test(href)) {
            return false;
        }

        aUrlPartMatch = href.match(/^([^:]+):(?:\/\/)?([^\/]+)/);

        //Ignore mutant URLs
        if (aUrlPartMatch === null) {
            return false;
        }

        urlHostname = this.decodeURI(aUrlPartMatch[2]);

        aThisHostnameMatch = document.location.hostname.match(/^(www\.)?(.*)/i);

        if (aThisHostnameMatch === null) {
            return false;
        }

        //Ignore off-site links
        if (urlHostname.toLowerCase().indexOf(aThisHostnameMatch[2].toLowerCase()) === -1) {
            return false;
        }

        this.setTitle(urlHostname);

        return true;
    },

    deselect: function (p_oTitleEl) {
        p_oTitleEl.next('dd').removeClass('selected');
        p_oTitleEl.children('a').removeClass('selected');
        this.setTitle(this.getOriginalTitle());
    }
};

jQuery(function () {
    danbettles.setOriginalTitle(danbettles.getTitle());

    jQuery('div.sites dl *').each(function () {
        var oCurrEl = jQuery(this);

        switch (oCurrEl.get(0).tagName) {
            case 'DT':
                oCurrEl
                    .hover(
                        function () {
                            danbettles.select(jQuery(this));
                        },
                        function (e) {
                            var oCurrEl = jQuery(e.relatedTarget),
                                oDescEl = jQuery(this).next('dd');

                            if (oCurrEl.closest('dd').get(0) === oDescEl.get(0)) {
                                return;
                            }

                            danbettles.deselect(jQuery(this));
                        }
                    );

            break;

            case 'DD':
                oCurrEl
                    .mouseout(function (e) {
                        var oCurrEl = jQuery(e.relatedTarget),
                            oTitleEl;

                        if (oCurrEl.closest('dd').get(0) === this) {
                            return;
                        }

                        oTitleEl = jQuery(this).prev('dt');

                        //@todo Or a child of the title element
                        if (oCurrEl === oTitleEl.get(0)) {
                            return;
                        }

                        danbettles.deselect(oTitleEl);
                    })
                    .click(function () {
                        var oThisEl = jQuery(this),
                            href = oThisEl.prev('dt').children('a').attr('href');

                        document.location.href = href;
                    });

            break;
        }
    });
});