﻿var SND_CLIP_PLAYER = "clipplayer/player2.swf";
var SND_CLIP_LINK_TITLE = "Click the word to play sound clip or right-click on the word and select Save As... to save the sound clip.";
var SND_CLIP_LIMIT_PER_PAGE_IE = 40;
var SND_CONTAINER_ID = "__flashsound_container";
var isIE = false;

var TXT_EXCEED_LIMIT_EN = "Why I can't play sound clips directly on this page? Internet Explorer users please read...";
var TXT_EXCEED_LIMIT_CN = "为什么我无法直接在该页中播放声音？ Internet Explorer 用户请阅读此处...";

function SndClip_Init()
{
	var arrLinks = document.getElementsByTagName("a");
	var arrTargetLinks = new Array();
	for (var i=0;i<=arrLinks.length-1;i++) {
		var oLink = arrLinks[i];
		if ((new String(oLink.className)).toLowerCase().indexOf("sound_clip")=="-1" && (new String(oLink.rel)).toLowerCase().indexOf("pronunciation")=="-1") continue;
		arrTargetLinks[arrTargetLinks.length] = oLink;
	}
	//alert(document.getElementById("iebar").style.display);

	/*
	if (  !fnIsBrowserCapable()) {
		if (location.href.indexOf("_cn.html")!=-1) {
			document.getElementById("iebar").getElementsByTagName("a")[0].innerHTML = TXT_EXCEED_LIMIT_CN;
		}
		else {
			document.getElementById("iebar").getElementsByTagName("a")[0].innerHTML = TXT_EXCEED_LIMIT_EN;
		}
		return;
	}
	*/
	
	for (var i=0;i<=arrTargetLinks.length-1;i++) {
	  //for (var i=0;i<=14;i++) {
		var oLink = arrTargetLinks[i];
		//document.write(SndClip_GenCode(oLink.href));
		//oLink.innerHTML = SndClip_GenCode(oLink.href) + "&nbsp;" + oLink.innerHTML;
		//oLink.innerHTML = "<span></span>&nbsp;" + oLink.innerHTML;
		//oLink.onmouseover = fnLnkMsOver;
		//oLink.onmouseout = fnLnkMsOut;
		oLink.className = "pronunciation";
		oLink.title += "\n" + SND_CLIP_LINK_TITLE;
		oLink.onclick = fnPlaySound;
		oLink.style.textDecoration = "none";
	}
}

function fnPlaySound(evt) {
	evt = evt || window.event;
	fnCancelEvent(evt);
	var oCon = document.getElementById(SND_CONTAINER_ID);
	if ( oCon == null) {
		oCon = document.createElement("div");
		oCon.id = SND_CONTAINER_ID;
		oCon.style.background = "#00f";
		oCon.style.position = "absolute";
		oCon.style.top = "-100%";
		oCon.style.left = "-100%";
		//oCon.style.visibility= "hidden";
		document.getElementsByTagName("body")[0].appendChild(oCon);
	}

	oCon.innerHTML = SndClip_GenCode(this.href);

	return false;
}

function fnCancelEvent(evt) {
	try {
		evt.preventDefault(true);
	}
	catch(e) {
	    evt.returnValue = false;
	}
}

function fnIsBrowserCapable() {
	if (!isIE) return true;
	var oIeBar = document.getElementById("iebar");
	//alert(oIeBar.currentStyle.display);
	return (oIeBar.currentStyle.display=="none");
}

function SndClip_GenCode(sSoundFile) {
	var sRet = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" ';
	sRet += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"'
	sRet += '  class="sndclip_obj">';
	sRet += '<param name="allowScriptAccess" value="sameDomain" />';
	sRet += '<param name="movie" value="'+ SND_CLIP_PLAYER +'" />';
	sRet += '<param name="flashVars" value="' + 'autoload=1&autostart=1&file='+ sSoundFile +'" />';
	sRet += '<param name="loop" value="false" />';
	sRet += '<param name="menu" value="false" />';
	sRet += '<param name="quality" value="high" />';
	sRet += '<param name="scale" value="exactfit" />';
	sRet += '<param name="bgcolor" value="#cccccc" />';

	sRet += '<embed src="'+  SND_CLIP_PLAYER +  '" ';
	sRet += 'flashVars="'+ 'autoload=1&autostart=1&file=' + escape(sSoundFile) +'" ';
	sRet += 'loop="false" menu="false" quality="high" scale="exactfit" ';
	sRet += 'bgcolor="#cccccc" align="absmiddle" ';
	sRet += 'allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	sRet += '</object>';

	return sRet;
}

function fnAddEventListener(obj, name, handler)
{
	//alert(typeof(obj.attachEvent));
	if (typeof(obj.attachEvent)=="object") {
		obj.attachEvent("on" + name, handler);
	} else {
		obj.addEventListener(name, handler, false);
	}

};

