

//$.fn.fixed = function() { 
//    return this.each(function() { 
//        var header = $(this); 
//        var offset = header.offset(); 
//        var floater = $('<div></div>').append(header.clone(true)); 
//        floater.hide().appendTo(document.body).addClass('floating-toolbar'); 
//
//        $(window).scroll(function() { 
//            var s = $(this).scrollTop(); 
//            if (s > offset.top) { 
//                header.css('visibility', 'hidden'); 
//                floater.show(); 
//            } else { 
//                floater.hide(); 
//                header.css('visibility', 'visible'); 
//            } 
//        }); 
//    }); 
//}; 


var rp = rp || {};
    
rp.codeDisplay = function() {
    var clip = null;
    
    String.prototype.repeat= function(n){
        n= n || 1;    
        return Array(n+1).join(this);
    }
    
    // Array Remove - By John Resig (MIT Licensed)
    Array.prototype.remove = function(from, to) {
        var rest = this.slice((to || from) + 1 || this.length);
        this.length = from < 0 ? this.length + from : from;
        return this.push.apply(this, rest);
    };    

    Number.prototype.paddedLeft = function( length, padChar ) {
        padChar = padChar || "&nbsp;";

        result = padChar.repeat( length ) + this;
        result = result.substring( result.length - length );
        return result; 
    }    
    
    function showShadow() {
        $("#shadow").css( "height", $( document ).height() );
        $("#shadow").show();
    }

    function hideShadow() {
        $("#shadow").hide();    
    }

    function fixedLengthNumber( number, length, padChar ) {
        padChar = padChar || "&nbsp;";

        result = padChar.repeat( length ) + number;
        result = result.substring( result.length - length );
        return result; 
    }

    function init() {    
        ZeroClipboard.setMoviePath( "http://www.rogerpence.com/ZeroClipboard.swf" );
        clip = new ZeroClipboard.Client();
        clip.setHandCursor( true );    
        
        clip.addEventListener( "complete", function() {
            //showShadow();
            clip.element.html( "Copied to clipboard" );
            setTimeout( function() {
                            clip.element.fadeOut( 500, function() {
                                             clip.element.hide();
                                             clip.element.html( "Copy" ); 
                                             clip.element.fadeIn( 1000 );
                                             //hideShadow();
                                        } );
                        }, 500);      
        });                    
    }        
    
    function configureZeroClipboard( clipboardElement ) {  
        var codeContainer = jQuery( clipboardElement ).
                                              parent().
                                                next().
                                            children().
                                                 eq(0);
        var code = codeContainer.text();
        
        var codeLines = code.split( "\n" );
        // http://ejohn.org/blog/javascript-array-remove/
        //codeLines.remove( codeLines.length - 1 );
        //codeLines.remove( 0 );
        
        jQuery.each( codeLines, function( index, value ) {
            codeLines[ index ] = codeLines[ index ].replace( /^\d{3}/, "" );
        });
        
        var block = codeLines.join( "\n" );

        clip.setText( block );        
        
        clip.element = $( clipboardElement );

        if ( clip.div ) {
            clip.receiveEvent( 'mouseout', null );
            clip.reposition( clipboardElement );
        }
        else {
            clip.glue( clipboardElement );
        }   
        
        // gotta force these events due to the Flash movie
        // moving all around.  This insures the CSS effects
        // are properly updated.
        clip.receiveEvent( 'mouseover', null );   
    };                
    
    function toggleCodeDisplay( clickedElement ) {
        var $this = $( clickedElement );
        var $copyToClipboardElement = $( ".copyCodeToClipboard", clickedElement );
        
        var codeContainer = $this.next();
       
        codeContainer.toggleClass( "hideCode showCode" );
        
        if ( codeContainer.hasClass( "showCode" ) ) {    
            $this.css( "background-position", "0 -30px" );   
            $copyToClipboardElement.show();  
            // Shouldn't this focus the ZeroClipboard element?
            configureZeroClipboard( $copyToClipboardElement.get(0) );
            clip.show();
        }    
        else {
            $this.css( "background-position", "0 -6px" );            
            $( $copyToClipboardElement ).hide();
            clip.hide();
        }    
    }        

    return {
        init : init,
        toggleCodeDisplay : toggleCodeDisplay,
        configureZeroClipboard : configureZeroClipboard
    };
}();
