var settings = {
  //debug: true,
  flash_url:"images/swf/swfupload.swf",
  prevent_swf_caching: false,
  preserve_relative_url: true,
  button_placeholder_id:"spanButtonPlaceHolder",
  button_image_url: "images/blank.gif",
  upload_url:"image/upload",
  file_post_name:"Filedata",
  file_size_limit:"5 MB",
  file_types:"*.jpg; *.jpeg",
  file_types_description:"Image Files",
  file_upload_limit:5,
  file_queue_limit:1,
  button_width:"310",
  button_height:"34",
  button_text:"",
  button_text_style:"",
  button_text_left_padding:0,
  button_text_top_padding:0,
  button_window_mode:"transparent",
  file_dialog_complete_handler:function()
  {
    this.startUpload();
  },
  upload_start_handler:function(b)
  {
    onStartUpload(b);
  },
  upload_progress_handler:function(d,b,c)
  {
    uploadProgress(d,b,c);
  },
  file_queue_error_handler:function(b,d,c)
  {
    if(d===SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED)
    {
      alert( i18n.CAN_UPLOAD_ONLY_ONE );
      return;
    }
    switch(d){
      case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT: alert( i18n.LARGE_FILE_SIZE );break;
      case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE: alert( i18n.ZERO_FILE_SIZE );break;
      case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE: alert( i18n.WRONG_FILE_TYPE );break;
    }
  },
  upload_success_handler:function(c,e)
  {
    onUploadSuccess(c,e)
  },
  upload_error_handler: function()
  {
    jQuery("#bar_proc").stopTime();
    alert( i18n.ERROR_WHILE_UPLOADING );
  }
};

function onStartUpload(b)
{
  jQuery("#bar_proc").css("backgroundPosition","0px 0px");
  jQuery("#step1").css("top","-1000px");
  jQuery("#step2").show();
  jQuery("#bar_proc").css("width","0px");
  jQuery("#bar_proc").everyTime(50, function(){var c=parseInt($("#bar_proc").css("backgroundPosition"))+2;$("#bar_proc").css("backgroundPosition",c+"px 0px")})
}

function uploadProgress(d,b,c)
{
  var e=b/c;
  document.getElementById("bar_proc").style.width = Math.ceil(244*e)+"px";
}

function onUploadSuccess(c, e)
{
  var b=e.substr(0,1);
  if(b!="0")
  {
    jQuery("#step1").css("top",0);
    jQuery("#step2").hide();
    jQuery("#step3").hide();
    jQuery("#bar_proc").stopTime();
    switch(b)
    {
      case"1":
      case"2":
      case"3":
      case"4":
        alert( e.substr(3) );
        break;
      default:
        alert( i18n.ERROR_WHILE_UPLOADING );
        break;
    }
  }
  else
  {
    var indexOfSharp = e.indexOf("#");
    if( indexOfSharp > 0 )
    {
      var avatar_id = e.substr(indexOfSharp + 1);
      document.cookie = "avatar_id=" + avatar_id + "; path=/";
    }

    var d=parseInt(e.substr(1));
    if(d==0)
    {
     jQuery.ajax({
       type: "GET",
       dataType: "text",
       url: "image/isuploaded",
       cache: false,
       success: function(f){
        if( f.substr(0,2) != "no" ) {
          jQuery("#step2").hide();
          jQuery("#bar_proc").stopTime();
          jQuery("#step3").hide();
          jQuery("#step4").show();
        } else {
          jQuery("#step3").stopTime();
          jQuery("#step3").hide();
          jQuery("#step2").hide();
          jQuery("#step1").css("top",0);
          alert( i18n.UPLOADING_FAILD );
        }
       },
       error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus + ' | ' + errorThrown);
       }
     });
    }
    else
    {
      jQuery("#step3 #waittime span").html(rus_text( i18n.SECOND_DECLENSION , d ));
      jQuery("#step3 #waittime span").everyTime(1000,function(){var f=parseInt($(this).html());f--;if(f<1){$(this).stopTime();$(this).html("совсем немного")}else{$(this).html(rus_text( i18n.SECOND_DECLENSION , f ))}});
      jQuery("#step2").hide();
      jQuery("#step3").show();
      jQuery("#bar_proc").stopTime();
      jQuery("#roundbar div").css("backgroundPosition","0px 0px");
      jQuery("#roundbar div").css("left","0px");
      jQuery("#roundbar div").everyTime(140,function(){
          var g=parseInt($(this).css("left"));
          var f=parseInt($(this).css("backgroundPosition").split(" ")[0]);
          var h=parseInt($(this).css("backgroundPosition").split(" ")[1]);
          if(h==0){if(g>=162){h=27;g=g-27;f=f+27}else{g=g+27;f=f-27}}else{if(g<=0){h=0;g=g+27;f=f-27}else{g=g-27;f=f+27}}$(this).css("backgroundPosition",f+"px "+h+"px");$(this).css("left",g+"px")
      });
      jQuery("#step3").oneTime(d*1000, function() {
         jQuery("#step3").everyTime(2000, function() {
            $.get("image/isuploaded?"+Math.random(),function(f){if(f.indexOf("1")==0){jQuery("#step3").stopTime();jQuery("#step3").hide();jQuery("#step4").show()}});
         });
         $.get("image/isuploaded?"+Math.random(),function(f){if(f.indexOf("1")==0){jQuery("#step3").stopTime();jQuery("#step3").hide();jQuery("#step4").show()}});
      })
    }
  }
}

/* */
$(document).ready(function(){
  $("#warn a").click(function(){
         $("#warn").hide();
         $("#step1").show();
  });
  $("#swfuploaddiv").mouseover(function(){
         $("#select_file").removeClass("select_file");
         $("#select_file").addClass("select_file_hover")
  });
  $("#swfuploaddiv").mouseout(function(){
        $("#select_file").addClass("select_file");
        $("#select_file").removeClass("select_file_hover")
  });
  var a=$('<img src="images/close.png" alt="" />');
  a.prependTo("#hint");
  $("#hint img").click(function(){
         $(this).parent().hide();
         $("#hintbg").hide();
         $("#news").css("z-index",15)
  });
  $("#select_descr a").click(function(){
         if($("#hint").css("display")=="none"){
            $("#news").css("z-index",5);
            $("#hint").show();
            $("#hintbg").show();
            $("#hintbg").height($("#hint").height())
         }
         else{
            $("#news").css("z-index",15);$("#hint").hide();$("#hintbg").hide()
         }
         return false
  });

  var swfu = new SWFUpload(settings);
});
