// **********************
// Hide/Show Contents 
// **********************
// index of widgets to hide. The numbers start from zero in the upper left        0 2 4
// and increases as you count each one below, then each one to the right. e.g ->  1 3 5
//
// NOTE:
// This script will hide widgets you don't want hidden if the page layout is changed since
// it uses these indexes to determine which widgets to hide. I would have used titles, but
// they vary in length and have spaces.
//
// Usage:
//  addHideShowToggle({
//   showContent : "(+)",
//   hideContent : "(-)",
//   dontHide    : "Welcome|Online",
//   linkOnLeft  : true
//  });
//
// by Rob G (aka Mottie)

function addHideShowToggle(d){
 var o = { hidden: [] };
 $.extend(o,d);
 // Name the cookie based on the guildID and TabID
 var cookie = $.cookie("gp" + getGroupId() + getTabId());
 o.hidden = cookie ? cookie.split("|").getUnique() : [];
// alert(o.hidden);
 // Add links
 $('.gpWidget').each(function(){
  var wtitle = $(this).find('.ContentBoxTitle,.ContentBoxTitleNoBackground');
  if (!wtitle.html().match( o.dontHide )){
   wtitle[(o.linkOnLeft) ? 'prepend' : 'append'](' <span class="showHideSelector tooltip" rel="150" title="Show or hide content">' + o.hideContent + '</span> ');
  }
 })
 // Remember content that was hidden
 $.each( o.hidden, function(){
  toggleme( $('.gpWidget:eq(' + (parseInt(this,10) - 1) + ')') , o);
 })
 // Add click function to hide
 $('.showHideSelector').click(function(){
  toggleme( $(this).closest('.gpWidget') , o);
 })
}
function toggleme(w,o){
 var c = (w.find('.blkContent').length) ? w.find('.blkContent') : w.find('.ContentBoxBody')
 c.toggle();
 var isHidden = c.is(':hidden');
 w.find('.showHideSelector').html( isHidden ? o.showContent : o.hideContent );
 var indx = ($('.gpWidget').index( w ) + 1).toString();
 var tmp = o.hidden.getUnique();
 if (isHidden) {
  // add index of widget to hidden list
  tmp.push(indx);
 } else {
  // remove index of widget from the hidden list
  tmp.splice( tmp.indexOf(indx) , 1);
 }
 o.hidden = tmp.getUnique();
 $.cookie("gp" + getGroupId() + getTabId(), o.hidden.join('|') );
//  console.log(o.hidden);
}
// Return a unique array. Will remove zeros, so start with base 1
Array.prototype.getUnique = function() {
 var o = new Object();
 var i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 var a = new Array();
 for (e in o) {a.push (e)};
 return a.sort(function(a,b){return a-b;});
}