wgStylePath = 'http://tratu.baamboo.com/searchbox/vuvu/';
wgScriptPath = 'http://tratu.baamboo.com/';
var html = "";
var browserID = 0;


function BBTTcreateCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function BBTTreadCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function BBTTeraseCookie(name) {
	BBTTcreateCookie(name, "", -1);
}

function BBTTcheckCookieSearchBox() {	
	if (BBTTreadCookie( 'isAddedBBTratu_vuvu_en_vn' ) == 1
	|| BBTTreadCookie( 'isAddedBBTratu_vuvu_vn_vn' ) == 1) {		
		return true;
	}
	else {		
		return false;
	}
}

function closeAddSearchBox () {	
	divBoxSearchObj = document.getElementById('divBoxSearch');
	if (divBoxSearchObj) {
		divBoxSearchObj.style.display = 'none';
	} 		
}

// ID for each Browser type
// 1: Firefox
// 2: IE6
// 3: IE7

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

///////////////////
if (BrowserDetect.browser == "Firefox"){
	browserID = 1;
} else if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "6") {
	browserID = 2;
} else if (BrowserDetect.browser == "Explorer" && BrowserDetect.version == "7") {
	browserID = 3;
}
///////////////////
function addEngine(name,name2,ext,cat) {
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addSearchEngine == "function"))
	{
		window.sidebar.addSearchEngine(
		
			"http://tratu.baamboo.com/searchbox/"+name+".src",
			"http://tratu.baamboo.com/searchbox/"+name2+"."+ext,
			name,cat);
	}		
}

function addIE(name)
{
    window.external.AddSearchProvider(name);        
}

//--------------------------------------------------------
//NgaTT: functions to manipulate multiple dict and cookies
//--------------------------------------------------------

function sBoxDict(dict) {		
	sBoxDictHiddenObj = document.getElementById('sBoxDictHidden');
	if (sBoxDictHiddenObj) {
		sBoxDictHiddenObj.value = dict;
	}
}

function getDictName(dict) {
	switch (dict) {
		case 'en_vn': result = 'Từ điển Anh - Việt'; break;
		case 'vn_vn': result = 'Từ điển Việt - Việt'; break;
		default: result = 'Từ điển Anh - Việt';	
	}
	return result;
}

function getSearchFile(dict) {
	switch (dict) {
		case 'en_vn': result = 'BaambooTraTu(AV)'; break;
		case 'vn_vn': result = 'BaambooTraTu(VV)'; break;
		default: result = 'BaambooTraTu(AV)';
	}
	return result;	
}

function addSearchBox() {	
	dictName = document.getElementById('sBoxDictHidden').value;
	dictFile = getSearchFile(dictName);	
	ieFile = 'http://tratu.baamboo.com/searchbox/' + dictFile + '.xml';	
	if(browserID=='3') {		
		addIE(ieFile);
		//incCounter();
	}
	else if(browserID=='1') {
		addEngine(dictFile,'BaambooTraTu','png','Dictionary');
		//incCounter();
	}
	setCookieSearchBox(dictName, '1');
	closeAddSearchBox();
	
}

function doNotOpen() {
	setCookieSearchBox( 'en_vn' , '1');
	setCookieSearchBox( 'vn_vn' , '1');
	closeAddSearchBox();
}

function setCookieSearchBox(dict, value) {	
	BBTTcreateCookie('isAddedBBTratu_vuvu_'+dict,value,100);	
}

function incCounter() {
	new Ajax.Request('searchbox/counter.php?add=1',
	  {});
}

//---------------//
function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function overlay(curobj, subobjstr, opt_position){
if (document.getElementById){
var subobj=document.getElementById(subobjstr)
subobj.style.display=(subobj.style.display!="block")? "block" : "none"
var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)
if(opt_position=='above'){
	var xpos=getposOffset(curobj, "left")-198;
	var ypos=getposOffset(curobj, "top")-108;
}

subobj.style.left=xpos+"px"
subobj.style.top=ypos+"px"

return false
}
else
return true
}

