﻿addLoadEvent(navEvent);
addLoadEvent(loadSearchBar);

var post_handle = ''; //POST ACTION 中省略的文件名
var site_domain = 'jianzhi.3job.com'; //当前站点域名
var system_domain = '3job.com'; //主站域名
var vedio_url = 'http://vedio.3job.com/vedio'; //视频请求URL根目录

//处理子站域名跳转
function domain_referer(){
	if( -1 == location.href.indexOf(site_domain) )
		location.href = location.href.replace(document.domain,site_domain);
}
domain_referer();

function navsLink(event,model,root_path,url,islogin,iscompany,isperson){
	eval('var islogin='+islogin);
	eval('var iscompany='+iscompany);
	eval('var isperson='+isperson);
	if( url.indexOf('desktop/company') != -1 ){
		//是否登录
		if( !islogin ){
			location.href = root_path+'login';
			return;
		}
		//会员类型是否正确
		if( iscompany )
			window.location.href = url;
		else
			alert('您目前的身份为人才会员，只有单位会员才可以使用该功能。');
	}else{
		//是否登录
		if( !islogin ){
			location.href = root_path+'login';
			return;
		}
		//会员类型是否正确
		if( isperson )
			window.location.href = url;
		else
			alert('您目前的身份为单位会员，只有人才会员才可以使用该功能。');
	}
}

function loadSearchType(rPath,type){
	var form = $I('form_qsearch');
	if( !form ) return;
	if( type == 'position' ){
		//$I('qsearch_by_1').checked = true;
		form.action = rPath+'position';
		$I('qsearch_by_1').value = '-- 找工作 --';
		$I('qsearch_by').value = 'position';
	}else if( type == 'resume' ){
		//$I('qsearch_by_2').checked = true;
		form.action = rPath+'resume';
		$I('qsearch_by_1').value = '-- 找人才 --';
		$I('qsearch_by').value = 'resume';
	}
}

function loadSearchBar(){
	var box = $I('search_key');
	var tip = ' 请输入要查询职位的核心关键词...';
	if( !box ) return;
	if( box.value == '' ){box.value=tip; box.style.color='#aaa';}
	box.onfocus = function(){
		if( this.value == tip ) this.value = '';
		this.style.color = '#000';
	}
	box.onblur=function(){
		if( this.value.trim() == '' ){this.style.color='#aaa'; this.value=tip;}
	}
	$I('form_qsearch').onsubmit = function(){if(box.value==tip)box.value='';}
}

function quickSearch(key){
	$I('search_key').value = key;
	$I('form_qsearch').submit();
}

function searchHelpBox(event){
	if( $I('qsearch_help_box') ){$('#qsearch_help_box').slideToggle("fast");return;}
	var htm = '<div id="qsearch_help_box" style="position:absolute;display:none;width:180px;height:340px;border:4px solid #ffbdbb;background:#fff;padding:12px 8px 8px 8px;">';
	htm    +=   '<a href="#" onclick="$(\'#qsearch_help_box\').slideUp(\'fast\');return false;" style="font-size:14px;font-weight:bold;color:#f00;position:absolute;margin-top:-10px;margin-left:170px;">×</a>';
	htm    +=   '<h1 style="font:bold 14px;color:#000;padding:0; margin:16px 0;text-align:center">搜索小技巧</h1>';
	htm    +=   '<strong>一、关键词技巧</strong>';
	htm    +=   '<p style="margin:6px; line-height:1.8em;">◇ 搜索时请尽量用简短的核心关键词，如您搜索“销售经理”只需输入“销售”二字即可；<br />这样您就可以获得与“销售”相关的所有信息，如“销售总监、销售经理、销售工程师……等”</p>';
	htm    +=   '<strong>二、查询技巧</strong>';
	htm    +=   '<p style="margin:6px; line-height:1.8em;">◇ 请在搜索时尽量尝试多次用不同的关键词搜索，如您搜索“文员”，您可搜索“文员”后再搜索“文秘”或“文职”则可得到不同的搜索结果！</p>';
	htm    +=   '<h6 style="font:bold 12px;color:#000;padding:0;margin:8px 0;text-align:center;"><a href="#" onclick="$(\'#qsearch_help_box\').slideUp(\'fast\');return false;">【关闭】</a></h6>';
	htm    += '</div>';
	$(document.body).append(htm);
	event = event || window.event;
	var box = $I('qsearch_help_box');
	box.style.left = event.clientX - 140 + 'px';
	box.style.top  = event.clientY + document.documentElement.scrollTop + 20 + 'px';
	$(box).slideDown("fast");
}

