/*********************************************************************************
* 威客平台 JS 程序
* public/js/weike.js
* author:  我图网
* version: 0626
********************************************************************************/

/** 
* ajax函数
* params: ajaxUrl 请求链接
*/
function ajaxAction(ajaxUrl) {
	
}


/** 
* 报名
* params: taskid 任务id
*		  uid    用户id
*/
function baoming(taskid, uid) {
	var ajaxUrl = "http://weike.ooopic.com/task/baoming/uid/" + uid + "/taskid/" + taskid + "/r/" + Math.random();
	var rand= new Date();
	ajaxUrl = ajaxUrl.replace(/\+/g,"%2B");
	url = encodeURI(ajaxUrl+"&rand="+rand);
	
	 $.ajax({url: url,
		type: 'GET',
		dataType: 'html',
		timeout: 720000,
		success: function(html){
			if ($.trim(html) != '') {
				if ($.trim(html) == 'suc') {
					dialog("","id:sucInfo",320,'auto','', true);
				} else {
					dialog("","id:errInfo",320,'auto','');
				}
			} else {
				dialog("","text:出错了！\n",320,'','', true);
			}
		},
		error: function(html) {
			dialog("","text:出错了：\n" + html,320,'auto','', true);
		}
	});
}


/** 
* 投标
* params: taskid 任务id
*		  uid    用户id
*/
function toubiao(taskid, uid) {
	var url = "http://weike.ooopic.com/zhaobiao/toubiao/taskid/" + taskid + "/uid/" + uid;
	dialog("", "iframe:" + url, 560, 390, '');
}

/** 
* 获取页面高度
*/
function pageHeight(){ 
	if($.browser.msie){ 
		return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight : document.body.clientHeight; 
	}else{ 
		return self.innerHeight; 
	} 
}; 


/** 
* 获取指定id对象的高度
* params: id  对象ID
*/
function elementHeight(id) {
	return $("#" + id).height(); 
}

/** 
* 关闭弹出DIV
*/
function hideBox() {
	$("#floatBox .title span").trigger('click');
	clearTimeout(t);
}


/** 
* 弹出DIV 
* params: title 	弹出框标题
*		  content   弹出框内容
		  width 	框宽
		  height 	框高
		  cssName	框样式
		  autoClose	自动关闭
*/
var dialogFirst=true;
var t = '';
function dialog(title, content, width, height, cssName, autoClose){
	//生成div
	if(dialogFirst == true){//唯一
		var temp_float=new String;
		temp_float="<div id=\"floatBox\" class=\"floatDialog\">"
		temp_float += "<div class=\"floatBox\">";
		temp_float += "<div class=\"title\"><h4></h4><span>×</span></div>";
		temp_float += "<div class=\"content\"></div>";
		temp_float += "</div></div>";
		$("body").append(temp_float);
		dialogFirst = false;
	}
	
	//框头部赋值
	$("#floatBox .title h4").html(title);
	contentType = content.substring(0,content.indexOf(":"));
	content = content.substring(content.indexOf(":") + 1,content.length);
	switch(contentType){
		case "text":
		$("#floatBox .content").html(content);
		break;
		case "id":
		$("#floatBox .content").html($("#" + content + "").html());
		break;
		case "iframe":
  		$("#floatBox .content").html("<iframe src=\""+content+"\" width=\"100%\" height=\""+(parseInt(height)-30)+"px"+"\" scrolling=\"auto\" frameborder=\"0\" marginheight=\"0\" marginwidth=\"0\"></iframe>");
		break;
	}
	
	//关闭
	$("#floatBox .title span").click(function(){
		$("#floatBox").animate({top:($(document).scrollTop() - (height == "auto" ? 300 : parseInt(height)))+"px"},"fast",function(){$(this).hide();}); 
	});

	//显示
	$("#floatBox").show();
	$("#floatBox").attr("class","floatDialog " + cssName);
	$("#floatBox").css({display:"block",left:(($(document).width()) / 2 - (parseInt(width) / 2)) + "px",top:($(document).scrollTop() - (height == "auto" ? 300 : parseInt(height))) + "px", width:width, height:height});
	$("#floatBox").animate({top:((pageHeight() - elementHeight("floatBox"))/2 + $(document).scrollTop()) + "px"},"fast"); 
	
	//自动关闭
	if (autoClose == true) {
		t = setTimeout('hideBox()',2000);
	}	
}


