var _dict_config = {
	'ss':1//默认为1, 0:悬停发音，1点击发音
};
function addFavorite(sURL, sTitle){
	sURL = sURL || 'http://dict.cn';
	sTitle = sTitle || unescape("%u6D77%u8BCD-%u5728%u7EBF%u8BCD%u5178%2C%u82F1%u8BED%u5B66%u4E60");
	try{
		window.external.addFavorite(sURL, sTitle);
	}
	catch(e){
		try{
			window.sidebar.addPanel(sTitle, sURL, "");
		}catch(e){
			alert(unescape("%u52A0%u5165%u6536%u85CF%u5931%u8D25%uFF0C%u8BF7%u4F7F%u7528Ctrl+D%u8FDB%u884C%u6DFB%u52A0"));
		}
	}
}
function setHomepage(){
	var url = 'http://dict.cn';
	if(document.all){
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
	}
	else if(window.sidebar){
		if(window.netscape){
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e)
			{
				alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
			}
		}
		var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage','url');
	}
}
function getFlashMovieObject(a) {
	if (window.document[a]) {
		return window.document[a];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[a]) {
			return document.embeds[a];
		}
	} else {
		return document.getElementById(a);
	}
}
ssplay.sound = null;
$(function(){
	if($.isFunction(window.getPlayer)){
		ssplay.sound = getPlayer('#sound');
	}
});
function ssplay(b){
	if(!ssplay.sound){
		return;
	}
	ssplay.sound.pause();
	ssplay.sound.load('http://audio.dict.cn/mp3.php?q='+b);
	ssplay.sound.play();
}
function ssstop(){
	if(ssplay.sound) ssplay.sound.pause();
}
function getCookie(key) {
	var tmp =  document.cookie.match((new RegExp(key +'=.*?($|;)','g')));
	if(!tmp || !tmp[0]){
		return null;
	}
	else {
		return unescape(tmp[0].substring(key.length+1,tmp[0].length).replace(';','')) || null;
	}
}
function getGMT(ttl){
	if (parseInt(ttl) == 'NaN' ){
		return '';
	}
	else {
		now = new Date();
		now.setTime(now.getTime() + (parseInt(ttl) * 60 * 1000));
		return now.toGMTString();
	}
}
function setCookie(key, value, ttl, path, domain, secure) {
	var cookie = [key+'='+escape(value),'path='+ ((!path || path=='')?'/' : path),'domain='+ ((!domain || domain=='')?  'dict.cn' : domain)];
	if (ttl){
		cookie.push('expires='+getGMT(ttl));
	}
	if (secure){
		cookie.push('secure');
	}
	return document.cookie = cookie.join(';');
}
function unsetCookie(key, path, domain) {
	path   = (!path   || typeof path   != 'string') ? '' : path;
	domain = (!domain || typeof domain != 'string') ? 'dict.cn' : domain;
	if (getCookie(key)){
		setCookie(key, '', 'Thu, 01-Jan-70 00:00:01 GMT', path, domain);
	}
}
function myEncodeURI(s){
	s = encodeURIComponent(s);
	return s.replace(/(^\s+)|(\s+$)/, '').replace(/_/g,'_5F').replace(/\./g,'_2E');
}
function substring(str, len){
	if(str.length > len && len > 3){
		return str.substring(0,len-3)+'...';
	}else{
		return str;
	}
}
loadConfig.re = /([a-z]+)(\d+)/gi;
saveConfig.check=['ss','se','sa'];
function saveConfig2Server(){
	var savecfg = getCookie('DictCN_savecfg');
	if(savecfg && _dict_config && _dict_config.rv > 0){
		$.getJSON('http://www'+_dict_config.rv+'.dict.cn/ajax/user_config.php?act=save&'+savecfg+'&jsoncallback=?',function(data){
			if(data){
				if(data.flag == 'ok'){
					unsetCookie('DictCN_savecfg');
				}else if(data.flag == 'no'){
					alert(data.err);
				}
			}
		});
	}
}
function saveConfig(){
	if(!(_dict_config && _dict_config.load)){
		loadConfig();
	}
	
	var arr, replace=save='', flag, cookieTime;
	var cfg = getCookie('DictCN_cfg');
	var copy = {};
	$.extend(copy,_dict_config);
	while((arr = loadConfig.re.exec(cfg))!= null){
		flag = false;
		if($.inArray(arr[1],saveConfig.check) != -1){
			if(_dict_config[arr[1]] != parseInt(arr[2])){
				save += arr[1]+'='+_dict_config[arr[1]]+'&';
				flag = true;
			}
		}
		if(flag){
			replace += arr[1]+_dict_config[arr[1]];
		}else{
			replace += arr[1]+arr[2];
		}
		delete copy[arr[1]];
	}
	var add = '';
	$.each(copy,function(i,v){
		if(i!='load')add += i+v
	});
	if(add != ''){
		replace += add;
		save += add;
	}
	cookieTime = parseInt(getCookie('DictCN_time'));
	setCookie('DictCN_cfg', replace, cookieTime);
	if(_dict_config.sv > 0 || _dict_config.rv > 0)
		setCookie('DictCN_savecfg', save);
}