function navEvent(){
	if( !$I('nav') ) return;
	var navs = $I('nav').getElementsByTagName('LI');
	for(var i=0; i<navs.length; i++){
		if( navs[i].className != 'l' && navs[i].className != 'line' && navs[i].className != 'c' && navs[i].className != 'lc' ){
			navs[i].onmouseover = function(){this.className += ' c';}
			navs[i].onmouseout  = function(){this.className = this.className=='r c'?'r':'';}
		}else if( navs[i].className == 'l' ){
			navs[i].onmouseover = function(){this.className = 'lc';}
			navs[i].onmouseout  = function(){this.className = 'l';}
		}
	}
}

function loading(flag,message,second){
	if( !message ) var message = '正在与服务器通信...';
	if( flag == 1 ){
		if( $I('loading') ) return;
		var layer = document.createElement('DIV');
		layer.id = 'loading';
		layer.className = 'loading';
		layer.innerHTML = message;
		document.body.appendChild(layer);
	}else if( flag == 0 ){
		function remove(){if($I('loading'))document.body.removeChild($I('loading'));}
		var layer = $I('loading');
		if( second != null && typeof(second)=='number' )
			window.setTimeout(remove);
		else
			remove();
	}
}

function ifrmLoad(boxid,frmid,frmurl,width,height,scrolling,ext,time){
	var w = null==width?'width="360"':width; if( typeof(w)=='number' ) w = 'width="'+w+'"';
	var h = null==height?'height="22"':height; if( typeof(w)=='number' ) h = 'height="'+h+'"';
	var s = scrolling || 'no'; ext = ext || ''; time = time || 100;
	function tfunc(){
		$I(boxid).innerHTML = '<iframe id="'+frmid+'" '+ext+' name="'+frmid+'" src="'+frmurl+'" '+w+' '+h+' scrolling="'+s+'" frameborder="0"></iframe>';
	}
	window.setTimeout(tfunc,time);
}

function upload(){
	if($I('file_data').value.trim() == ''){alert('请选择要上传的文件'); return false;}
	window.parent.loading(1,'请稍候 文件上传中...'); return true;
}

function imgLoad(img,loading,model){
	loading.style.display = 'none';
	$(img).slideDown('slow',tfunc);
	function tfunc(){
		var width = 120-2;
		var height = 150-2;
		if( model == 'banner' ) width = 400;
		else if( model == 'logo' ) width = 400;
		var w = img.offsetWidth;
		var h = img.offsetHeight;
		if( w/h > width/height )
			img.width = w>width?width:w;
		else
			img.height = h>height?height:h;
	}
}

function callback(model,flag,message,field,fname,furl){
	loading(0);
	var fpath = furl.replace(furl.substr(furl.indexOf('upload/')),'images/');
	switch( flag ){
		case 'error':
			alert(message);
			history.back();
			break;
		case 'ready':
			if( fname && furl ){
				var htm = '<span id="view_loading_'+model+'"><img src="'+fpath+'loading.gif" border="0" align="absmiddle" /> 图片载入中...</span>';
				htm += '<a href="'+furl+'" target="_blank" title="点击查看原图"><img src="'+furl+'" border="0" style="display:none;" onload="imgLoad(this,$I(\'view_loading_'+model+'\'),\''+model+'\');" /></a>';
				$I('view_box_'+model).innerHTML = htm;
				$I('view_box_'+model).style.display = 'block';
				$I(field).value = fname;
			}else{
				$I(field).value = '';
				$('#view_box_'+model).slideUp('fast');
			}
			break;
	}
}

//检测视频面试请求
var vedio_timer;
function vedioCheck(islogin,us_username,us_group,us_name,us_jobs){
	if( !islogin ) return;
	function v_c(){
		loadScript(vedio_url+'/control?model=check_request&u='+us_username+'&g='+us_group+'&n='+us_name+'&j='+us_jobs+'&'+Math.random());
		//window.open(vedio_url+'/control?model=check_request&u='+us_username+'&g='+us_group+'&n='+us_name+'&j='+us_jobs);
	}
	vedio_timer = window.setInterval(v_c,5000);
}

