initPage = function (){ 	

  if ( $('startPage') ) {
	  showSidebar("geoloc"); 
  } else {
	  if ($('map')) showSidebar("map");
  }

  // clear hint text in text input fields on focus
  if ($$('input.clearonfocus')){ 
    $$('input.clearonfocus').map( function(element){
  	  element.observe('click', function(event){  		 
  		  element.removeClassName('clearonfocus');
  		  element.writeAttribute('value', '');
  	  });
  	});
  }
  
  // enlarged textarea field on focus
  if ($$('textarea.enlargeonfocus')){ 
    $$('textarea.enlargeonfocus').map( function(element){
      element.observe('click', function(event){
        if (!element.hasClassName('large')) {
          element.addClassName('large');
        }
      });
    });
  } 
  
  // clear alert class from input fields 
  if ($$('.required')){ 
    $$('.required').map( function(element){
      element.observe('click', function(event){
        if (element.hasClassName('alert')) {
          element.removeClassName('alert');
        }
      });
    });
  }    
  
  // clear alert class from select fields 
  if ($$('select.required')){ 
    $$('select.required').map( function(element){
      element.observe('click', function(event){
        if (element.hasClassName('alert')) {
          element.removeClassName('alert');
        }
      });
    });
  }      
    
  // auto-focus fields
  if ($$('.focus')){ 
    $$('.focus').map( function(element){
      element.focus();
    });
  }    
  
  // showcase highlighting
  if ($('showcase')){ 
	/*@cc_on
	@if (@_jscript_version <= 5.6)
	  return;
	@else @*/
	
	/*@end @*/ 
	    	
    $$('#showcase div.imageContainer').map( function(element){    	  
      var thumbEl = element.up('div.thumbnail'); 
      element.observe('mouseover', function(event){
        showcaseHighlight(thumbEl);
      });   
      element.observe('mouseout', function(event){
        showcaseRemoveHighlight(thumbEl);
      });
    });
  }      
  
  setTimeout ( "updateToolbar()", 1000 ); 
}

// clear hint text in search fields on submit
submitSearch = function (){ 
  if ($('searchkey') && $('searchkey').hasClassName('clearonfocus')) $('searchkey').writeAttribute('value', '');
}

// select all checkboxes
selectAll = function (){ 
  if ($$('td.checkbox')){ 
    $$('td.checkbox input').map( function(element){
      element.writeAttribute('checked', 'checked');
    });
  }   
}  

// clear all checkboxes
clearAll = function (){ 
  if ($$('td.checkbox')){ 
    $$('td.checkbox input').map( function(element){
      element.removeAttribute('checked');
    });
  }   
}    

// toggle titter updates
toggleTwitterUpdates = function (){ 
  $('twitter_div').toggle();
}    

/*-------------------------------------------------------------------------------
 toolbar functions
--------------------------------------------------------------------------------*/

var curSidebar = null;
var curIcon = null;

toggleSidebar = function (name) {	
  if ( curSidebar == $(name+'Sidebar') ) return;
  
  var s = curSidebar;
  
  if ( curSidebar ) {
    hideSidebar();
  } 
  
  if ( s != $(name+'Sidebar') ) {
    showSidebar(name);
  }
  
}

showSidebar = function (name) {
  if (!$(name+'Icon')) return;
	
  curIcon = $(name+'Icon');
  curIcon.addClassName('selected');
  curSidebar = $(name+'Sidebar');
  curSidebar.show();
  
}

hideSidebar = function () {
  if (curSidebar) {
    curSidebar.hide();
    curSidebar = null;
    curIcon.removeClassName('selected');
    curIcon = null;
  }
}

updateToolbar = function () {
  $$('#toolbar a.notify').map( function(element){
    Effect.Pulsate(element, { pulses: 3, duration: 2.5 });
  });  
}

limitChars = function (id, limit) {
  new PeriodicalExecuter(function () {
   checkChars(id, limit);
  }, 0.2);  
}

checkChars = function (id, limit) {  
  if ($F(id).length > limit) $(id).value = $F(id).substr(0, limit);
  
  $(id+'_chars').update(limit - $F(id).length);  
}

document.observe("dom:loaded", initPage );