function fnExternalLinks() {
	var arrLinks = document.getElementsByTagName("a");

	for (var i=0;i<=arrLinks.length-1;i++) {
		var oLink = arrLinks[i];
		if ((new String(oLink.rel)).toLowerCase() == "external")
		{
			oLink.target="_blank";
		}
	}
}

function fnSetFontStyleSheet(nMode , bSave)
{

	var sStyleFile = "";

	switch(nMode)
	{
		case 1:
			sStyleFile = "s/font_small.css";break;
		case 2:
			sStyleFile = "s/font_normal.css";break;
		case 3:
			sStyleFile = "s/font_big.css";break;
		case 4:
			sStyleFile = "s/font_large.css";break;
			
	}


	if (sStyleFile == "") return false;
	
	try
	{
		var oStyle = fnFontSizeStyle(); 
		
		oStyle.href = sStyleFile;
		oStyle.media = "all";
		//alert(oStyle.href);
		window.FontSize = nMode;
		if (bSave)
		{

		  var oExp = new Date(2100,12,31);
		  setCookie("Pref_Font_Size",  nMode ,oExp);

		}
		return true;
	}
	catch(e) {return false;}
}

function fnFontSizeStyle() {
	var oStl = document.getElementById("font_size_stylesheet");
	if (oStl!=null ) return oStl;

	var oNStl = document.createElement("link");
	oNStl.rel = "stylesheet";
	oNStl.id = "font_size_stylesheet";
	oNStl.type = "text/css";
	document.getElementsByTagName("head")[0].appendChild(oNStl);
	//alert(oNStl);
	return oNStl;
}

function fnToggleLayout(nLayout) {
  var oCnt = document.getElementById("main_wrap");
  if (!oCnt) return;

  var nCurrLayout = parseInt(getCookie("layout"));
  if (nCurrLayout == nLayout) return;

if (nLayout == -1) { // fluid layout:
if (window.external) {//IE
if (confirm("You need to reload this page to use the fluid layout.\n\nReload now?")) location.reload();
newCookie("layout" , nLayout); // Save Cookie;
return;
}

oCnt.style.margin = "0 10px 0 10px";
oCnt.style.width  = "auto";
newCookie("layout" , nLayout); // Save Cookie;
}

if (nLayout == 1) { // narrow layout:
oCnt.style.margin = "0 auto 0 auto";
oCnt.style.width  = "820px";
newCookie("layout" , nLayout); // Save Cookie;
}

if (nLayout == 2) { // bigger layout:
oCnt.style.margin = "0 auto 0 auto";
oCnt.style.width  = "1000px";
newCookie("layout" , nLayout); // Save Cookie;
}
}

function fnLoadSavedLayout() {
var nLayout = parseInt(getCookie("layout"));
if (isNaN(nLayout)) return;
var sHtm = "<style>";

sHtm += "#main_wrap{";

if (nLayout == -1) { // fluid layout:
sHtm += "margin:0 10px 0 10px;";
sHtm += "width:auto;";
}

if (nLayout == 1) { // narrow layout:
sHtm += "margin:0 auto 0 auto;";
sHtm += "width:820px;";

}

if (nLayout == 2) { // bigger layout:
sHtm += "margin:0 auto 0 auto;";
sHtm += "width:1000px;";
}
sHtm += "}</style>";
document.write(sHtm);
}

function getCookie(name) {
var cookiefound=false
var start=0
var end=0
var cookiestring=document.cookie;
var i=0;
while (i<=cookiestring.length)
{
  start=i
  end=start+name.length 
  if (cookiestring.substring(start,end)==name)
 {cookiefound=true;
  break;
}
i++;
}
if (cookiefound==true)
{
 start=end+1;
 end=cookiestring.indexOf(";",start);
 if (end<start)
{
 end=cookiestring.length;}
 return cookiestring.substring(start,end);
}
return "";
}

function setCookie(id,value,guoqi) 
{
var expires=new Date()
expires.setTime(expires.getTime()+24*60*60*3990*1000)
var expiryDate=expires.toGMTString();
document.cookie=id+"="+value+";expires="+expiryDate; //+";domain=/";
}

function fnHide(sid) {
var oObj = document.getElementById(sid);
if (oObj) oObj.style.display="none";
}


fnAddEventListener( window , "load", SndClip_Init);
fnAddEventListener( window , "load", fnExternalLinks);
//fnLoadSavedLayout();

if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {isIE=true;}


var nFontSize = parseInt(getCookie("Pref_Font_Size"));

if (!isNaN(nFontSize) )
{
	fnSetFontStyleSheet(nFontSize);
}
else
{
	window.FontSize = 2;//Normal;
}