var global = {
    init: function(){
        
        //global.iHaveJavascript();
        if ($('#site dd').length > 0) global.navHideShow();
        if ($('.hub .aside .promo').length > 0) global.equalHeightHubs();
        if ($('a.more').length > 0) global.moreLinks();
        if ($('a.view').length > 0) global.viewLinks();
        if ($('.tagcloud').length > 0) global.tagCloud();
        if ($('a.back').length > 0) global.backLinks();
        if ($('a[rel=external]').length > 0) global.newWindow();
        if ($('li.haslink').length > 0) global.addHand();
        if ($('.column').length > 0) global.equalHeights($('.column'));
        if ($('.mailto').length > 0) global.defuscateEmail();
        
        // Replace submit buttons with fancy-links
        $("form input[type=submit], form button[type=submit]").each(function() {
            $(this).replaceWith(
                $('<a class="fancy-link submit-btn" href="#">' + ($(this).val() || $(this).text()) + '</a>')
                .click(function() {
                    var formarray = $(this).parents("form:first");
                    if(0 < formarray.length && typeof(formarray[0].onsubmit) == 'function') {
                        return formarray[0].onsubmit();
                    }
                    $(this).parents("form:first").submit();
                    return false;
                }))
        });
        
        // Init cufon
        global.cufon();
        
        // Init office selector
        global.initOfficeSelector();
        
    },
    //removing no-js class
//    iHaveJavascript: function() {
//        $('#page').removeClass('no-js');
//    },
    //make the main nav hide and show
    navHideShow: function() {
        $('#site dd').hide();
        $('#site dl').hover(function () {
            $(this).find('dd').show();
        }, function() {
            $(this).find('dd').hide();
        });
        $('#site dt a').click(function(){
            return false;
        });
    },
    // Sets up the events for the language selector
    initOfficeSelector: function() {
        $('.select-office a').click(function(){
            global.changeOffice($(this).attr('href'));
            return false;
        });
        $('.select-office').hover(function(){
            $('.select-office a').show();
        },
        function(){
            $('.select-office a:not(:first-child)').hide();
        })
        $('.select-office a:not(:first-child)').hide();
        $('.select-office a').removeClass('first');
        $('.select-office a:first-child').addClass('first');        
    },
    changeOffice: function (office) {
        var item = $('.select-office a[href=' + office + ']').detach();
        $(item).insertBefore($('.select-office a'));
        $('.office').hide();
        $(office).show();
        $('.select-office a').removeClass('first');
        $('.select-office a:first-child').addClass('first');
        global.setCookie('geo-office', office);
        $('.select-office a:not(:first-child)').hide();
    },
    setCookie: function (name, value) {
        var expire = new Date();
        expire.setDate(expire.getDate() + 30);
        document.cookie = name+ '=' + escape(value) + ';expires=' + expire.toUTCString();
    },
    getCookie: function (name) {
        if (document.cookie.length > 0) {
            var start = document.cookie.indexOf(name + "=");
            if (start != -1) {
                start = start + name.length + 1;
                var end = document.cookie.indexOf(";", start);
                if (end == -1) end = document.cookie.length;
                return unescape(document.cookie.substring(start, end));
            }
        }
        return '';
    },
    //set site functions
    equalHeightHubs: function() {
        var height = 0;
        $('.hub .aside .promo').each(function(){
            if ($(this).innerHeight() > height) height = $(this).innerHeight();
        });
        $('.hub .aside .promo').each(function(){
            if ($(this).hasClass('plist')) {
                $(this).find('a.fancy-link').css({
                    marginTop: (height - $(this).height()) + 'px'
                })
                $(this).height(height + 8);
            } else {
                $(this).find('a.fancy-link').css({
                    marginTop: height - $(this).height() + 'px'
                })
                $(this).height(height);
            }
            
        });
    },
    equalHeights: function(elements) {
        var tallest = 0;
        //find tallest height
        $(elements).each(function(){
            if ($(this).height() > tallest) {
                tallest = $(this).height();
            }
        })
        //set height on element
        if ($.browser.msie && $.browser.version == 6.0) {
            $(elements).css({
                'height' : tallest
            });
        } else {
            $(elements).css({
                'min-height':tallest
            });
        }
    },

    viewLinks: function(){
        $('a.view').each(function(){
            var element = $(this)
            var link = element.attr('href');
            element.addClass('indent');
            element.parent().click(function(){
                window.location.href = link;
            });
        });
    },
    moreLinks: function(){
        $('a.more').each(function(){
            var element = $(this)
            var link = element.attr('href');
            element.hide();
            element.parent().hover(function(){
                element.show();
            }, function(){
                element.hide();
            });
            element.parent().click(function(){
                window.location.href = link;
            });
        });
    },
    tagCloud: function() {
        var previousSize;
        $('.tagcloud a').each(function() {
            if($(this).hasClass(previousSize)) {
                $(this).removeClass(previousSize).addClass(previousSize == "taglarge"? "tagmedium" : "tagsmall");
            }
            previousSize = this.className;
        });
    },
    backLinks: function(){
        $('a.back').each(function(){
            $(this).click(function(){
                history.go(-1);
                return false;
            });
        });
    },
    cufon: function(){
        Cufon.replace('#site dt a', {
            hover:true
        });
        Cufon.replace('h1', {
            hover:true
        });
        Cufon.replace('h2:not(#projects h2)', {
            hover:true,
            hoverables: {
                h2: true
            }
        });
        Cufon.replace('h2 a', {
            hover:true
        });
        Cufon.replace('h3:visible');
        Cufon.replace('p.feature');
        Cufon.replace('a.fancy-link:visible', {
            hover:true
        });
        Cufon.replace('#home .article a.fancy-link', {
            hover:true
        });
        Cufon.replace('a.share', {
            hover:true
        });
        Cufon.replace('a.back', {
            hover:true
        });
        Cufon.replace('.addthis_toolbox span');
        Cufon.replace('.pagination span');
        Cufon.replace('.pageno', {
            hover:true
        });
        Cufon.replace('.pagination .active', {
            hover:true
        });
        Cufon.replace('#events .past .speaker-list dd.speaker a', {
            hover:true
        });
        Cufon.replace('#events .current .speaker-list dt', {
            hover:true
        });
				
        Cufon.replace('.archive h4 a', {
            hover:true
        });
		
		
        Cufon.replace('.featquote blockquote');
        //Cufon.replace('.mapMarker .vcard span');
        Cufon.replace('.filter-nav #sub dt a', {
            hover:true
        });
    },
    newWindow: function() {
        $("a[rel='external']").click(function(e) {
            e.preventDefault();
            window.open($(this).attr("href"));
        });
    },
    defuscateEmail: function() {
        var defuscatedEmail;
        $(".mailto").each(function() {
            defuscatedEmail = $(this).html().replace(/([A-Z0-9._%-]+)( ?\(.+\) ?)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})/gi, "$1@$3");
            $(this).replaceWith('<a href="mailto:' + defuscatedEmail + '" title="Send email to ' + defuscatedEmail + '">' + defuscatedEmail + '</a>');
        });
    },
    addHand: function() {
        if ($.browser.msie && $.browser.version == 6.0) {
            $("li.haslink").css({
                cursor:'pointer'
            });
        }
    }
}
$('html').removeClass('no-js');
//call head function
$(function(){
    
    global.init();    
    
});

