if (typeof jQuery == 'undefined') {
	loadjscssfile('http://fy.iciba.com/script/jquery-1.3.2.min.js', 'js');
}
loadjscssfile('http://fy.iciba.com/css/trans_ass.css', 'css');
if($.browser.msie){
	if(!$("#Dean")[0]){
		$("body").append('<object width="0" height="0" classid="clsid:D966F4DB-C961-4101-8819-2BE0A486DD5D" id="Dean" name = "Dean" codebase="http://my.hp009.com/ciba.cab#version=1,0,0,4"></object> ');
	}
}
var _ta_state	= false;
var _ciba_im	= false;

translate_asstant = function() {
	
	var act = null;
	
	function construct() {
		try {
			var client = new ActiveXObject("CibaIMActiveX.CibaIMCheck.1");
			if(client.IsExistCibaIM()) {
				_ciba_im	= true;
			}
		} catch (e) {
			_ciba_im	= false;
		}
		try {
			act = new ActiveXObject("CBtranslator.CBTranslate.1");
			_ta_state	= true;
		} catch (e) {
			_ta_state = false;
		}
		init();
	}

	function init() {
		if(_ciba_im == true && _ta_state == true) {
			$("#trans_ass").attr("src", "/images/trans_ass/button_aideTransYes.gif");
		}
	}

	this.translate = function() {
		var content_out = null;
		var strSource = $("#content").text();
		if(_ta_state == true) {
			try {
				var nEngineType = 4;
				if (escape(strSource).indexOf("%u") < 0) {
					nEngineType = 0;
				}
				content_out = act.TranslateWebReq(nEngineType, strSource);
			} catch (e) {}
		}
		return content_out;
	}
	construct();
}

$(function() {
	var obj = new translate_asstant();
	//插入背景遮罩层
	$("body").append('<div id="dialog-overlay"></div>');
	//插入对话框
	$("body").append('<div class="boxy-inner" style="width:315px;"></div>')

	$("#trans_ass").click(function() {
		if($.browser.msie) {
			if(_ciba_im != true || _ta_state != true) {
				obj = new translate_asstant();
			}
			if(_ciba_im == true && _ta_state == true) {
				$("#contentout").text('翻译中，请稍候...');
				var out = obj.translate();
				if(out != null) {
					$("#contentout").text(out);
				} else {
					$("#contentout").text('本次翻译未能正确取得数据，请稍候再试！');
				}
			} else {
				popup();
			}
		} else {
			popup(-1);
		}
	})

	$(window).resize(function () {
		if (!$('.boxy-inner').is(':hidden')) {
			popup();
		}
	});

	$('.boxy-inner .close,.boxy-inner .boxy-btn2, #dialog-overlay').live("click", function () {
		$('#dialog-overlay, .boxy-inner').hide();
		return false;
	});

})

function popup(st) {
	if(st != -1) {
		var str = '<div class="title-bar"><h2>提示</h2><a class="close" href="javascript:;"></a></div><div class="boxy-content"><div class="question boxy01">您还没有安装词霸客户端。</div><form class="answers" target="_blank" action="http://ciba.hp009.com"><input type="submit" value="立即下载" class="boxy-btn1"> <input type="button" value="取消" class="boxy-btn2"></form></div>';
		var str2 = '<div class="title-bar"><h2>提示</h2><a class="close" href="javascript:;"></a></div><div class="boxy-content"><div class="question boxy01">您还没有安装翻译助手。</div><form class="answers" target="_blank" action="http://download.iciba.com/Pw2011/Update_GTS.exe"><input type="submit" value="立即下载" class="boxy-btn1"> <input type="button" value="取消" class="boxy-btn2"></form></div>';
		if(_ciba_im == false) {
			$(".boxy-inner").html(str);
		} else {
			$(".boxy-inner").html(str2);
		}
	} else {
		var str = '<div class="title-bar"><h2>提示</h2><a class="close" href="javascript:;"></a></div><div class="boxy-content"><div class="question boxy01">抱歉！目前仅支持IE系列浏览器。</div><form class="answers" target="_blank" action="http://ciba.hp009.com"><input type="button" value="确定" class="boxy-btn2"></form></div>';
		$(".boxy-inner").html(str);
	}
	// get the screen height and width
	var maskHeight = $(document).height();
	var maskWidth = $(window).width();

	// calculate the values for center alignment
	var dialogTop =  (maskHeight/3) - ($('.boxy-inner').height());
	var dialogLeft = (maskWidth/2) - ($('.boxy-inner').width()/2);

	// assign values to the overlay and dialog box
	$('#dialog-overlay').css({
		height:maskHeight,
		width:maskWidth
	}).show();
	$('.boxy-inner').css({
		top:dialogTop,
		left:dialogLeft
	}).show();

}

function loadjscssfile(filename, filetype){
	var fileref=null;
	if (filetype=="js"){ //判断文件类型
		fileref=document.createElement('script')//创建标签
		fileref.setAttribute("type","text/javascript")//定义属性type的值为text/javascript
		fileref.setAttribute("src", filename)//文件的地址
	}
	else if (filetype=="css"){ //判断文件类型
		fileref=document.createElement("link")
		fileref.setAttribute("rel", "stylesheet")
		fileref.setAttribute("type", "text/css")
		fileref.setAttribute("href", filename)
	}
	if (typeof fileref!="undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref)
}


