$(function(){
    var template = "";
    var template_request = {
            url : app.EPWEB_CONTEXT_ROOT + "template/admin/managerAnnounceUploadTemplate.html",
            data : {},
            type : "GET",            
            dataType : "html"
        };
        result = ajaxCall(template_request);
        result.done(function(data){
            template = data;           
        });
   
    var url = "";
    var propertyNames = ["files", "originalFiles", "paramName", "fileInputs"];
    $["blueimp"]["fileupload"] = false;
    $("#fileupload").fileupload({
        autoUpload : false,
        url : url,
        dataType : "json",
		singleFileUploads: true,
		uploadTemplateId:null,
		downloadTemplateId:null,
		maxFileSize : 2097152,
		acceptFileTypes : /(\.|\/)(pdf)$/i,
		uploadTemplate: function (data) {
			//return $("#template-upload").tmpl(data);
		    for (var i = 0;i < data.files.length; i++){
		        data.files[i].lang = managerAnnouncement.currentLanguage;
		    }
		    return lang.updateUIMessagesInHtml($.tmpl(template,data));
		},	
		
		downloadTemplate: function (data) {
			return $("#template-download").tmpl(data);
		},
		done:function(e,data){
		    var fileTrs = $(".template-upload", e.currentTarget);
		    fileTrs.each( function() {
	            var item = $(this).data("data");
	            item.jqXHR = null ;
	            $(this).find('.start').prop("disabled",false);	            
	        });
		    app.uiLoadingStatus.setIsLoading(false);
		    managerAnnouncement.isSaving = false;
		    $("#files").attr("disabled", false);
		   
		    if (data.result.data.data){
		        showSaveResult(data.result);
	            app.trigger('load', 'admin/managerAnnouncementList',
	                        managerAnnouncement.announceType,
	                        managerAnnouncement.currentLanguage,
	                        managerAnnouncement.pageNo
	                     );
		    }
		    else{		        
		        if(data.result.meta.code == "600") {
		            dialog.set({disableLanguage:true}).show( lang.getMessage('fileTooLarge') + "(" + data.result.meta.errorMessage + ")" );
		            managerAnnouncement.changeButtonStatus(false);
		        }
		    }
		    
		},
		fail:function(e,data){
		    app.uiLoadingStatus.setIsLoading(false);
		    managerAnnouncement.isSaving = false;
		    $("#files").attr("disabled", false);
		    managerAnnouncement.changeButtonStatus(false);
		    dialog.show(lang.getMessage("msgFileUploadFailed"), "Ok"); 		    
		}
		
    })
    .bind("fileuploadadded", function(e, data) {
       
		var datas = [data];
		var fileTrs = $(".template-upload", e.currentTarget);
		fileTrs.each( function() {
			var item = $(this).data("data");
			if (!datas.some( function(x) { return x === item; })){
				datas.push(item);
			}
		});
		function concat(objs, propName) {
			var list = [];
			objs.forEach( function(x) {
				list = list.concat(x[propName]);
			});
			return list;
		}
		
		//fileInputs includes all fileInput control
		data.fileInputs = [data.fileInput];
		propertyNames.forEach( function (x) {
			data[x] = concat(datas, x);
		});

		data.context = fileTrs;
		fileTrs.each( function() {
			$(this).data("data", data);
		});
	});
    
    window.cancelUpload = function (button, fileNameAndSize) {        
        if ( (managerAnnouncement.skipButtonEvent() && managerAnnouncement.isClearFiles) || managerAnnouncement.isSaving ){
            return ;
        }   
		var row = $(button).closest(".template-upload");
		var data = row.data("data");
		var i = 0;
		for (; i < data.files.length; i++){
			if (data.files[i].name + (data.files[i].size?data.files[i].size:"") === fileNameAndSize){
				break;
			}
		}
		
		$(row).remove();
		
		propertyNames.forEach( function (x) {
			data[x].splice(i, 1);
		});
		//data.files.splice(i, 1);
		//data.originalFiles.splice(i, 1);
		//data.paramName.splice(i, 1);
		data.context = $(".template-upload");
		return false;
	};
	
});
