/**
 * upload_controls.js
 * Description: UploadControl class
 * Author: John Hughes
 * Date: 08/03/2008
 */
 
UploadControl = Class.create();
UploadControl.prototype = {
  initialize: function(o) {
    this.m_options = {
      ClassroomID: 0,
      controller_url: "/Controller/MediaController.cfc",
      upload_method: "remoteHandleMediaUpload",
      render_method: "remoteRenderUpload",
      form_method: "remoteGetRenderedUploadForm",
      public_upload: false,
      restrict_size: false,
      absolute_links: false
    };
    Object.extend(this.m_options, o || { });
  },
  
  onActionStart: function() { WaitControl.busy(); },
  onActionEnd: function() { WaitControl.ready(); },
  
  uploadStart: function() {
    this.onActionStart();
  },
  
  showUploadForm: function() {
    new Ajax.Request(
      this.m_options.controller_url, {
        method: 'post',
        parameters: $H({ method: this.m_options.form_method,
                        ClassroomID: this.m_options.ClassroomID,
                        public_upload: this.m_options.public_upload,
                        restrict_size: this.m_options.restrict_size }).toQueryString(),
        onCreate: this.onActionStart,
        onComplete: this.onActionEnd,
        onSuccess: function(transport) {
          $("upload-container").replace(transport.responseText.strip());
        }.bind(this),
        onFailure: function(transport) {
          $('debug').innerHTML = transport.responseText.strip().replace(/\r|\n/g, '');
        }
    });
  },
  
  hideUploadForm: function(e) {
    if(e)
      Event.stop(e);
    $("upload-container").update();
    $("upload-container").hide();
  },
  
  uploadDone: function(content) {
    this.onActionEnd();
    //container = $('upload-container');
    //prev = container.previous();
    //container.remove();
    
    
    $("upload-container").replace(content.stripScripts());
    
    Nifty("#upload-container", "big same-height");
  },
  
  insertMedia: function(id, align) {
  	
    url = "/Controller/MediaController.cfc?method=remoteRenderUpload&UploadID=" + id;
    
    if(this.m_options.absolute_links) {
    	url = location.protocol + "//" + location.hostname + url;
      tinyMCE.activeEditor.settings.relative_urls = false;
      tinyMCE.activeEditor.settings.remove_script_host = false;
    }
    
    img = $(Builder.node('img', {src: url, title: "Content Image"}));
    switch(align) {
      case "right":
        img.setStyle({cssFloat: 'right'});
        break;
      case "left":
        img.setStyle({cssFloat: 'left'});
        break;
      default:
      break;
    }
    img.setStyle({padding: '5px'});
    ser = new tinymce.dom.Serializer();
    
    // For some reason if the editor window doesnt have focus,
    // in IE the rshHistoryFrame does, so force focus in this case.
    if(tinyMCE.activeEditor.selection.getStart().id == "rshHistoryFrame") { 
      tinyMCE.activeEditor.focus();
    }

    tinyMCE.activeEditor.selection.setContent(ser.serialize(img));
  }
};
