/* Tabbed content for quotes, medias, people, etc. */
function createTabbedContent(){
  var tabs = $$('.tab');
  for (var i = 0; i < tabs.length; i++){
  	$(tabs[i].id).onclick = function(){
  	  selectTab(this.id.substring(3));
  		showTabData(this.id.substring(3));
  		this.blur()
  		return false;
  	}
  }
}
function selectTab(i){
  $$('.tab').each(
    function(t){
	    t.removeClassName('selected')
	  });
	$('tab' + i).addClassName('selected');
}
function showTabData(tab){
	$$('.tabcontent').each(
	  function(t){
	    t.removeClassName('selected')
	  });
	$('tabcontent' + tab).addClassName('selected');
}
/* End tabbed content */

/* Advanced search form functions */
function submitSearch(){
  if ($('search').value != ''){
    switch($('search_type').value)
    {
      case 'Quotes':
        checked = Form.getInputs($('search_form'), "checkbox", 'quote_type[]').findAll(function(item) {
          return item.checked;
        }).pluck("value");
        
        // Remove checkboxes if all quotes are selected
        if ( (checked.find(function(s) { return s == 'all'; })) || (checked.length == 0) ){
          $('search_quotes_options').remove();
        }
        
        $('search_people_options').remove();
        $('type_span').remove();
        $('search_form').action = "/quotes";
        $('search_form').submit();
        break;    
      case 'People':
        checked = Form.getInputs($('search_form'), "checkbox", 'person_type[]').findAll(function(item) {
          return item.checked;
        }).pluck("value");
        
        // Remove checkboxes if all people are selected
        if ( (checked.find(function(s) { return s == 'all'; })) || (checked.length == 0) ){
          $('search_people_options').remove();
        }
        
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_form').action = "/people";
        $('search_form').submit();
        break;
      case 'BookMedias':
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_people_options').remove();
        $('search_form').action = "/books";
        $('search_form').submit();
        break;
      case 'MovieMedias':
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_people_options').remove();
        $('search_form').action = "/movies";
        $('search_form').submit();
        break;
      case 'TVShowMedias':
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_people_options').remove();
        $('search_form').action = "/tv_shows";
        $('search_form').submit();
        break;
      case 'MusicMedias':
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_people_options').remove();
        $('search_form').action = "/music_albums";
        $('search_form').submit();
        break;
      case 'ViedoGameMedias':
        $('search_quotes_options').remove();
        $('type_span').remove();
        $('search_people_options').remove();
        $('search_form').action = "/video_games";
        $('search_form').submit();
        break;
    } /* End switch statement */
  } /* End if search not empty */
}
function changeSearchType(){
  $('search_option_divs').childElements().each(function(e){
    e.hide();
    
    if ($('search_type').value == "Quotes"){
      $('search_quotes_options').show();
    }
    else if ($('search_type').value == "People"){
      $('search_people_options').show();
    }
  });
}
function checkAll(name){
  checked = Form.getInputs($('search_form'), "checkbox", name).findAll(function(item) {
    return item.checked;
  }).pluck("value");
  
  if (checked.find(function(s) { return s == 'all'; })){
    Form.getInputs($('search_form'), "checkbox", name).findAll(function(item){return item.value != ''; }).each(function(el){
      el.checked = true;
    })
  }
}
function checkOne(name, value){
  Form.getInputs($('search_form'), "checkbox", name).find(function(item) {
    return item.value == 'all';
  }).checked = false;
  
  checked = Form.getInputs($('search_form'), "checkbox", name).findAll(function(item) {
    return item.checked;
  }).pluck("value");
  
  current = Form.getInputs($('search_form'), "checkbox", name).find(function(item) {
    return item.value == value;
  });
  if (checked.length == 0){
    current.checked = true;
  }
}
/* End advanced search */

/* Utility functions */
function stripUnsafeChars(id){
  nonAlphaNum = /[^0-9,a-z,A-Z.?!;: -]/gi;
  $(id).value = $(id).value.replace(nonAlphaNum, '');
}
function addToStringArray(arr, el){
  if (arr != ''){
    arr += ',';
  }
  arr += el;
  return arr;
}
/* End utility functions */

/* Png transparency fix for IE */

/* End png transparency fix for IE */

// Select first textarea on page if not readonly
Event.observe(window, 'load', function() {
  var e = $A(document.getElementsByTagName('*')).find(function(e) {
    return (e.tagName.toUpperCase() == 'INPUT' && (e.type == 'text' || e.type == 'password') && (e.name != 'q'))
      || e.tagName.toUpperCase() == 'TEXTAREA' || e.tagName.toUpperCase() == 'SELECT';
  });
  if (e){
    if (!e.readOnly) { e.focus(); }
  } 
  
  createTabbedContent();
});
