window.addEvent("domready", function(){
  $$(".pagnav").each(function(o){
    o.addEvent("click", function(){
      document.dirsearchform.offset.value = (this.innerHTML - 1) * 10;
      document.dirsearchform.submit();
    });
  });
  
  if ($('lastbtn')){
	  $('lastbtn').addEvent('click', function(){
	  	document.dirsearchform.offset.value = (($('dirResultsHead').getProperty('maxpages')*1)-1) * 10;
	  	document.dirsearchform.submit();
	  });
  }

  if ($('firstbtn')){  
	  $('firstbtn').addEvent('click', function(){
	  	document.dirsearchform.offset.value = 0;
	  	document.dirsearchform.submit();
	  });
  }

  $("btnSubmit").addEvent('click', function(){
    document.forms.dirsearchform.offset.value = "";
  });
  
  $('qsField').addEvent('change', function(){
  	$('qs').value = '';
  	$('qs').focus();
  });
  
  
  if ($('directoryResults').getSize().y > 100){
  	new Fx.Scroll(window).toElement('directoryResults');
  }
  
  
  
  /*national directory features*/
  $('natSubmit').addEvent('click', function(){
    var urlTpl = 'http://www.whitepages.com/search/FindPerson?extra_listing=mixed&form_mode=opt_b&post_back=1&firstname_begins_with=1&firstname=##FNAME##&name=##LNAME##&street=&city_zip=##CITY##&state_id=##STATE##&localtime=survey';
    var theurl = urlTpl.replace('##LNAME##', $('natlname').value);
    theurl = theurl.replace('##FNAME##', $('natfname').value);
    var citystate = $('mrktName').value.split(',');
    theurl = theurl.replace('##CITY##', citystate[0]);
    theurl = theurl.replace('##STATE##', citystate[1]);

    var mbW = Math.floor(window.getSize().x * 0.75);
	var mbH = Math.floor(window.getSize().y * 0.75);
    Mediabox.open(theurl, 'Search powered by <a href="http://www.whitepages.com">WhitePages.com</a>', '640 400');
  });

  $('natPhoneSubmit').addEvent('click', function(){
    var urlTpl = 'http://www.whitepages.com/search/ReversePhone?full_phone=##PHONE##&localtime=survey';
    var theurl = urlTpl.replace('##PHONE##', $('natPhone').value);

    var mbW = Math.floor(window.getSize().x * 0.75);
	var mbH = Math.floor(window.getSize().y * 0.75);
    Mediabox.open(theurl, 'Search powered by <a href="http://www.whitepages.com">WhitePages.com</a>', '640 400');
  });


  $$('#natlname, #natfname').each(function(o){
  	o.addEvent('keydown', function(ev){
  		if (ev.key == 'enter') $('natSubmit').fireEvent('click');
  	});
  });
  
  $('natPhone').addEvent('keydown', function(ev){
  	if (ev.key == 'enter') $('natPhoneSubmit').fireEvent('click');
  });
  
});