function loadConfig(server){
	saveConfig2Server();
	if(window._dict_config && window._dict_config.load) return;//only load once
	var cfg = getCookie('DictCN_cfg');
	var obj = {};
	server = server||0;
	if(cfg && cfg.length > 1){
		obj.load = 1;
		var cfg = getCookie('DictCN_cfg');
		var arr;
		while((arr = loadConfig.re.exec(cfg))!= null){
			obj[arr[1]] = parseInt(arr[2]);
		}
	}else{
		if(server > 0){
			$.getJSON('http://www'+server+'.dict.cn/ajax/user_config.php?act=load&jsoncallback=?',function(data){
				if(data){
					if(data.flag == 'ok'){
						loadConfig();
					}else if(data.flag == 'no' && data.server > 0){
						loadConfig(status);
						return;
					}else{
						if(window._dict_config){
							window._dict_config.load = 1;
						}else{
							window._dict_config = {
								'load':1
							};
						}
					}
				}
			});
			return;
		}else{
			obj.load = 1;
		}
	}
	
	if(window._dict_config){
		$.extend(window._dict_config, obj);
	}else{
		window._dict_config = obj;
	}
}

function setdictEnable(dict_enable){
	var cookieTime = parseInt(getCookie('DictCN_time'));
	
	if (dict_enable){
		dict_enable = false;
		setCookie('dicthuaci', 'on', cookieTime);
		setCookie("dictstate", '1',  cookieTime);
	 }else{
		setCookie('dicthuaci', 'off', cookieTime);
		setCookie("dictstate", '0',  cookieTime);
	 }
}

function huaciSwitch(on){
	if(on){
		if(typeof dictEnable != undefined){
			setdictEnable(on);
		} else {
			unsetCookie("dicthuaci");
		}
	} else {
		if(typeof dictDisable != undefined){
			setdictEnable(on);
		}else{
			setCookie('dicthuaci','off',43200);
		}
	}
}


function dictLogined(){
	return (getCookie('DictCN_auth') != null && getCookie('dictsid') != null);
}
audioAttach.over = function(){
	if((_dict_config.ss&1) == 0){
		ssstop();
		ssplay($(this).attr('audio'));
	}
};
audioAttach.out = function(){
	if((_dict_config.ss&1) == 0)ssstop();
}
audioAttach.click = function(){
	ssstop();
	var a = $(this).attr('audio');
	ssplay($(this).attr('audio'));
};
function audioAttach(o){
	o.bind('mouseover',audioAttach.over).bind('mouseout',audioAttach.out).bind('click',audioAttach.click);
}
/*
 * Add Google Analytics
 */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-138041-2']);
_gaq.push(['_trackPageview']);

(function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
 
var dialog;
function showFback(obj){
	if(!dialog){
	  dialog = new dDialog({content:'正在加载...',drag:true,width:480,height:330,skin:'class',title:'意见反馈',position:{top:46,right:40},animate:false});  
	  dialog.show();
	  $.getJSON("http://en.dict.cn/api/feedback/index?callback=?&ref="+window.location.href,function(data){dialog.setContent(data);});
	}else{dialog.show();}
}

//search-butt
$(document).ready(function(){
	$('.butt').hover(function(){
		$(this).addClass('hover1');
	}, function(){
		$(this).removeClass('hover1');
	});
	
	loadConfig();
});

$(function(){
	//tab-切换
	$(".tab>h6>span").not('.wait').click(
		function(){
			$(this).addClass("current").siblings().removeClass("current");
			$(".tab>.tabcontent").eq($(".tab>h6>span").index(this)).show().siblings(".tabcontent").hide();
		}
	).hover(function(){$(this).css('backgroundColor', '#FFF');}, function(){$(this).css('backgroundColor','');});
	
	//消息提示
	$('#close').click(function(){$('#close-alert').hide()});
	
	$.getJSON("http://my.dict.cn/home-space-do-nupm.html?callback=?",function(data){
		if(data) {
			var str = String(data).split('&');
		
			if(str.length > 0) {
				var newprompt = str[0].split('=');
				var newpm     = str[1].split('=');
	
				if(newprompt[1] > 0) {
					$('#more-menu1 ul:eq(0) li:eq(0) a').append('('+newprompt[1]+')');
					//$('.more-20').addClass('more-23');
				}
				
				if(newpm[1] > 0) {
					$('#more-menu1 ul:eq(0) li:eq(1) a').append('('+newpm[1]+')');
					//$('.more-20').addClass('more-23');
				}
				if(getCookie('newprompt') != data) {
					if(newprompt[1] > 0 && newpm[1] > 0) {
						$('#close-alert').append('<p>'+newprompt[1]+'个新提醒，<a href="http://my.dict.cn/home-space-do-notice.html">查看提醒</a></p>');
						$('#close-alert').append('<p>'+newpm[1]+'条新短消息，<a href="http://my.dict.cn/home-space-do-pm-filter-newpm.html">查看短消息</a></p>');
						$('#close-alert').fadeIn("slow");
					}
					else if(newprompt[1] > 0) {
						$('#close-alert').append('<p>'+newprompt[1]+'个新提醒，<a href="http://my.dict.cn/home-space-do-notice.html">查看提醒</a></p>').fadeIn("slow");
					}
					else if(newpm[1] > 0) {
						$('#close-alert').append('<p>'+newpm[1]+'条新短消息，<a href="http://my.dict.cn/home-space-do-pm-filter-newpm.html">查看短消息</a></p>').fadeIn("slow");
					}
					setCookie('newprompt',data);
				}
			}	
		}
	});
	setTimeout("$('#close-alert').fadeOut('slow');",5000);
});

	

