/***********************/
/*  Code By Nokersang  */
/*   2008-03-27 3:00   */
/***********************/

/* Javascript  area */
var jQobj;			//jQuery object
var checktime;		//check timer
var rolltime;		//roll timer

/* length check dispose */
function showlength(obj){
	var maxlength = obj.attr("ref");
	var alllength = obj[0].value.length;
	var chslength = obj[0].value.replace(/([^\x00-\xff])/ig,'00').length/2;
		//alert(jQobj[0].value.length)
	if(chslength > maxlength){
		obj.parent().find(".showlength")[0].innerHTML = "<span class=\"red\">！（当前长度&nbsp;"+chslength+"&nbsp;汉字）</span>最长&nbsp;<span class=\"blue\">"+maxlength+"</span>&nbsp;个汉字";
		obj.addClass("inputtexterror");
	}else{
		obj.parent().find(".showlength")[0].innerHTML = "（当前长度&nbsp;<span class=\"red\">"+chslength+"</span>&nbsp;汉字）最长&nbsp;"+maxlength+"&nbsp;个汉字";
		obj.removeClass("inputtexterror");
	}
}
/* distribute money show */
function showmoney(){
	var distributemoney = 0;
	var moneyinput;
	$(".nodistribute").removeClass("red");
	$(".distribute").removeClass("inputtexterror");
	$(".distribute").each(function(){
		if(/^[^1-9]+/.test($(this).val())||/([^0-9]+)/.test($(this).val())){
			$(this).val($(this).val().replace(/^[^1-9]+/ig,"").replace(/([^0-9]+)/ig,""));
		}
		moneyinput = $(this).val()==""?0:parseInt($(this).val());
		distributemoney = distributemoney + moneyinput;
	});
	$(".distributed").html(distributemoney.toString()+"元");
	$(".nodistribute").html((parseInt($(".allmoney").html())-distributemoney).toString()+"元");
	if(parseInt($(".nodistribute").html())<0||parseInt($(".nodistribute").html())>parseInt($(".allmoney").html())){
		$(".distribute").addClass("inputtexterror");
		$(".distributed").html($(".distributed").html()+"<br /><span class=\"red\">（任务款分配出错，当前已分配金额超过可分配总金额，请修改！）</span>");
	}
}

/* make daylist html */
function makedaylist(nowday,selectday,prevday,nextday,firstday,showstring,noselect){
	for(i=1;i<32;i++){
		if(i==1){
			firstday.setDate(1);
			for(j=0;j<firstday.getDay()&&i==1;j++){
				showstring = showstring + "<li class=\"overpast\"></li>";
			}
		}
		if(firstday.getTime()<=nowday.getTime()||firstday.getTime()<prevday.getTime()){
			if(firstday.getTime()==selectday.getTime()&&haveselect){
				showstring = showstring + "<li class=\"selectday\">"+i+"</li>";
			}else if(firstday.getTime()==nowday.getTime()&&nowday.getTime()>=prevday.getTime()){
				showstring = showstring + "<li class=\"nowday\">"+i+"</li>";
			}else{
				showstring = showstring + "<li class=\"overpast\">"+i+"</li>";
			}
		}
		if(firstday.getTime()>=prevday.getTime()&&firstday.getTime()>nowday.getTime()&&firstday.getTime()<=nextday.getTime()){
			if(firstday.getTime()==selectday.getTime()){
				showstring = showstring + "<li class=\"selectday\">"+i+"</li>";
			}else{
				showstring = showstring + "<li>"+i+"</li>";
			}
		}
		if(firstday.getTime()>nextday.getTime()){
			showstring = showstring + "<li class=\"overpast\">"+i+"</li>";
		}
		firstday.setTime(firstday.getTime()+86400000);
		if((i+1)!=firstday.getDate()){
			break;
		}
	}
	return showstring;
}

