document.observe("dom:loaded", function() {
    deal_with_flyouts();

    if($('message')){
//      Effect.Highlight('message');
    }

    if($('nav')){
      deal_with_tab_states();
    }

    $$('input.select-all-toggle').each(function(el){
      var basename = el.id.split('-');
      select_all_toggle(basename[0]);
      });

    if($('carousel-content') && ! $('edit')){
           var firstElement = $$('#carousel-content .slide')[0];
           var htmlCopy = firstElement.cloneNode(true);
           Element.insert('carousel-content', htmlCopy);
           new Carousel('carousel-wrapper', $$('#carousel-content .slide'), $$('a.carousel-control'), {auto: true, circular: true, frequency: 15}); 
    }
    
});


function select_all_toggle(objectName){
  $(objectName + '-select-all').observe('click', function(){
      $$('input.' + objectName +'_check_selector').each(function(el){
        // will make it a toggle
//        if(el.checked == true){
//          el.checked = false;
//          } else {
          el.checked = true;
//          }
        });
      });
}

function deal_with_flyouts(){
  $('content-wrapper').select('[id*="details-toggle"]').each(function(el){
    var id = el.id.replace(/\D/g, '');
    // Hide the container via javascript. Currently we don't require javascript for the front-end anywhere, so this'll keep all data available.
    $('details-' + id).hide();
    el.observe('click',function(){
      $('details-' + id).toggle();
    });
  });
}

function deal_with_tab_states(){
  $('nav').select('a').each(function(el){
      var windowlocation = window.location + "";
      if(el.href == windowlocation){
        el.addClassName('on');
        } else if( el.href.match(/my\-members\/?$/i) && windowlocation.match(/member_manager\/.+/i)){
        el.addClassName('on');
        } else if( el.href.match(/messages\/?$/i) && windowlocation.match(/messages\/show/i)){
        el.addClassName('on');
        }
      });

}

TableKit.Sortable.addSortType(new TableKit.Sortable.Type('priority_column_sort_customization', {
					pattern : /^[High|Normal|Low]$/,
					normal : function(v) {
						var val = 3;
						switch(v) {
							case 'High':
								val = 0;
								break;
							case 'Normal':
								val = 1;
								break;
							case 'Low':
								val = 2;
								break;
						}
						return val;
					}
				}
			));


// inspired by http://gorondowtl.sourceforge.net/wiki/Cookie
// Modified by DJCP to support setting a path.
var Cookie = {
  set: function(name, value, daysToExpire, path) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    }
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire + '; path=' + ((path) ? escape(path) : '/') ) ;
  },
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  },
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  },
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    }
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');
  }
};

