/* general js functions for the corpreps site  */


/* open photos in a popup window  */
function popImage(img,win,vars) {
 var pwin = window.open(img,win,vars);
 pwin.focus();
}

/* gets a passed # value from the url and adds a clash to the relevant menu link with  the same name */
/* for in page navigation. Used in privacy and fees  */
function hash_links(hash_name){
	var aname_val = "";
	if (hash_name){
		aname_val = hash_name;
	}
	else {
		aname_val = window.location.hash.replace(/^#(.*)/, '$1');
	}
	var aname_check = "p_" + aname_val;
  $('.hash_nav li').each(function(){
	if($(this).attr('id') == aname_check){
 		$(this).addClass('l_sel');
	}
	else{
		$(this).removeClass('l_sel');
	}
	});
}

function scroll_fix(top){

      var y = $(this).scrollTop();
      // whether that's below the form
      if (y >= top) {
			 if (y >= ($("#footer").offset().top) - $(this).height()) {  // if object won't fit in above footer then fix it a set number of pixels above the footer
				$('#left-menu').addClass('bottomfixed');
				$('#left-menu').removeClass('fixed');
				$('#btop').addClass('visible');
			}
			else {
        // otherwise, add the fixed class
        $('#left-menu').addClass('fixed');
        $('#btop').addClass('visible');
			$('#left-menu').removeClass('bottomfixed');
			}
      } else {
        // otherwise remove it
        $('#left-menu').removeClass('fixed');
        $('#left-menu').removeClass('bottomfixed');
        $('#btop').removeClass('visible');
      }
}

/* used to keep left menu visible as page scrolls down  */
function scroll_menu(){
	var top = $('#left-menu').offset().top - parseFloat($('#left-menu').css('margin-top').replace(/auto/, 0));
		scroll_fix(top);
    $(window).scroll(function (event) {
		scroll_fix(top);
    });

}



/* implement the prompt text for the search box */
/* #formquery is the id of the search box */
/* For other forms that need prompt text, add the inc-prompt class to the div containing the field */
$(document).ready(function(){
  $('input#formquery[type=text][title],input[type=password][title],.inc-prompt textarea[title]').each(function(i){
    $(this).addClass('input-prompt-' + i);
    var promptSpan = $('<span class="input-prompt"/>');
    $(promptSpan).attr('id', 'input-prompt-' + i);
    $(promptSpan).append($(this).attr('title'));
    $(promptSpan).click(function(){
      $(this).hide();
      $('.' + $(this).attr('id')).focus();
    });
    if($(this).val() != ''){
      $(promptSpan).hide();
    }
    $(this).before(promptSpan);
    $(this).focus(function(){
      $('#input-prompt-' + i).hide();
    });
    $(this).blur(function(){
      if($(this).val() == ''){
        $('#input-prompt-' + i).show();
      }
    });
  });
});
