/*ajax upload*/
$().ready(function(){
	
	/** upload **/
    Delete_Cookie("delete_images");
    
    $("ul.upload-files li a.delete").click(function()
    {
        imgs = Get_Cookie("uploadImage");
        if(imgs) {
            var val = imgs.replace(this.id+";", "");
            val = val.replace(";;", ";");
            if(val == '') {
                Delete_Cookie("uploadImage");
            }
            else {
                Set_Cookie( "uploadImage", val, 1);
            }
        }
        
        var delete_images   = Get_Cookie("delete_images");
        if(!delete_images) delete_images = '';
        delete_images      += this.id+ ';';
        Set_Cookie('delete_images', delete_images, 1);
        
        $(this).parents("li:first").remove();
    });
    
	 var button = $('#button1'), interval;
	 
	new AjaxUpload('file', {
		action: getBaseURL()+'upload-handler.php', 
		name: 'file',
		onSubmit : function(file, ext){
			// Allow only images. You should add security check on the server-side.
	        if (ext && /^(jpg|png|jpeg|gif)$/i.test(ext)) {                            
	            /*this.setData({
	                'key1': 'This should be alerted',
	                'key2': '...'
	            });*/
	        } else {
	        	if($("#file").parents(":first").find("div"))
	        	{
	        		$("#error-upload-image").remove();
	        	}
	        	
        		$("#file").parents(":first").append($("<div id='error-upload-image' style='float:left;display:inline;color:red;font-weight:bold'>Bạn chỉ có thể đăng ảnh có định dạng: *.gif, *.jpg, *.jpeg,*.png</div>"));
	        	
	            return false;
	        }
	        
			// change button text, when user selects file			
			//button.text('Uploading');
			$("#error-upload-image").remove();				
			
			// If you want to allow uploading only 1 file at time,
			// you can disable upload button
			this.disable();
			
			$('<li class="loading">&nbsp;</li>').appendTo('ul.upload-files');
			// Uploding -> Uploading. -> Uploading...
			interval = window.setInterval(function(){
				/*var text = button.text();
				if (text.length < 13){
					//button.text(text + '.');					
				} else {
					//button.text('Uploading');				
				}*/
				// add file to the list
				
			}, 2000);
		},
		onComplete: function(file, response){
			//button.text('Upload');
			$('ul.upload-files li.loading').remove();
			window.clearInterval(interval);
			
			//alert(response);			
			// enable upload button
			this.enable();
			
			if(response!='error1' && response!='error2')
			{
				//var img = new Image();
				//$(img).attr('src', "'+getBaseURL()+'data/images/'+file+'").prepend(this);
				// add file to the list
				$('<li><a id="'+response+'" href="#img"  class="delete" title="xóa">xóa</a><img src="'+getBaseURL()+'data/images/upload/tmp/'+response+'" /></li>').appendTo('ul.upload-files');						
				
				
				$("a.delete").click(function()
				{
					//alert(this.id);
					imgs = Get_Cookie("uploadImage");
									
					var val = imgs.replace(this.id+";","");
					val = val.replace(";;",";");
					if(val=='')
					{
						Delete_Cookie("uploadImage");
					}
					else
					{
						Set_Cookie( "uploadImage", val, 1);
					}
					$(this).parents("li:first").remove();
				});
				
				//save cookie
				if(Get_Cookie("uploadImage"))
				{				
					imgs = Get_Cookie("uploadImage")+response+";";
					Delete_Cookie("uploadImage");
					Set_Cookie( "uploadImage", imgs, 1);
					//alert(Get_Cookie("uploadImage"));
				}
				else
				{					
					Set_Cookie( "uploadImage", response+";", 1);
				}
			}
			else
			{
				
				if(response=="error1")
				{					
					$("#file").parents(":first").append($("<div id='error-upload-image' style='float:left;display:inline;color:red;font-weight:bold'>Bạn chỉ có thể đăng ảnh có định dạng: *.gif, *.jpg, *.jpeg,*.png</div>"));
				}
				
				if(response=="error2")
				{
					$("#file").parents(":first").append($("<div id='error-upload-image' style='float:left;display:inline;color:red;font-weight:bold'>Không thể tải được ảnh lên. Bạn vui lòng thử lại !</div>"));
				}
			}
			
		}
	});
});
