var SlidingFloat = new Class({
	initialize: function(name) {
		this.target = $(name);
		this.pos = this.target.getPosition();
		this.old = this.pos.y;
		this.fx = this.target.get('tween', { property: 'top', wait: false, duration: 500, transition: Fx.Transitions.Quad.easeInOut });
		this.floating = false;
		
		this.update_float.periodical(50, this);
	},
	
	enable: function() {
		this.target.setStyles({
			'position': 'absolute',
			'left': this.pos.x,
			'top': this.pos.y
		});
		this.floating = true;
	},
	
	disable: function() {
		this.target.setStyles({
			'position': '',
			'left': '',
			'top': ''
		});
		this.floating = false;
	},
	
	update_float: function() {
		var scrolling = Window.getScrollHeight() > Window.getHeight();
		if (this.floating && !scrolling) {
			this.disable();
		} else if (!this.floating && scrolling) {
			this.enable();
		}
		
		
		var top = Math.max(this.pos.y, Window.getScrollTop());
		if (top != this.old) {
			var cur = this.target.getPosition();
			this.fx.start(cur.y, top);
			this.old = top;
		}
	}
	
});

var ScrollEvents = new Class({
	Implements: [ Options, Events ],
	
	options: {
		fireOnInitialize: false,
		onScrollingActive: $empty,
		onScrollingInactive: $empty
	},
	
	initialize: function(options) {
		this.setOptions(options);
		this.fireOnInitialize = this.options.fireOnInitialize;
		this.scrolling = null;
		window.addEvent('resize', this.update_scrolling.bind(this));
		this.update_scrolling();
	},
	
	update_scrolling: function() {
		var scrolling = Window.getScrollHeight() > Window.getHeight();
		if (scrolling &&
				(this.scrolling === false || this.fireOnInitialize)) {
			this.fireEvent('onScrollingActive');
		} else if (!scrolling &&
							(this.scrolling === true || this.fireOnInitialize)) {
			this.fireEvent('onScrollingInactive');
		}
		this.fireOnInitialize = false;
		this.scrolling = scrolling;
	}
});

/**
 * @nti-5p@m
 */
function decryptCharcode(n,start,end,offset) {
  n = n + offset;
  if (offset > 0 && n > end)	{
    n = start + (n - end - 1);
  } else if (offset < 0 && n < start)	{
    n = end - (start - n - 1);
  }
  return String.fromCharCode(n);
}

function decryptString(enc,offset) {
  var dec = "";
  var len = enc.length;
  for(var i=0; i < len; i++)	{
    var n = enc.charCodeAt(i);
    if (n >= 0x2B && n <= 0x3A)	{
      dec += decryptCharcode(n,0x2B,0x3A,offset);
    } else if (n >= 0x40 && n <= 0x5A)	{
      dec += decryptCharcode(n,0x40,0x5A,offset);
    } else if (n >= 0x61 && n <= 0x7A)	{
	  dec += decryptCharcode(n,0x61,0x7A,offset);
    } else {
      dec += enc.charAt(i);
    }
  }
  return dec;
}

function linkTo_DecryptMailto(s) {
	location.href = decryptString(s,1);
}

function fixField(field) {
  var field = document.getElementById(field);
  if (field && field.value.match(/^#!@/)) {
    field.value = decryptString(field.value.substr(3),1);
  }
}
