//
// This file contains all the scripts to handle the spoiler settings.
// It includes creating, fetching  and deleting cookies, writing the
// spoiler form, and changing the second stylesheet to the appropriate
// file.  It needs to be called at page load to check if the user has
// already entered their spoiler settings on another page.
//
///////////////////////////////////////////////////////////////////////

//Creates a cookie with a name of [name] and saves value [value] in it.
function setCookie (name, value) {
   var curCookie = name + "=" + value + ";";
   document.cookie = curCookie;
}//End function setCookie (value)

//Finds a cookie with name of [name] and returns its value.
function getCookie (name) {
   var dc = document.cookie;
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0)
         return null;
   }//End if (begin == -1)
   else
      begin += 2;
   var end = document.cookie.indexOf(";", begin);
   if (end == -1)
      end = dc.length;
   return unescape(dc.substring(begin + prefix.length, end));
}//End function getCookie (name)

//Deletes the cookie named [name]
function deleteCookie (name) {
   if (getCookie("stylesheet")) {
      document.cookie = name + "=" +
         "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }//End if (getCookie("stylesheet"))
}//End function deleteCookie()

//Prints out the spoiler setting control panel
function form () {

   //Creating a temporary test cookie.
   setCookie( "test", "none");

   var test = getCookie('test');

   //If getCookie succeeds, cookies are enabled, since the cookie was successfully created.
   if (test != null) {

      //Deleting the test cookie since it's no longer needed.
      deleteCookie("test");

      //Creating the form and beginning the current setting message.
      var print = "   <A NAME='controlpanel'></A>\n";
      print = print + "   <DIV CLASS='spoilerform'>Spoiler Settings Control Panel</DIV>\n";
      print = print + "   <FORM NAME='spoilercheck'>\n      <DIV CLASS='setting'><B>Setting:</B> ";
      print = print + "<FONT CLASS='d'>Censor all spoilers</FONT>";
      print = print + "<FONT CLASS='e-a'>English anime info</FONT>";
      print = print + "<FONT cLASS='e-m'>English manga info</FONT>";
      print = print + "<FONT cLASS='j-a'>Japanese anime info</FONT>";
      print = print + "<FONT CLASS='j-m'>All info uncensored</FONT>";
      print = print + "</DIV>";

      //Gets the cookie containing the spoiler settings.
      var name = getCookie("stylesheet");

      //To be used when determining which button should be checked on default.
      var dchecked = (name == null || name == "default.css") ? " checked" : null;
      var eachecked = (name == "e-anime.css") ? " checked" : null;
      var emchecked = (name == "e-manga.css") ? " checked" : null;
      var jachecked = (name == "j-anime.css") ? " checked" : null;
      var jmchecked = (name == "j-manga.css") ? " checked" : null;

      //Creating the cookie form.
      print = print + "   <FORM NAME='spoilercheck'>\n";
      print = print + "      <INPUT TYPE=\"radio\" NAME=\"spoiler\" onClick=\"setCookie('stylesheet', 'default.css'); stylesheet('default.css')\"" + dchecked + "> Censor all spoilers<BR>\n";
//      print = print + "      <INPUT TYPE=\"radio\" NAME=\"spoiler\" onClick=\"setCookie('stylesheet', 'e-manga.css'); stylesheet('e-anime.css');\"" + eachecked + "> Only English anime info<BR>\n";
      print = print + "      <INPUT TYPE=\"radio\" NAME=\"spoiler\" onClick=\"setCookie('stylesheet', 'e-manga.css'); stylesheet('e-manga.css');\"" + emchecked + "> Only English manga info<BR>\n";
      print = print + "      <INPUT TYPE=\"radio\" NAME=\"spoiler\" onClick=\"setCookie('stylesheet', 'j-anime.css'); stylesheet('j-anime.css');\"" + jachecked + "> Only Japanese anime info<BR>\n";
      print = print + "      <INPUT TYPE=\"radio\" NAME=\"spoiler\" onClick=\"setCookie('stylesheet', 'j-manga.css'); stylesheet('j-manga.css');\"" + jmchecked + "> All info uncensored<BR>\n";
      print = print + "   </FORM>";

      //Write the form
      document.write (print);
   }//End if (getCookie("test"))
}//End function form ()

//Prints out the explanation of the spoiler setting control panel if the user sees it.
function message () {

   //Creating a temporary test cookie.
   setCookie( "test", "none");

   var test = getCookie('test');
   var text;

   //If getCookie succeeds, cookies are enabled, since the cookie was successfully created.
   if (test != null) {

      //Print the control panel message.
      text = "   <P>\nThis site assumes you have at least read throught the most recent English manga volume of Bleach.";
      text = text + " However, it lists information up through the most recent Japanese manga volume to appear in";
      text = text + " <A HREF='http://jump.shueisha.co.jp/bleach/index.html'><I>Weekly Shonen Jump Magazine</I></A>.";
      text = text + " You can customize how much or how little of this information appears in the site by using the";
      text = text + " <A HREF='#controlpanel'>spoiler settings control panel</A> in the sidebar on any page. The option";
      text = text + " \"censor all spoilers\" will make any information past the most recent English manga volume";
      text = text + " appear as white text that can be highlighted if you wish to read it.</P>\n";

      document.write (text);
   }//End if (getCookie("test"))

   //Else if cookies are not enabled.
   else {

      //Print the first spoiler explanation paragraph
      text = "   <P>\nThis site assumes you have at least read throught the most recent English manga volume of Bleach.";
      text = text + " However, it lists information up through the most recent Japanese manga volume to appear in";
      text = text + " <A HREF='http://jump.shueisha.co.jp/bleach/index.html'><I>Weekly Shonen Jump Magazine</I></A>.";
      text = text + " If you can enable your browser to use cookies you can customize how much or how little of this";
      text = text + " information appears in the website.  However, if you cannot or do not want to enable cookies, any";
      text = text + " information past the most recent English manga volume will appear as white text that can be";
      text = text + " highlighted if you wish to read it.</P>\n";

      document.write (text);
   }//End else (!getCookie("test"))
}//End function message()

//Creates a cookie calling setCookie and stores a value [filename] in it.
function stylesheet (filename) {

   //Making a copy of the head (needed to add and delete the link to the stylesheet)
   var head = document.getElementsByTagName("head").item(0);

   //Making a copy of the current stylesheet link (so it can be deleted)
   var sheet = document.getElementById("spoilersheet");

   //Creating a new stylesheet link
   //The last line is important because the ID for the stylesheet link needs to remain the same.
   var spoilersheet = document.createElement("link");
   spoilersheet.rel = "stylesheet";
   spoilersheet.type = "text/css";
   spoilersheet.media = "screen";
   spoilersheet.href = filename;
   spoilersheet.id = "spoilersheet";

   //Deleting the old stylesheet link
   head.removeChild(sheet);

   //Adding the new stylesheet link to the head
   head.appendChild(spoilersheet);
}//End function stylesheet (filename)


/////////////////////////////////////
//
//  To be executed upon page load
//
/////////////////////////////////////

//Getting the current spoiler setting
var setting = getCookie("stylesheet");

//If the spoiler setting is not default, change it to the correct one.
if (setting != null && setting != "default") 
   stylesheet(setting);