/* reload header */
function reloadheader(username,message,mail,truename,corporation){
	var headerstring = "<ul><li>您好，"+username+"！</li><li style=\"margin-left:0;\"><a href=\"#\">[退出]</a></li>";
	if(message){
		headerstring = headerstring + "<li><a href=\"/my/message_inbox.shtml\" id=\"headermsg\">站内信[<span style=\"color:#F00;\">"+message+"</span>]</a></li>";
	}else{
		headerstring = headerstring + "<li><a href=\"/my/message_inbox.shtml\">站内信</a></li>";
	}
	headerstring = headerstring +"<li><a href=\"/my/favorite_user.shtml\">收藏夹</a></li><li><a href=\"#\" id=\"headerpay\">任务宝</a></li><li>认证：</li>"
	if(mail){
		headerstring = headerstring + "<li style=\"margin-left:3px;\"><img src=\"/images/share/icon/mail.gif\" title=\"您已通过邮箱认证\" /></li>";
	}else{
		headerstring = headerstring + "<li style=\"margin-left:3px;\"><a href=\"#\"><img src=\"/images/share/icon/unmail.gif\" title=\"您还未通过邮箱认证&#13;点击本图标前往邮箱认证页面\" /></a></li>";
	}
	if(truename){
		headerstring = headerstring + "<li style=\"margin-left:6px;\"><img src=\"/images/share/icon/truename.gif\" title=\"您已通过实名认证\" /></li>";
	}else{
		headerstring = headerstring + "<li style=\"margin-left:6px;\"><a href=\"#\"><img src=\"/images/share/icon/untruename.gif\" title=\"您还未通过实名认证&#13;点击本图标前往实名认证页面\" /></a></li>";
	}
	if(corporation){
		headerstring = headerstring + "<li style=\"margin-left:6px;\"><img src=\"/images/share/icon/corporation.gif\" title=\"您已通过企业认证\" /></li></ul>";
	}else{
		headerstring = headerstring + "</ul>";
	}
	$("#headerlogin").html(headerstring);
	if($(".inputvalidate")[0]){
		$(".inputvalidate").html("验证码：<img src=\"http://passport.taskcn.com/user/authimg\" style=\"position:relative;top:2px;\" />&nbsp;<input name=\"\" type=\"text\" class=\"inputtext tel1\" style=\"position:relative;top:-3px;\" maxlength=\"4\" />");
	}
	return false;
}
/* index roll */
function roll(){
	$(".newbuylistshowbox ul:first").animate({top:"-21px"},300,function(){
		$(".newbuylistshowbox ul:first").css("top","0");
		$(".newbuylistshowbox li:first").insertAfter(".newbuylistshowbox li:last");
	});
}
/* date select */
function dateselect(){
	$(".date").next().html("").removeClass("dateiconuplayer");
	$(".dateicon").bind("click", function(){
		$(this).prev().focus().click();
	});
	$(".date").click(function(){
		$(".date").next().html("").removeClass("dateiconuplayer");
		$(".dateicon").bind("click", function(){
			$(this).prev().focus().click();
		});
		$(this).next().addClass("dateiconuplayer");
		$(".date").eq($(".date").index(this)).next().unbind("click");
		var nowday = new Date(2000,0,1,12);
		var selectday = new Date(2000,0,1,12);
		var prevday = new Date(2000,0,1,12);
		var nextday = new Date(2200,0,1,12);
		var firstday = new Date(2000,0,1,12);
		var noselect = true;
		var getnowdate = $(this).attr("nowdate").split("-");
		var getselectdate = $(this).val().split("-");
		var getprevdate = "";
		var getnextdate = "";
		for(x=0;x<$(".date").length;x++){
			if(x<=($(".date").index(this)-1)&&$(".date")[x].value){
				getprevdate = $(".date")[x].value.split("-");
			}
			if(($(".date").length-x-1)>=($(".date").index(this)+1)&&$(".date")[($(".date").length-x-1)].value){
				getnextdate = $(".date")[($(".date").length-x-1)].value.split("-");
			}
		}
		nowday.setDate(getnowdate[2]);
		nowday.setMonth(getnowdate[1]-1);
		nowday.setFullYear(getnowdate[0]);
		if(getselectdate!=""){
			selectday.setDate(getselectdate[2]);
			selectday.setMonth(getselectdate[1]-1);
			selectday.setFullYear(getselectdate[0]);
			haveselect = true;
		}else{
			selectday.setTime(nowday.getTime());
			haveselect = false;
		}
		if(getprevdate!=""){
			prevday.setDate(getprevdate[2]);
			prevday.setMonth(getprevdate[1]-1);
			prevday.setFullYear(getprevdate[0]);
		}else{
			prevday.setTime(nowday.getTime());
		}
		if(getnextdate!=""){
			nextday.setDate(getnextdate[2]);
			nextday.setMonth(getnextdate[1]-1);
			nextday.setFullYear(getnextdate[0]);
		}
		if(haveselect){
			firstday.setTime(selectday.getTime());
		}else{
			firstday.setTime(prevday.getTime());
		}
		//alert(nowday+"/"+selectday+"/"+prevday+"/"+nextday+"/"+firstday);
		var showstring = "<div class=\"dateselect\"><div class=\"datetitleword\">请选择日期</div><div class=\"datemonth\"><div class=\"nextmonth\" title=\"下一个月\"></div><div class=\"prevmonth\" title=\"上一个月\"></div><span class=\"showyear\">"+firstday.getFullYear()+"</span>年<span class=\"showmonth\">"+(firstday.getMonth()+1)+"</span>月</div><ul class=\"dateweek\"><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class=\"datelist\">";
		showstring = makedaylist(nowday,selectday,prevday,nextday,firstday,showstring,noselect)+"</ul><div class=\"dateclose\">关闭窗口</div></div>"
		$(this).next().html(showstring);		//首次日历输出
		showstring = "";
		mousebind();

		$(".prevmonth").click(function(){
			firstday.setTime(firstday.getTime()-86400000);
			firstday.setDate(1);
			firstday.setTime(firstday.getTime()-86400000);
			$(".showyear").html(firstday.getFullYear());
			$(".showmonth").html((firstday.getMonth()+1));
			$(".datelist").html(makedaylist(nowday,selectday,prevday,nextday,firstday,showstring,noselect));		//上月日历输出
			mousebind();
		});
		$(".nextmonth").click(function(){
			$(".showyear").html(firstday.getFullYear());
			$(".showmonth").html((firstday.getMonth()+1));
			$(".datelist").html(makedaylist(nowday,selectday,prevday,nextday,firstday,showstring,noselect));		//下月日历输出
			mousebind();
		});

		$(".dateselect").bind("click", function(){
			$(this).parent().prev().focus();
		});
		$(".dateclose").click(function(){
			$(".dateicon").html("");
			$(".dateicon").bind("click", function(){
				$(this).prev().focus().click();
			});
		});
		$(".dateclose").hover(function(){$(this).addClass("datecloseactive");
		},function(){$(this).removeClass("datecloseactive");});
		function mousebind(){
			$(".datelist li[class!='overpast']").hover(function(){$(this).addClass("mouseoverday");
			},function(){$(this).removeClass("mouseoverday");}).click(function(){
				$(".dateselect").parent().prev().val($(".showyear").html()+"-"+$(".showmonth").html()+"-"+$(this).html());
				$(".dateclose").click();
			});
		}
	});
}

