// Initializes User Scripts:
$(document).ready(function() {
  // Selects text in Search Box when focused:
  $('input[type=text].clearText').each(function() {
    var txtSearchText = $(this).val();
    $(this).focus(function() {
      if ($(this).val() == txtSearchText) {
        $(this).val('');
      }
    });
    $(this).blur(function() {
      if ($(this).val() == '') {
        $(this).val(txtSearchText);
      }
    });
  });
  
  // Opens up links in new window.
  $('.lnkBlank').click(function() {
    window.open($(this).attr('href'), 'newWindow'+ new Date().getTime(), '');
    return false;
  });
  
  if ( document.URL.indexOf('EditListItem.aspx') == -1 ) {
    $("img").each(function() {
      var img = $(this).attr("src");
      if ( typeof(img) == 'undefined' || img == '' || img == '/' || img.substr(0, 1) == '?' || img.substr(0, 2) == '/?' ) $(this).hide();
    });
    $("img").error(function() {
      $(this).hide();
    });
  }
  
  // Site Navigation / dropdowns:
  pixelsilk2.renderSkin({skin: '['+'[Menu1]'+']', path: ''}, function(html) {
    $("#nav").after('<div id="subNavigation" style="display:none;"></div>');
    $("#subNavigation").html(html);
    var sections = $("#subNavigation div");
    var topElements = $("#nav>ul>li");
    for (z = 0; z < sections.length; z++) {
      var li = topElements[z];
      var lihtml = li.innerHTML;
      li.innerHTML = lihtml + sections[z].innerHTML;
    }
    $("#nav").navMenu({
      menuWidth: 200,
      containerElement: "nav#nav>ul",
      menuElement: "ul",
      rightArrow: ' &raquo;'
    });
    $('#nav ul li').hover(function(){
      $(this).addClass('over');
    }, function(){
      $(this).removeClass('over');
    });
    $("#subNavigation").remove();
    
    // Remove the bottom border on all last items in ULs:
    $('nav#nav').find('ul').each(function() {
      $(this).find('li:last-child').css({'border-bottom':'0'});
    });
    
  });
  
  // Add facebook like button via JavaScript because iFrame code breaks HTML5 validation:
  $('#header .fbLike').html('<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2FDeschutesChildrensFoundation&amp;send=false&amp;layout=button_count&amp;width=80&amp;show_faces=false&amp;action=like&amp;colorscheme=light&amp;font=trebuchet+ms&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:80px; height:21px;" allowTransparency="true"></iframe>');
  
  $('body').ieDetector();
  
  // Add IE <=8 styles:
  if (jQuery.browser.msie && jQuery.browser.version <= 8 ) {
    $('ul.staff li:nth-child(4)').css({'margin-right':'0'});
  }
  
  // Find all outbound links in the content area and open in new window when clicked:
  $('#contentArea a[href*=http]').each(function() {
    var domain = location.hostname;
    var link = $(this).attr('href');
    if (link.indexOf(domain) == -1) {
      $(this).attr('target','_blank');
    }
  })
  
});
