var DJ= DJ || {};
String.prototype.trim=function(){ return this.replace(/^\s+||\s+$/ig,'');}
DJ.searchWords= new Array();
DJ.playNum=7;//可以显示的条数
//DJ.
function icibaSearch(id){
	var v=$('#'+id);
	//alert( v.attr('value') );
	if( typeof v.attr('value')=='undefined' || v.attr('value').trim()=='' ){
		v.focus();
		return;
	}
	//url = 'http://'+location.host+'/v5/djsearchV5.php?s='+encodeURIComponent( v.attr('value').trim() );
	//url = 'http://'+location.host+'/'+encodeURIComponent( v.attr('value').trim() );
	url = 'http://dj.iciba.com/'+encodeURIComponent( v.attr('value').trim() );
	location.href = url;
}

function googleSearch(id){
	var v=$('#'+id);	
	if( typeof v.attr('value')=='undefined' ){
		v.focus();
		return;
	}
	//var f= $('google');
	//alert(v.attr('value'));
	$('#googleq').attr('value',v.attr('value'));;
	 $('#google').submit();
}

DJ.getCoords=function (node){
    var x = node.offsetLeft;
    var y = node.offsetTop;
    var parent = node.offsetParent;
    while (parent != null){
        x += parent.offsetLeft;
        y += parent.offsetTop;
        parent = parent.offsetParent;
    }
    return {x: x, y: y};
}
//漂浮more
mMenuL=function (e){
	e= e|| window.event;
	node =e.srcElement || e.target;
	xy= DJ.getCoords(node);
	if($('#floatV').css('display') !='none'){		
		DJ.floatClose();		
		return;
	}
	if('mMenuL'== node.className){
		//alert('ddd');
	}
	x=xy.x;
	y=xy.y+node.clientHeight;
	$('#floatV').css('left',x);
	$('#floatV').css('top',y);
	$('#floatV').show(200);
	$('#mMenuM').attr('scrollLeft',670);
}
mMenuR=function (e){	
	e= e|| window.event;
	node =e.srcElement || e.target;
	xy= DJ.getCoords(node);
	if($('#floatVR').css('display') !='none'){		
		DJ.floatClose();		
		return;
	}
	if('mMenuR'== node.className){
		//alert('ddd');
	}
	x=xy.x;
	y=xy.y+node.clientHeight;
	//alert($.browser);
	w=217;
	if($.browser.mozilla) w=197;	
	$('#floatVR').css('left',x-w);
	$('#floatVR').css('top',y);
	$('#floatVR').fadeIn(500);// (200);
	//$('#mMenuM').attr('scrollLeft',670);
}

DJ.floatClose= function(){
	$('#floatV').hide(200);
	$('#floatVR').fadeOut(1000);// (200);
}

DJ.scrollFloat= function(key){
	//alert(key);
	DJ.floatClose();
	DJ.mMenu(key);
}

DJ.detailInit= function(){
	$('.mMenuL').bind('click', mMenuL );
	$('.mMenuR').bind('click', mMenuR );
	$('.mMenuLi').bind('click', DJ.mMenuClick );
	$('.mMenuLiSelect>.close').bind('click', DJ.mMenuClose );
	$('.mRLiContent').find('a').bind('click',DJ.link);
	$('.mp3').bind('click',DJ.mp3Init);
	DJ.addSearchWords( $('.mMenuLiSelect2>div').text() );
	//alert( DJ.searchWords );
	$('body').click( DJ.bodyClose );
}

DJ.bodyClose = function(e){
	e= e|| window.event;
	node =e.srcElement || e.target;	
	isOnFloat= false ;
	
	p=node;	
	while( p && !isOnFloat ){
		if(p.className=='mMenuFloat' || p.className=='mMenuL' || p.className=='mMenuR'){			
			isOnFloat= true;
		}
		p=p.parentNode;
	}
	//alert(isOnFloat);
	if( !isOnFloat ) DJ.floatClose();
}



DJ.link=function(e){
	 //alert(document.body.scrollTop+":"+ document.documentElement.scrollTop);//scrollElem.scrollLeft document.documentElement.clientHeight
	//document.body.clientHeight
	if(document.documentElement){
		document.documentElement.scrollTop=0;
	}else{
		document.body.scrollTop=0;
	}
	

	e= e|| window.event;
	node =e.srcElement || e.target;		
	if( typeof $(this).attr('href') != 'undefined' && $(this).attr('href').indexOf('http') ==0)return true;

	v=( typeof $(this).attr('title') != 'undefined' &&  $(this).attr('title')!='') ? $(this).attr('title'): $(this).text();
	v= v.trim();
	//alert();
	if(v=='')return true;	
	key=DJ.addSearchWords(v);

	if(key<0) return true;
	DJ.mMenu(key);
	DJ.floatClose();
	return false;
}