//弹出视频请求提示
function vedioRequest(isperson,iscompany,channel,uid,uid2,aid,aid2){
	if( window.window_vedio ) return;
	var w = '', g = '';
	if( isperson ){
		w = '邀请您';
		g = 'Candidate';
	}
	else if( iscompany ){
		w = '申请';
		g = 'Enterprise';
	}
	var htm = '<div id="vedio_request" class="vedio_request" style="display:none;">';
	htm +=      '<h1><span><a href="#shutdown" onclick="vedioAlert(true);$(\'#vedio_request\').slideUp(\'slow\',function(){document.body.removeChild($I(\'vedio_request\'))});return false;">关闭</a></span></h1>';
	htm +=      '<p>3JOB远程现场招聘提示</p>';
	htm +=      '<p><a href="#" onclick="vedioAlert(true);window.open(\''+vedio_url+'/talking/?status=getrequest&vc='+channel+'&group='+g+'&uid='+uid+'&uid2='+uid2+'&aid='+aid+'&aid2='+escape(aid2)+'\',\'_blank\',\'width=786,height=583\');$(\'#vedio_request\').slideUp(\'slow\',function(){document.body.removeChild($I(\'vedio_request\'))});return false;" style="color:#1f336b;font-weight:bold;" title="'+aid2+'">'+aid2.substr(0,15)+'</a><br />'+w+'进行远程现场面试</p>';
	htm +=      '<p><a href="#" onclick="vedioAlert(true);window.open(\''+vedio_url+'/talking/?status=getrequest&vc='+channel+'&group='+g+'&uid='+uid+'&uid2='+uid2+'&aid='+aid+'&aid2='+escape(aid2)+'\',\'_blank\',\'width=786,height=583\');$(\'#vedio_request\').slideUp(\'slow\',function(){document.body.removeChild($I(\'vedio_request\'))});return false;" style="color:#f00;font-weight:bold;">进入查看</a></p>';
	htm +=    '</div>';
	$(document.body).append(htm);
	if( window.vedio ){
		if( !window.vedio.isOwn(aid) ){
			$('#vedio_request').slideDown('slow',function(){window.vedio.push(aid);});
			vedioAlert();
		}
	}else{
		$('#vedio_request').slideDown('slow',function(){window.vedio=new Array(aid)});
		vedioAlert();
	}
}

//请求提示
var vediotimer = 0;
function vedioAlert(clear){
	if( clear ){
		window.clearInterval(vediotimer);
		document.title=window.doctitle;
		return;
	}
	if( !vediotimer ) window.doctitle = document.title;
	var flag = true;
	function t(){
		document.title = flag ? '☆您有新的面试请求！☆' : '■■■■■■■■■■■■■■■';
		flag = !flag;
	}
	vediotimer = window.setInterval(t,500);
}

//打开视频窗口
function startTalk(islogin,iscompany,isperson,uid,uid2,aid,aid2,group){
	if( typeof islogin == 'string' ) eval('islogin='+islogin+';');
	if( typeof iscompany == 'string' ) eval('iscompany='+iscompany+';');
	if( typeof isperson == 'string' ) eval('isperson='+isperson+';');
	if( !islogin ){alert('您还没有登录，请先登录再使用远程面试功能。'); try{$I('login_username').focus();}catch(e){}; return;}
	if( iscompany ){alert('您目前的身份为企业会员，无权进行职位申请。'); return;}
	uid2 = escape(uid2); aid2 = escape(aid2);
	window.open(vedio_url+"/talking?status=request&group="+group+"&uid="+uid+"&uid2="+uid2+"&aid="+aid+"&aid2="+aid2,"_blank","width=786,height=583");
}

