// does google toolbar like translation of words and selected text
// works on ie and firefox, epiphany, safari, and Opera but still has bugs on Konqueror
//
//  author: Paul Cheffers paul@securecottage.com
//  Copyright 2008
//  all rights reserved
//

var SCGoogleAPIKey = "ABQIAAAAIL6lIdsFmMh3QB6iZZYqHBT5zsu4IrEqi6aTPGb6EkJ6C2zK0BQyWgLmv16JfxNy3RYUKg7GyR6XAg";
var SCstrTranslationPopups = "GoogleTrans -";
var SCstrOn = "ON";
var SCstrOff = "OFF";
var SCstrLoading = "LOADING";

var SCtranslate="FrenchToEnglish";//redundant
var SCtranslateFrom = "en"; // any language
var SCtranslateTo = "fr";

function SCappendCSS(text) {
	var s = document.createElement('style');
	s.type = 'text/css';
	s.rel = 'stylesheet';
	if (s.styleSheet) s.styleSheet.cssText = text //IE
	else s.appendChild(document.createTextNode(text + '')) //Safari sometimes borks on null
	document.getElementsByTagName('head')[0].appendChild(s);
	return s;
}

SCappendCSS(
'.highlight {border:1px solid ' + 'black;background-color:pink; ' +
'             padding:4px;} ' +
'  .userData { behavior:url(#default#userdata);} ' +
'#SCitem span small, #SCitem small ' + '{font-size:80%;}' +
'#SCitem, #SCitem span {color:black}'
);

SCappendCSS(
 '.gBrandingText {background : white;   }'
);


var SCloadedScripts = {}; // included-scripts tracker
function SCimportScriptURI(url) {
	if (SCloadedScripts[url]) {
		return null;
	}
	SCloadedScripts[url] = true;
	var s = document.createElement('script');
	s.setAttribute('src',url);
	s.setAttribute('type','text/javascript');
	document.getElementsByTagName('head')[0].appendChild(s);
	return s;
}

function SChookEvent(hookName, hookFunct) {
	if (window.addEventListener) {
		window.addEventListener(hookName, hookFunct, false);
	} else if (window.attachEvent) {
		window.attachEvent("on" + hookName, hookFunct);
	}
}


 
var SCbGoogleLanguageLoaded = false;
function SCGoogleLanguageLoaded() {
   SCbGoogleLanguageLoaded = true;
  //alert("Google language is loaded"); 
}
 
function SCloadGoogleLanguage() {
  google.load("language", "1", {"callback" : SCGoogleLanguageLoaded,"locale":SCtranslateTo,"language":SCtranslateTo});
}

function SCImportScripts()
{
 var hl = "";
 if(typeof SCtranslateTo != "undefined" &&
    SCtranslateTo != "")
    hl = "&" + "hl=" + SCtranslateTo;
  
  SCimportScriptURI("http://www.google.com/jsapi?callback=SCloadGoogleLanguage&key="+SCGoogleAPIKey + hl);

if(navigator.appVersion.match(/MSIE/i)||
   navigator.appName.match(/Netscape/i)
)
  SCimportScriptURI("http://www.securecottage.com/dictionarylookup15a.js");
else
  SCimportScriptURI("http://www.securecottage.com/dictionarylookup15b.js");

  }
 
 function SConloadscitem() {
        var span1 = document.createElement('DIV');
        span1.setAttribute('id','SCitem');
        var thebody = document.getElementsByTagName('BODY');
        thebody[0].appendChild(span1);

     var span2 = document.createElement('SPAN');
        span2.setAttribute('id','SCPersistElement');
        span2.setAttribute('class','userData');
        thebody[0].appendChild(span2);

      SCImportScripts();

     if(typeof SCbDictionaryLookup12aLoaded != "undefined" &&
        SCbDictionaryLookup12aLoaded)
        SCsetLanguages()
     else setTimeout("SCTimeoutsetLanguages()",1000);

    }

function SCTimeoutsetLanguages()
{
    
    if(typeof SCbDictionaryLookup12aLoaded != "undefined" &&
        SCbDictionaryLookup12aLoaded)
        SCsetLanguages()
    else setTimeout("SCTimeoutsetLanguages()",1000);
}
//SChookEvent("load",SConloadscitem);

