var imServer = getParameter("imserver");
imServer = getImServer(imServer);
var errorid = getParameter("error");
if (errorid == -1) {
	var paramStr = "{title:'提示',cat:0,content:'图片格式错误，请重新选择！'}";
	var params = "{command:9006,params:" + paramStr + "}";
	sendCommand(params);
}
var lvid = getParameter("lvid");
var sign = getParameter("sign");
var groupId = getParameter("groupid");
if (!groupId) {
	groupId = -1;
}
var tag = getParameter("tag");
var src = getParameter("src");
var rootPath = groupId == -1 ? "P" + lvid : "G" + groupId;
var headUrl = imServer + "head_tmp/" + rootPath + "/" + src + "?rand=" + new Date().getTime();
var classicsHead = '';
var selectArr = [40, 5, 163, 125];
var x1 = selectArr[0],
	y1 = selectArr[1],
	x2 = selectArr[2],
	y2 = selectArr[3];
var nowHeadUrl;
jQuery(function($) {
	$("#chageBnt").click(function() {
		openSelectWindow();
	});
	$("#nativeBnt").click(function() {
		openSelectWindow();
	});
	var classicsHeadPage = $("#classics_head_page");
	var customHeadPage = $("#custom_head_page");
	classicsHeadPage.click(function() {
		$("#yulan100").removeAttr("style");
		$("#yulan60").removeAttr("style");
		$("#custom_head_zone").hide();
		$("#custom_head_init_zone").hide();
		$("#classics_head_zone").show();
		$('.mainL').css('height','303px');
		classicsHeadPage.parent().addClass("head-on");
		customHeadPage.parent().removeClass("head-on");
		customHeadPage.parent().parent().addClass("classicsTop").removeClass("changeTop");
		tag = 2;
		initClassicsHead();
		$("#buttonDiv").show();
	});
	customHeadPage.click(function() {
		$("#classics_head_zone").hide();
		$("#custom_head_zone").hide();
		$('.mainL').css('height','340px');
		$("#custom_head_init_zone").show();
		customHeadPage.parent().addClass("head-on");
		classicsHeadPage.parent().removeClass("head-on");
		classicsHeadPage.parent().parent().addClass("changeTop").removeClass("classicsTop");
		initHeadUpload();
		$("#buttonDiv").hide();
	});
	if (!tag) {
		customHeadPage.click();
	} else if (tag != 2) {
		$("#custom_head_init_zone").hide();
		$("#classics_head_zone").hide();
		$("#custom_head_zone").show();
		customHeadPage.parent().addClass("head-on");
		classicsHeadPage.parent().removeClass("head-on");
		classicsHeadPage.parent().parent().addClass("changeTop").removeClass("classicsTop");
		initCustomHead();
		$("#buttonDiv").show();
	} else {
		classicsHeadPage.click();
	}
});

/**
 * 初始化头像上传界面
 */
function initHeadUpload() {
	loadSrcIcon();
	setTimeout(function() {
		$("#loading-popup").hide();
		$("#main").show();
	}, 1000);
};

/**
 * 初始化自定义头像
 */
function initCustomHead() {
	$("#target").attr("src", headUrl);
	$("#yulan60").attr("src", headUrl);
	$("#yulan100").attr("src", headUrl);
	$('.mainL').css({'height':'303px','overflow':'hidden'});
	$(".PopupQueding").hide();
	$("#alertpopupSureBut").click(function() {
		$(".PopupQueding").hide();
	});
	selectArr = countSize();
	var jcrop_api, boundx, boundy;
	var $preview = $('#preview-pane'),
		$pcnt = $('#preview-pane .imgsize'),
		$pimg = $('#preview-pane .imgsize img'),
		xsize = $pcnt.width(),
		ysize = $pcnt.height();
	var $preview2 = $('#preview-pane2'),
		$pcnt2 = $('#preview-pane2 .imgsize'),
		$pimg2 = $('#preview-pane2 .imgsize img'),
		xsize2 = $pcnt2.width(),
		ysize2 = $pcnt2.height();
	$('#target').Jcrop({
		aspectRatio: 1,
		boxHeight: 195,
		boxWidth: 300,
		allowSelect: false,
		onRelease: clearCoords,
		setSelect: countSize(),
		keySupport: false,
		minSize: [32, 32],
		onChange: updatePreview,
		onSelect: updatePreview
	}, function() {
		var bounds = this.getBounds();
		boundx = bounds[0];
		boundy = bounds[1];
		jcrop_api = this;
		updatePreview(this.tellSelect());
	});

	function countSize() {
		var width = $("#target").width();
		var height = $(".nativeHead").height();
		if (parseInt(width) > parseInt(height)) {
			selectArr = [(width - height) / 2, 5, height - 10, height - 10];
		} else {
			selectArr = [(height - width) / 2, 5, width - 10, width - 10];
		}
		x1 = selectArr[0];
		y1 = selectArr[1];
		x2 = selectArr[2];
		y2 = selectArr[3];
		return selectArr;
	}

	function updatePreview(c) {
		showCoords(c);
		if (parseInt(c.w) > 0) {
			var rx = xsize / c.w;
			var ry = ysize / c.h;
			$pimg.css({
				width: Math.round(rx * boundx) + 'px',
				height: Math.round(ry * boundy) + 'px',
				marginLeft: '-' + Math.round(rx * c.x) + 'px',
				marginTop: '-' + Math.round(ry * c.y) + 'px'
			});
			var rx2 = xsize2 / c.w;
			var ry2 = ysize2 / c.h;
			$pimg2.css({
				width: Math.round(rx2 * boundx) + 'px',
				height: Math.round(ry2 * boundy) + 'px',
				marginLeft: '-' + Math.round(rx2 * c.x) + 'px',
				marginTop: '-' + Math.round(ry2 * c.y) + 'px'
			});
		}
	};
	setTimeout(function() {
		$("#loading-popup").hide();
		$("#main").show();
	}, 1000)
}

