
Event.observe(window, 'load', init_search_swapper);

/*
	Make the <dl> into a swappable search.
// TODO: speedtip: just call this function sans event in the html right after
// the <dl> html.
TODO: investigate switching away from behaviour.js to event:selectors.
*/
function init_search_swapper(event) {
  // get the search_area <dl>
  var rules = {
    '#search_area dt' : function (element) {
      Event.observe(element, 'click', switch_search_tab, true);
    }
  };
  Behaviour.register(rules);
  Behaviour.apply();
	document.getElementsBySelector('#search_area dd.selected .search_value')[0].focus();
} 
  
/*
Swap "active" search tabs after being clicked.
Requires prototype.js and behaviour.js
TODO: see about using mod_deflate / mod_gzip
TODO: smoosh many of these little scripts into one file
TODO: come up with more comprehensive yet easy to read JS framework.

*/
function switch_search_tab(event) {
  var search_area_selector = '#search_area';
  var tab_selector = 'dt';
  var form_selector = 'dd';
  var active_tab = search_area_selector + ' ' + tab_selector + '.selected';
  var active_form = search_area_selector + ' ' + form_selector + '.selected';
  var old_tab = document.getElementsBySelector(active_tab)[0];
  var old_form = document.getElementsBySelector(active_form)[0];
  var new_tab = Event.findElement(event, "DT");
  var new_form= new_tab;
  var header = $('header');
  while(new_form&& new_form.nodeName != "DD") {
    new_form = new_form.nextSibling;
  }

  if(new_tab != old_tab) {
    var value = document.getElementsBySelector(active_form + ' .search_value');
    if( value && value.length > 0) {
      value = value[0].value;
    } else { value = ""; }
    Element.removeClassName(old_tab, "selected");
    Element.removeClassName(old_form, "selected");
    Element.addClassName(new_tab, 'selected');
    Element.addClassName(new_form, 'selected');
    var active_input = document.getElementsBySelector(active_form + ' .search_value')[0];
		if(active_input) {
			active_input.value = value;
			active_input.focus();
		}

    if(header) {
      header.className = new_tab.id.split('_')[1];
    }


  }
  Event.stop(event);
  return false;
}






// TODO: evaluate the need for the following functions
function getUrls(url) {
	// if it's a phrase (with blank spaces) or has any punctuation don't do anything
	var reSpPu = new RegExp(/[^-a-zA-Z0-9_\\.]/);
	if (reSpPu.test(url)) {
 		return '';						
	} else {	
		var results ='<td width="1%" class="leftDash">&nbsp;</td><td valign="top" class="padding" width="39%">' +
					'<h1 class="O">Suggestions</h1>' +
					'<p>Were you looking for one of these URL\'s?</p>' +
					'<ul>' +
						'<li><a href="http://www.alexa.com/data/details/main?url='+ url +'.com">'+ url +'.com</a></li>' +
						'<li><a href="http://www.alexa.com/data/details/main?url='+ url +'.net">'+ url +'.net</a></li>' +
						'<li><a href="http://www.alexa.com/data/details/main?url='+ url +'.org">'+ url +'.org</a></li>' +
					'</ul>' +
					'</td>';
		return results;
	}
}

function copyField(form_name,orig,copy_to) {    
	// define the variables
	var orig_data = eval('document.'+form_name+'.'+orig+'.value');	
	eval('document.'+form_name+'.'+copy_to+'.value=orig_data');
}


// grabs variables from the address bar
function advs() {
	 var variable = 'advs';
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if (pair[0] == variable) {
		     document.getElementById('advanced').href = '/data/ds/advanced?'+window.location.search.substring(1);
        }
    } 
      //alert('Query Variable ' + variable + ' not found');
}

