var isIE = (document.all) ? true : false;
var $ = function (id) {
    return "string" == typeof id ? document.getElementById(id) : id;
};

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var Extend = function(destination, source) {
        for (var property in source) {
                destination[property] = source[property];
        }
}

var Bind = function(object, fun) {
        return function() {
                return fun.apply(object, arguments);
        }
}

var Each = function(list, fun){
        for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};

var FileUpload = Class.create();
FileUpload.prototype = {
  initialize: function(form, folder, options) {
        
        this.Form = $(form);
        this.Folder = $(folder);
        this.Files = [];
        
        this.SetOptions(options);
        
        this.FileName = this.options.FileName;
        this._FrameName = this.options.FrameName;
        this.Limit = this.options.Limit;
        this.Distinct = !!this.options.Distinct;
        this.ExtIn = this.options.ExtIn;
        this.ExtOut = this.options.ExtOut;
        
        this.onIniFile = this.options.onIniFile;
        this.onEmpty = this.options.onEmpty;
        this.onNotExtIn = this.options.onNotExtIn;
        this.onExtOut = this.options.onExtOut;
        this.onLimite = this.options.onLimite;
        this.onSame = this.options.onSame;
        this.onFail = this.options.onFail;
        this.onIni = this.options.onIni;
        
        if(!this._FrameName){
                this._FrameName = "uploadFrame_" + Math.floor(Math.random() * 1000);
                var oFrame = isIE ? document.createElement("<iframe name=\"" + this._FrameName + "\">") : document.createElement("iframe");
                oFrame.name = this._FrameName;
                oFrame.style.display = "none";
                document.body.insertBefore(oFrame, document.body.childNodes[0]);
        }
        this.Form.target = this._FrameName;
        this.Form.method = "post";
        this.Form.encoding = "multipart/form-data";
        this.Ini();
  },
  SetOptions: function(options) {
    this.options = {
                FileName:        "picture[]",
                FrameName:        "",
                onIniFile:        function(){},
                onEmpty:        function(){},
                Limit:0,
                onLimite:        function(){},
                Distinct:        true,
                onSame:                function(){},
                ExtIn:                [],
                onNotExtIn:        function(){},
                ExtOut:                [],
                onExtOut:        function(){},
                onFail:                function(){},
                onIni:                function(){}
    };
    Extend(this.options, options || {});
  },
  Ini: function() {
        this.Files = [];
        Each(this.Folder.getElementsByTagName("input"), Bind(this, function(o){
                if(o.type == "file"){ o.value && this.Files.push(o); this.onIniFile(o); }
        }))
        var file = document.createElement("input");
        file.name = this.FileName; 
		file.type = "file"; 
		file.onchange = Bind(this, function(){ this.Check(file); this.Ini(); });
        this.Folder.appendChild(file);
        this.onIni();
  },
  Check: function(file) {
        var bCheck = true;
        if(!file.value){
                bCheck = false; this.onEmpty();
        } else if(this.Limit && this.Files.length >= this.Limit){
                bCheck = false; this.onLimite();
        } else if(!!this.ExtIn.length && !RegExp("\.(" + this.ExtIn.join("|") + ")$", "i").test(file.value)){
                bCheck = false; this.onNotExtIn();
        } else if(!!this.ExtOut.length && RegExp("\.(" + this.ExtOut.join("|") + ")$", "i").test(file.value)) {
                bCheck = false; this.onExtOut();
        } else if(!!this.Distinct) {
                Each(this.Files, function(o){ if(o.value == file.value){ bCheck = false; } })
                if(!bCheck){ this.onSame(); }
        }
        !bCheck && this.onFail(file);
  },
  Delete: function(file) {
        this.Folder.removeChild(file); this.Ini();
  },
  Clear: function() {
        Each(this.Files, Bind(this, function(o){ this.Folder.removeChild(o); })); this.Ini();
  }
}


function AddList(rows){
        var FileList = $("idFileList"), oFragment = document.createDocumentFragment();
        Each(rows, function(cells){
                var row = document.createElement("tr");
                Each(cells, function(o){
                        var cell = document.createElement("td");
                        if(typeof o == "string"){ cell.innerHTML = o; }else{ cell.appendChild(o); }
                        row.appendChild(cell);
                });
                oFragment.appendChild(row);
        })
        while(FileList.hasChildNodes()){ FileList.removeChild(FileList.firstChild); }
        FileList.appendChild(oFragment);
}
function callBack(id)
{
	base.setMessage('<p>照片上传完毕！</p>');
	setTimeout(function(){window.location.href = "action.php?ac=album&id="+id;}, 2000);
}