// rating
function ratingstar_toggle(content_id, score, toggle)
{
	for ( var i = 1; i <= score; i++ )
	{
		var obj = $('#ratingstar'+content_id+'__'+i)[0];
		obj.src = ( toggle == 'show' ) ? (obj.src).replace(/rate0/, 'rate1') : (obj.src).replace(/rate1/, 'rate0');
	}
}


function submit_rating(type, member_id, content_id, score)
{
	$.post(virpath+'index.php?m=rating', {'type':type,'score':score,'member_id':member_id,'content_id':content_id},
		function(response) {
			var response = response.split("\n");
			if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('#rating_'+type+'_box'+content_id).html('<p class="response">' + response[1] + '</p>');
					return true;
				}
			}

		}
	);
}


// reports
function submit_report(type, member_id, content_id, noreason)
{
	var reason = $('#report_'+type+'_reason'+content_id).val();
	if ( !reason ) { alert(noreason); return; }

	$.post(virpath+'index.php?m=report', {'type':type,'reason':reason,'member_id':member_id,'content_id':content_id},
		function(response) {
			var response = response.split("\n");
			if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('#report_'+type+'_box'+content_id).html('<p class="response">' + response[1] + '</p>');
					return true;
				}
			}

		}
	);
}


// polls
function submit_poll(poll_id, answer_id)
{
	$.post(virpath+'index.php?m=vote', {'poll_id':poll_id,'answer_id':answer_id},
		function(response) {
			var response = response.split("\n");
			if ( response.length == 3 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('#poll_votes_'+poll_id).html(response[1]);
					var votes = response[2].split(",");
					for ( var i = 0, totalvotes = votes.length; i < totalvotes; i++ ) {
						var counters = votes[i].split("=");
						if ( counters.length == 2 )
						{
							$('#poll_'+poll_id+'_answer_votes_'+counters[0]).html(counters[1]);
							$('#poll_'+poll_id+'_answer_votes_'+counters[0]+'_wrap').css('display', 'inline');
						}
						$('#poll_'+poll_id+'_answer_'+i).attr('disabled','disabled');
						//$('poll_'+poll_id+'_answer_'+i+'_wrap').hide();
					}
					return true;
				}
			}
			else if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('#poll_votes_'+poll_id).html(response[1]);
					for ( var i = 0; $('#poll_'+poll_id+'_answer_'+i) != null; i++ ) {
						$('#poll_'+poll_id+'_answer_'+i).attr('disabled','disabled');
						//$('#poll_'+poll_id+'_answer_'+i+'_wrap').hide();
					}
					return true;
				}
			}
		}
	);
}


// toggles
function row_toggle(name)
{
    for (var i = 0; document.getElementById(name+'_'+i) != null;  i++)
    {
        if (document.getElementById(name+'_'+i).style.display == "none")
            $('#'+name+'_'+i).show();
        else
            $('#'+name+'_'+i).hide();
    }
}


// tabs
var active_tab = 0;
var active_tab_first = 1;
function switch_tabs(prefix, new_tab, first)
{
	if ( typeof(first) != 'undefined' ) {
		if ( active_tab_first == 1 ) {
			$('#'+prefix + '_' + new_tab + '_content').show();
			active_tab = new_tab;
			active_tab_first = 0;
		}
	}
	else if ( active_tab != new_tab ) {
		$('#'+prefix + '_' + active_tab + '_content').hide(300);
		$('#'+prefix + '_' + new_tab + '_content').show(300);
		active_tab = new_tab;
	}
}


function showhide_field(name, show)
{
    if ( $('#'+name).css("display") == "none"  &&  (show == '' || show == undefined)  ||  show == 1 )
    {
        $('#'+name).fadeIn("fast");
    }
    else
    {
        $('#'+name).fadeOut("fast");
    }
}


//------------------------------------------------
// Misc
//------------------------------------------------
function confirmLink(question, url)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && url != '')
        window.location = url;

    return is_confirmed;
}

function confirmForm(question, form)
{
    var is_confirmed = confirm(question);

    if (is_confirmed && form != '')
        eval('document.' + form + '.submit()');

    return is_confirmed;
}

