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

var APP = {

open_menu: '',
add_nav_handler: function(){
  Event.observe( document, 'click', function( event ){
    element = Event.element( event );
    $$('ul.menu').invoke('hide'); 
    $$('li.has_subnav').invoke( 'removeClassName', 'current' );
    if( element.hasClassName( 'catdrop' ) ){
      if( element.id != APP.open_menu ){
        APP.open_menu = element.id;
        Element.show( $(element.id + '_m') );
        element.up().addClassName( 'current' );
      } else {
        Element.hide( $(element.id + '_m') );
        APP.open_menu = '';
      }
    }
    return( false );
  } );
},

limit_input: function( event, limit ){
  if( this.value.length >= limit ){
    switch( event.keyCode ){  
      case Event.KEY_BACKSPACE:
      case Event.KEY_LEFT:
      case Event.KEY_UP:
      case Event.KEY_RIGHT:
      case Event.KEY_DOWN:
      case Event.KEY_DELETE: break;
      default: Event.stop( event ); break;
    }
  }
  if( this.character_remainder_field )
    this.character_remainder_field.innerHTML = ( limit - this.value.length );
},

add_input_limit: function( oid, limit ){
  var o = $(oid);
  if( o ){
    var r = $(oid+'_remainder');
    if( r ){ 
      r.innerHTML = ( limit - o.value.length );
      o.character_remainder_field = r;
    }
    Event.observe( o, 'keydown', this.limit_input.bindAsEventListener( o, limit ) );
    Event.observe( o, 'keypress', this.limit_input.bindAsEventListener( o, limit ) );
    Event.observe( o, 'keyup', this.limit_input.bindAsEventListener( o, limit ) );
  }
},

increment: function ( oid ){  
  counter = $(oid)
  if (counter) counter.innerHTML = parseInt( counter.innerHTML ) + 1;
},

add_input_placeholders: function( form ){
  if( $(form) ){
    Form.getElements( form ).each( function( elt, i ){
      if( elt.title && elt.title != '' ){
        elt.original_color = elt.style.color;
        if( elt.value == '' || elt.value == elt.title )
          elt.value = elt.title;
        else
          elt.style.color = '#333';
        Event.observe( elt, 'focus', function(){
          if( this.value == '' || this.value == this.title ){
            this.value = '';
            this.style.color = '#333';
          }
        } );
        Event.observe( elt, 'blur', function(){
          if( this.value == '' || this.value == this.title ){
            this.value = this.title;
            this.style.color = this.original_color;
          }
        } );
      }
    } );
    Event.observe( form, 'submit', function(){
      Form.getElements( this ).each( function( elt, i ){
        if( elt.title && elt.title != '' )
          if( elt.value == elt.title ) elt.value = '';
      } );
    } );
  }
}

};

var PROFILE = {
toggle_tabs : function( tab ) {
  var children = tab.parentNode.childNodes;
  for (var i = 0; i < children.length; i++) children[i].className = '';
  tab.className = 'current';
}
};

APP.add_nav_handler();