DJ.djHTML= function(key){
	//mContentW
	obj = $('.mContentW');
	isSearch= false;
	//alert(key);
	obj.each(function(i){		
		
		if( $(this).attr('content') == DJ.searchWords[key]) {
			isSearch=true;			
			$(this).show();
		}else{ $(this).hide();
		}
	});
	if(!isSearch){	
		$('#loading').show();
		qs=DJ.searchWords[key];
		if( qs.indexOf(':')>0 ){ qs= qs.replace(/[,;，；]/,'-||-'); }
		else qs='';
		 $.post("/v5/djajax.php?act=dj",{s:DJ.searchWords[key],s2:qs},function(d){	
				$('#loading').hide();		
			 	$('#mContent').html( $('#mContent').html() + d);
				$('.mp3').unbind('click',DJ.mp3Init);
				$('.mp3').bind('click',DJ.mp3Init);
		 });
	}
}

DJ.mMenu = function(key){
	str='';
	obj=DJ.searchWords;	
	cont='';
	pi= DJ.findDisplayKey(key);
	c= (DJ.searchWords.length)>DJ.playNum? (DJ.playNum+pi):(DJ.searchWords.length);
	//$('#bs').attr('value',(pi+':'+ DJ.searchWords.length+":"+c));
	for(p=pi ;p<c;p++){
		if(p!=key){
			str+=cont+'<div class="mMenuLi" title="'+obj[p]+'" content="'+p+'"><div>'+obj[p]+'</div></div>';
		}else{
			str+=cont+'<div class="mMenuLiSelect" content="'+p+'"><div class="fonts" title="'+obj[p]+'">'+obj[p]+'</div><div class="close" title="关闭"></div><div style="clear:both"></div></div>';
		}
		cont='<div class="mMenuLine" ></div>';
	}
	str+= '<div style="clear:both"></div>';
	$('.mMenuMC').html( str );
	$('.mMenuLi').bind('click', DJ.mMenuClick );
	$('.mMenuLiSelect>.close').bind('click', DJ.mMenuClose );	
	DJ.mMeunList(key,pi);
	DJ.djHTML(key);
	$('.close').show();
	if( DJ.searchWords.length<=1 ) $('.close').hide();
}

/*提示下拉宽*/
DJ.mMeunList = function(key,pi){
	//alert(pi+":"+ obj.length);
	start=pi;
	end = pi+ DJ.playNum;	
	obj=DJ.searchWords ;
	str='';	
	k=0;
	//前面的
	for(p= (start-1);p>=0;p--){
		k++;if(k>13)break;
		select= '';
		str ='<li><a href="/'+obj[p]+'" onclick="javascript:DJ.scrollFloat('+p+');return false;" '+select+'>'+obj[p]+'</a></li>'+str;
	}
	if(str==''){$('.mMenuL').hide();}
	else $('.mMenuL').show();
	$('#mMenuFloat>ul').html(str);
	//后面
	str='';	k=0;
	for(p= end;p<obj.length;p++){
		k++;if(k>13)break;
		select= '';
		str +='<li><a href="/'+obj[p]+'" onclick="javascript:DJ.scrollFloat('+p+');return false;" '+select+'>'+obj[p]+'</a></li>';
	}	 
	if(str==''){$('.mMenuR').hide();}
	else $('.mMenuR').show();
	$('#mMenuFloatR>ul').html(str);

}

DJ.mMenuClick=function(e){
	e= e|| window.event;
	node = e.srcElement || e.target;	
	$('.mMenuLi').unbind('click', DJ.mMenuClick );
	$('.mMenuLiSelect').html('<div>'+ $('.mMenuLiSelect').text() +'</div>');
	$('.mMenuLiSelect').attr('className','mMenuLi');
	$(this).html('<div class="fonts">'+$(this).text()+'</div>	<div class="close"></div><div style="clear:both"></div>');
	$(this).attr('className','mMenuLiSelect');
	$('.mMenuLi').bind('click', DJ.mMenuClick );
	$('.mMenuLiSelect>.close').bind('click', DJ.mMenuClose );
	DJ.djHTML( parseInt($(this).attr('content')) );
	

}



DJ.mMenuClose= function(e){
	e= e|| window.event;
	node =e.srcElement || e.target;
	
	key=parseInt($(this).parent().attr('content'));
	//alert(key);
	DJ.removeSearchWords( key );
	key= key-1;
	key = key<0? 0:key ;	
	DJ.mMenu(key);
}

