
//In the letters array of images, for each letter
//which array element is lowercase
var lettersLowercaseIndex = 0;
//...and which is uppercase
var lettersUppercaseIndex = 1;

var Options = { shuffleObjects: true,
                shuffleLetters: false,
                attributionMode: false,
                sounds: true,
                canPlaySounds: true,  //we'll try to play a sound to test this in UpdateOptionsDialog()
                lettersImgIndex: lettersLowercaseIndex };

var _optionsCookieName='alphabetoptions';                

//We want to load the options from a cookie if set - do it now.               
LoadOptions();

function ToggleShuffleObjects(shuffle) 
{
   if(shuffle) {
      Options.shuffleObjects = true;
   } else {
      Options.shuffleObjects = false;
   }   
   //Populate all grids to get rid of ticks in letters as well as objects
   FillGrids()
   SaveOptions();
}

function ToggleShuffleLetters(shuffle) 
{
   if(shuffle) {
      Options.shuffleLetters = true;
   } else {
      Options.shuffleLetters = false;
   }   
   //Populate all grids to get rid of ticks in objects as well as letters
   FillGrids();
   SaveOptions();
}

function ToggleUppercaseLetters(useUpperCase)
{
   if(useUpperCase) {
      Options.lettersImgIndex = lettersUppercaseIndex;
   } else {
      Options.lettersImgIndex = lettersLowercaseIndex;
   }   
   //Populate *both* grids to get rid of ticks in objects as well asletter
   FillGrids();
   SaveOptions();
}

function ToggleAttributionMode(mode)
{
   if(mode) {
      Options.attributionMode = true;
      AddAttributionLinks();
   } else {
      Options.attributionMode = false;
      RemoveAttributionLinks();
   }
   SaveOptions();
}

function ToggleSounds(soundsOn)
{
   if(soundsOn) {
     Options.sounds = true;
   } else {
     Options.sounds = false;
   }
   SaveOptions();
}


function SaveOptions()
{
  //Set the Expiry date to be a year from now
   var date = new Date();
   date.setTime(date.getTime()+(365*24*60*60*1000));

   var expirestring = '; expires='+date.toGMTString();

   var value='';
   if(Options.shuffleObjects) {
      value +="shuffleObjects=1";
   } else {
      value +="shuffleObjects=0";
   }
   if(Options.shuffleLetters) {
      value +=":shuffleLetters=1";
   } else {
      value +=":shuffleLetters=0";
   }
   if(Options.attributionMode) {
      value +=":attributionMode=1";
   } else {
      value +=":attributionMode=0";
   }
   if(Options.sounds) {
      value +=":sounds=1";
   } else {
      value +=":sounds=0";
   }
   
   value +=":lettersImgIndex="+Options.lettersImgIndex;
 
   document.cookie = _optionsCookieName+'='+value+expirestring+'; path=/';
}

function strip(string)
{
     return string.replace(/^\s*|\s*$/g,'');
}


function LoadOptions()
{
   var cookiesearchstring = _optionsCookieName + '=';
   var optionscookie = null;
   
   //Create an array of all the cookies we can access
   var setcookies=document.cookie.split(';');

   for(var i = 0; i < setcookies.length; i++) {
      var cookie = setcookies[i];

      strip(cookie);
		
      if (cookie.indexOf(cookiesearchstring) == 0) {
         optionscookie = cookie;
         break;
      }
   }

   if(optionscookie) {
      //Just take the value from the name=value format cookie
      var equalpos=optionscookie.indexOf('=');
      var optionscookievalue=optionscookie.slice(equalpos+1);
  
      var options = optionscookievalue.split(':');
      
      for(var i = 0; i < options.length; i++) {
         var option = options[i];
         var namevaluearray = option.split('=');
         
         if(namevaluearray.length == 2) {
            var optionname  = strip(namevaluearray[0]);
            var optionvalue = strip(namevaluearray[1]);
            
            switch(optionname) {
               case 'shuffleLetters':
               case 'shuffleObjects':
               case 'attributionMode':
               case 'sounds':
                  if(parseInt(optionvalue)) {
                     Options[optionname] = true;
                  } else {
                     Options[optionname] = false;
                  }
                  break;

               case 'lettersImgIndex':
                  Options[optionname] = parseInt(optionvalue);
                  break
                  
               default:
                  alert('Error #1 in cookie: '+optionscookie);
            }
         } else {
            alert('Error #2 in cookie: '+optionscookie+'.Option was: '+option+' array length was: '+namevaluearray.length);
         }
      }      
   }
}

function CheckCanPlaySounds()
{
   try {
      var canPlayOgg = false;
  
      var audioTag = document.createElement('audio');

      if(audioTag.canPlayType) {
         var oggSupport = audioTag.canPlayType("audio/ogg");
  
         // Currently canPlayType(type) returns: "no", "maybe" or "probably"
         if( ("no" != oggSupport) && ("" != oggSupport)) {
            canPlayOgg = true;
         }
    
         //We currently only support Ogg
         Options.canPlaySounds = canPlayOgg;
      } else {
         Options.canPlaySounds = false;
      }
   } catch(e) {
      Options.canPlaySounds = false;
   }
}

function UpdateOptionsDialog()
{
   CheckCanPlaySounds();
   
   var shuffleletters_cbox = document.getElementById('shuffleletters');
   
   if(shuffleletters_cbox) {
      if(Options.shuffleLetters) {
         shuffleletters_cbox.checked = true;
      } else {
         shuffleletters_cbox.checked = false;
      }
   }
   var shuffleobjects_cbox = document.getElementById('shuffleobjects');
   
   if(shuffleobjects_cbox) {
      if(Options.shuffleObjects) {
         shuffleobjects_cbox.checked = true;
      } else {
         shuffleobjects_cbox.checked = false;
      }
   }
   
   var attributionmode_cbox = document.getElementById('attributionmode');
   
   if(attributionmode_cbox) {
      if(Options.attributionMode) {
         attributionmode_cbox.checked = true;
      } else {
         attributionmode_cbox.checked = false;
      }
   }   

   var sounds_cbox = document.getElementById('sounds');
   
   if(sounds_cbox) {
      if(Options.sounds) {
         sounds_cbox.checked = true;
      } else {
         sounds_cbox.checked = false;
      }
      if(!Options.canPlaySounds) {
         sounds_cbox.disabled=true;
      }
   }   
   
   var useuppercase_cbox = document.getElementById('useuppercase');
   
   if(useuppercase_cbox) {
      if( Options.lettersImgIndex == lettersUppercaseIndex ) {
         useuppercase_cbox.checked = true;
      } else {
         useuppercase_cbox.checked = false;
      }
   }
}
