
// this tells jquery to run the function below once the DOM is ready
$(document).ready(function showhide() {

var cookie= getCookie("boxes");
var showhide= cookie.split("|");

  // assign images for show/hide icons
  var showText='<img class="arrow" src="/images/downarrow.png" align="right" />';
  var hideText='<img class="arrow" src="/images/uparrow.png" align="right" />';

  // initialise the visibility check
  //var is_visible = true;

  // append show/hide links to the elements
  if(showhide[0]==1){
  $('#qlinks').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
  } else {
  $('#qlinks').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
  }
  
  if(showhide[1]==1){
  $('#news').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
  } else {
  $('#news').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
  }
  
  if(showhide[2]==1){
  $('#yourgroups').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
  } else {
  $('#yourgroups').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
  }
  
  if(showhide[3]==1){
  $('#tools').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
  } else {
  $('#tools').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
  }
  
  if(showhide[4]==1){
  $('#tagcloud').prev().append(' <a href="#" class="toggleLink">'+showText+'</a>');
  } else {
  $('#tagcloud').prev().append(' <a href="#" class="toggleLink">'+hideText+'</a>');
  }
  
  if(showhide[5]==1){
  $('#slideshow').prev().append('<a href="#" class="toggleLink"> Show Slideshow </a>');
  } else {
  $('#slideshow').prev().append('<a href="#" class="toggleLink"> Hide Slideshow </a>');
  }
  
  //if(showhide[5]==1){
  //$('#slideshow').prev().append('<div style="position: absolute; left: 780px; top: 175px;"><a href="#" class="toggleLink"> Show Slideshow </a></div>');
  //} else {
  //$('#slideshow').prev().append('<div style="position: absolute; left: 780px; top: 175px;"><a href="#" class="toggleLink"> Hide Slideshow </a></div>');
  //}
  
  
  // show all of the elements with a class of 'toggle'
  //$('.toggle').show();

  // capture clicks on the toggle links
  $('a.toggleLink').click(function() {
  
    // switch visibility
    //is_visible = !is_visible;
  
    // change the link depending on whether the element is shown or hidden
    if($(this).parent().next().attr('id')=="slideshow"){
      $(this).html( ($(this).parent().next('.toggle').is(':hidden')) ? "Hide Slideshow" : "Show Slideshow");
    } else {
      $(this).html( ($(this).parent().next('.toggle').is(':hidden')) ? hideText : showText);
    }
    
    // toggle the display - uncomment the next line for a basic "accordion" style
    //$('.toggle').hide();$('a.toggleLink').html(showText);
    $(this).parent().next('.toggle').slideToggle('slow');
    
    //this if loop is to record the state of the box so that it can be stored in a cookie
    if($(this).parent().next().attr('id')=="qlinks"){
    showhide[0]=switchonoff(showhide[0]);
    } else if ($(this).parent().next().attr('id')=="news"){
    showhide[1]=switchonoff(showhide[1]);
    } else if ($(this).parent().next().attr('id')=="yourgroups"){
    showhide[2]=switchonoff(showhide[2]);
    } else if($(this).parent().next().attr('id')=="tools"){
    showhide[3]=switchonoff(showhide[3]);
    } else if($(this).parent().next().attr('id')=="tagcloud"){
    showhide[4]=switchonoff(showhide[4]);
    } else if($(this).parent().next().attr('id')=="slideshow"){
    showhide[5]=switchonoff(showhide[5]);
    } else {
    }
    
    setCookie('boxes',showhide.join("|"),365)
    
    // return false so any link destination is not followed
    return false;
  });
});

function switchonoff(num){
  if(num==0){
  return 1;
  }else {
  return 0;
  }
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}

//-->
