/**
* It's appears/disapperas hidden div
* Use as <a href="#" onclick="popup_menu(this,'dlg1');">
*
* @param element - ID of hidden DIV
*/
function popup_menu(cur, element, page_tmpl)
{
	// VALIDATE USER INPUT
	document.getElementById('pagenate').onsubmit = function()
	{
		var value = $('#pagenate input').val();
		if (value.match(/^\d+$/))
		{
			location.href=page_tmpl.replace(/%(.*)NUM%/g, ('1' == value ? '' : '$1'+value));
			return false;
		}
		else
			return false;
	}
	// END VALIDATE
	var offset = findPos(cur);
	var el = document.getElementById(element);
	el.style.left = offset.x+"px";
	el.style.top = offset.y+cur.offsetHeight+"px";
	$(el).slideToggle('fast');
	document.onclick = function(e) {
		var obj = document.all ? event.srcElement : e.target;
		if (el.style.display == 'block' && obj != el && obj != cur && checkParent(obj,element)) {
			$(el).slideUp('fast');
		}
	}

	function checkParent(t, parent)
	{
		while(t.parentNode){ 
			if(t==document.getElementById(parent)){ 
				return false 
			}
			t=t.parentNode 
		}
		return true 
	};

	function findPos(obj)
	{
		var curleft = obj.offsetLeft || 0;
		var curtop = obj.offsetTop || 0;
		while (obj = obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		return {x:curleft,y:curtop};
	};
};
/* END */

function voting(id, rating)
{
	$.post(root+"exstars.php", {action: "vote", id: id, rating: rating}, function(result) {
		if(result == 'already')
		{
			alert('{$lang.error_already_voted}');
		}
		else
		{
			$("#stars" + id).html(result);
		}
	});
}

/**
* Stars for comment
* Using: $('input#ID').stars();
*
* @param string path URL to templates directory where located img/ with images
*/
$.fn.stars = function(path)
{
	var value = $(this);// Input linked with stars
	var clicked = false;

	// Create container for stars and for text
	value.after('<div id="stars" style="float:left"></div><span style="margin-left:10px;clear:right;">&nbsp;</span>');
	// If no stars was clicked leave all stars gray colored.
	$('div#stars').mouseout(function() {
		if (!clicked)
		{
			$('div#stars img').attr('src', path+'img/gray.png');
//			$('div#stars + span').html('&nbsp;')
			value.val('0');
		}
	});
	// Fill container with stars and binds events handler
	for (var i = 1; i <= 10; i++)
	{
		$('div#stars').append('<img src="'+path+'img/gray.png" alt=">Rate this: '+i+'<" title="Rate this: '+i+'"/>');
		$('div#stars img:last').bind('mouseover', {val: i}, over);
		$('div#stars img:last').bind('click', {val: i}, setValue);
	}
	// If input has value, than show corresponding number of stars
	var _def = new Object();
	_def.data = new Object();
	if ((_def.data.val = value.val()) > 0) over(_def);

	function over(event)
	{
		$('div#stars img').each( function(i) {
			if (i < event.data.val)
				this.src = path+'img/gold.png';
			else
				this.src = path+'img/gray.png';
		});
//		$('div#stars + span').html('Rate this:&nbsp;' + event.data.val)
		clicked = false;
	}

	function setValue(event)
	{
		value.val(event.data.val);
		clicked = true;
	}
}

/**
* Remove member's avatar
*/
function rmAvatar()
{
	var h = function(data)
	{
		$("div#msg").html(data);
		$("div#msg").attr("class", "notif");
		$("div#msg").animate( { backgroundColor: '#FFFF66' }, 300).animate( { backgroundColor: '#E7FDE7' }, 300);

		$("span#rm_avatar,div#avatar_preview").remove();
	}
	$.post(root + "account.php", {action: 'rm_avatar'}, h);
}
