$(document).ready(function() {
  hideExtraHRinRelated();
  watchCalendarCells();
  recolorEmptyCalendarCells();
  slideShow();
  collapseTableBordersForIE();
  initialiseExternalLinkListener();
  initialiseForumListing();
  initialiseStartTopicForms();
  initialiseTabs();
  setStartFormTriggers();
  shortcutGrid();
  //simulateHoverPseudoClass();
  transplantLabelText();
});

function recolorEmptyCalendarCells() {
  $('div.calendar td:empty')
    .css('background-color', "#ddd")
    .text(String.fromCharCode(160));
  
  return;
}


/* */
function clearTextfields() {
  jQuery.each($('input[type="text"], textarea'), function() {
    $(this).one('click', function() {
      $(this).attr({value: ''});
    });
  });
}


/*
 * IE doesn't understand the border-collapse: separate css properly
 */
function collapseTableBordersForIE() {
  if($.browser.msie) {
    $(function() {
      $('table').css('border-collapse', 'collapse');
    });
  }
}


function initialiseExternalLinkListener() {
  var triggers = $('a.external');
  
  triggers.each(function() {
    $(this).attr('title', "opens in new window");
    $(this).click(function() {
      window.open(this.href);
      
      return false;
    });
  });
}


function initialiseForumListing() {
  var forums = $('div.forum');
  
  $(forums).addClass("collapsed");
  $('div#selected-forum')
    .removeClass("collapsed")
    .addClass("expanded");
  
  setForumListingListeners();
}


function setForumListingListeners() {
  var forums = $('div.forum');
  var triggers = $('div.forum h3');

  triggers.each(function() {
    $(this).mouseover(function() {
      $(this).css({ cursor: 'pointer' });
    });

    $(this).click(function() {
      $(forums)
        .removeClass("expanded")
        .addClass("collapsed");
      
      $(this).parent()
        .toggleClass("collapsed")
        .toggleClass("expanded");
    });
  });
}


function initialiseStartTopicForms() {
  $('form.start').hide();
  $('a.start').fadeIn();
  
  return;
}


function setStartFormTriggers() {
  var triggers = $('a.start');
  
  triggers.each(function() {
    $(this).click(function() {  
      var target = $("form[rel='"+$(this).attr('id')+"']");
      
      initialiseStartTopicForms();
      $(this).hide();
      $(target).fadeIn();
      
      return false;
    });
  });
  
  $('a.cancel').click(function() {
    initialiseStartTopicForms();
    
    return false;
  });
}


function initialiseTabs() {
  var panels = $('div.panel');
  var hometabs = $('div#hometabs li');
  var othertabs = $('div.navigation-outer div.tabs li');
  var anchor = checkForAnchor();
  
  resetPanels(panels);
  resetTabs(hometabs, othertabs);
  watchTabs(panels, hometabs, othertabs);
  
  /* If the url contains an anchor reference then we need to highlight the matching content
  ** so we don't reset the page each time the user presses the back, forward or refresh
   */
  if (anchor.length > 0) {
    var panel = "div#" + anchor + ".panel";
    var tab = 'li[rel="' + anchor +'"]'

    $(panel).show();

    $(tab)
      .removeClass('inactive')
      .addClass('active');
      
    $(tab).attr('id', "selected");
  }
  else {
    $('div.panel:first').show();

    $('div#hometabs li:first')
      .removeClass('inactive')
      .addClass('active')
      .attr('id', "selected");
  }  
}

/* */
function checkForAnchor() {
  var anchor = '';
  var url = window.location.href;

  if (url.search("#") >= 0) {
    var url_parts = url.split("#");

    if (url_parts.length > 1) {
      anchor = url_parts[1];
    }

  }

  return anchor;  
}

/* */
function resetPanels(panels) {
  panels.each(function() {
    $(this).hide();      
  });
  
  return;
}

/* */
function resetTabs(hometabs, othertabs) {
  hometabs.each(function() {
    $(this)
      .removeClass('active')
      .removeClass('hovered')
      .addClass('inactive')
      .removeAttr('id');
  });
  
  othertabs.each(function() {
    $(this)
      .removeClass('active')
      .removeClass('hovered')
      .addClass('inactive')
  });
  
  return;
}

/* */
function watchTabs(panels, hometabs, othertabs) {
  hometabs.each(function() {
    if ($(this).attr('id') != "selected") {
      $(this).mouseover(function() {
        $(this).addClass('hovered');
      });

      $(this).mouseout(function() {
        $(this).removeClass('hovered');
      });
      
      $(this).click(function() {
        var target_id = "div#" + $(this).attr("rel");

        resetPanels(panels);
        resetTabs(hometabs, othertabs);

        $(this)
          .removeClass('inactive')
          .removeClass('hovered')
          .addClass('active')
          .attr('id', "selected");

        $(target_id).show();

        return false;
      });
    }
  });
  
  othertabs.each(function() {
    if ($(this).attr('id') != "selected") {
      $(this).mouseover(function() {
        $(this).addClass('hovered');
      });

      $(this).mouseout(function() {
        $(this).removeClass('hovered');
      });
    }
  });
}


function watchCalendarCells() {
  var cells = $('div.calendar td a');
  
  cells.each(function() {
    $(this).mouseover(function() {
      $(this).parent().addClass('hovered');
    });

    $(this).mouseout(function() {
      $(this).parent().removeClass('hovered');
    });
  });
}

/*
 * IE doesn't understand the hover pseudo class
 */
function simulateHoverPseudoClass() {
  $('*').hover(
    function() {
      $(this).toggleClass('hover');
    }
  );
}


/* */
function slideShow() {
  var prefix = '';
  var triggers = $('div.slides img');

  triggers.mouseover(function() {
    $(this).css('cursor', 'pointer');
  });
  
  triggers.each(function() {
    $(this).click(function() {
      var source = $('div#projector img').attr("src");

      $(this).css('cursor', 'pointer');
      
      $('div#projector img').hide();

      if (source.search('md_') >= 0) {
        prefix = 'md_';
      }
      
      if (source.search('lg_') >= 0) {
        prefix = 'lg_';
      }

      $('div#projector img')
        .attr("src", $(this).attr("src").replace('sm_', prefix))
        .show();        
    });
  });
}


/* */
function shortcutGrid() {
  $('div.shortcut').hover(
    function() {
      $(this).find('div.text').css('z-index', "100");
    },
    function() {
      $(this).find('div.text').css('z-index', "1");
    }
  );
}


/* */
function transplantLabelText() {
  // Find labels
  var labels = $('label.transplant');

  // Grab the 'for' attribute and text for each label
  jQuery.each(labels, function() {
   var label_for = $(this).attr('for');
   var label_text = $(this).text();

   // Find any corresponding input elements
   if ('input#'+label_for) {

     // Set the field 'value' attribute to the label's text
     var target = 'input#'+label_for;
     
     if (!$(target).attr('value'))
     {
       $(target).attr('value', label_text);
     }
   }

   // Find any corresponding input elements
   if ('textarea#'+label_for) {

     // Set the field 'value' attribute to the label's text
     var target = 'textarea#'+label_for;
     
     if (!$(target).text())
     {
       $(target).text(label_text);
     }
     
   }
   
   // Hide the labels
   $(this).hide();
  });
}


function hideExtraHRinRelated() {
  var line = $('div.related hr:last');
  
  line.hide();
  
  var line = $('body.content div.sub-col-three hr:last');
  
  line.hide();
}