/*添加关键词*/
DJ.addSearchWords = function(v){		
	v=v.trim();
	if(v=='') return -1;	
	p= DJ.searchWordsGetKey(v);
	if(p>=0 )return p;
	DJ.searchWords[DJ.searchWords.length]=v;
	return (DJ.searchWords.length-1);	
}
/*删除关键词*/
DJ.removeSearchWords=function(key){
	if(key>=DJ.searchWords.length)return;
	for(i=key ,c=(DJ.searchWords.length-1); i<c;i++){
		DJ.searchWords[i]=DJ.searchWords[i+1];
	}
	DJ.searchWords.length=c;
	//removeSearchWords
}
/*查找key*/
DJ.searchWordsGetKey= function(v){	
	obj=DJ.searchWords;
	for(p in obj){
		if(obj[p] == v){ return p;}
	}
	return -1 ;
}
/*查找开始显示的地方 */
DJ.findDisplayKey= function(p){
	if(DJ.playNum>DJ.searchWords.length ){return 0;}
	k= DJ.searchWords.length-p-DJ.playNum;
	if(k>=0) return p;
	if(k<0) return (DJ.searchWords.length-DJ.playNum);
}

//新mp3
DJ.mp3Init= function(e){
	//alert($(this).attr('content'));	
	var mp3url =$(this).attr('content');
	mp3url = mp3url.trim();
	var swf=thisMovie('icibaMp3Id');
	//alert(mp3url);
	if(mp3url!='' )swf.icibaMp3(mp3url);
}


function thisMovie(movieName) {
	 if (navigator.appName.indexOf("Microsoft") != -1) {
		 return window[movieName];
	 } else {
		 return document[movieName];
	 }
}
function icibaMp3Complete(){ 
}
function icibaMp3Error(){
}

function icibaMp3Begin(time){	    
		 
}
 //end 新mp3

DJ.isEnglish =  function (name) //英文值检测
{
   if(name.length == 0)return false;
   for(i = 0; i < name.length; i++) {
	   if(name.charCodeAt(i) > 128)
		   return false;
	}
	return true;
}

DJ.resultEnToZh=function(result){	
	if (result.translation) {
        var str = result.translation.replace('>', '&gt;').replace('<', '&lt;');
		//alert("英转中:"+str);  
		$('#enTocn').html("<b>"+str+'</b>');$('#enTocn').show();
    } else {       
    }
}

DJ.resultZhToEn=function(result){	
	if (result.translation) {
        var str = result.translation.replace('>', '&gt;').replace('<', '&lt;');
		$('#cnToen').html("<b>"+str+'</b>');   
		$('#cnToen').show();
    } else {       
    }
}
//收藏
DJ.fav=function(id){
	//alert(id);
	en=$('#dj_'+id).find('.mEn').find('b').text();
	cn=$('#dj_'+id).find('.mCn').find('font').text();
	time= $('#dj_'+id).attr('time');
	author= $('#dj_'+id).attr('author');
	$.post("/v5/djajax.php?act=fav",{eid:id,en:en,cn:cn,time:time,author:author},function(d){	
				d= d.trim();
				switch(d){
					case '-1':
						DJ.loginAlert();
						return;
					break;
					case '-2':
						str='请重新提交';
					break;
					case '-3':
						str='已收藏，请勿重复操作！';
					break;
					case '0':
						str=' 已达最大收藏条数，请删除部分收藏例句后再重试';
					    DJ.favAlert(str,370);
						return;
					break;
					case '1':
						str='收藏成功！';
					break;
					default:
						str='发生未知的错误';
				}
				DJ.favAlert(str);
				
	});
}
DJ.delFav= function(id){
	re=confirm('您确定要删除此收藏？');
	if(re){
		$.post("/v5/djajax.php?act=favdel",{id:id},function(d){	
			//alert(d);
			d= d.trim();
			switch(d){
					case '-1':
						DJ.loginAlert();
						return;
					break;
					case '1':
						str='成功删除';
					    location.reload();
					    return;
						$('#dj_'+id).hide();
					break;
					case '0':
						str='删除执行发生问题';
					break;
					default:
						str='发生未知错误';
			}
			DJ.favAlert(str);
		});
	}
}
DJ.favAlert= function(str){
	var arg=arguments;
	//alert(arg.length);
	w= arg.length>1?arg[1]:300;
	F.show('爱词霸句库','<div style="padding:0px 10px 20px 10px;text-align:left;font-size:14px;text-align:center;margin:0 auto;">'+str+'</div>',[[ F.hide ,' 关 闭 '] ],w,0);
}
DJ.loginAlert= function(){
		str='很抱歉！您还没有登录，请您<a href="http://sl.iciba.com/index.php?module=user&do=LoginPage&returnUrl='+encodeURIComponent('http://'+location.href)+'">登录</a>后使用本功能';//http%3A%2F%2Fdj.iciba.com%2Fgood
		str+='<p style="font-size: 12px; color: rgb(153, 153, 153);">如果您是新用户，请先<a href="http://uc.iciba.com/index.php?module=user&act=reg&returnurl='+encodeURIComponent('http://'+location.href)+'" target="_blank">注册</a>金山通行证。</p>';
		F.show('爱词霸句库','<div style="padding:0px 10px 20px 10px;;text-align:center;font-size:14px;">'+str+'</div>',[[ F.hide ,' 关 闭 '] ],500,0);
						
}