function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}


function BBShowText()
{
	html+='<link rel="stylesheet" type="text/css" href="'+wgStylePath+'style.css" />';
	
	html+=''+
	'	<div id="SelectDictionary" style="display: none;">\n'+
	'	Hướng dẫn cài đặt plugin trên hệ trình duyệt<br />\n'+
	'	Mục đích: Tra cứu nghĩa của từ trên tất cả các website<br />\n'+
	'	<br />\n'+
	'	<strong>Các bước cài đặt</strong><br />\n'+
	'	<br />\n'+
	'	Bước 1: Chọn loại từ điển mình muốn tra nghĩa từ <br />\n'+
	'				  (A-V : Anh Việt hoặc V-V : Việt –Việt)<br />\n'+
	'	Bước 2 : Ấn vào nút “Tra nghĩa từ tổng hợp”. Một ô cửa sổ <b>Add Search Engine</b> hiện ra<br />\n'+
	'	Bước 3: Click để chọn tính năng Start using it right away<br />\n'+
	'	Bước 4: Ấn nút Add : Bạn đã cài xong plugin vào hệ trình duyệt<br />\n'+
	'	<br />\n'+
	'	<strong>Cách sử dụng:</strong><br />\n'+
	'	<br />\n'+
	'	Bước 1: Đánh chữ cần tra vào ô tìm kiếm ở ngoài cùng bên phải của màn hình trên hệ trình duyệt<br />\n'+
	'	Bước 2: Ấn biểu tượng hình kính lúp để tra từ<br />\n'+
	'	</div>';
	
	html+='\n'+ 
	'	<div id="divBoxSearch" name="divBoxSearch">\n'+
	'        <div id="AddToolboxl1_pnlAddSearchBox" align="right">\n'+   
	'			<table border="0" cellpadding="0" cellspacing="0" width="210">\n'+
	'                <tbody>\n'+
	'                <tr>\n'+         	
	'					<td colspan="2" align="right">\n'+
	'                    <a id="closeButton" onclick="closeAddSearchBox()" style="cursor:pointer;"><img src="'+wgStylePath+'close.jpg" style="padding:2px 0 0 10px;" height="9px" width="9px"></a> </td>\n'+
	'                </tr>\n'+
	'                <tr style="height: 28px; vertical-align: middle;">\n'+
	'					<td height="22px" align="center" colspan="2">\n'+
	'                        <a onClick="addSearchBox();"><img id="addButton" src="'+wgStylePath+'addbutton.jpg" style="border-width: 0px;"></a>\n'+
	'						<input type="hidden" id="sBoxDictHidden" name="sBoxDictHidden" value="en_vn">\n'+
	'					</td>\n'+
	'                  </tr>\n'+
	'                <tr align="center">\n'+
	'                    <td width="50%"><div class="divRadioText" style="font-size:11px;">\n'+
	'							<input type="radio" onclick="sBoxDict(\'en_vn\');" name="boxDict" value="en_vn" checked="checked" /> A-V \n'+
	'							<input type="radio" onclick="sBoxDict(\'vn_vn\');" name="boxDict" value="vn_vn" /> V-V\n'+
	'						</div></td>\n'+
	'                    <td width="50%">\n'+					      
	'						<span onclick="return overlay(this, \'SelectDictionary\', \'rightbottom\');" style="cursor:pointer;"><img src="'+wgStylePath+'cahsudung.jpg" border="0" /></span>\n'+
	'					</td>\n'+
	'                </tr>\n'+
	'            </tbody></table>\n'+
	'		</div>\n'+
	'	</div>';
	document.writeln(html);

}

if (BBTTcheckCookieSearchBox() || browserID=='2') {
	closeAddSearchBox();
}
else
{
	window.onload = BBShowText();	
}