var getPosLeft = function (el) { return (el.offsetParent) ? el.offsetLeft+getPosLeft(el.offsetParent) : el.offsetLeft; }
var getPosTop = function (el) { return (el.offsetParent) ? el.offsetTop+getPosTop(el.offsetParent) : el.offsetTop; }

var menu = new function () {

	this.moveTime = 300;

	this.init = function () {
		var layer = document.getElementById('menu');
		this.links = layer.getElementsByTagName('a');
		
		for (var i = 0; i < this.links.length; i++) {
			if (this.links[i].className.match(/active/))
				continue;
			this.links[i].onmouseover = function (e) {
		
				new Motion(this, 'top', 'px', true, 0, -16, menu.moveTime);
				new Motion(this, 'backgroundColor', null, false, '#323232', '#b00060', menu.moveTime);
			}
			this.links[i].onmouseout = function (e) {
				
				new Motion(this, 'top', 'px', true, -16, 0, menu.moveTime);
				new Motion(this, 'backgroundColor', null, false, '#b00060', '#323232', menu.moveTime);
			}
		}
	}
}

var userSlide = new function () {

	this.locked = false;
	this.defaultWidth; this.defaultHeight; this.defaultRatio; this.defaultLayerWidth;
	this.marginRight = 16;
	this.borderWidth = 5;
	this.timeouts = [];
	this.lastPos = 0.5;
	
	this.init = function (pid) {
		this.layer = document.getElementById(pid);
		if (!this.layer) return;
		
		this.links = this.layer.getElementsByTagName('a');
		
		this.defaultWidth = parseInt(this.links[0].getElementsByTagName('img')[0].getAttribute('width'));
		this.defaultHeight = parseInt(this.links[0].getElementsByTagName('img')[0].getAttribute('height'));
	//	this.defaultRatio = this.defaultWidth / this.defaultHeight;
		
	//	this.defaultLayerWidth = (this.defaultWidth + this.marginRight + 2*this.borderWidth) * this.links.length;
	//	this.layer.style.width = this.defaultLayerWidth + 'px';
		
	/*	var elements = this.layer.getElementsByTagName('*');
		for (var i = 0; i < elements.length; i++) {
			elements[i].onmouseover = function (e) { };
			elements[i].onmouseout = function (e) { };
		} */
		
		this.layer.onmouseover = function (e) {
			userSlide.listenMove(true);
		}
		
		this.layer.onmouseout = function (e) {
			userSlide.listenMove(false);
		}
		
		
		for (var i = 0; i < this.links.length; i++) {
			var image = this.links[i].getElementsByTagName('img')[0];
			image.style.width = '100%';
			image.style.height = '100%';
			this.links[i].style.marginRight = (i == this.links.length - 1) ? '0' : this.marginRight + 'px';
			this.links[i].style.background = 'none';
		}
		
		this.pointerMove(0.5);
	}


	this.clearTimeouts = function () {
		for (var i in this.timeouts)
			window.clearTimeout(this.timeouts[i]);
		this.timeouts = [];
	}
/*
	this.pointerIn = function (pos) {
		if (this.locked) return;
		this.locked = true;
		
		
		this.listenMove(true);
	//	this.clearTimeouts();
		
		var inc = (pos < 0.5) ? -0.05 : 0.05;
		var newPos = this.lastPos;
		var time = 20;
		while (Math.abs(newPos - pos) > 0.03) {
			this.timeouts.push(window.setTimeout('userSlide.pointerMove('+newPos+');', time));
			newPos += inc;
			time += 50;
		}
		this.timeouts.push(window.setTimeout('userSlide.locked = false;', time)); 
		
		this.locked = false;
	} */
	
	this.listenMove = function (listen) {
		if (listen) {
			document.onmousemove = function (e) {
				var abs = (document.all) ? event.x + document.body.scrollLeft : e.pageX;
				var pos = (abs - getPosLeft(userSlide.layer)) / userSlide.layer.offsetWidth;
				userSlide.pointerMove(pos);
			}
		} else
			document.onmousemove = null;
	}
	
	this.pointerMove = function (pos) {
		if (this.locked) return;
		this.locked = true;
		
		
		
		var sumWidth = 0;
		for (var i = 0; i < this.links.length; i++) {
			var style = this.getStyle(i, pos);
			sumWidth += parseInt(style['width']) + 2*this.borderWidth + this.marginRight;
			for (var property in style)
				this.links[i].style[property] = style[property];
		}
		
		this.layer.style.width = (sumWidth - this.marginRight) + 'px';
		this.lastPos = pos;
	
		this.locked = false;
	}
	
	this.getStyle = function (index, pos) {
		var center = (1/(2*this.links.length)) * (2*index + 1);
	
	/*	var scale = 1 - Math.abs(center - pos);
		if (scale < 0.5) scale = 0.5; */
		
		var scMin = 1/3;
	/*	if (center < pos)
			var scale = ((1 - scMin)/Math.pow(pos,2)) *Math.pow(center,2) + scMin;
		else
			var scale = ((1 - scMin)/Math.pow(1-pos,2)) *Math.pow(center-1,2) + scMin; */
			
		if (center < pos-0.5 || center > pos+0.5)
			var scale = scMin;
		else  if (center < pos)
			var scale = 4*(1-scMin)*Math.pow(center-pos+0.5, 2) + scMin;
		else
			var scale = 4*(1-scMin)*Math.pow(center-pos-0.5, 2) + scMin;

		
		return {	'width':		Math.round(this.defaultWidth * scale) + 'px',
					'height':		Math.round(this.defaultHeight * scale) + 'px',
					'marginTop':	Math.round((this.defaultHeight * (1 - scale)) / 2) + 'px',
					'opacity':		scale		};
	}

}


