// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// This is for handling of tabbed navigation
document.observe('dom:loaded', function() {
  if( window.location.hash != "" && $$('div.pages').size() > 0) {
    show_faq(window.location.hash.substr(1).split('.')[0], window.location.hash.substr(1).split('.')[1]);
  }

  if ( $('lon') && $('lat') ) {
    Event.observe($('lon'), 'click', function() {
      if( $('lon').defaultValue == $('lon').value) {
        $('lon').value = "";
      }
    })

    Event.observe($('lat'), 'click', function() {
      if( $('lat').defaultValue == $('lat').value) {
        $('lat').value = "";
      }
    })
  }
  
  if($('what')) {
    Event.observe($('what'), 'click', function() {
      if($('what').defaultValue == $('what').value ) {
        $('what').value = "";
      }
    })
    Event.observe($('what'), 'blur', function() {
      if($('what').value == "" ) {
        $('what').value = $('what').defaultValue;
      }
    })
  };
  if($('where')) {
    Event.observe($('where'), 'click', function() {
      if($('where').defaultValue == $('where').value) {
        $('where').value = "";
      }
    })
    Event.observe($('where'), 'blur', function() {
      if($('where').value == "" ) {
        $('where').value = $('where').defaultValue;
      }
    })
    
  }
    
  if($('search-it')){
    _btn    = $('btn');
    _input  = $('where');
    _form   = $('search-it');
    _error  = $('search-it-error');
    
    Event.observe($(_btn), 'click', function() {
      if($(_input).value == $(_input).defaultValue) {
        $(_error).show();
        new Effect.Highlight($(_error), { startcolor: '#debbbb' });
        Effect.Fade($(_error), { duration: 0.5, delay: 15.0 });
      } else {
        $(_form).submit();
      }
      return false;
    });
  }
  
  if($('search-btn')) {
    _btn    = $('search-btn');
    _what   = $('what');
    _where  = $('where');
    _form   = $('search');
    
    Event.observe(_form, 'submit', function() {
      if(_what.defaultValue == _what.value) {
        _what.value = '';
      };
      if(_where.defaultValue == _where.value) {
        _where.value = '';
      };
    });
  }
  
  if($('discuss')) {
    Effect.Fade($('discuss'), { duration: 0.5, delay: 15.0 });
  }
  
});

function switch_to(_div) {
  $$('div.pages').each(function(e) { e.hide(); });
  $(_div).show();
  return 0;
}

function show_faq(_div, _id) {  
  switch_to(_div);
  if(typeof(_id) == "undefined") {
    _id = "1"; 
  }

  document.location = "#"+_div+"."+_id;
}

function showScratchpad() {
  if( $('api_type_xml').checked) {
    format = $('api_type_xml').value;
  } else if ($('api_type_json').checked) {
    format = $('api_type_json').value;
  }
  
  window.open('http://api.outside.in/radar.' + format + '?lat=' + $('lat').value + '&lng=' + $('lon').value, 'preview_window', 'toolbar=0,scrollbars=1,location=yes,statusbar=0,menubar=0,resizable=1,width=750,height=550');
}

function toggleSearchHelp(){
  if ($('help').visible()) { 
    $('show-examples').update('Show examples &raquo;'); 
    Effect.BlindUp('help', { duration : 0.2 }); 
  } else { 
    $('show-examples').update('Hide examples &laquo;'); 
    Effect.BlindDown('help', { duration : 0.2 }); 
  }
  return false;
}

function toggle_category(obj, category) {
  $(category).toggle();
  if (obj.innerHTML == "►" || obj.innerHTML == "&#9658;" ) {
    obj.innerHTML = "&#9660;";
  } else {
    obj.innerHTML = "&#9658;";
  }
  
  return false;
}