function fenpei(val) {
	if (val == 1) {
		$("#fenpei").show();
	} else {
		$("#fenpei").hide();
	}
}


/**
* 添加奖项
*/
function addJiang() {
	var numbers = ['一','二','三','四','五','六','七','八','九','十'];
	var lis = $("#jiang li");
	var countLi = lis.size();

	if (countLi < 10) {
		var newLi = '<li>' + numbers[countLi] + '等奖 <input type="text" size="10" name="jiang[]"/> 人，平分 <input type="text" size="12" name="jmoney[]"/> 元 <a id="li_'+ (countLi + 1) +'" class="task_form_cancel"></a></li>';
		$("#jiang").append(newLi);
		setCancel(countLi + 1);
		$("#countJiang").val(parseInt($("#countJiang").val()) + 1);
	} else {
		alert("最多添加10个");
	}

}

/**
* 设置取消按钮
* params countLi  li的个数
*/
function setCancel(countLi) {
	for(var i = 1; i <= countLi; i++) {
		$("#li_" + i).html("");
		if (i == countLi) {
			$("#li_" + i).html("取消");
			$("#li_" + i).click(function(event) {
				if (i > 3){
					removeJiang((i - 1));
				}
			});
		}
	}
	
}

/**
* 删除奖项
* params id   要删除的li的id编号
*/
function removeJiang(id) {
	var countLi = $("#jiang li").size();
	$("#li_" + id).parent().remove();
	setCancel(countLi - 1);
	$("#countJiang").val(parseInt($("#countJiang").val()) - 1);
}


/**
* 校验表单
* params 
*/
function checkForm() {
	var classid = $("#classid").val();
	var title = $("#title").val();
	var money = $("#money").val();
	var zhouqi = $("#zhouqi").val();
	var content = KE.html("content2");
	var phone = $("#phone").val();
	var biaomode = $("#biaomode").val();
	
	if (classid == 0) {
		alert('请选择任务分类');
		return false;
	}
	if (title == '') {
		alert('请填写任务标题');
		return false;
	}
	if (title.length > 35) {
		alert('标题最多35个字符');
		return false;
	}
	if (money == 0) {
		alert('请填写任务赏金');
		return false;
	}
	if (zhouqi == 0) {
		alert('请填写任务周期');
		return false;
	}
	if (biaomode == 1) {//多人竞标
		var i = 0;
		var err = 0;
		var allMoney = 0;
		$("#jiang input").each(function(){
			if($(this).val() == '') {
				err ++;
			}
			if ((i % 2) == 1) {//计算总金额
				allMoney += parseInt($(this).val());
			}
			i++;
		});
		if (err > 0 || allMoney != money) {
			alert("请正确分配奖金");
			return false;
		}
	}
	if (content == '') {
		alert('请填写任务描述');
		return false;
	}
	if (phone == '') {
		alert('请填写电话/手机');
		return false;
	}
	$("#addForm").submit();
}


/**
* 判断对象是否未定义
* params variable   待判断对象
*/
function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}

var aid = 1;
var attachexts = new Array();
var attachwh = new Array();

/**
* 删除附件
* params id   附件临时id
*/
function delAttach(id) {
	$("#temp_" + id).parent().remove();
	$("#attach_" + id).remove();
}