/*functions for popupDiv begin*/
function checkSelectItem(sObj,fname,limit){
	function setValue(){
		var values = '', shows = '', t1='', t2 = '';
		$(':checkbox[@name=select_item_'+fname+']:checked').each(function(){
			values += (t1+this.value); t1=',';
			shows  += (t2+this.value); t2=' + ';
		});//构建Value
		$I(fname).value = values;//更新Value到控件
		if( shows.trim() == '' ){
			$I(fname+'_1').value = $('#'+fname).data('title');
			$I(fname+'_1').title = $('#'+fname).data('title');
		}else{
			showNum = Math.floor( $I(fname+'_1').offsetWidth/12 );
			$I(fname+'_1').value = ' ' + ( shows.len()/2>showNum ? shows.substr(0,showNum)+'...' : shows );
			$I(fname+'_1').title = shows;
		}//显示到按钮
	}
	sObj.blur();//虚线框
	var items = $I(fname).value.split(',');
	if( $I(fname).value.trim() == '' ) items=Array();
	limit = limit || 3;
	if( !sObj.checked ){
		sObj.parentNode.style.color = '#000';//文字变色
		setValue();
	}else{
		if(items.length >= limit){
			alert('最多可以选择 '+limit+' 项！');
			sObj.checked = false;
		}else{
			sObj.parentNode.style.color = '#f60';//文字变色
			setValue();
			if(limit == 1){$('#dialog_'+fname).slideToggle("fast",function(){$('#mask_div').slideToggle('fast');$I(fname+'_1').focus();});selectDisplay(fname);}//如果只能选择一项->自动关闭
		}
	}
	//if( $I(fname).value.trim() == '' )$I(fname+'_1').value = $('#'+fname).data('title');
}

function selectDisplay(fname){
	if(! ($.browser.msie && $.browser.version=='6.0')  ) return;
	$('select').each(function(){
		this.style.display=(!$I('dialog_'+fname) || $I('dialog_'+fname).style.display=='none')?'none':'';
	}); 	
}

function popupClose(fname){
	selectDisplay(fname);
	$('#dialog_'+fname).slideUp('fast',function(){$('#mask_div').slideToggle('fast');$I(fname+'_1').focus();});
}

function popupDiv(event,fname,array,limit,title,width){
	limit = limit || 1;
	title = title || $I(fname+'_1').value;
	width = width || 520;
	$('#'+fname).data('title',title);//缓存当前Title
	$('#'+fname).data('value',$I(fname).value);//缓存当前Value
	selectDisplay(fname);//隐藏select控件
	var vals = $I(fname).value.split(',');
	if( $I('dialog_'+fname) ){$('#dialog_'+fname).slideToggle("fast",function(){$('#mask_div').slideToggle('fast');$I(fname+'_1').focus();}); return;}//如果popupDIV已创建->显示
	//popupDIV
	var htm = '<div id="dialog_'+fname+'" class="popup" style="width:'+width+'px;"><div class="top"><div class="tl"></div></div>';
	htm    +=   '<div class="title"><span>[ <a href="#" onclick="popupClose(\''+fname+'\');return false" onfocus="this.blur();">确定</a> ] [ <a href="#" onfocus="this.blur();" onclick="popupClose(\''+fname+'\');return false">关闭</a> ]</span>'+title+'</div>';
	htm    +=   '<div class="content">';
	htm    +=   '<div class="desc">您最多可以选择<strong>'+limit+'</strong>项</div>';
	htm    +=     '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
	for( var i=0; i<array.length; i+=2){
		var checkItA = '', colorA = ''; if( vals.isOwn(array[i][1]) ){ checkItA = ' checked="checked"'; colorA = ' style="color:#f60;"';};
		var checkItB = '', colorB = ''; if( array[i+1] && vals.isOwn(array[i+1][1]) ){ checkItB = ' checked="checked"';  colorB = ' style="color:#f60;"';}
		htm    += '<tr><td width="50%"><label for="select_item_'+fname+'_'+i+'"'+colorA+'><input onclick="checkSelectItem(this,\''+fname+'\','+limit+')" class="checkbox" type="checkbox" name="select_item_'+fname+'" id="select_item_'+fname+'_'+i+'" value="'+array[i][1]+'"'+checkItA+' />'+array[i][2]+'</label></td>';
		htm    += (array[i+1]?'<td><label for="select_item_'+fname+'_'+(i+1)+'"'+colorB+'><input onclick="checkSelectItem(this,\''+fname+'\','+limit+')" class="checkbox" type="checkbox" name="select_item_'+fname+'" value="'+array[i+1][1]+'"'+checkItB+' id="select_item_'+fname+'_'+(i+1)+'" />'+array[i+1][2]+'</label></td></tr>':'<td>&nbsp;</td></tr>');
	}
	htm    +=     '<tr><td colspan="2"><div class="tool">[ <a href="#" onclick="popupClose(\''+fname+'\');return false" onfocus="this.blur();">确定</a> ] [ <a href="#" onfocus="this.blur();" onclick="popupClose(\''+fname+'\');return false">关闭</a> ]</div></td></tr></table>';
	htm    +=     '</table>';
	htm    +=   '</div><div class="bottom"><div class="bl"></div><div class="bline"></div></div></div>';
	//蒙板DIV
	if( !$I('mask_div') ) htm +='<div id="mask_div" style="position:absolute;left:0px;top:0px;width:'+$(document).width()+'px;height:'+$(document).height()+'px;z-index:999;background:#000;filter:alpha(opacity=10);opacity:0.1;"></div>';
	$(document.body).append(htm);
	//鼠标效果
	$('#dialog_'+fname+' label').each(function(){
		this.onmouseover = function(){this.style.background='#e3e4f5';};
		this.onmouseout  = function(){this.style.background='#fff';};
	});
	//显示位置
	event = event || window.event;
	var box = $I('dialog_'+fname);
	box.style.left = $(document).width()/2 - 290 + 'px';
	var t = event.clientY + document.documentElement.scrollTop - 280;
	box.style.top  = ( t>100 ? t : 100 ) + 'px';
	//显示
	$('#mask_div').slideDown('fast',function(){$('#dialog_'+fname).slideDown('fast');/*$I(fname+'_1').focus();*/});
}
/*functions for popupDiv end*/

