Smarts['.brushemail'] = function() {
    var mouseover = function() {
        var localpart = this.href.substr(this.href.lastIndexOf('=')+1);
        var address = localpart + String.fromCharCode(64) + "brush.co.nz";
        var url = "mailto:" + address
        if (this.getAttribute('subject') != null) {
            url += "?subject=" + this.getAttribute('subject');
            if (this.getAttribute('body') != null) {
                url += "&body=" + this.getAttribute('body');
            }
        }
        this.href = url
        if (this.getAttribute('nodisplay') == null) {
            this.firstChild.nodeValue = address;
        }
    };
    this.event('mouseover', mouseover, true);
}

Smarts['.mainitem'] = function() {
    var sub = this.$('.sub');
    if (!sub) return;
    this.event('mouseenter', function() {
        sub.show();
    });
    this.event('mouseleave', function() {
        sub.hide();
    });
};

Smarts['.servicespic'] = function() {
  var divs = [this.$('#software-service'), this.$('#electronics-service'), this.$('#web-service')]

  var onmouseenter = function() {
    for (var i in divs) {
        divs[i].removeClass('active');
        divs[i].unevent('mouseenter', onmouseenter);
    }
  };
  
  for (var i in divs) {
    divs[i].event('mouseenter', onmouseenter);
  }
};

var active_popup = null;
Smarts['a.magnify-thumb'] = function() {
    this.event('click', function(e) {
        if (active_popup) active_popup.hide();
        var popup = this.$('.popup') || $(this.parentNode).$('.popup');
        popup.show();

        var relative_to = popup.parentNode;
        while ($(relative_to).computedStyle().position == 'static') relative_to = relative_to.parentNode;
        
        var rel_dimensions = $(relative_to).dimensions();
        var ratio = e.mouse.x / viewport().width;
        popup.style.left = (e.mouse.x-rel_dimensions.x-popup.dimensions().width*ratio)+'px';
        popup.style.top = (e.mouse.y-rel_dimensions.y)+'px';
        e.stop();
        
        active_popup = popup;
        
        $(document).event('click', function(e) {
            if (!e.target.outside(popup)) return;
            if (active_popup) active_popup.hide();
            e.remove();
        });
    });
};

Smarts['.close'] = function() {
    this.event('click', function(e) {
        $(this.parentNode).hide();
    });
};

// submenu hover shadow background
preload('/images/boxes/submenu-shadow-top.png');
preload('/images/boxes/submenu-shadow-middle.png');
preload('/images/boxes/submenu-shadow-middle-ie6.png');
preload('/images/boxes/submenu-shadow-bottom.png');

// misc
preload('/images/green-arrow-right.png');
preload('/images/electronics-icon-full.png');
preload('/images/software-icon-full.png');
preload('/images/web-icon-full.png');
preload('/images/buttons/magnify-active.png');
preload('/images/buttons/magnify-inactive.png');

// homepage icons that appear when you hover
preload('/images/brush-icon-active.png');
preload('/images/brush-icon-inactive.png');
preload('/images/software-icon-active.png');
preload('/images/software-icon-inactive.png');
preload('/images/electronics-icon-active.png');
preload('/images/electronics-icon-inactive.png');
preload('/images/web-icon-active.png');
preload('/images/web-icon-inactive.png');

// shadow background for popup box (eg if they come via the portfolio page) 
preload('/images/boxes/popup-top.png');
preload('/images/boxes/popup-bottom.png');
preload('/images/boxes/popup-bg.png');

// buttons
preload('/images/buttons/onclick-primary.png');
preload('/images/buttons/normal-primary.png');
preload('/images/buttons/onclick-secondary.png');
preload('/images/buttons/normal-secondary.png');


