
function CreateCopyableTextbox(Id){           
   
    // New ids
    var TextareaId  = 'Textarea' + Id;
    var ButtonId    = 'BackBtn' + Id;     

    // Id = Highlighted code view div 
    Id = '#' + Id; 

    $(Id).bind("click", function(){
        
        // Highlighted code
        var Content = jQuery.trim($(this).text());
        
        // Code lines
        var Lines   = Content.split((jQuery.browser.msie)?"\r":"\n").length; 
        
        if (jQuery.browser.msie) Lines += 3;
        if (jQuery.browser.opera) Lines += 3;  
        
        // ampersands (&)
        Content = Content.replace(/\&/g,'&amp;');
        
        // less-thans (<)
        Content = Content.replace(/\</g,'&lt;');
        
        // greater-thans (>)
        Content = Content.replace(/\>/g,'&gt;');
        
        // Replace quotes if it isn't for display,
        // since it's probably going in an html attribute.
        Content = Content.replace(new RegExp('"','g'), '&quot;');
        

        // Code area (textarea)
        var Code  = '<textarea name="'+TextareaId+'" id="'+TextareaId+'" rows="'+Lines+'" cols="20" wrap="off" style="width: 100%; margin-top: 11px;">';
            Code += Content;
            Code += '</textarea>';
            Code += '<input type="button" name="'+ButtonId+'" id="'+ButtonId+'" value="Return to highlighted code view" />';
        
        $(this).after(Code);
        
        // Hide highlighted code view div 
        $(this).hide();
        
        // Select textarea
        $('#'+TextareaId).select();
        
        $('#'+TextareaId).bind("dblclick", function(){ $(this).select(); }); 
        
        // Button event
        $('#' + ButtonId).bind("click", function(){
            
            // Hide button
            $(this).hide();

            // Hide textarea  
            $(this).prev().hide(); 
            
            // Show highlighted code view
            $(this).prev().prev().show();
            
        });  

        return false;                
    });

}

function InitCopyableTextbox(){        
    CreateCopyableTextbox('source_code');        
    CreateCopyableTextbox('example_code');        
}

jQuery.event.add( window, "load", InitCopyableTextbox);