function SCPersistantSave(key,value)
{
  try {
/*
   if(SCbIsIE)
       SCUserDataSave(key,value);
   else if(typeof globalStorage != "undefined") 
       SCGlobalStorageSave(key,value);
   else */
   SCsetcookieVal(key,value);
  }
  catch(err) { }
}
function SCPersistantLoad(key)
{
   var retValue="";
   try {
   /*
   if(SCbIsIE)
       return SCUserDataLoad(key);
   else if(typeof globalStorage != "undefined") 
       return SCGlobalStorageLoad(key);
   else */
   retValue = SCgetcookieVal(key);
   }
   catch(err) { retValue = ""; }

   return retValue;
 
}

function SCUserDataSave(key,value)
{
  SCPersistElement.setAttribute(key,value);
  SCPersistElement.save("oXMLStore");
}
function SCUserDataLoad(key)
{
  SCPersistElement.load("oXMLStore");
  var retc = SCPersistElement.getAttribute(key);
  if(retc == null)
    retc = "";
  return retc;
}
function SCGlobalStorageSave(key,value)
{
  if(globalStorage)
  {
    eval ("globalStorage['www.securecottage.com']." +
          key + "='" + value + "'");
  }
}
function SCGlobalStorageLoad(key)
{
  var retc = "";
  if(globalStorage)
  {
     retc =     eval ("globalStorage['wikipedia.org']." +
          key );
     if (retc == null)
        return "";
     
     return retc.value;
     
  }
  return "";
}

function SCsetcookieVal(cookieKey,cookieValue)
{
   var ExpireDate = new Date();
   var days=180;
   ExpireDate.setTime(ExpireDate.getTime()+(days*24*60*60*1000));

   document.cookie =  cookieKey + '=' + cookieValue + 
          "; path=/; " +
          "domain=" + document.domain + "; " 
          + "expires=" + ExpireDate.toGMTString();
}

function SCgetcookieVal(cookieName)
{
  
  var aCookie = "";
  var thisCookie;
  
  
  aCookie = document.cookie;
  thisCookie = aCookie.split("; ");

  var i;
  var retCookie = "";
  for(i=0;i<thisCookie.length;i++)
  {
     if(cookieName == thisCookie[i].split("=")[0].substring(0,cookieName.length))
     {
           retCookie = thisCookie[i].split("=")[1];
           break;
     }
  }
  
  return retCookie;

}

var SCTranslationPopups=false;
var SCSettingsUponStartup = false;

if(SCPersistantLoad('TranslationPopups') == '1')
   SCTranslationPopups = true;


if(SCTranslationPopups)
   SConloadscitem();

function SCdoTranslationPopups()
{
   var bIsOn = SCPersistantLoad('TranslationPopups');
   if(typeof SCbDictionaryLookup12aLoaded == "undefined"
      && (bIsOn == "0" || bIsOn == ""))
   {
      SCSettingsUponStartup = true;
      SConloadscitem();
      SCTranslationPopups = true;
   }
   else if(typeof SCbDictionaryLookup12aLoaded != "undefined")
   {
     SCSettings();
     SCTextSettings();
   }
   
   SCTextSettings();
}
function SCTextSettings()
{
  
  
  SCPersistantSave('TranslationPopups',(SCTranslationPopups)?"1":"0");
  var popsid = document.getElementById("SCTranslationPopups");
  if(!popsid)
      setTimeout("SCTextSettings()",1000);
  
//  var anchor1 = popsid.getElementsByTagName( "a" )[0];
  var anchor1 = popsid;
  if(!anchor1)
     return;

  var text1; 
  if(SCTranslationPopups &&
     typeof SCbDictionaryLookup12aLoaded == "undefined")
  {
   text1 = document.createTextNode(SCstrTranslationPopups + SCstrLoading);
  }
 
  else 
  if(SCTranslationPopups &&
     typeof SCbDictionaryLookup12aLoaded != "undefined")
    text1 = document.createTextNode(SCstrTranslationPopups + SCstrOn);
  else 
    text1 = document.createTextNode(SCstrTranslationPopups + SCstrOff);
  

 // delete text from anchor in tab
 while(anchor1.childNodes.length>0)
 {
   anchor1.removeChild(anchor1.childNodes[0]);
 }
 // replace text in anchor in tab
 anchor1.appendChild(text1);
   
if(typeof SCbGoogleLanguageLoaded != "undefined" &&
   SCbGoogleLanguageLoaded)
{

google.language.getBranding('SCTranslationPopups');
}

}

SCTextSettings();
var SCbDictionarylookup12Loaded = true;
