/**
 * Copy to Clipboard for jQuery
 * Copy a text sring to the clipboard
 *
 * Copyright (c) 2010 Matt Nicholson (matt@dda.co.uk)
 *
 * Adaptation of Bradley Sepo's script to overcome Flash player 10+ security limitations
 * This script overlays a flash button over the jQuery element you request. This way the clipboard call is invoked from Flash legitimately via a user interaction.
 * 
 * Version 1.0
 * Updated 14/06/2010
 *
 *
 * Usage: $(object).copyToClipboard(String:text);
 * 
-----------------------------------------------------------------------------------
Example:
 
$(document).ready(function(){
	$('a.copy-me').each(function(){		
		$(this).copyToClipboard($(this).attr('alt'),{swfpath:"/javascripts/jquery/jquery.copytoclipboard.swf"});
	});
});

-----------------------------------------------------------------------------------
 *
 * 
 */

 
(function($) {
$.fn.copyToClipboard = function( text , options) {
	
	// Scope variables...
	// ---------------------------------------------------------------------
	var init = false;
	var mode;
	var defaults = {
   		swfpath: "jquery.copytoclipboard.swf"
 	 };
  
  	var options = $.extend(defaults, options);
	
	// IE detection...
	// ---------------------------------------------------------------------
	var iecopydetect = function(){
		// Check for IE method
		if ( typeof window.clipboardData != "undefined" ){
			return true;
		}else{
			return false;	
		}
	};
	
	// Initialise function...
	// ---------------------------------------------------------------------
	var initClipboard = function(){
		
		// Establish mode
		mode = iecopydetect() ? 'native' : 'flash';
		
		// We have initialised
		init = true;
	};
	
	if(!init){	initClipboard();}
	
	// The copyToClipboard function...
	// ---------------------------------------------------------------------		
	
	// If natively supported, attach to click event...
	if (mode == 'native'){
		$(this).click(function(){window.clipboardData.setData( "Text", text )});
	}else{ // Flash overlay...
	
		// Create the flash container
		var span;
		span = $( "<span/>" )
			.attr( "class", "j-copy-text" )
			.css( "width", "100%" )
			.css( "height", "100%" )
			.css( "overflow","hidden")
			.css( "position","absolute")
			.css( "top",0)
			.css( "left",0)
			.appendTo( this )
			.html( "" );
		// Create the helper swf
		// Use embed method since we're only targeting non-IE browsers anyway
		var swf;
		swf = $( '<embed name="j_clipboard_swf" src="'+options.swfpath+'" scale="exactfit" wmode="transparent" flashvars="text='+escape(text)+'" type="application/x-shockwave-flash"></embed>' );
		$( swf )
			.css( "width", "100%" )
			.css( "height", "100%" )
			.appendTo( span );
		// Make sure our container is relatively positioned
		$(this).css( "position","relative").css("height","auto").css("width","auto");
	}	
}
})(jQuery);

 
	/*
	
	Based on Clipboard - Copy utility for jQuery	(http://bradleysepos.com/projects/jquery/clipboard/) by Bradley Sepos
	 
	Released under an MIT-style license
	
	LICENSE
	------------------------------------------------------------------------
	
	Copyright (c) 2009 Bradley Sepos / 2010 Matt Nicholson 
	
	Permission is hereby granted, free of charge, to any person obtaining a
	copy of this software and associated documentation files (the
	"Software"), to deal in the Software without restriction, including
	without limitation the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to
	permit persons to whom the Software is furnished to do so, subject to
	the following conditions:
	
	The above copyright notice and this permission notice shall be included
	in all copies or substantial portions of the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
	OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
	MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
	IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
	CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
	TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	
	------------------------------------------------------------------------
	
	*/
