$(document).ready(function() {

  // enable special fx for JS enabled browsers
  $('body').removeClass('js_disabled');  
  $('body').addClass('js_enabled');
	
	/*email protect*/
	$("span.safemail").each(function(){
		exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
		match = exp.exec($(this).text());
		addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
		link = match[2] ? match[2] : addr;
		subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
		$(this).after('<a href="mailto:'+addr+subject+'">'+ link + '</a>');
		$(this).remove();
	});    

	// Open links in new window when rel="external" is applyed to a tag
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});
	
  //hide email and add the hidden email_To on submit
  $("#contact-form").submit(function() {
    var s1 = 'frank';
    var s2 = '@';
    var s3 = 'wagts.org.au';
    $(this).append('<input type="hidden" name="Email_To" value="' + s1 + s2 + s3 + '" />');
  });

  $('#ctas .item a.viewmore').wrap('<div class="popup pngFix"></div>'); // wrap popup bubbles around viewmore links
  
  // when user clicks a cta div, this script
  // sets the browser's location to the specified url
  $('#ctas .item').click(function() {
    var thisLink = $(this).find('a.viewmore').attr('href');
    window.location = thisLink;
  });
  
  // CTA Popup Bubbles
  // http://jqueryfordesigners.com/coda-popup-bubbles/
  $(function () {
    $('#ctas .item').each(function () {
      
      // options
      var distance = 10;
      var time = 250;
      var hideDelay = 50;

      var hideDelayTimer = null;

      // tracker
      var beingShown = false;
      var shown = false;

      var trigger = $(this);
      var popup = $('.popup', this).css('opacity', 0);

      // set the mouseover and mouseout on both element
      $([trigger.get(0), popup.get(0)]).mouseover(function () {
        // stops the hide event if we move from the trigger to the popup element
        if (hideDelayTimer) clearTimeout(hideDelayTimer);

        // don't trigger the animation again if we're being shown, or already visible
        if (beingShown || shown) {
          return;
        } else {
          beingShown = true;

          // reset position of popup box
          popup.css({
            top: -38,
            left: 60,
            display: 'block' // brings the popup back in to view
          })

          // (we're using chaining on the popup) now animate it's opacity and position
          .animate({
            top: '-=' + distance + 'px',
            opacity: 1
          }, time, 'swing', function() {
            // once the animation is complete, set the tracker variables
            beingShown = false;
            shown = true;
          });
        }
      }).mouseout(function () {
        // reset the timer if we get fired again - avoids double animations
        if (hideDelayTimer) clearTimeout(hideDelayTimer);

        // store the timer so that it can be cleared in the mouseover if required
        hideDelayTimer = setTimeout(function () {
          hideDelayTimer = null;
          popup.animate({
            top: '-=' + distance + 'px',
            opacity: 0
          }, time, 'swing', function () {
            // once the animate is complete, set the tracker variables
            shown = false;
            // hide the popup entirely after the effect (opacity alone doesn't do the job)
            popup.css('display', 'none');
          });
        }, hideDelay);
      });
    });
  });

});
