function showSearchLabel(ele) {
  if (jQuery.trim(ele.value) === '') {
         $(ele).prev("label").show();
      }
}

function rotate_chr(chr) {
  var chrInt = chr.charCodeAt(0);
       
  if (chrInt >= 65 && chrInt <= 90) {
    chrInt += 13;
    if (chrInt > 90) { chrInt -= 26; }
  }
  if (chrInt >= 97 && chrInt <= 122) {
    chrInt += 13;
    if (chrInt > 122) { chrInt -= 26; }
  }
  if (chrInt == 46) { chrInt += 13; }
  if (chrInt == 59) { chrInt -= 13; }

  return String.fromCharCode(chrInt);
}

function rotate_string(text) {
  var new_text = '';
  for (var i=0; i < text.length; i++) {
    new_text += rotate_chr(text.charAt(i));
  }
  return new_text;
}

$(document).ready(function() {
    
    //decode email addresses
    $("a.email").each(function() {
       var url = $(this).attr('href');
       var urls = url.split("/");
       if (urls.length === 4) {
          var email = rotate_string(urls[2]) + "@" + rotate_string(urls[3]);
          $(this).attr('href', 'mailto:' + email);
          $(this).html(email);
          $(this).removeAttr('target');
       }
    });
    

    ////home page actions
    if ($("body").hasClass("home") || jQuery.trim($("body").attr("class")) === 'home') {
        //featured activities
        $("#featuredActivities div.body div.activity div.activity-description a.activity-title").each(function() {
              $(this).css("text-decoration", "none");
              $(this).removeAttr("href");
              
         });

        $("#featuredActivities div.body div.activity div.activity-description a.activity-title").click(function() {
           $(this).parent().toggleClass("expanded");
           $(this).parent().prev("div.activity-background").toggleClass("expanded");
        });

    } else {
    }

    
    $("a#login_link, a.login_link").click(function() {
       var left = ($(document).width() - $("#login_overlay").width()) /2;
       $("#login_overlay").css("left", left);
      
       $("#login_underlay").show();
       $("#login_overlay").show();
       //set left
       return false;
    });
    
    $("#login_overlay .cancel, #login_underlay").click(function() {
       $("#login_underlay").hide();
       $("#login_overlay").hide();
    });
    
    //if there are login errors, show login form
    if ($("form#login .error-with-field").length > 0 && $("#content .error-with-field").length == 0) {
       $("a#login_link").click();
    }

    $(".contact_link a").click(function () {
       $(this).next("div.contact_overlay").toggle();
       return false;
    });

    $(".contact_link .cancel").click(function () {
       $(this).parent("div.contact_overlay").hide();
    });

    $(".search input").each(function() {
      showSearchLabel(this)
    });

    $(".search label").click(function() {
        $(this).next("input").focus();
    });

    $(".search input").focus(function() {
        $(this).prev("label").hide();
    });

    $(".search input").blur(function() {
           showSearchLabel(this)
    });


    
    //setup autocompletes
    if (typeof(orgIdArray) !== "undefined") { 
      $("#contact_organization_1_name").autocompleteArray(orgIdArray, { matchContains:true, width:590, 
            onItemSelect: function(li) {
              $("#contact_organization_1_id").attr("value", li.extra);
            }
      });
      
      $("#contact_organization_2_name").autocompleteArray(orgIdArray, { matchContains:true, width:590, 
            onItemSelect: function(li) {
              $("#contact_organization_2_id").attr("value", li.extra);
            }
      });
      $("#contact_organization_3_name").autocompleteArray(orgIdArray, { matchContains:true, width:590, 
            onItemSelect: function(li) {
              $("#contact_organization_3_id").attr("value", li.extra);
            }
      });

      //if the values are already set, look them up in the array
      //("#contact_organization_1_name, #contact_organization_2_name, #contact_organization_3_name")
    }

    

});

 
