/* On Page Load */
/* $(document).ready(function() {

  // Add rollover function
  $('.rollover')
    .hover(function() {
      curr = $(this).find('img').attr('src');
      overlen = curr.length;
      over = curr.substr(0, overlen-4);
      over = over+'_o.jpg';
      $(this).find('img').attr({ src: over});
    },
    function() {
      $(this).find('img').attr({ src: curr});
    }
  );

  // Preload rollover images
  $('.rollover').find('img').each(function(i) {
    temp = this.src;
    prelen = temp.length;
    pre = temp.substr(0, prelen-4);
    pre = pre+'_o.jpg';
    preload_image_object = new Image();
    preload_image_object.src = pre;
  });

});
 */
 
// Preload images function
jQuery.preloadImages = function() {
  for(var i = 0; i<arguments.length; i++) {
    jQuery('<img>').attr('src', arguments[i]);
  }
}

// Preload navigation rollovers
  $.preloadImages(
    '/themes/rock10/images/nav/nav_logo_over.jpg',
    '/themes/rock10/images/nav/nav_army_over.jpg',
    '/themes/rock10/images/nav/nav_stuff_over.jpg',
    '/themes/rock10/images/nav/nav_search_over.jpg');
 
// On Page load
$(document).ready(function() {

  // Attach Jquery Tabs if they exist
  $(document).ready(function() {
    $('#tabcontainer').each(function() { $(this).tabs(); });
  });
  
  // Change background color of search box when focused and blurred
  $('#searchinput')
    // Set default value of input to "default" attribute value, unless already set
    .each(function() {
      if ( $(this).val() == '')
        $(this).val( $(this).attr('default') );
    })
     // When focused, change background color and clear default value
    .focus(
      function() {
        $(this).addClass('blacktext');
        if ($(this).val() == $(this).attr('default'))
          $(this).val('');
        $(this).parents('li').css('background',
          $(this).parents('li').css('background').split('_off').join('_over'));
      })
    // When blurred, change background color again and set default value if needed
    .blur(
      function() {
        $(this).removeClass('blacktext');
        $(this).parents('li').css('background', 
          $(this).parents('li').css('background').split('_over').join('_off'));
        if ($(this).val() == '') 
          $(this).val( $(this).attr('default') );
      })
    .click(function() {
      $(this).focus();
      $(this).select();
    });
});


// IE likes this one
attachRollOverEvent = function(imageId){
	$(imageId).hover(
      function(){$(this).find('img').attr('src',
        $(this).find('img').attr('src').split('_off').join('_over'))},
      function(){$(this).find('img').attr('src',
        $(this).find('img').attr('src').split('_over').join('_off'))});
}

// Firefox like this one
if ($.browser.mozilla) {
    attachRollOverEvent = function(imageId){
    	$(imageId).mouseover( function(){ $(this).find('img').attr('src',
            $(this).find('img').attr('src').split('_off').join('_over')) } );
    	$(imageId).mouseout( function(){ $(this).find('img').attr('src',
            $(this).find('img').attr('src').split('_over').join('_off')) } );
    }
}

$().ready(function(){
	attachRollOverEvent('.rollover');
});


/* Flash popup code for doing popups in the outsourced flash videos */
function flashPopUp(URL, options) {
    var flashPopUp = null;
    flashPopUp = window.open(URL, 'flashPopUp', options);
    flashPopUp.focus();
    return false;
}

/* Replace all FellowshipOne hyperlinks with input buttons */
$(document).ready(function() {
  $('a[href^="https://integration.fellowshipone.com/"]').each(function() {
    if ( $(this).html() != '') {
      $(this).after('<input type="button" style="font-weight: bold" \
        class="fellowship-button" link="'+$(this).attr('href')+'" value="'+$(this).html()+'" \
        class="inputbutton" /> <img src="/media/system/images/popupicon.gif" class="noborder" />');
      $(this).remove();
    }
  });
});

/* Replace all FellowshipOne hyperlinks with input buttons */
/* $(document).ready(function() {
  if($('#pg_admin').html() == null) {
    var regex = new RegExp('<a.*?href=(\'|")?(https://integration.fellowshipone.com/(\w|\\?|=|&|\/|\.)*).*>(.*?)</a>', 'ig');
    var oldBody = $('#mc').html();
    var newBody = oldBody.replace(regex, '<input type="button" style="font-weight: bold" class="fellowship-button" link="$2" value="$4" class="inputbutton" />');
    $('#mc').html(newBody);
  }
}); */

/* Make the FellowshipOne input buttons clickable and put up a popup when clicked */
$(document).ready(function() {
  $('.fellowship-button').click(function() {
    window.open($(this).attr('link'), '', 'width=720, height=550, top=100, left=100, resizeable=yes, scrollbars=yes, menubar=no, toolbar=no, status=no, location=1');
    return false;
  });
});

$(document).ready(function() {
  $('.email_address').each(function() {
    $(this).html( decrypt_all_emails($(this).html()) );
  });
  $('a[href*="/contact/"]').each(function() {
    $(this).attr('href', decrypt_mailtos(this.href) );
  });
});

function decrypt_mailtos(href) {
  var address = href.replace(
      /.*contact\/([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z0-9]+)(\+([a-z0-9]*))?/i,
      function($0, $1, $2, $3, $4, $5) {
        var rot5 = ($5 != null && $5.length > 0) ? '.' + $.rotate13($5) : '';
        return $.rotate13($1) + '@' + $.rotate13($2) + '.' + $.rotate13($3) + rot5;
      }
    );
  return 'mailto:'+address;
}

function decrypt_all_emails(href) {
  var address = href.replace(
      /([a-z0-9._%-]+)\+([a-z0-9._%-]+)\+([a-z0-9]+)(\+([a-z0-9]*))?/i,
      function($0, $1, $2, $3, $4, $5) {
        var rot5 = ($5 != null && $5.length > 0) ? '.' + $.rotate13($5) : '';
        return $.rotate13($1) + '@' + $.rotate13($2) + '.' + $.rotate13($3) + rot5;
      }
    );
  return address;
}

// ShadowBox Stuff

function testKnowGodVideoState(obj) {
  if (obj.newstate == 'COMPLETED') {
      $('#sb-body-inner').html('<a href="/knowinggod/"><img src="/media/site/images/knowgodendscreen.gif" width="613" height="345" alt=""></a>');
    }
}

function setKnowGodVideoListener() {
  var player = $('#sb-content').get(0);
  if (player) {
    player.addModelListener('STATE', 'testKnowGodVideoState');
  }
}

$(document).ready(function() {
  $('.sblink').bind('mousedown', function(e) {
    if (e.which == 2)
      $(this).click();
  });
  $('.sblink').mouseover(function() {
    window.status = $(this).attr('status');
  });
  $('.sblink').mouseout(function() {
    window.status = '';
  });
});