﻿/// <reference path="Common.js" />
/// <reference name="MicrosoftAjax.js" />

Type.registerNamespace('eCommerce.Web');

eCommerce.Web.FileUpload = function(currentFrameID, 
                                    uploadButtonID, 
                                    currentFileCount, 
                                    maxFilesCount, 
                                    previewItemHeight, 
                                    uploadClientID, 
                                    progressAreaID) {

  this.currentFrame = parent.$get(currentFrameID);
  
  this.uploadButton = $(uploadButtonID);
  
  this.currentFileCount = currentFileCount;
  
  this.maxFilesCount = maxFilesCount;
  
  this.previewItemHeight = previewItemHeight;
  
  this.uploadHandlers = null;
  
  this.upload = GetRadUpload(uploadClientID);
  
  this.init(); 
};

eCommerce.Web.FileUpload.registerClass('eCommerce.Web.FileUpload');

eCommerce.Web.FileUpload.submitParentForm = null;

eCommerce.Web.FileUpload.prototype = {
 
  init : function () {
    this.disableUploadButton();
    this.changeFrameSize();
    if (this.upload) {
      this.upload.OnClientAdding = Function.createDelegate(this, this.addFieldHandler);
      this.upload.OnClientDeleting = Function.createDelegate(this, this.deleteFieldHandler);
      this.upload.OnClientDeletingSelected = Function.createDelegate(this, this.deleteSelectedFieldHandler);
      this.upload.OnClientFileSelected = Function.createDelegate(this, this.selectFileClickHandler);      
    }
    if (this.uploadButton) {
      $addHandler(this.uploadButton, 'click', Function.createDelegate(this, this.uploadFileClickHandler));
    }
  },
  
  changeFrameSize : function() {
    if (this.currentFrame) {
      if(this.currentFileCount == this.maxFilesCount) {
        this.currentFrame.height = this.previewItemHeight * this.currentFileCount + 180;
      }
      else {
        this.currentFrame.height = this.previewItemHeight * this.currentFileCount + 200;
      }
      if(iFramework.Browser.isIE() || iFramework.Browser.isOpera()) {
        this.currentFrame.height = parseInt(this.currentFrame.height) + 10;
      }
    }
},

  disableUploadButton : function() {
    if (this.uploadButton) {
        this.uploadHandlers = this.uploadButton.onclick;
        $clearHandlers(this.uploadButton);
        this.uploadButton.addClassName('RadUploadButtonDisabled');
    }
  },
  
  enableUploadButton : function() {
    if (this.uploadButton) {
      this.uploadButton.onclick = this.uploadHandlers;
     // this.uploadButton.removeClassName('RadUploadButtonDisabled');
    }
  },
  
  addFieldHandler : function(radUpload, args) {
    if(!this.currentFileCount) {
      this.currentFileCount = 0;
    }
    this.currentFileCount++;
    this.enableUploadButton();
    this.changeFrameSize();
  },
    
  deleteFieldHandler : function(radUpload, args) {
    if(!this.currentFileCount) {
      this.currentFileCount = 0;
    }
    else {
      this.currentFileCount--;
    }
    if(this.currentFileCount == 0) {
      this.disableUploadButton();
    }
    this.changeFrameSize();  
  },    
  
  deleteSelectedFieldHandler : function(radUpload, args) {
    if(!this.currentFileCount) {
      this.currentFileCount = 0;
    }
    else {
      this.currentFileCount -= args.FileInputFields.length;
    }
    if(this.currentFileCount == 0) {
      this.disableUploadButton();
    }
    this.changeFrameSize();  
  },
  
  uploadFileClickHandler : function() {
    if(!this.currentFileCount) {
      this.currentFileCount = 0;
    }
    this.changeFrameSize();
    this.currentFrame.height = parseInt(this.currentFrame.height) + 100;
    if(iFramework.Browser.isIE() || iFramework.Browser.isOpera()) {
      this.currentFrame.height = parseInt(this.currentFrame.height) + 20;
    }
  },
    
  selectFileClickHandler : function() {
    this.enableUploadButton();  
  }
};


