// 相册部分的上传
$(document).on('change', ".file2 .album_upload_button", function(e){
	if($('.upprocess').length <= 0){
		html = '<div class="upprocess"><div class="name"><span>上传进度</span></div><div class="con"></div></div>';
		$(".file2").after(html);
		var X = $(".file2").offset().top; 
		var Y = $(".file2").offset().left;  
		$(".upprocess").css({right:0, bottom:0});  
		var files  = e.currentTarget.files;  
		for(var i=0; i<files.length; i++){
			if(!files[i]['name'].match(/.jpg|.gif|.png/i)){　　//判断上传文件格式 
				$.alert(files[i]['name']+' 上传的图片格式不正确，请重新选择');
				$(".upprocess .con").html('').hide();
				return;
			}else{
				var timestamp = Date.parse(new Date());
				timestamp = timestamp / 1000;
				ii = i+timestamp;
				pro_html = '<div class="upprcs_one upprcs_one_'+ii+'"><em><img src="'+PUBLIC_PATH+'images/simg.png"/></em><em>'+files[i]['name']+'</em><em>'+files[i]['size']+'k</em><em>上传中</em></div>';
				if($('.upprocess .upprcs_one').length && $('.upprocess .upprcs_one').length > 0){
					$(".upprocess .upprcs_one:first-child").before(pro_html);
				}else{ 
					$(".upprocess .con").html(pro_html);
				}
				upload_button_action(files[i], '.upprcs_one_'+ii); 
			}
		}
		upload_check();
	}
});
function upload_button_action(file, upprcs_id){ 
	var fd = new FormData();
	fd.append("myfile", file);
	var cid = $("#atree_ck").val(); 
	$.ajax({
		url: "member.php?m=album&a=upload&path=goods&cid="+cid,
		type: "POST",
		data: fd,
		processData: false, // 告诉jQuery不要去处理发送的数据
		contentType: false, // 告诉jQuery不要去设置Content-Type请求头
		xhr: function(){ 
			xhr = $.ajaxSettings.xhr();	
			if(xhr.upload) {
				xhr.upload.addEventListener("progress" ,(function(evt){  
					loaded = evt.loaded;     			//已经上传大小情况 
					tot = evt.total;      				//附件总大小 
					per = Math.floor(100*loaded/tot);  	//已经上传的百分比  
					//$(upprcs_id+" em:last").css("width", per+"%");		//进度条
					$(upprcs_id+" em:last").html(per+"%");
					if(per == 100){
						$(upprcs_id+" em:last").html('<span style="color:red;">√</span>');
						$(upprcs_id).attr('data-ok','1');
						upload_check();
					}
				}), false);
				return xhr;
			}
		}
	}); 
} 

// 检测是否上传完
function upload_check(){
	var total = 0;
	var ok = 0;
	$(".upprcs_one").each(function(){
		$this = $(this);
		if($this.attr('data-ok') == '1'){
			ok++;
		}
		total++;
	}); 
	if(total == 0){
		$(".upprocess").html('').remove();
	}else{
		if(total == ok){
			$(".upprocess").html('').remove();
			if(total != 0){
				$.alert('上传完成');
			}
			cid = $("#atree_ck").val(); 
			get_album_list(cid);
		}		
	}
	
}

// 弹窗的上传
$(document).on('change', ".upload_button #upimg_button", function(e){ 
	var file  = e.currentTarget.files;  
	for(var i=0; i<file.length; i++){
		if(!file[i]['name'].match(/.jpg|.gif|.png/i)){　　//判断上传文件格式 
			$.alert(file[i]['name']+' 上传的图片格式不正确，请重新选择'); 
		}else{
			var timestamp = Date.parse(new Date());
			timestamp = timestamp / 1000;
			ii = i+timestamp; 
			html_con = '<div class="img upprgs upprcs_one_'+ii+'" data-id="0"><img src="" data-name=""/><i><span>&nbsp;</span></i></div>';
			imgBoxNum = $('.ajax_album_img .img').length; 
			if(imgBoxNum > 0){ 
				$('.ajax_album_img .img:first-child').before(html_con);
			}else{
				$('.ajax_album_img').html(html_con); 
			}  
			// 加载图片
			var reader = new FileReader();  
			reader.readAsDataURL(file[i]); 
			reader.onload=function(e){
				 $(".upprcs_one_"+ii+" img").attr('src', e.target.result);
			}
			// 上传图片
			upimg_action(file[i], '.upprcs_one_'+ii); 
		}
	}
 
});
function upimg_action(file, upprgs_id){ 
	var fd = new FormData();
	fd.append("myfile", file);
	var cid = $("input#ajax_album_menu_type").val();
	$.ajax({
		url: "member.php?m=album&a=upload&path=goods&cid="+cid,
		type: "POST",
		data: fd,
		processData: false, // 告诉jQuery不要去处理发送的数据
		contentType: false, // 告诉jQuery不要去设置Content-Type请求头
		xhr: function(){ 
			xhr = $.ajaxSettings.xhr();	
			if(xhr.upload) {
				xhr.upload.addEventListener("progress" ,(function(evt){  
					loaded = evt.loaded;     			//已经上传大小情况 
					tot = evt.total;      				//附件总大小 
					per = Math.floor(100*loaded/tot);  	//已经上传的百分比  
					$(upprgs_id+" i span").css("width", per+"%");		//进度条
					//$(upprcs_id+" em:last").html(per+"%");
					if(per == 100){
						$(upprgs_id+"i").html(""); 
						$(upprgs_id).attr('data-ok','1'); 
					}
				}), false);
				return xhr;
			}
		},
		success(result){ 
			var data = jQuery.parseJSON(result);
			$(upprgs_id).removeClass("upprgs").attr('data-id', data.id).attr('data-name', data.name);
			$(upprgs_id+" img").attr('data-name', data.name).attr('src', data.url); 
			$(upprgs_id+" i").html('');
		}
		
	}); 
}

