var news_process = false;

function news_vote(type, id, vote, undo)
{
	if (typeof undo == 'undefined')
	{
		undo = false;
	}

	if (news_process)
	{
		return false;
	}

	NewsVote = new _News_Vote(type, id, vote, undo);
	NewsVote.process_vote();

	return false;
}

function _object_reference()
{
	this.ref = new Array();

	this.create_ref = function(obj)
	{
		this.ref.push(obj);

		return this.ref.length - 1;
	}

	this.get_ref = function(ref)
	{
		return this.ref[ref] ? this.ref[ref] : null;
	}
}

function _News_Vote(type, id, vote, undo)
{
	var nv_me = this;

	this.undo = undo;
	this.type = type;
	this.id = id;
	this.fullid = 'news_item_c_' + id;
	this.vote = vote;
	this.ref = -1;
	this.HexMapper = new _Hex_Mapper;
	this.forwarded = 0;
	this.steps = 5;
	this.step = -1;
	this.size = 11;
	this.working = false;

	this.process_vote = function()
	{
		news_process = true;

		colour = this.vote == -1 ? '000000' : 'FFFF00';

		this.forward = this.HexMapper.map_transition('FFFFFF', colour, this.steps);
		this.rewind  = this.HexMapper.map_transition(colour, 'FFFFFF', this.steps);

		this.ref = ObjectReferences.create_ref(this);

		this.create_clone();
		setTimeout("ObjectReferences.get_ref(" + this.ref + ").animate('forward');", 1);

		curvote = parseInt(fetch_object(this.fullid).innerHTML.replace('+', ''));

		if (this.vote == -1)
		{
			curvote--;
		}
		else
		{
			curvote++;
		}

		curvote = curvote > 0 ? '+' + curvote : (curvote == 0 ? '0' : curvote);

		fetch_object(this.fullid).innerHTML = curvote;
		fetch_object(this.fullid + '_clone').innerHTML = curvote;

		setTimeout("ObjectReferences.get_ref(" + this.ref + ").animate('rewind');", 10);

		this.ajax = new vB_AJAX_Handler(true);
		this.ajax.onreadystatechange(this.handle_vote);
		this.ajax.send('news.php?do=vote&id=' + this.id + '&score=' + this.vote + '&type=' + this.type + (this.undo ? '&change=1' : ''));
	}

	this.check_ajax = function()
	{
		if (nv_me.ajax.handler.readyState == 4 && nv_me.ajax.handler.status == 200)
		{
			if (nv_me.ajax.handler.responseText && nv_me.ajax.handler.responseText != 'fail')
			{
				return true;
			}
		}
	}

	this.handle_vote = function()
	{
		if (nv_me.check_ajax())
		{
			if (nv_me.type == 'story')
			{
				data = nv_me.ajax.handler.responseText;
				fetch_object('story_vote').innerHTML = data;
			}
			else
			{
				data = nv_me.ajax.handler.responseText.split('@:-:^^__^^:-:@');

				fetch_object('news_item_' + nv_me.id).innerHTML = data[1];

				try
				{
					vbmenu_register('moderate_news_' + nv_me.id, true);
				}
				catch(e)
				{
				}
			}

			news_process = false;
		}
	}

	this.create_clone = function()
	{
		clone = document.createElement('div');
		clone.className = 'smallfont news_clone';

		clone.id = this.fullid + '_clone';
		clone.innerHTML = fetch_object(this.fullid).innerHTML;

		document.body.appendChild(clone);

		fetch_object(this.fullid).style.visbility = 'none';
	}

	this.remove_clone = function()
	{
		fetch_object(this.fullid + '_clone').parentNode.removeChild(fetch_object(this.fullid + '_clone'));
		fetch_object(this.fullid).style.visbility = '';
	}

	this.push_clone = function(size)
	{
		offset = vB_Popup_Menu.prototype.fetch_offset(fetch_object(this.fullid));

		clone = fetch_object(this.fullid + '_clone');
		clone.style.fontSize = size + 'px';

		if (!is_ie)
		{
			loffset = (clone.innerHTML.toString().length - 2) * 2;
		}
		else
		{
			loffset = 0;
		}

		clone.style.top		= (offset['top'] - ((size - 11) / 2))  + 'px';
		clone.style.left	= (offset['left'] - (loffset)) + 'px';
	}

	this.animate = function(dir)
	{
		return false;

		nv_me = ObjectReferences.get_ref(nv_me.ref);

		if (dir == 'rewind' && nv_me.forwarded < nv_me.steps)
		{
			setTimeout("ObjectReferences.get_ref(" + nv_me.ref + ").animate('" + dir + "');", 100);

			return false;
		}

		if (dir == 'forward')
		{
			nv_me.forwarded = nv_me.step;
		}

		nv_me.step++;

		if (nv_me.step > nv_me.steps)
		{
			if (dir == 'rewind')
			{
				nv_me.remove_clone();
			}

			nv_me.step = 0;

			return false;
		}

		nv_me.push_clone(dir == 'forward' ? (nv_me.size + (nv_me.step)) : (5 + (nv_me.size - (nv_me.step))));

		eval('colour = nv_me.' + dir + '[' + nv_me.step + '];');

		fetch_object(nv_me.fullid + '_clone').style.color = colour;

		setTimeout("ObjectReferences.get_ref(" + nv_me.ref + ").animate('" + dir + "');", 1);
	}
}

