jQuery(function ($) {
    var $viewport = $('#viewport'),
        hash = window.location.hash;
    
    if ($viewport.length) {
        var $articleNav = $('ul.articleNav a').click(function () {
            var el = this;
            $viewport.removeClass().addClass(this.className); // FIXME also includes 'selected'
            
            var parent = $(this).parent().get(0);
            $articleNav.parent().removeClass('selected').filter(function () {
                return (this == parent);
            }).addClass('selected');
            return false;
        });
        
        // if (hash) {
        //     if (/#comment-[\d]+/.test(hash)) {
        //         hash = '#allcomments';
        //     }
        //     $articleNav.filter('[hash=' + hash + ']').click();
        //     
        //     // console.log('clicking', $articleNav.filter('[hash=' + hash + ']'), '[hash=' + hash + ']');
        // }
    }
    
    $('a.flash_video').click(function (event) {
        var src = this.href.split('/'),
            file = src[src.length-1];
        window.location = 'http://jqueryfordesigners.com/video.php?f=' + file;
        event.preventDefault();
    });
    
    $('a.email').each(function () {
        var origEmail = this.href.replace(/^mailto:/, '');
        var email = origEmail.replace(/[a-zA-Z]/g, function(c) {
            return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
        });
        
        this.innerHTML = this.innerHTML.replace(origEmail, email);
        this.href = 'mailto:' + email;
    });

    if ($.browser.msie) {
    	$('pre').each(function () {
    	    if (this.scrollWidth > this.offsetWidth) {
    		    $(this).css({ 'overflow-y' : 'hidden', 'padding-bottom' : '20px' });
    	    }
    	});
    }
    
});
