var Cookie = {
  set: function(name, value, daysToExpire) {
    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);
  },
  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');
  }
};

function tdept(element) {
	var style = Prototype.Browser.IE ? 'appear' : 'blind';
	Effect.toggle($(element).up('.item').down('.info'), style);
	$(element).blur();
}

function hadept() {
	$$('#body div.content .departments .item .info').each(Element.hide);
}

function acheck() {
	return Cookie.get('is_admin') == 'true';
}

function alink() {
	if (!acheck()) return;
	
	var e = $$('#menu a.last').first();
	if (e != null) {
		var level = parseInt(e.up('body').id.gsub(/\D+/,''));
		var depth = '';
		level.times(function() { depth += '../'; });
		var html = '<a class="last" href="' + depth + 'logout">Logout</a>';
		Insertion.After(e, html);
		
		e.removeClassName('last');
	}
}

function aevents() {
	if (!acheck()) return;
	
	$$('#body div.content div.event .desc').each(function(e) {
		var id = e.up('.event').id.gsub(/\D+/,'');
		var html = '<div class="controls admin"><p>';
		html += '<a href="../update.html?id=' + id + '">Edit</a> | '
		html += '<a href="../destroy.html?id=' + id + '" onclick="return confirm(\'Are you sure you wish to delete this event?\');">Delete</a>'
		html += '</p></div>';
		Insertion.After(e, html);
	});
	
	var e = $$('#body div.content h1.event').first();
	if (e != null) {
		var html = '<p class="admin"><a href="../create.html">Add Event</a></p>';
		Insertion.After(e, html);
	}
}

function arecognition() {
	if (!acheck()) return;
	
	$$('#body div.content div.recognition').each(function(e) {
		var id = e.id.gsub(/\D+/,'');
		var html = '<div class="controls admin"><p>';
		html += '<a href="../update.html?id=' + id + '">Edit</a> | '
		html += '<a href="../destroy.html?id=' + id + '" onclick="return confirm(\'Are you sure you wish to delete this recognition?\');">Delete</a>'
		html += '</p></div>';
		Insertion.Bottom(e, html);
	});
	
	var e = $$('#body div.content h1.recognition').first();
	if (e != null) {
		var html = '<p class="admin"><a href="../create.html">Add Recognition</a></p>';
		Insertion.After(e, html);
	}
}

Event.observe(window, 'load', alink);