/*内容*/
function con_html(arr){
	return '<div class="abox">'+
		'<div class="img"><a href="javascript:;"><img src="'+arr.images+'"><input type="checkbox" name="ck_img" value="'+arr.id+'"/></a><i></i></div>'+
		'<div class="opt">'+
			'<div class="rt">'+
				'<a href="javascript:;" class="copy" data-url="'+arr.url+'" title="复制">复制</a>'+
				'<a href="javascript:;" data-id="'+arr.id+'" data-type="'+arr.utype+'" class="del" data-ntag="1" title="删除">删除</a>'+
			'</div>'+
		'</div>'+
	'</div>'; 
}
function get_album_list(pid, url){
	pid = pid?pid:0; 
	if(url){
		post_url = url;
	}else{
		post_url = 'member.php?m=album&a=ajax_list&type='+pid;
	}
	$.getJSON(post_url, function(data){ 
		chtml = '';
		$.each(data.html, function(i,k){
			chtml += con_html(k);
		}); 
		if(chtml){
			$(".ajax_album_img").html(chtml);
			$(".ajax_album_page").html(data.page);
		}else{
			$(".ajax_album_img").html('<div class="bfb"><div class="nomsg"><i></i><span>当前相册为空</span></div></div>');
			$(".ajax_album_page").html(''); 
		} 
	}); 	
} 
//--------------------公共右侧目录管理-----------------------//
$(function(){ 
	// AJAX调取
	jQuery.extend({ 
		// 左侧的图片列表
		album_menu:function (pid, callback){
			$.get('member.php?m=album&a=ajax_type&pid='+pid, function(result){ 
				data = jQuery.parseJSON(result); 
				if(data.error == '0'){
					html = '<div class="atree_box">';
					$.each(data.list, function(i,k){
						html += '<div class="atree_box_one type_'+k.id+' ptype_'+k.pid+'"><a href="javascript:;" data-id="'+k.id+'"><i class="icon"></i><span>'+k.name+'</span></a></div>';
					});
					html += '</div>';	
				}else{
					html = '';	
				}
				callback(html); 
			}); 		
		}
	}); 
	
	
	
	// 点击左侧树菜单，执行其他操作
	$.fn.album_tree = function(options){
		var defaults = {
			conhtml: function(){   },
		};
		var settings = $.extend({}, defaults, options);
	 	self = $(this);
		// 左侧菜单
		self.on('click', '.atree_box_one a', function(evt){
			slc = $('.atree_box_one a', evt.selector); 
			$this = $(this); 
			if($this.hasClass('open')){
				$this.removeClass('open'); 
				$this.nextAll().remove(); 
			}else{
				$this.addClass('open');
				// 加载分类
				var pid = $this.attr('data-id'); 
				$.album_menu(pid, function(html){
					$this.after(html); 					 
				});
				// 加载其他内容
				settings.conhtml(pid);
			}
			// 选中当前 
			slc.removeClass('ck');	
			$this.addClass('ck');  
		}); 
	}
});


