function show_reply_form(comment_id){
        if (comment_id){
                jQuery("#cancel-comment-reply-link").show();
        }
        else{
                jQuery("#cancel-comment-reply-link").hide();
        }
        jQuery("#respond").appendTo("#comment_"+comment_id);
        jQuery("#comment_parent_id").attr("value", comment_id);
        jQuery( "#comment_form_errors" ).empty();
        return false;
}

function remove_comment(pcomment_id, post_url, show_warning){
        
        if (show_warning){
                if (!confirm('Remove this comment?  All child comments will also be removed')){
                        return false;
                }
        }
        jQuery.post(post_url, {fmt: 'json', comment_id: pcomment_id, comment_remove_ajax: '1'},
         function(data) {
                 
                 if (!data['success']){
                         alert('Could not remove comment: '+data['message'])
                         return
                 }
                 else{
                         
                         jQuery('#comment-'+data['comment_id']).hide()
                         
                 }

           
         }, 
         "json"
        );        
        
        //alert('Are you sure?');
        //jQuery('#comment-'+comment_id).hide()
     //   return false;
        
}

function handle_comment_submit(event){
    /* stop form from submitting normally */
    event.preventDefault(); 
        
    /* get some values from elements on the page: */
    var form_data = jQuery( this ).serialize();
  url =  jQuery(this).attr( 'action' );
     

    /* Send the data using post and put the results in a div */
    jQuery.post( url, form_data,
      function( data ) {
          var content =data;// $( data ).find( '#content' );
              //alert(content['error_html']);
              
          jQuery( "#comment_form_errors" ).empty().append( content['error_html'] );
              if (content['refresh'] ){                  
                window.location.reload();
                return false;
              }
      }, "json"
    );

}

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();
  }
}
}