function selectedValue(){
	var values = '', t = '';
	$(':checkbox:checked',$('#form_list')).each(function(){
		if( !this.disabled ){
			values += (t + this.value);
			t = ',';
		}
	});
	return values;
}

function post(url,data,message,from,second,callback){
	$.ajax({
		type : 'post',
		url : url,
		cache : false,
		dataType : 'text',
		data : data,
		timeout : 20000,
		beforeSend : function(XMLHttpRequest){
			//loading(1);
			$(':button').each(function(){this.disabled=true;});
			$(':submit').each(function(){this.disabled=true;});
		},
		success: function(text){
			var a = text.split('|');
			if( a[0] == 'OK' ){
				window.ready = null;
				if(second!=null && typeof(second)=='number'){
					loading(0,message,second);
				}else{
					if(message)alert(message);
					loading(0);
				}
				if( -1 == from ) history.back();
				else if( 0 == from ) location.reload();
				else if( from ) location.href = from;
				eval(callback);//if( callback )try{eval(callback)}catch(e){};
			}else if( 'ERR'==text ){
				alert('操作失败：没有权限！');
			}else{
				alert(a[0]);
				try{if(a[1]){$I(a[1]).disabled=false;$I(a[1]).focus()}}catch(e){};
				loading(0);
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('请求超时，请重试。');
			loading(0);
		},
		complete: function(XMLHttpRequest){
			$(':button').each(function(){this.disabled=false;});
			$(':submit').each(function(){this.disabled=false;});
		}
	});
}

function get(url,message,removeid,from,callback){
	$.ajax({
		type : 'get',
		url : url,
		cache : false,
		dataType : 'text',
		timeout : 20000,
		beforeSend : function(XMLHttpRequest){
			//loading(1);
			$(':button').each(function(){this.disabled=true;});
			$(':submit').each(function(){this.disabled=true;});
		},
		success: function(text){
			var a = text.split('|');
			if( a[0] == 'OK' ){
				window.ready = null;
				if(message)alert(message);
				loading(0);
				try{
					if( removeid != null ){
						var arr = removeid.toString().split(',');
						for( var i=0; i<arr.length; i++ ){
							var tr = $I('info_list_'+arr[i].trim());
							if( tr ){
								$I('info_list').removeChild( tr );
								$I('total').innerHTML = parseInt($I('total').innerHTML)-1;
							}
						}
					}
				}catch(e){}
				if( -1 == from ) history.back();
				else if( 0 == from ) location.reload();
				else if( from ) location.href = from;
				if( callback )try{eval(callback)}catch(e){};
			}else if( 'ERR'==text ){
				alert('操作失败：没有权限！');
				loading(0);
			}else{
				alert(a[0]);
				loading(0);
				try{if(a[1]){$I(a[1]).disabled=false;$I(a[1]).focus()}}catch(e){};
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			alert('请求超时，请重试。');
			loading(0);
		},
		complete: function(XMLHttpRequest){
			$(':button').each(function(){this.disabled=false;});
			$(':submit').each(function(){this.disabled=false;});
		}
	});
}