$(document).ready(function() {

  // Block double clicks
    $("#screenBlock").click(function(){return false;});
	$('a').click(function(){ 
	  $("body").append("<div id='screenBlock'></div>");
		setTimeout (function() {			
			$("#screenBlock").remove();
		}, 1000 );
		return true;
	});


  // Ensure coloring for notebook pages
  $(".notebook-thumb a").click(function(){
	$(this).find("img").addClass("visited").css({"borderColor": "#96F"});
  });

  // lazyload images
  $("img").lazyload({
    threshold : 200,
    placeholder : "/wp-content/themes/edgerton-beta/js/images/less-trans-white.png"
  });

  // Text default values (almost repeated in custom-nowrap... this customized for changed values)
	$('input[type="text"], textarea').addClass("idleField");
	$('input[type="text"], textarea').focus(function() {
		$(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue && !$(this).hasClass('filledTranscription')){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('input[type="text"], textarea').blur(function() {
		if ($.trim(this.value) == ''){
			$(this).removeClass("focusField").addClass("idleField");
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});  


  // global:
  setup_nav_menu();
  setup_resizeable_textareas();
//  setup_imagebox();
  setup_nav_clicking();
  
  // make shadowbox clicks also set the hashurl, so it's bookmarkable
  // $('.widerightcolumn [rel=shadowbox]').click(set_hash_url);

  Shadowbox.init({
//    skipSetup: true,
		players:    ["iframe","hybrid"],
		handleOversize: 'resize'
  });
  open_hashpage();
  
  // techniques top page:
  setup_hover_sync();

  // search results page:
  if ($('body').hasClass('search')) {
    setup_search_columns();
    setup_vertical_resizing();
    setup_result_scrollers();
    loadNotebooks();
    loadPhotos();
  }

  if ($('body').hasClass('galleries')) {
	setup_vertical_resizing();
  }
  // home page:
  setup_home_video();
  $('.story').click(function(){
    $('.front').removeClass('front');
    $(this).addClass('front');
  });
  $('.top-image-next').click(top_rotate_image);

  
  // notebook pages:
  if ($('.filterswitch').length) {
    // set the number of pages total
    $('.filterswitch[value=all], label[for=filter-all]').attr('title',$('.notebook-thumb').length);

    var filters = ['photograph','diagram','comments','transcribed'];
    for (var i in filters) {
      var filter = filters[i];
      var count = $('.notebook-thumb.'+filter).length;
      if (!count)
        $('.filterswitch[value='+filter+']').attr('disabled',true);
      else {
        $('.filterswitch[value='+filter+'], label[for=filter-'+filter+']').attr('title',count);
      }
    }

    // nottranscribed is special
    var count = $('.notebook-thumb:not(.transcribed)').length;
    if (!count)
      $('.filterswitch[value=nottranscribed]').attr('disabled',true);
    else {
      $('.filterswitch[value=nottranscribed], label[for=filter-nottranscribed]').attr('title',count);
    }

    if (!($('.filterswitch:checked').length))
      $('.filterswitch').eq(0).attr('checked',true);
    $('.filterswitch').change(notebook_filter_handler);
    notebook_filter_handler();
  }


//  $('#nav li ul').css('z-index',3);
  
// *********************** CLOSING JQUERY READY *************************** //
});

function open_hashpage() {
	var loc = (window.location.href);
	if (loc.indexOf('#') > -1) {
	  
    var hashpage = jQuery(loc.substring(loc.indexOf('#')));

    // if we can't find the item, return false
	  if (!hashpage.length)
	    return false;

    // if shadowbox hasn't created the container yet, try again 100ms later.
    if (!Shadowbox.util.get('sb-container')) {
      //console.log('try again later');
      window.setTimeout(open_hashpage,100);
      return;
    }

    hashpage.click();
  }
}

function set_hash_url() {
  window.location = '#'+this.id;
}

function notebook_filter_handler() {
  var filter = $('.filterswitch:checked').val();
  switch(filter) {
    case 'photo':
      filter = 'photograph'
    case 'photograph':
    case 'diagram':
    case 'comments':
    case 'transcribed':
      $('.notebook-thumb.'+filter).show();
      $('.notebook-thumb:not(.'+filter+')').hide();
      break;
    case 'nottranscribed':
      $('.notebook-thumb.transcribed').hide();
      $('.notebook-thumb:not(.transcribed)').show();
      break;
    default: // including 'all'
      $('.notebook-thumb').show();
  }
  // make it act like the window scrolled, so that lazyload is triggered
  $(window).scroll();
}