function toggleMessages(form, check)
{
    for (var i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == 'checkbox')
        {
            form.elements[i].checked = check;
        }
    }
}

function stylizeBooleanBox(prefix, boxid)
{
	var options = document.getElementById(prefix + boxid);
	for ( var i = 0, length = options.length; i < length; i++ )
	{
		options[i].style.background = ( options[i].value == 1 ) ? '#DEFAE0' : '#FBDDDD';
		if ( options[i].value == 1 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#DEFAE0';
		else if ( options[i].value == 0 && options[i].selected )
			document.getElementById(prefix + boxid).style.background = '#FBDDDD';
	}
}


$.fn.ToolTip = function(customclass)
{
	this.mouseover(
		function(e)
		{
			if((!this.title && !this.alt) && !this.tooltipset) return;
			// get mouse coordinates
			// based on code from http://www.quirksmode.org/js/events_properties.html
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			// if there is no div containing the tooltip
			if(!this.tooltipdiv)
			{
				// create a div and style it
				var div = document.createElement("div");
				this.tooltipdiv = div;
				$(div).addClass(customclass || 'tooltip');
				// add the title/alt attribute to it
				$(div).html((this.title || this.alt));
				this.title = "";
				this.alt = "";
				$("body").append(div);
				this.tooltipset = true;
			}
			$(this.tooltipdiv).fadeIn("fast").css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mouseout(
		function()
		{
			if(this.tooltipdiv)
			{
				$(this.tooltipdiv).fadeOut("fast");
			}
		}
	);
	return this;
}


$.fn.ToolPic = function(customclass)
{
	this.mouseover(
		function(e)
		{
			if((!this.name) && !this.tooltipset) return;
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			if(!this.tooltipdiv)
			{
				var div = document.createElement("div");
				this.tooltipdiv = div;
				$(div).addClass(customclass || 'tooltip');
				$(div).html('<img src="' + this.name + '"/>');
				this.title = "";
				this.alt = "";
				$("body").append(div);
				this.tooltipset = true;
			}
			$(this.tooltipdiv).fadeIn("fast").css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mousemove(
		function(e)
		{
			if((!this.name) || !this.tooltipset) return;
			var mouseX = e.pageX || (e.clientX ? e.clientX + document.body.scrollLeft : 0);
			var mouseY = e.pageY || (e.clientY ? e.clientY + document.body.scrollTop : 0);
			mouseX += 10;
			mouseY += 10;
			$(this.tooltipdiv).css({left: mouseX + "px", top: mouseY + 3 + "px"});
		}
	).mouseout(
		function()
		{
			if(this.tooltipdiv)
			{
				$(this.tooltipdiv).fadeOut("fast");
			}
		}
	);
	return this;
}

function selectSmiley(smiley_id, textbox)
{
	var val = $('#'+textbox).val();
	val = val + '' + smilies[smiley_id]['shortcut'];
	$('#'+textbox).val(val);
}

//------------------------------------------------
// Chat
//------------------------------------------------
var chatwincurr = 0;
var autocheck = 0;
var autodelay = 8;
var virpath = '';
chatwins = new Array();

function checkChat(path,check,delay)
{
    virpath = path;
    autocheck = check;
    autodelay = delay;
    checkChatText();
}

function checkChatText() {
	$.post(virpath+'chat.php?p=check', {},
		function(member_id) {
  			if ( member_id != '0' ) {
		        openChatWindow(member_id);
			}
			else {
			    if ( autocheck ) {
			        setTimeout('checkChatText()', (autodelay*1000));
				}
			}
		}
	);
}

function openChatWindow(member_id) {
    if (member_id > 0 && (typeof(chatwins['chat' + member_id]) != "object"  ||  chatwins['chat' + member_id].closed)) {
        chatwins['chat' + member_id] = window.open(virpath + 'index.php?m=account_chat&p=request&id=' + member_id, 'chat' + member_id, 'width=470,height=410,resizable=yes,scrollbars=no,toolbar=no,location=no,status=no,menubar=no');
        if ( chatwins['chat' + member_id] && chatwins['chat' + member_id].open)
        	chatwins['chat' + member_id].focus();
		else
			alert('Could not open a chat window. Please disable your popup blocker.');
    }
}

