// generic JS fixes

// various JavaScript object.
var Blueprint = {};

// jump to the value in a select drop down
Blueprint.go = function(e) {
  var destination = e.options[e.selectedIndex].value;
  if (destination && destination != 0) location.href = destination;
};

// prevent users from clicking a submit button twice
Blueprint.formCheck = function() {
  // only apply this to node and comment and new user registration forms
  var forms = $("#node-form>div>div>#edit-submit,#comment-form>div>#edit-submit,#user-register>div>#edit-submit");

  // insert the saving div now to cache it for better performance and to show the loading image
  $('<div id="saving"><p class="saving">Saving&hellip;</p></div>').insertAfter(forms);

  forms.click(function() {
    $(this).siblings("input[@type=submit]").hide();
    $(this).hide();
    $("#saving").show();

    var notice = function() {
      $('<p id="saving-notice">Not saving? Wait a few seconds, reload this page, and try again. Every now and then the internet hiccups too :-)</p>').appendTo("#saving").fadeIn();
    };

    // append notice if form saving isn't work, perhaps a timeout issue
    setTimeout(notice, 24000);
  });
};






// Global Killswitch.
if (Drupal.jsEnabled) {
  $(document).ready(function(){
  	Blueprint.formCheck();
  	//setup_fpo_toggle();
  	externalize_pdf_links();
  	ohb_search_setup();
  //
  });
}



function setup_fpo_toggle(){
	$('body').append('<a id="fpo-toggler" style="cursor:pointer; display:block; z-index: 1000; padding: 3px 8px; position:absolute; top:0; right:10px; border: 1px solid red; border-width: 0 1px 1px 1px; background:pink; color: red;">Toggle FPO</a>');
	$('#fpo-toggler').click(function(){
		obh_toggle_fpo();
	});
}

function obh_toggle_fpo(){
	$('body').toggleClass("fpo");
}

function ohb_search_setup(){
	$("#search-button").click(function(){
		return false;
	}).click(function(){
		var qs = "?keys="+$("#search-field").val()+"&sitenbr="+$("#sitenbr-hidden").val();
		var getURL = $("#searchform").attr("action") + qs;
		document.location = getURL;
	});
}

// finds any links with .pdf extensions and modifys their target attribute
function externalize_pdf_links(){
	
	$("a").each(function(){
    if( $(this).attr('href').indexOf('.pdf') > 0 ){
				$(this).addClass("filetype-pdf").attr('target', '_blank');
    }
	});

}





