// focus used by all popups
function openFocus() {
    window.focus();
}

// handle contitional link on popus opened by outside sites
// variable used in all popups
var openedByOutside = false;
if (opener == null) {
    this.openedByOutside = true;
} else if (!opener.openedByOutside) { // opened by outside js
    // alert("not opened by 1704");
    this.openedByOutside = true;
}
 
var switcher = "js switcher hasn't been set";
// hide switcher from non-js browser
function setSwitcher() {
    // alert(" got to switcher, maybe set var, then write");
    this.switcher = '<ul>' +
        '<li><a href="javascript:window.print()"><img src="/mgg/common/images/icon_print.gif" alt="print" width="53" height="20" border="0" align="middle"></a></li>' +
        '<li class="label">Text size: </li> ' +	
        '<li id="style-left"><a href="styleswitch.html" onclick="activeCSS(\'smalltext\'); return false">A</a></li>' +
        '<li id="style-mid"><a href="styleswitch.html" onclick="activeCSS(\'default\'); return false">A</a></li>' +
        '<li id="style-right"><a href="styleswitch.html" onclick="activeCSS(\'largetext\'); return false">A</a></li>' +
        '</ul>';
}
// hide switcher from non-js browser
function setNoPrintSwitcher() {
    // alert(" got to switcher, maybe set var, then write");
    this.switcher = '<ul>' +
        '<li class="label">Text size: </li> ' +	
        '<li id="style-left"><a href="styleswitch.html" onclick="activeCSS(\'smalltext\'); return false">A</a></li>' +
        '<li id="style-mid"><a href="styleswitch.html" onclick="activeCSS(\'default\'); return false">A</a></li>' +
        '<li id="style-right"><a href="styleswitch.html" onclick="activeCSS(\'largetext\'); return false">A</a></li>' +
        '</ul>';
}

// --------------- GLOSSARY  ----------------
// for each site change the hard wired site paramter  e.g. mgg
// this file is site-specific i.e. /mgg/common/scripts.js
function glossary(invariant) {
    var theURL = "/stories/mgg/resources/glossary.do?invariant=" + invariant;
    window.open(theURL,'glossary','location=yes,scrollbars=yes,resizable=yes,width=350,height=350')
}

// --------------- ARTIFACT  ----------------
function artifact(shortName) {
    var theURL = "/stories/mgg/resources/artifact.do?shortName=" + shortName;
    window.open(theURL,'artifact','location=yes,scrollbars=yes,resizable=yes,width=800,height=700')
}

// --------------- PERSON  ----------------
function person(shortName) {
    var theURL = "/stories/mgg/resources/person.do?shortName=" + shortName;
    window.open(theURL,'person','location=yes,scrollbars=yes,resizable=yes,width=700,height=550')
}

// --------------- CUSTOM  ----------------
function custom(shortName) {
    var theURL = "/stories/mgg/resources/custom.do?shortName=" + shortName;
    window.open(theURL,'custom','location=yes,scrollbars=yes,resizable=yes,width=600,height=550')
}

// --------------- ATTACHMENT  ----------------
// attachments PDFs
function attachment(shortName, sourceSiteName) {
    var theURL = "/" + sourceSiteName + "/teach/attachments/" + shortName + ".pdf";
    window.open(theURL,'attachment','location=yes,scrollbars=yes,resizable=yes,width=700,height=650')
}

// --------------- INVESTIGATE FURTHER  ----------------
function further(level, id) {
    var theURL = "/stories/mgg/" + level + "/further.do?id=" + id ;
    window.open(theURL,'further','location=yes,scrollbars=yes,resizable=yes,width=600,height=500')
}

// --------------- HTML TIMELINE BOOK  ----------------
function book(shortName) {
    var theURL = "/mgg/timeline/media/web_assets/xml/" + shortName + ".xml";
    window.open(theURL,'book','location=yes,scrollbars=yes,resizable=yes,width=600,height=550')
}

// --------------- PRINTABLE (HTML) SCENE SLIDESHOW  ----------------
function htmlslide(id) {
    var theURL = "/stories/mgg/htmlslide.do?id=" + id ;
    window.open(theURL,'htmlslide','location=yes,scrollbars=yes,resizable=yes,width=500,height=500')
}


// --------------- CSS FUNCTIONS ----------------
// css styleswitcher for user-controlled text sizing
// activeCSS: Set the active stylesheet
function activeCSS(title) {
  var i, oneLink;
  for (i = 0; (oneLink = document.getElementsByTagName("link")[i]); i++) {
    if (oneLink.getAttribute("title") && findWord("stylesheet", oneLink.getAttribute("rel"))) {
      oneLink.disabled = true;
      if (oneLink.getAttribute("title") == title) {
        oneLink.disabled = false;
      }
    }
  }
}

// findWord: Used to find a full word (needle) in a string (haystack)
function findWord(needle, haystack) {
  return haystack.match(needle + "\\b");
}

// Set the cookie 
function setCookie(name,value,days) {
 if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = ";expires="+date.toGMTString();
 } else {
   expires = "";
 }
 document.cookie = name+"="+value+expires+";";
}

// Set the active stylesheet
function activeCSS(title) {
  var i, oneLink;
  for (i = 0; (oneLink = document.getElementsByTagName("link")[i]); i++) {
    if (oneLink.getAttribute("title") && findWord("stylesheet", oneLink.getAttribute("rel"))) {
 oneLink.disabled = true;
      if (oneLink.getAttribute("title") == title) {
         oneLink.disabled = false;
      }
    }
 }
 setCookie("mystyle", title, 365);
}
window.onload = initCSS;

// initCSS: If there's a "mystyle" cookie, set the active stylesheet when the page loads
function initCSS() {
 var style = readCookie("mystyle");
 if (style) {
   activeCSS(style);
 }
}

// Read the cookie
function readCookie(name) {
 var needle = name + "=";
 var cookieArray = document.cookie.split(';');
 for(var i=0;i < cookieArray.length;i++) {
   var pair = cookieArray[i];
   while (pair.charAt(0)==' ') {
     pair = pair.substring(1, pair.length);
   }
   if (pair.indexOf(needle) == 0) {
     return pair.substring(needle.length, pair.length);
   }
 }
 return null;
}
// end of style switcher code


// --------------- TEMPORARY FUNCTIONS ----------------
// temp - for models only

function popupGlossary(theURL) {
window.open(theURL,'popup','scrollbars=yes,resizable=yes,width=350,height=350')
}
function popupResource(theURL) {
window.open(theURL,'popup','scrollbars=yes,resizable=yes,width=500,height=550')
}
function popupOutside(theURL) {
window.open(theURL,'popup','scrollbars=yes,resizable=yes,location=yes,status=yes,menubar=yes')
}
function popupWindow(theURL, windowName) {
    if (windowName == undefined) {
        windowName = "popup";
    }
    window.open(theURL,windowName,'scrollbars=yes,resizable=yes,width=550,height=450')
}