var hoverBorder = new function () {

	this.moveTime = 400;

	this.init = function () {
		var elements = document.getElementsByTagName('*');
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].className.match(/(user_preview)/) && elements[i].parentNode.id != 'latest_user') {
			//	elements[i].style.opacity = '0.5';
				elements[i].onmouseover = function (e) {
					var color = this.className.match(/female/) ? '#b00060' : '#236a99';
					new Motion(this, 'borderColor', null, false, '#ffffff', color, hoverBorder.moveTime);
				}
				elements[i].onmouseout = function (e) {
					var color = this.className.match(/female/) ? '#b00060' : '#236a99';
					new Motion(this, 'borderColor', null, false, color, '#ffffff', hoverBorder.moveTime);
				}
			}
		}
		
		this.initButton();
	}
	
	this.initButton = function () {
		var buttons = ['button_mailbox', 'button_friends'];
		for (var i = 0; i < buttons.length; i++) {
			var button = document.getElementById(buttons[i]);
			if (button) {
				var num = parseInt(button.getElementsByTagName('span')[0].innerHTML);
				if (num > 0) {
					window.setInterval('hoverBorder.blinkButton("'+buttons[i]+'");', 700);
				}
			}
		}
	}
	
	this.blinkButton = function (id) {
		var button = document.getElementById(id);
		var moveTime = 300;
		if (button) {
			new Motion(button, 'borderColor', null, false, '#b00060', '#0e1012', moveTime);
			new Motion(button, 'borderColor', null, false, '#0e1012', '#b00060', moveTime, moveTime);
		}
	}
}

var message = new function () {
	this.moveTime = 400;
	
	this.init = function () {
		var elements = document.getElementsByTagName('*');
		for (var i = 0; i < elements.length; i++) {
			if (elements[i].className.match(/message/)) {
				
				var link = elements[i].getElementsByTagName('a')[3];
				link.mailText = link.parentNode.getElementsByTagName('div')[1];
				link.mailHeight = link.mailText.offsetHeight;
				link.mailOpen = false;
				
				link.mailText.style.height = '0px';
				
				
				link.onclick = function (e) {
					if (!this.mailOpen) {
						var href = this.href.split('/');
						var id = parseInt(href[href.length-1]);
						new Motion(this.mailText, 'height', 'px', true, 0, this.mailHeight, message.moveTime);
						http('GET', '/ajax/?message_read&id='+id, message.recieve);
					} else
						new Motion(this.mailText, 'height', 'px', true, this.mailHeight, 0, message.moveTime); 
					this.mailOpen = !this.mailOpen;
					return false;
				}
				
			}
		}
	}
	
	this.recieve = function (data) {
	
	}

}


var scroll = new function () {
	
	this.init = function () {
		if (!document.body.id || document.body.id != 'alias_start') return;
		
		var layer = document.getElementsByTagName('div');

		for (var i = 0; i < layer.length; i++) {
			if (layer[i].className.match(/content_preview/)) {
				this.initArea(layer[i]);
			}
		}
	}
	
	this.initArea = function (layer) {
		var childs = layer.childNodes;
		var count = 0;
		var height = 0;
		for (var i = 0; i < childs.length; i++) {
			if (childs.item(i).nodeType == 1) {
				if (count == 3) { 
					layer.style.overflow = 'hidden';
					layer.style.height = (height - 3) + 'px';
					var scrollHeight = height;
				}
				height += childs.item(i).offsetHeight + 1;
				count++;
			}
		}
		if (count <= 3) return;
		
		var maxMargin = scrollHeight - height;
		
		var firstChild = layer.getElementsByTagName('div')[0];
		firstChild.style.marginTop = '0px';
		
		
		
		var layerTop = getPosTop(layer);
		var interval = null;
		
		var setMargin = function (diff) {
			var marginTop = parseInt(firstChild.style.marginTop) + diff;
			
			if (marginTop > 0 || marginTop < maxMargin)
				return;
			
			firstChild.style.marginTop = marginTop + 'px';
		}
		
		var scrollDown = function () {
			setMargin(3);
		}
		
		var scrollUp = function () {
			setMargin(-3);
		} 

		
		var area = 2;
		var mousemove = function (e) {
			var top = (document.all) ? event.y + document.body.scrollTop : e.pageY;
			top -= layerTop;
			var pos = top / scrollHeight;
			

			
			
			if (pos <= 1/3) {
				if (area != 1) {
					area = 1;
					if (interval)
						window.clearInterval(interval);
					interval = window.setInterval(scrollDown, 20);
				}
			} else if (pos >= 2/3) {
				if (area != 3) {
			
					area = 3;
					if (interval)
						window.clearInterval(interval);
					interval = window.setInterval(scrollUp, 20);
				}
			} else {
				area = 2;
				if (interval)
						window.clearInterval(interval);
			}
		}
		
		layer.onmouseover = function (e) {
			document.onmousemove = mousemove;
		}
		layer.onmouseout = function (e) {
			document.onmousemove = false;
		/*	if (interval) {
				area = 2;
				window.clearInterval(interval);
			} */
		}
	
	}
}
