function elementGetX(e)
{
	var x = 0;
	while (e)
	{
		x += e.offsetLeft;
		e = e.offsetParent;
	}
	return x;
}

function elementGetY(e)
{
	var y = 0;
	while (e)
	{
		y += e.offsetTop;
		e = e.offsetParent;
	}
	return y;
}

function my$(e)
{
	return document.getElementById(e);
}


Function.prototype.bind = function(obj)
{
        var method = this,
        temp = function()
        {
                return method.apply(obj, arguments);
        }
        return temp;
}

	

/*

	id: self identifier, string
	div: div used for displaying the tip, string
	server: page on the server to receive the information
	title: message explaining what the tip is for
	msg: message to be sent
	friend_email: what's above the input field for the friend's email
	your_name: what's above the input field for your name
	your_email: what's above the input field for your email
	to_email: email of the receiptant
	from_email: email of the sender
	elem_offset: element to offset from
	elem_side: side of the element to offset from
	x_offset: offset from left in x pixels
	y_offset: offset from top in y pixels
	x_axis: left/right
	y_axis: top/bottom

*/


function Tip( id, div, server, title, friend_email, your_name, your_email, lang, to_email, from_email, elem_offset, elem_side, x_offset, y_offset, x_axis, y_axis )
{
	this.id = id;
	this.div = div;
	this.server = server;
	this.title = title;
	this.friend_email = friend_email;
	this.your_name = your_name;
	this.your_email = your_email;
	this.to_email = to_email;
	this.from_email = from_email;
	this.elem_offset = elem_offset;
	this.elem_side = elem_side;
	this.x_offset = x_offset;
	this.y_offset = y_offset;
	this.x_axis = x_axis;
	this.y_axis = y_axis;


	this.close_id = "tip_close_" + lang;
	this.send_id = "tip_send_" + lang;

	if ( lang == "en" )
	{
		this.close_name = 'Close';
		this.send_name = 'Send';
	}
	else
	{
		this.close_name = 'St\u00e4ng';
		this.send_name = 'Skicka';
	}
}

Tip.prototype.draw = function()
{
	var title = '<div class="tip_other"><b>' + this.title + '</b></div>';
	var to_email = '<div><input type="text" id="to_email" value="' + this.to_email + '" /></div>';
	var from = '<div><input type="text" id="from"/></div>';
	var from_email = '<div><input type="text" id="from_email" value="' + this.from_email + '" /></div></div>';
	var text_to_email = '<div class="tip_middle"><div>' + this.friend_email + '</div>';
	var text_from = '<div>' + this.your_name + '</div>';
	var text_from_email = '<div>' + this.your_email + '</div>';
	var link_close = '<a href="#" onclick="' + this.id + '.close();"><div class="tip_close"><b>' + this.close_name + '</b></div></a><div class="clear"></div></div>';
	var link_send = '<div class="tip_other"><a href="#" onclick="' + this.id + '.send();"><div class="tip_send"><b>' + this.send_name + '</b></div></a>';
	
	var str = title + text_to_email + to_email + text_from + from + text_from_email + from_email + link_send + link_close;

	var div = my$(this.div);
	div.innerHTML = str;
	div.style.display = 'block';
	my$('to_email').focus();

	var x = 0;
	var y = 0;
	if ( this.elem_offset )
	{
		x = elementGetX(my$(this.elem_offset)) + 0;
		y = elementGetY(my$(this.elem_offset)) + 0;
	}

	if ( this.elem_side == 'right' ) { x += div.offsetWidth; }
	if ( this.x_offset ) { x += this.x_offset; }
	if ( this.elem_side == 'bottom' ) { y += div.offsetHeight; }
	if ( this.y_offset ) { y += this.y_offset; }

	if ( this.x_axis == 'left' ) { div.style.left = x + "px"; }
	if ( this.x_axis == 'right' ) { div.style.right = x + "px"; }
	if ( this.y_axis == 'top' ) { div.style.top = y + "px"; }
	if ( this.y_axis == 'bottom' ) { div.style.top = y - div.offsetHeight + "px"; }
}

Tip.prototype.close = function(div)
{
	my$(this.div).style.display = 'none';
}

Tip.prototype.send = function()
{
	var http = new HTTP();
	var r = http.newRequest();
	var url = "http://" + document.domain + '/o.o.i.s?template=.forward.t';
	url += '&to_email=' + my$('to_email').value;
	url += '&from=' + my$('from').value;
	url += '&from_email=' + my$('from_email').value;
	url += '&url=' + document.URL;

	r.open("GET", url, true);

	var temp = this.close.bind(this);

	r.onreadystatechange = function()
	{
		if ( r.readyState == 4 )
		{
			//r.tip.close();
			temp();
		}
	}
		
	r.send(null);
}