/**
 * 初始化经典头像
 */
function initClassicsHead() {
	$(".PopupQueding").hide();
	$("#alertpopupSureBut").click(function() {
		$(".PopupQueding").hide();
	});
	$("#classics_ul li img").each(function(index, imgObj) {
		$(imgObj).click(function() {
			var srcall = $(this).attr("src");
			srcall = srcall.substring(srcall.indexOf("HEAD-"));
			var classics100Head = "";
			if (srcall.indexOf(".jpg") > 0) {
				classicsHead = srcall.replace(/32\.jpg$/g, '64.jpg');
				classics100Head = srcall.replace(/32\.jpg$/g, '96.jpg');
			} else if (srcall.indexOf(".png") > 0) {
				classicsHead = srcall.replace(/32\.png$/g, '64.png');
				classics100Head = srcall.replace(/32\.png$/g, '96.png');
			}
			$("#yulan100").attr("src", requestUrlMap['classicsHeadPrefix']['url'] + classics100Head);
			$("#yulan60").attr("src", requestUrlMap['classicsHeadPrefix']['url'] + classicsHead);
		})
	});
	loadSrcIcon();
}

function showCoords(c) {
	x1 = c.x;
	y1 = c.y;
	x2 = c.x2;
	y2 = c.y2;
	w = c.w;
	h = c.h;
};

function clearCoords() {
	$('#coords input').val('');
};

/**
 * 提交截图
 */
function tiJiao() {
	$('.savemask').show();
	var params = '{"command":9012,"params":{"lvid":' + lvid + '}}';
	var result = $.parseJSON(sendCommand(params));
	if (result['errorid'] == 0 && result['status'] != 0) {
		if (!tag) {
			closeDialogBox();
		}
		if (tag != 2) {
			submitCustomHead();
		} else {
			submitClassicsHead();
		}
	}
}

/**
 * 加载源头像
 */
function loadSrcIcon() {
	if (nowHeadUrl) {
		$("#yulan100").attr("src", transferHeadSize(nowHeadUrl, 96));
		$("#yulan60").attr("src", transferHeadSize(nowHeadUrl, 64));
		return;
	}
	var requestUrl, params, resultTitle, iconTitle;
	if (groupId != -1) {
		params = {
			'lvid': lvid,
			'sign': sign,
			'groupid': groupId
		}
		requestUrl = requestUrlMap['cloudGroupInfo']['url'];
		resultTitle = "groupinfo";
		iconTitle = "iconurl";
	} else {
		params = {
			'lvid': lvid,
			'sign': sign
		}
		requestUrl = requestUrlMap['userInfo']['url'];
		resultTitle = "userinfo";
		iconTitle = "headurl";
	}
	$.getJSON(requestUrl, params, function(data) {
		if (!data || data['errorid'] != 0) {
			return;
		}
		var info = data[resultTitle];
		var iconUrl = info[iconTitle];
		if (iconUrl) {
			nowHeadUrl = iconUrl;
			$("#yulan100").attr("src", transferHeadSize(nowHeadUrl, 96));
			$("#yulan60").attr("src", transferHeadSize(nowHeadUrl, 64));
		}
	})
}

/**
 * 提交自定义头像
 */
function submitCustomHead() {

	var params = {
		'lvid': lvid,
		'sign': sign,
		'groupid': groupId,
		'src': src,
		'x1': parseInt(x1),
		'x2': parseInt(x2),
		'y1': parseInt(y1),
		'y2': parseInt(y2)
	};
	$.getJSON(requestUrlMap['cutImage']['url'], params, function(data) {
		if (data.errorid == 0) {
			var headType = groupId == -1 ? 1 : 2;
			sendCommand("{command:9003, params:{headtype:" + headType + ",lvid:" + lvid + ",groupid:" + groupId + "}}");
			closeDialogBox();
		} else {
			$("#alertMess").html(data.errmsg);
			$(".PopupQueding").show();
		}
	});
}

/**
 * 提交保存经典头像
 */
function submitClassicsHead() {
	if (!classicsHead) {
		closeDialogBox();
		return;
	}
	var params = {
		'lvid': lvid,
		'sign': sign,
		'groupid': groupId,
		'head': "classic/" + classicsHead
	};
	$.getJSON(requestUrlMap['classicsHead']['url'], params, function(data) {
		if (data.errorid == 0) {
			var headType = groupId == -1 ? 1 : 2;
			sendCommand("{command:9003, params:{headtype:" + headType + ",lvid:" + lvid + ",groupid:" + groupId + "}}");
			closeDialogBox();
		} else {
			$("#alertMess").html(data.errmsg);
			$(".PopupQueding").show();
		}
	});
}

/**
 * 关闭对话框
 */
function closeDialogBox() {
	sendCommand("{command:9001, params:{lvid:" + lvid + "}}");
}

//打开本地图片选择框
function openSelectWindow() {
	try {
		var result = sendCommand('{command:9019, params:{lvid:' + lvid + ', groupid:' + groupId + ', clientkey: "' + clientKey + '" }}');
		var resultObj = $.parseJSON(result);
		if (resultObj['errorid'] == 1) {
			$(".pictureBox").hide();
			$("#loading-popup p").html("正在上传...");
			$("#loading-popup").show();
		}
	} catch (e) {}
}

//拉起头像自拍
function userphofun() {
	sendCommand('{command:9011, params:{lvid:' + lvid + ', groupid:' + groupId + ', clientkey: "' + clientKey + '" }}');
}