document.observe('dom:loaded', function() {
  if (document.location.hash.startsWith('#')) {
    var id = document.location.hash.substr(10);
    if ($$('#'+id).size())
      new Effect.ScrollTo(id);
      new Effect.Highlight($(id), { duration: 1.0, queue: 'end' })
  }
});
Event.addBehavior({
	'form input[name~=authenticity_token]' : function () { authenticity_token = this.value },
	'a[rel=confirm_destroy]' : function() { 
		this.href = this.href.replace("confirm-destroy",""); 
		//TODO Make Remote
		//Remote.Link(this, {method: 'delete', parameters: {authenticity_token:authenticity_token}})
		},
		'a[rel=confirm_destroy]:click' : function() {
			if (confirm("Are you sure you want to remove this item?")) {
		    var f = document.createElement('form');
		    f.style.display = 'none';
		    this.parentNode.appendChild(f);
		    f.method = 'POST';
		    f.action = this.href
		    f.appendChild($input({type:'hidden', name:'_method', value:'delete'}));
  			f.appendChild($input({type:'hidden', name:'authenticity_token', value:authenticity_token}));
  			f.submit();
  		  }
		  return false;
		},
		'a[rel~=modal_ajax]' : function() {
			var param_adder = ((this.href.match(/\?/) == null) ? "?" : "&")
			if (this.rel.match("require_login") == null || 
					$('login_button') == null)
			{
				this.href = this.href + param_adder + "layout=false&format=html"
				new Control.Modal(this,{
					fade: true,
					fadeDuration: 0.50,
					requestOptions: {method: 'get'},
					onSuccess: function() {
						$('control_modal_cancel').rel = "control_modal_cancel";
					},
					afterAppear: function() {
					 	var inputs = $$("#modal_container input[type=text]").concat($$("#modal_container textarea"));
						if (inputs.size() > 0)
						  inputs[0].activate();
					}
				});
			}
		},
		// This makes goofy looking overlays. I'm not having any cursor problems?
		
    // //Fix for missing cursor in Firefox on modal windows
    // 'a[rel~=modal_ajax]:click' : function() {
    //  $('modal_overlay').style.position = 'absolute';
    //  $('modal_overlay').style.height = $('body').getHeight() + 'px';
    // },
		'a[rel=control_modal_cancel]:click' : function() {Control.Modal.close(); return false;},
		'a[rel=select_rating]:click, a[rel="select_privatar"]:click' : function() { this.previous('input').checked = "checked"; this.blur(); },
		'input[rel=turn_off]' : function() { this.hide(); },
		'a:focus' : function(){ this.blur() },
		
    // Add prototip2 tool tips for all form elements with class 'tooltip' and a title attribute
    'form .form_tooltip' : function(){
      var tip = this.readAttribute('title')
      new Tip(this, tip, {
        stem: 'topLeft',
        hook: { tip: 'topLeft'},
        offset: { x: 14, y: 14 },
        radius: 0,
        showOn: 'focus',
        hideOn: 'blur',
        width: 270
      })
      this.removeAttribute('title')
    }
})


function confirm_destroy(element, action) {
  if (confirm("Are you sure?")) {
    var f = document.createElement('form');
    f.style.display = 'none';
    element.parentNode.appendChild(f);
    f.method = 'POST';
    f.action = action;
    var m = document.createElement('input');
    m.setAttribute('type', 'hidden');
    m.setAttribute('name', '_method');
    m.setAttribute('value', 'delete');
    f.appendChild(m);
    f.submit();
  }
  return false;
}