/**
* 添加附件
*/
function insertAttach(id) {

	var path = $("#attach_" + id).val();
	var extpos = path.lastIndexOf('.');
	var ext = extpos == -1 ? '' : path.substr(extpos + 1, path.length).toLowerCase();
	var re = new RegExp("(^|\\s|,)" + ext + "($|\\s|,)", "ig");
	var localfile = path.substr(path.replace(/\\/g, '/').lastIndexOf('/') + 1);
	
	if(in_array(ext, ['jpg', 'rar', 'zip', 'doc', 'ppt', 'xls'])) {
		var file_item = '<li>';
		if($.browser.msie && ext == 'jpg') {
			file_item += '<a onclick="insertAttachtext('+aid+');" title="将图片插入">[插入]</a>';
		}
		file_item += '<a onclick="delAttach('+aid+');" id="temp_'+aid+'" imgsrc="'+path+'">[删除]</a> 上传文件名称:' + localfile + '</li>';
		$("#fileList").append(file_item);
		if (ext == 'jpg') {
			var temp_img = '<img src="' + path + '" id="img_'+aid+'" />';
			$("#img_hidden").append(temp_img);
		}
		
		$("#attach_" + id).hide();
		$("#fileInputs").append('<input type="file" id="attach_'+ (aid + 1) +'" name="attach[]" size="30" onchange="insertAttach('+ (aid + 1) +');" />');

		aid++;
	} else {
		alert("非法文件格式");
	}
}

/**
* 插入附件
* params id   附件临时id
*/
function insertAttachtext(id) {
	var wh = {'w' : $('#img_' + id).get(0).offsetWidth, 'h' : $('#img_' + id).get(0).offsetHeight};
	var newWH = setImageSize(wh, 500);
	AddText('<img src="' + $("#temp_" + id).attr("imgsrc") + '" width="' + newWH['w'] + '" height="' + newWH['h'] + '" name="tempImg" />');
}


function setImageSize(wh, toWH) {
	var newWH = [];
	if (wh['w'] > toWH || wh['h'] > toWH){
		if (wh['w'] > wh['h']) {
			newWH['w'] = toWH;
			newWH['h'] = Math.floor(wh['h']/wh['w'] * toWH);
		}else {
			newWH['h'] = toWH;
			newWH['w'] = Math.floor(wh['w']/wh['h'] * toWH);
		}
	}else{
		newWH['w'] = wh['w'];
		newWH['h'] = wh['h'];
	}
	return newWH;
}


/**
* 往编辑器中添加内容
* params txt   待添加内容
*/
function AddText(txt) {
	obj = KE.$("content2");
	KE.util.focus("content2");
	KE.util.selection("content2");
	KE.insertHtml("content2", txt);
}


/**
* 判断元素是否在数组中
* params needle      待判断元素
*		 haystack    数组
*/
function in_array(needle, haystack) {
	if(typeof needle == 'string' || typeof needle == 'number') {
		for(var i in haystack) {
			if(haystack[i] == needle) {
				return true;
			}
		}
	}
	return false;
}


/**
* 付款方式判断
* params kind     付款方式  4、网银 5、支付宝 9、财付通  0、淘宝 1、银行汇款
*/
function setPayKind(kind) {
	$("#paykind").val('');
	$("#payForm").attr("action", 'http://user.ooopic.com/user/paycenter/index.php');
	$("#banks").hide();
	if ($("#wotub").val() != '' && $("#wotub").val() > 0) {
		
		switch (kind) {
			case 4:
				$("#lx").val(4);
				$("#paykind").val('netbank');
				break;
			case 5:
				$("#lx").val(4);
				$("#paykind").val('alipay');
				break;
			case 9:
				$("#lx").val(9);
				break;
			case 1:
				$("#lx").val(0);
				$("#payForm").attr("action", '');
				$("#banks").show();
				break;
			case 0:
				$("#lx").val(0);
				$("#payForm").attr("action", 'http://user.ooopic.com/user/index.php?com=pay#6');
				break;
		}
	}
}


/**
* 付款提交
*/
function submitPay() {
	if ($("#wotub").val() > $("#userwotub").val()) {
		alert("您的余额不足");
	} else {
		var payType = $("input[name=payType]:checked").val();
		//$("#payForm").submit();
	}
}