var ImageUpload={


  init: function()
  {
    if (!Flash.init(9) || !$('change_photo')) return;

    var uploadSuccessEventHandler = function (file, server_data) {
      $('goal_picture').up().removeClassName('image_loading');
      ImageUpload.photo_uploaded({'responseText':server_data});
    };

    var d=$('change_photo');
    d=d.getDimensions();
    if (!d.width) d={'width':'70px', 'height':'15px'};

    var pp={ 'doupload': 1, 'ws':1};
    pp[vars.s[0]]=vars.s[1];

    swfu = new SWFUpload({
        upload_url : vars.upload_url,
        flash_url : JSRoot+"js/core/swfupload.swf",
        file_upload_limit : "0",
        file_queue_limit : "1",
        button_placeholder_id : "spanButtonPlaceholder",
        button_width: d.width,
        button_height: d.height,
        button_window_mode: SWFUpload.WINDOW_MODE.TRANSPARENT,
        button_cursor: SWFUpload.CURSOR.HAND,

        post_params: pp,
        file_dialog_start_handler: function(){
          //ProfileEdit.clear_photo_error();
        },
        file_dialog_complete_handler: function(nf,nq){
          if (nf==1)
          {
            //$('remove_photo').addClassName('hidden');
            //ImageUpload.last_image=$('user_face').src+'';
            ImageUpload.clear_photo_error();
            $('goal_picture').up().addClassName('image_loading');
            this.startUpload();
          }
        },
        upload_progress_handler : function(fo,bc,tb){
          //var h=64-(Math.ceil(bc*64/tb));
          //$('user_face_place').setStyle({'background':'#fff url("'+JSRoot+'i/wh.gif") repeat-x 0px '+h+'px'});
        },
        upload_success_handler : uploadSuccessEventHandler
    });
  },

  photo_uploaded: function(r)
  {// alert(r.responseText);
    try {
      var res = eval('(' + Just.req(r.responseText) + ')');
    } catch (e) {
      var res = {'error':1}
    }
    ImageUpload.clear_photo_error();

    if (res.data.multimedia)
    {
      var gpic=$('goal_picture');
      gpic.src=res.data.multimedia.url;
      gpic.up().href=res.data.multimedia.href;
      $('image_id').value=res.data.multimedia.id;
      if ($('output').hasClassName('goal'))
      {
        gpic.width=res.data.multimedia.medium.width;
        gpic.height=res.data.multimedia.medium.height;
        gpic.src=res.data.multimedia.medium.href;
        Mir.pop_set([res.data.multimedia.id, 'image']);
      }

      //$('remove_photo')?$('remove_photo')[res.data.face.stub?'addClassName':'removeClassName']('hidden'):false;
      /*
      if (res.data.undo)
      {
        HighLight.remove_undo();
        new Insertion.Top('wrapper-inner', res.data.undo);
        HighLight.init_undo();
      }
      ImageUpload.clear_photo_error();
      */

      return true;
    }
    else
    {

      if (ImageUpload.last_image)
      {
        if (ImageUpload.last_image.indexOf('nophoto')<0)
        {
          //$('remove_photo').removeClassName('hidden');
        }
        $('goal_picture').src=ProfileEdit.last_image;
      }

      if (res.error && res.error.image)
      {
        if ($('change_photo').next('em'))
        {
          $('change_photo').next('em').update(res.error.image);
        }
        else
        {
          $('change_photo').insert({'after':'<em>'+res.error.image+'</em>'});
        }
        //HighLight.error_light($('photo_info').down('em'));
      }
    }

  },

  clear_photo_error: function(){
    if ($('change_photo').next('em'))
    {
      $('change_photo').next('em').remove();
    }
  }


}

document.observe("dom:loaded", ImageUpload.init);