// ----------------- 弹出上传框 ------------------- // 
//　弹出窗口 
function upload_manage(postion, file_id, ck_status){ 
	if($('.dg5_box_bar').length && $('.dg5_box_bar').length > 0){
		$(".dg5_box_bar").show(); 
		$(".dg5_bgcolor").show(); 
		$(".ajax_album_ck_box").html('');
		$(".ajax_album_img .img").removeClass('ajax_album_ck');
		// 重新把添加写入
		if(!file_id || file_id == 'undefined'){ 
			file_id = '';
		}
		ck_status = ck_status?1:0;
		htmls = '<input type="hidden" id="ajax_album_ck_status" value="'+ck_status+'"/>'+ 
				'<a href="javascript:;" class="insert_button" onclick="upload_insert(\''+postion+'\',\''+file_id+'\');">插入图片</a>'; 
		$(".img_album_insert").html(htmls);
	}else{
		path = 'goods';
		if(!file_id || file_id == 'undefined'){ 
			file_id = '';
		}
		ck_status = ck_status?1:0;
		width = '750';
		height = '460';
		dialog = '<div class="dg5_bgcolor"></div>'+
		'<div class="dg5_box_bar">'+
			'<div class="dg5_box" id="dg5_box">'+
				'<div class="name"><span>图片管理</span><em onclick="img_close();">×</em></div>'+
				'<div class="con ajax_album_frame">'+
					'<div class="ajax_album_menu">&nbsp;</div>'+
					'<input type="hidden" id="ajax_album_menu_type" value="0"/>'+
					'<div class="ajax_album_con">'+
						'<div class="ajax_album_img">&nbsp;</div>'+
						'<div class="ajax_album_do">'+
							'<div class="upload_button"><a href="javascript:;" class="file">上传图片<input type="file" name="images[]" multiple="multiple" id="upimg_button"/></a></div>'+
							'<!--div class="upload_postion_box"><input type="file" name="file_upload" id="file_upload" /></div-->'+
							'<div class="ajax_album_page">&nbsp;</div>'+
						'</div>'+
					'</div>'+
				'</div>'+ 
				'<div class="ajax_album_ck_box"></div>'+
				'<div class="img_album_insert">'+
					'<input type="hidden" id="ajax_album_ck_status" value="'+ck_status+'"/>'+ 
					'<a href="javascript:;" class="insert_button" onclick="upload_insert(\''+postion+'\',\''+file_id+'\');">插入图片</a>'+
				'</div>'+ 
			'</div>'+
		'</div>';
		$("body").after(dialog).find(".dg5_box_bar").css({opacity:'0',marginTop:'-10px'}).animate({opacity:'1',marginTop:'0'},200); 
		$(".dg5_box").css({"width":width,"height":height, "left": $(window).width()/2 - width/2, "top": $(window).height()/2 - height/2 - 50 }).addClass('ch');
	  	$.album_menu('0', function(){ 
			h_html =  '<div class="atree_box">'+
					'	<div class="atree_box_one type_0 ptype_0">'+
					'		<a href="javascript:;" data-id="0" class="open ck"><i class="icon"></i><span>全部分类</span></a>'; 
			h_html +=		html +
					'	</div>'+
					'</div>'; 
			$(".ajax_album_menu").html(h_html);
		}); 
		$(".ajax_album_menu").album_tree({
			conhtml: function(pid){  
				album_slist(pid);
				$("#ajax_album_menu_type").val(pid);	//小图标选中则标记
			}					
		}); 
		album_slist('0');  
	}
}
function album_slist_html(result){
	html = '';
	$.each(result.html, function(i,k){
		html += '<div class="img" data-id="'+k.id+'" data-name="'+k.url+'">'+
				'	<img src="'+k.images+'" data-name="'+k.url+'"/><i></i>'+
				'</div>'; 
	});
	$(".ajax_album_img").html(html);
	$(".ajax_album_page").html(result.page);	
}
// 相册右侧的内容
function album_slist(pid){ 
	pid = pid?pid:0; 
	$.getJSON('member.php?m=album&a=ajax_list&type='+pid, function(result){ 
		album_slist_html(result);
	});
}
// 关闭弹窗
function img_close(){
	$(".dg5_box_bar").hide(); 
	$(".dg5_bgcolor").hide();
}
 
// 相册弹窗 分页操作
$(document).on('click', ".ajax_album_page a", function(event){ 
	var jump_url = $(this).attr('href');
	if(jump_url){
		$.getJSON(jump_url, function(result){
			album_slist_html(result);
		});
	} 
	if(window.event){
		window.event.returnValue = false;
	}else{  
		event.preventDefault();	 //for firefox
	}
});  

// 选中图片的操作
$(document).on("click",".ajax_album_img .img",function(){
	$this = $(this); 
	data_id = $this.attr('data-id');
	img_url = $this.find('img').attr('src');
	img_name = $this.find('img').attr('data-name');
	ck_status = $("#ajax_album_ck_status").val(); 
	if(ck_status == '1'){ 	//多选
		is_ck = $this.attr('data-ck');
		if(is_ck == 'ck'){ 
			$this.removeClass('ajax_album_ck').attr('data-ck',false); 
			$(".ck_value_"+data_id).remove();
			$(".ck_name_"+data_id).remove();
		}else{ 
			$this.addClass('ajax_album_ck').attr('data-ck','ck'); 
			$(".ck_value_"+data_id).remove();
			$(".ck_name_"+data_id).remove();
			$('.ajax_album_ck_box').append('<div style="display:none;" class="ajax_album_ck_value ck_value_'+data_id+'" data-id="'+data_id+'" data-url="'+img_url+'" data-name="'+img_name+'"/>');
		}
	}else{	//单选 
		$this.addClass('ajax_album_ck').attr('data-ck','ck').siblings().removeClass('ajax_album_ck'); 
		$('.ajax_album_ck_box').html('<div style="display:none;" class="ajax_album_ck_value ck_value_'+data_id+'" data-id="'+data_id+'" data-url="'+img_url+'" data-name="'+img_name+'"/>');
	}
});

 