/* jQuery area */
$(document).ready(function(){

	/* index need */
	$(".searchinput").click(function(){
		$(this).addClass("searchinputbegin");
	});
	$(".searchbutton").hover(function(){$(this).addClass("searchbuttonbegin");
	},function(){$(this).removeClass("searchbuttonbegin");
	});
	$(".searchbar li").hover(function(){$(this).addClass("searchhover");
	},function(){$(this).removeClass("searchhover");
	}).click(function(){
		$(".searchbar li").removeClass("searchactive");
		$(this).addClass("searchactive");
		$(".searchclass").val($(".searchbar li").index(this));
	});
	$(".taskclasslabel a").mouseover(function(){
		$(".taskclasslabel a").removeClass("label_active");
		$(this).addClass("label_active");
		$(".classlist").hide();
		$(".classlist").eq($(".taskclasslabel a").index(this)).show();
	});
	$(".taskHotellabel a").mouseover(function(){
		$(".taskHotellabel a").removeClass("Hotel_active");
		$(this).addClass("Hotel_active");
		$(".Hotellist").hide();
		$(".Hotellist").eq($(".taskHotellabel a").index(this)).show();
	});
	$(".newbuylist").mouseover(function(){
		rolltime = window.clearInterval(rolltime);
	}).mouseout(function(){
		rolltime = window.setInterval("roll()",2000);
	});

	/*bbt mouseover*/
	$(".bt").hover(function(){$(this).addClass("btover");
	},function(){$(this).removeClass("btover");
	});

	/*leftmenu control*/
	$(".itemtitleopen").click(function(){
		$(this).toggleClass("itemtitleclose");
		$(this).parent().parent().toggleClass("itemhide");
		return false;
	});
	$("#m05,#m06,#m07,#m08").click();

	/*fb control*/
	if($("#moretaskoption")[0]){
		$("#moretaskoption").click(function(){
			$(".taskmode,.cjrestrict,.taskendhide").toggleClass("hide");
		});
		if($("#moretaskoption")[0].checked){
			$(".taskmode,.cjrestrict,.taskendhide").toggleClass("hide");
		}
		$(".restrict0").click(function(){
			$(".cjrestrict_credit,.cjrestrict_appoint").hide();
		});
		if($(".restrict0")[0].checked){
			$(".restrict0").click();
		}
		$(".restrict1").click(function(){
			$(".cjrestrict_appoint").hide();
			$(".cjrestrict_credit").show();
		});
		if($(".restrict1")[0].checked){
			$(".restrict1").click();
		}
		$(".restrict2").click(function(){
			$(".cjrestrict_credit").hide();
			$(".cjrestrict_appoint").show();
		});
		if($(".restrict2")[0].checked){
			$(".restrict2").click();
		}
	}
	if($("#taskpayradio")[0]){
		$("#directpayradio").click(function(){
			$("#taskhideselect")[0].value="0";
			$("#taskhideselect")[0].disabled=true;
		});
		$("#taskpayradio").click(function(){
			$("#taskhideselect")[0].disabled=false;
		});
		if($("#directpayradio")[0]){
			if($("#directpayradio")[0].checked){
				$("#directpayradio").click();
			}
		}
	}

	/*submit idcard*/
	$("#selectonline").click(function(){
		if($("#selectonline")[0].checked){
			$("#mailidcard,#faxidcard").hide();
			$("#unlineidcard").show();
		}
	})
	$("#selectmail").click(function(){
		if($("#selectmail")[0].checked){
			$("#faxidcard,#unlineidcard").hide();
			$("#mailidcard").show();
		}
	})
	$("#selectfax").click(function(){
		if($("#selectfax")[0].checked){
			$("#unlineidcard,#mailidcard").hide();
			$("#faxidcard").show();
		}
	})
	$("#selectonline").click();

	/*input onfocus*/
	$(".inputtext").focus(function(){
		$(this).addClass("inputtextfocus");
	}).blur(function(){
		$(this).removeClass("inputtextfocus");
	});
	$(".inputtextarea").focus(function(){
		$(this).addClass("inputtextareafocus");
	}).blur(function(){
		$(this).removeClass("inputtextareafocus");
	});

	/* length check */
	$(".checklength").each(function(){
		jQobj = $(this);
		showlength(jQobj);
	});
	$(".checklength").focus(function(){
		jQobj = $(this);
		checktime = window.setInterval("showlength(jQobj)",200);
	}).blur(function(){
		checktime = window.clearInterval(checktime);
	});

	/* active channel */
	if(typeof(topchannel)!='undefined'){
	$(topchannel).addClass("channelactive");
	}
	/* active item */
	if(typeof(menuitme)!='undefined'){
	$(menuitme).toggleClass("activeitem").parent().parent().removeClass("itemhide").children().children(".itemtitleopen").removeClass("itemtitleclose");
	}

	$(".morelink,.lesslink").click(function(){
		$(".morelink,.lesslink").toggle();
		$(".joinuserlistnob").slideToggle();
		return false;
	});
	$(".distribute").focus(function(){
		checktime = window.setInterval("showmoney()",200);
	}).blur(function(){
		checktime = window.clearInterval(checktime);
	});
	$(".step_bt").click(function(){
		switch($(".step_bt").index(this)){
			case 0:
			if($(".radio0")[0].checked||$(".radio0")[1].checked){
				$(".fabushowbox").hide();
				$(".stepshowbox").hide();
				if($(".radio0")[0].checked){$(".stepshowbox").eq(1).slideDown(600);}else{$(".stepshowbox").eq(4).slideDown(600);}
			}else{
				alert("请选择：您是想公开征集工作者来完成您的需求，还是指定工作者来完成您的需求？");
			}
			break
			case 1:
			$(".stepshowbox").hide();
			$(".fabushowbox").slideDown(600,function(){
				$(".stepshowbox").eq(0).slideDown(600);
			});
			break
			case 2:
			if($(".radio1")[0].checked||$(".radio1")[1].checked){
				$(".stepshowbox").hide();
				if($(".radio1")[0].checked){$(".stepshowbox").eq(2).slideDown(600);}else{$(".stepshowbox").eq(3).slideDown(600);}
			}else{
				alert("请选择：威客任务？还是招标任务？");
			}
			break
			case 3:
			$(".stepshowbox").hide();
			$(".stepshowbox").eq(1).slideDown(600);
			break
			case 4:
			$(".stepshowbox").hide();
			$(".stepshowbox").eq(1).slideDown(600);
			break
			case 5:
			$(".stepshowbox").hide();
			$(".fabushowbox").slideDown(600,function(){
				$(".stepshowbox").eq(0).slideDown(600);
			});
			break
		}
	})
	if($(".floatservice").html()){
		$(window).scroll(function(){
			if(document.documentElement.scrollTop){
				$(".floatservice").css("top",document.documentElement.scrollTop+"px"); 
			}
		}); 
	}
	if($(".distribute")[0]){
		showmoney();
	}
	dateselect();

	if($(".newbuylistshowbox").html()){
		rolltime = window.setInterval("roll()",2000);
	}
});