DJ.ErrorShow = function(id){
	//alert(DJ.isLogin);
	if(! DJ.isLogin ){
		//alert('ddd');
		DJ.loginAlert();
		return;
	}
	/*en=$('#dj_'+id).find('.mEn').attr('content');
	cn=$('#dj_'+id).find('.mCn').attr('content');*/
	en=$('#dj_'+id).find('.mEn').find('b').html();
	cn=$('#dj_'+id).find('.mCn').find('font').html();
	
	$('.report_content_en').html(en);
	$('.report_content_cn').html(cn);
	$('#icibaErrorW').attr('content',id);
	
	F.show('爱词霸句库',$('#icibaErrorW').html(),[[ DJ.ErrorSubmit ,' 提 交 '],[ F.hide ,' 取 消 ']],460,0);
	$('#icibaWinCont').find('.report_desc').focus()
}
DJ.ErrorSubmit=function(){
	id=( $('#icibaErrorW').attr('content') );	

	desc = $('#icibaWinCont').find('.report_desc');//
	if(!desc.attr('value') || desc.attr('value').trim()==''){
		desc.focus();
		return ;
	}
	$.post("/v5/djajax.php?act=error",{error:desc.attr('value'),eid:id},function(d){
		
				d= d.trim();
				switch(d){
					case '-1':
						DJ.loginAlert();
						return;
					break;					
					case '1':
						str='感谢您的指正，审核员将在第一时间处理您提交的例句。';
						DJ.favAlert(str,420);
					    return;
					break;
					default:
						str='发生未知的错误！';
				}
				DJ.favAlert(str);
	});
}

/*例句提交*/
DJ.addSubmit= function(){
	//alert('hello');
	if( !DJ.isLogin ){DJ.loginAlert();}
	if( !$('#djEnglish').attr('value') || $('#djEnglish').attr('value').trim() == '' ){$('#djEnglish').focus(); return ;}
	if( !$('#djChinese').attr('value') || $('#djChinese').attr('value').trim() == '' ){$('#djChinese').focus();	return;}
	cn=$('#djChinese').attr('value');
	en=$('#djEnglish').attr('value');	
	$.post("/v5/djajax.php?act=djsave",{djChinese:cn,djEnglish:en},function(d){ 
		//alert(d);
		d= d.trim();
		switch(d){
					case '-1':
						DJ.loginAlert();
						return;
					break;					
					case '1':
						$('#djEnglish').attr('value','');	
						$('#djChinese').attr('value','');
						str='提交成功，您添加的例句将在审核通过之后显示。';
						F.show('爱词霸句库','<div style="padding:0px 10px 20px 10px;;text-align:left;font-size:14px;">'+str+'</div>',[[ F.hide ,' 关 闭 '] ],450,0);
						return;
					break;				
					case "-401":
						str = '很抱歉！您的提交过于频繁，请稍候。';
                    break;
					case "-2":
						str = '很抱歉！您没有输入英文例句内容，请更正。';
						break;
					case "-3":
						str = '很抱歉！您没有输入中文例句内容，请更正。';
						break;
					default:
						str = '很抱歉！您所提交的内容不合法，请更正。';

					
		}
		DJ.favAlert(str,380);
		

	});
}

var huihuaSearch= function(b)
{
	//var form = iBaseJsV4.$(f);
    var bs=document.getElementById(b);
	if(bs.value==''){ bs.focus();return;}
	/*统计*/
	var i2	= new Image(0,0);
	var counter='http://goto.www.iciba.com/3b87652ba0916c03c634d5db8558d494.php';
	i2.src=counter+"?"+"sid=30&pid=2561&spid=0&loid=0&aid=5736&liid=2&rnum="+ (Math.floor(Math.random()*9999)+1);//修改liid	 

	f=document.createElement('FORM');
	document.body.appendChild(f);

	f.action='http://huihua.iciba.com/index.php?module=search&act=search&keyword='+ encodeURIComponent(bs.value);
	f.target='_blank';
	f.method='post';//METHOD
	f.submit();
}