function _Hex_Mapper()
{
	this.hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
	this.rgb = {'A' : 10, 'B' : 11, 'C' : 12, 'D' : 13, 'E' : 14, 'F' : 15};

	this.map_transition = function(_start, _finish, _steps)
	{
		transition = new Array();

		for (var s = 0; s <= _steps; s++)
		{
			transition.push('#' + this.build_hex(_start, _finish, _steps, s));
		}

		return transition;
	}

	this.rgb_bit = function(_bit)
	{
		return (this.rgb[_bit])? this.rgb[_bit] : eval(_bit);
	}

	this.hex_bit = function(_bit)
	{
		return this.hex[Math.floor(_bit / 16)] + this.hex[Math.floor(_bit - 16 * Math.floor(_bit / 16))];
	}

	this.rgb_to_hex = function(_r, _g, _b)
	{
		return this.hex_bit(_r) + this.hex_bit(_g) + this.hex_bit(_b);
	}

	this.hex_to_rgb = function(_hex)
	{
		return new Array(
			(this.rgb_bit(_hex.charAt(0)) * 16) + this.rgb_bit(_hex.charAt(1)),
			(this.rgb_bit(_hex.charAt(2)) * 16) + this.rgb_bit(_hex.charAt(3)),
			(this.rgb_bit(_hex.charAt(4)) * 16) + this.rgb_bit(_hex.charAt(5))
		);
	}

	this.rgb_steps = function(_start, _finish, _steps, _step)
	{
		return Math.floor(_start - ((_start - _finish) / _steps) * _step);
	}

	this.build_hex = function(_start, _finish, _steps, _step)
	{
		return this.rgb_to_hex(
			this.rgb_steps(
				this.hex_to_rgb(_start)[0],
				this.hex_to_rgb(_finish)[0],
				_steps,
				_step
			),
			this.rgb_steps(
				this.hex_to_rgb(_start)[1],
				this.hex_to_rgb(_finish)[1],
				_steps,
				_step
			),
			this.rgb_steps(
				this.hex_to_rgb(_start)[2],
				this.hex_to_rgb(_finish)[2],
				_steps,
				_step)
		);
	}
}

ObjectReferences = new _object_reference;

function save_view_as_default()
{
	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	cookie_string = new Array();

	for (type in viewsettings)
	{
		set_cookie('news_' + type, viewsettings[type], expires);
		cookie_string.push('news_' + type + '=' + viewsettings[type]);
	}

	set_cookies_ajax = new vB_AJAX_Handler(true);
	set_cookies_ajax.onreadystatechange(set_cookies_done);
	set_cookies_ajax.send('news.php?do=setcookies&' + cookie_string.join('&'));

	fetch_object('news_default_save').style.display = 'none';
	fetch_object('news_default_notice').style.display = '';

	return false;
}

function set_cookies_done()
{
	return false;
}

function delete_news_item(id)
{
	reason = fetch_object('news_reason_' + id).value;

	if (PHP.trim(reason) == '')
	{
		alert('You must specify a reason.');
		fetch_object('news_reason_' + id).focus();
		return false;
	}

	news_item_delete = new vB_AJAX_Handler(true);
	news_item_delete.onreadystatechange(do_delete_news_item);
	news_item_delete.newsid = id;
	news_item_delete.send('news.php?do=deletenews&threadid=' + id + '&reason=' + reason);
}

function do_delete_news_item()
{
	ajax_req = news_item_delete.handler;

	if (ajax_req.readyState == 4 && ajax_req.status == 200)
	{
		if (ajax_req.responseText == 'complete')
		{
			fetch_object('news_item_' + news_item_delete.newsid).parentNode.removeChild(fetch_object('news_item_' + news_item_delete.newsid));
		}
	}
}

function move_news_item(id)
{
	forumid = fetch_object('news_move_' + id).options[fetch_object('news_move_' + id).options.selectedIndex].value;
	news_item_move = new vB_AJAX_Handler(true);
	news_item_move.onreadystatechange(do_move_news_item);
	news_item_move.newsid = id;
	news_item_move.send('news.php?do=movenews&threadid=' + id + '&destforumid=' + forumid);
}

function do_move_news_item()
{
	ajax_req = news_item_move.handler;

	if (ajax_req.readyState == 4 && ajax_req.status == 200)
	{
		data = ajax_req.responseText.split('@:-:^^__^^:-:@');

		if (data[0] == 'complete')
		{
			_pulse_news_cache[news_item_move.newsid] = data[1];
			fetch_object('news_item_' + news_item_move.newsid).innerHTML = data[1];

			try
			{
				vBmenu.menus['moderate_news_' + news_item_move.newsid].hide();
			}
			catch(e)
			{
			}

			try
			{
				vbmenu_register('moderate_news_' + news_item_move.newsid, true);
			}
			catch(e)
			{
			}

		}
		else
		{
			alert('Permission Denied');
		}
	}
}