$(document).ready(function(){
	
	$("body").prepend("<textarea style=\"border:none;width:0px; height:0px;margin:0em;padding:1px;position:fixed;top:0px;left:0px;\" id=\"setselect\"></textarea>");
	var d = document.getElementById("setselect");
	
	var isCtrl = 0;
	var isC = 0;
	var footerlink = "\n\nRead more at: "+ location.href; 

	function change_selectionIE()
	{
		if (document.selection)  // IE 6/7
		{
			txt = document.selection.createRange().text;
			if (txt != '')
			{
				txt += footerlink;
				d.value = txt;
								
				range = d.createTextRange();
				range.collapse(true);
				range.moveStart('character', 0);
				range.moveEnd('character', d.value.length);
				range.select();
			}
		} 
	}
	
	function change_selection()
	{
		txt = '';
	    if (window.getSelection) { txt = window.getSelection();}
	    else if (document.getSelection)  { txt = document.getSelection();}
	    else return;
	    
	    if (txt != '')
	    {
			d.value = txt + footerlink;

            if (txt.rangeCount > 0)  txt.removeAllRanges();
            d.focus();
			d.setSelectionRange(0,d.value.length);
	    }
		
	}
	
	
	if (navigator.appName == 'Microsoft Internet Explorer'){ //For IE catch CTRL+C pressed
		
		document.onkeyup = function(e)
		{
			var e = e || window.event;
			if (e.keyCode == 17) isCtrl = 0;
			if (e.keyCode == 67) isC = 0;
			
		}
		document.onkeydown = function(e)
		{
			//if (!e) e=window.event;
			var e = e || window.event;
			if (e.keyCode == 17) isCtrl = 1;
			if (e.keyCode == 67) isC = 1;	
			if (isCtrl && isC) 
			{
				change_selectionIE();
			}
				
		}
		
		$(document).bind("contextmenu", function(e) {
	         change_selectionIE();
	   });
   
   }else { //For non-IE 
	
   		$(document).bind("copy", function(e) {
	    	change_selection();
     	});
   }
});
