
function insertAtCursor(textarea_id, myValue) {
	
	
	myField = document.getElementById(textarea_id);
    //IE support
    if (document.selection) {
        myField.focus();

        //in effect we are creating a text range with zero
        //length at the cursor location and replacing it
        //with myValue
        sel = document.selection.createRange();
        sel.text = myValue;

    //Mozilla/Firefox/Netscape 7+ support
    } else if (myField.selectionStart || myField.selectionStart == '0') {

        myField.focus();
        //Here we get the start and end points of the
        //selection. Then we create substrings up to the
        //start of the selection and from the end point
        //of the selection to the end of the field value.
        //Then we concatenate the first substring, myValue,
        //and the second substring to get the new value.
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        myField.setSelectionRange(endPos+myValue.length, endPos+myValue.length);
    } else {
        myField.value += myValue;
    }
}


function add_media(media){
  jQuery('#'+media.insert_id).attr('value',media.id);
  if (media.type == 'file'){
    jQuery('#input_'+media.insert_id).html('<a href="'+media.url+'">'+media.title+'</a> <a href="#here" title="Remove" onclick="remove_media(\''+media.insert_id+'\')">[x]</a>');
  }
  else{
    jQuery('#input_'+media.insert_id).html('<img src="'+media.url+'"> <a href="#here" title="Remove" onclick="remove_media(\''+media.insert_id+'\')">[x]</a>');
  }
  jQuery.fancybox.close();
}
function remove_media(insert_id){
  jQuery('#'+insert_id).attr('value',0);
  jQuery('#input_'+insert_id).html('');
}

function insert_media(media, textarea_name, use_wysiwyg){
original_text = jQuery('#'+textarea_name).attr('value');
if (!original_text){
  original_text = '';
}
if (media.type == 'file'){
  if (use_wysiwyg){
    tinyMCE.execInstanceCommand(textarea_name, "mceInsertContent",0 , '<a class="file" href="'+media.url+'">'+media.title+'</a>');
    jQuery.fancybox.close();
  }
  else{
    jQuery('#'+textarea_name).attr('value',original_text + ' <a class="file" href="'+media.url+'">'+media.title+'</a>');
    jQuery.fancybox.close();
  }
}
else{
  if (use_wysiwyg){	  
	  media.id = "" + media.id;
	  
	  //fix ie's select lost problem--
	  ed =  tinyMCE.get(textarea_name);
	  ed.selection.setRng(p_editor_bookmark);		
	  //--	  
	  
	  if (media.id && (media.id.substr(0, 18) == '_ams_custom_block_')){		  
		tinyMCE.execInstanceCommand(textarea_name, "mceInsertContent",0 , '<img class="'+media.id+'" src="'+media.url+'">');
	  }
	  else{

		tinyMCE.execInstanceCommand(textarea_name, "mceInsertContent",0 , '<a title="'+media.title+'" class="image" href="'+media.url_full+'"><img src="'+media.url+'"></a>');
	  }
   jQuery.fancybox.close();
  }
  else{
    jQuery('#'+textarea_name).attr('value',original_text + ' <a title="'+media.title+'" class="image" href="'+media.url_full+'"><img class="image" src="'+media.url+'"></a>');
    jQuery.fancybox.close();
  }
}
}
