// rating
function submit_rating(type, member_id, content_id, score, url)
{
	$('#rating_'+type+'_progress'+content_id).show();

	$.post(virpath+'index.php?hp=1&m=rate', {'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] )
				{
					if ( typeof(url) != 'undefined' ) {
						window.location = url;
					}
					else {
						var curr_score = parseFloat($('#rating_'+type+'_score'+content_id).html());
						var curr_votes = parseInt($('#rating_'+type+'_votes'+content_id).html());
						var new_score = curr_score*curr_votes+score;
						var new_votes = curr_votes+1;

						$('#rating_'+type+'_score'+content_id).html((new_score/new_votes).toFixed(2));
						$('#rating_'+type+'_votes'+content_id).html(new_votes);

						ratingstar_reset(type, content_id, 1);

						$('#rating_'+type+'_progress'+content_id).hide();
						return true;
					}
				}
			}

		}
	);
}

function updatefields(gender)
{
    if ( gender == 1 || gender == 'Man' || $('#field_gender1').val() == 1 ) {
    	gender = 'man';
	}
	else if ( gender == 2 || gender == 'Woman' || $('#field_gender1').val() == 2 ) {
    	gender = 'woman';
	}
	else {
    	gender = '';
	}

		$('.profileform > dd').each(function(i,obj){
		var id = (obj.id).substr(5);
		if ( id.substr(id.length-2) == '_m' ) {
			if ( gender == 'man' ) {
				$('#name_'+id).show();
				$('#data_'+id).show();
			}
			else {
				$('#name_'+id).hide();
				$('#data_'+id).hide();
			}
		}
		else if ( id.substr(id.length-2) == '_w' ) {
			if ( gender == 'woman' ) {
				$('#name_'+id).show();
				$('#data_'+id).show();
			}
			else {
				$('#name_'+id).hide();
				$('#data_'+id).hide();
			}
		}
	});

		$('.form').each(function(i,obj){
		var id = (obj.id).substr(6);
		if ( id.substr(id.length-2) == '_m' ) {
			if ( gender == 'man' ) {
				$('#group_'+id).show();
			}
			else {
				$('#group_'+id).hide();
			}
		}
		else if ( id.substr(id.length-2) == '_w' ) {
			if ( gender == 'woman' ) {
				$('#group_'+id).show();
			}
			else {
				$('#group_'+id).hide();
			}
		}
		
	});
	
}

function straight_gender_select(gender, field_label)
{
    
	if (field_label == 'gender1') {
		if ( gender == 1 || $('#field_gender1').val() == 1 ) {
			$('#field_gender2').val(3);
		}
		else if ( gender == 2 || $('#field_gender1').val() == 2 ) {
			$('#field_gender2').val(4);
		}
		
	}
	else if (field_label == 'gender2') {
		if ( gender == 3 || $('#field_gender2').val() == 3 ) {
			$('#field_gender1').val(1);
			updatefields(1);
		}
		else if ( gender == 4 || $('#field_gender2').val() == 4 ) {
			$('#field_gender1').val(2);
			updatefields(2);
		}
	}
}

// reports
function submit_report(type, member_id, content_id, noreason)
{
	var reason = $('#report_'+type+'_reason'+content_id).val();
	if ( !reason ) { alert(noreason); return; }

	$('#report_'+type+'_progress'+content_id).show();

	$.post(virpath+'index.php?hp=1&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+'_progress'+content_id).hide();
					$('#report_'+type+'_response'+content_id).html('<div class="response">' + response[1] + '</div>');
					setTimeout(function(){
						$('#report_'+type+'_response'+content_id).animate({ opacity: "hide" }, "slow");
					}, 2500);
					return true;
				}
			}

		}
	);
}

// send wink
function send_wink(member_id, message_id)
{
	window.location = virpath+'index.php?m=account_messages&p=quickmessage&id='+member_id+'&sid='+message_id;
}

// polls
function submit_poll(poll_id, answer_id)
{
	$('#poll_'+poll_id+'_progress').show();

	$.post(virpath+'index.php?hp=1&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_'+poll_id+'_results').html(response[1]);
					$('#poll_'+poll_id+'_results_wrap').show();
					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+'_results_'+counters[0]).html('['+counters[1]+']');
							$('#poll_'+poll_id+'_results_'+counters[0]).css('display', 'inline');
							$('#poll_'+poll_id+'_checkbox_'+counters[0]).hide();
							$('#poll_'+poll_id+'_checkbox_'+counters[0]+'_obj').attr('disabled','disabled');
						}
					}
					$('#poll_'+poll_id+'_progress').hide();
					return true;
				}
			}
			else if ( response.length == 2 )
			{
				if ( response[0] == 'ok' && response[1] )
				{
					$('#poll_'+poll_id+'_results').html(response[1]);
					$('#poll_'+poll_id+'_results_wrap').show();
					for ( var i = 0; $('#poll_'+poll_id+'_checkbox_'+i) != null; i++ ) {
						$('#poll_'+poll_id+'_checkbox_'+counters[0]).hide();
						$('#poll_'+poll_id+'_checkbox_'+counters[0]+'_obj').attr('disabled','disabled');
					}
					$('#poll_'+poll_id+'_progress').hide();
					return true;
				}
			}
		}
	);
}

// vote icons
function ratingstar_toggle(type, content_id, score)
{
	for ( var i = 1; i <= 5; i++ ) {
		if ( i <= score ) {
			if ( $('#ratingstar'+content_id+'__'+i).hasClass("over") == false ) {
				$('#ratingstar'+content_id+'__'+i).addClass("over");
			}
		}
		else {
			$('#ratingstar'+content_id+'__'+i).removeClass("over");
		}
	}
}

function ratingstar_reset(type, content_id, lock)
{
	var score = Math.round($('#rating_'+type+'_score'+content_id).html());
	if ( score < 0 || score > 5 ) score = 0;
	for ( var i = 1; i <= 5; i++ ) {
		if ( i <= score ) {
			if ( $('#ratingstar'+content_id+'__'+i).hasClass("over") == false ) {
				$('#ratingstar'+content_id+'__'+i).addClass("over");
			}
		}
		else {
			$('#ratingstar'+content_id+'__'+i).removeClass("over");
		}
		if ( typeof(lock) != 'undefined' && lock ) {
			$('#ratingstar'+content_id+'__'+i).attr("onclick", "return false;");
			$('#ratingstar'+content_id+'__'+i).attr("onmouseover", "return false;");
			$('#ratingstar'+content_id+'__'+i).attr("disabled", "disabled");
		}
	}
}

// fetch contacts
function fetch_contacts(lng_submit, lng_checker, lng_select_label, lng_empty_details, lng_invalid_details)
{
	if ( !$('#mem_username').val() || !$('#mem_password').val() ) {
		$('#emailnfo').html('<br>'+lng_empty_details);
		return;
	}

	$('#invite_progress').show();
	$('#serverstatus').attr("disabled", "true");
	$('#emailnfo').html('');
	$.post(virpath+'index.php?m=invitefriends&p=fetch_contacts', {'emailtype':emailtype,'username':$('#mem_username').val(),'password':$('#mem_password').val()},
		function(response) {
			var response = response.split("\n");
			if ( response.length > 1 )
			{
				var str = '';
				for ( i in response ) {
					if ( response[i] != 'ok' ) {
						email = (response[i]).split('|||');
						if ( typeof(email) != 'undefined' && email.length == 2 ) {
							str += '<label><input type="checkbox" name="emails[]" value="'+email[0]+"|||"+email[1]+'" /> '+email[0]+' ('+email[1]+')</label><br>';
						}
					}
				}
				if ( str ) {
					str += '<br><input class="submit" name="submit" value="'+lng_submit+'" type="submit">';
					str = '<br><label><input type="checkbox" name="check_all_messages" value="0" onclick="toggleMessages(this.form, this.checked)" /> '+lng_checker+'</label><br>' + str;
					$('#emailnfo').html('<br><span>'+lng_select_label+':</span>'+str);
				}
				else {
					$('#emailnfo').html('<br>'+lng_invalid_details);
				}
			}
			else if ( response == 'empty_details' )
			{
				$('#emailnfo').html('<br>'+lng_empty_details);
			}
			else
			{
				$('#emailnfo').html('<br>'+lng_invalid_details);
			}
			$('#invite_progress').hide();
			$('#serverstatus').removeAttr('disabled');
		}
	);
}

function contacts_switchmail(type)
{
	emailtype = type;
	switch (type)
	{
		case 'custom':
			$('#field_email_custom').show();
			$('#field_email_public').hide();
			break;
		case 'aol':
			$('#field_email_public').show();
			$('#field_email_custom').hide();
			$('#emailext').html('');
			break;
		default:
			$('#field_email_public').show();
			$('#field_email_custom').hide();
			$('#emailext').html('@'+type+'.com');
			break;
	}
	$('#mem_username').val('')
	$('#mem_password').val('')
}

// check username
function checkusername(username)
{
	$.post(virpath+'index.php?hp=1&m=checkusername', {'username':username},
		function(response) {
			var response = trim(response);
			response = response.split("\n");
			if ( response.length == 2 )
			{
				$('#field_username_status').html(response[1]);
				$('#field_username_status').removeClass('true');
				$('#field_username_status').removeClass('false');
				if ( response[0] == 'ok' ) {
					$('#field_username_status').addClass('true');
				}
				else {
					$('#field_username_status').addClass('false');
				}
				$('#field_username_status').show(300);
			}

		}
	);
}

// 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();
    }
}

// switches
function row_switch(name)
{
    $('#'+active_tab).hide();
    $('#'+name).show();
    active_tab = name;
}

// 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();
			$('#'+prefix + '_' + new_tab + '_tab').addClass("active");
			active_tab = new_tab;
			active_tab_first = 0;
		}
	}
	else if ( active_tab != new_tab ) {
		$('#'+prefix + '_' + active_tab + '_content').hide(300);
		$('#'+prefix + '_' + active_tab + '_tab').removeClass("active");

		$('#'+prefix + '_' + new_tab + '_content').show(300);
		$('#'+prefix + '_' + new_tab + '_tab').addClass("active");
		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 toggleItems(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';
	}
}

// tooltips
$.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;
}

// toolpics
$.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) {
    
        $('#chat_request_link').html('<li><a id="chat_request_notice" href="javascript:void(0);" onclick="openChatRequestWindow(' + member_id + ')" title="Chat requested">Chat requested <img border="0" src="' + virpath + '/media/chat_request.gif" /></a></li>');
}

function openChatRequestWindow(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');
		$('#chat_request_link').html('');
		if ( chatwins['chat' + member_id] && chatwins['chat' + member_id].open)
        	chatwins['chat' + member_id].focus();
    }
		
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g,"");
}

function save_search()
{
	var name = $('#field_search_save').val();
	var hash = $('#field_hash').val();

	if(name != '' && hash != '')
	{
		$('#save_search_progress').show();
		$('#save_search_submit').attr({disabled: "disabled"});

		$.post(virpath+'index.php?hp=1&m=searches&p=save', {'name':name,'s':hash},
			function(response) {
				var response = response.split("\n");
				if ( response.length > 0 )
				{
					if ( response[0] == 'ok' && response[1] )
					{
						$('#save_search_progress').hide();
						$('#save_search_response').html(response[1]);

						if ( response[2] )
						{
							setTimeout(function(){
								$('#div_save_search' ).animate({ opacity: "hide" }, "slow");
							}, 2000);
						}
						return true;
					}
				}
			}
		);
	}
}

// calculate sun sign
function calc_sunsign()
{
	
	var month = $('#field_age_month').val();
		if ( ! month ) return;
	
	var day = $('#field_age_day').val();
		if ( ! day ) return;
	
   if ( month == 1 && day <=19) {sign = "Capricorn"; sign_id = "539";}
   else if (month == 1 && day >=20) {sign = "Aquarius"; sign_id = "540";}
   else if (month == 2 && day <=18) {sign = "Aquarius"; sign_id = "540";}
   else if (month == 2 && day >=19) {sign = "Pisces"; sign_id = "541";}
   else if (month == 3 && day <=20) {sign = "Pisces"; sign_id = "541";}
   else if (month == 3 && day >=21) {sign = "Aries"; sign_id = "542";}
   else if (month == 4 && day <=20) {sign = "Aries"; sign_id = "542";}
   else if (month == 4 && day >=21) {sign = "Taurus"; sign_id = "543";}
   else if (month == 5 && day <=20) {sign = "Taurus"; sign_id = "543";}
   else if (month == 5 && day >=21) {sign = "Gemini"; sign_id = "544";}
   else if (month == 6 && day <=20) {sign = "Gemini"; sign_id = "544";}
   else if (month == 6 && day >=21) {sign = "Cancer"; sign_id = "545";}
   else if (month == 7 && day <=21) {sign = "Cancer"; sign_id = "545";}
   else if (month == 7 && day >=22) {sign = "Leo"; sign_id = "546";}
   else if (month == 8 && day <=21) {sign = "Leo"; sign_id = "546";}
   else if (month == 8 && day >=22) {sign = "Virgo"; sign_id = "547";}
   else if (month == 9 && day <=21) {sign = "Virgo"; sign_id = "547";}
   else if (month == 9 && day >=22) {sign = "Libra"; sign_id = "548";}
   else if (month == 10 && day <=21) {sign = "Libra"; sign_id = "548";}
   else if (month == 10 && day >=22) {sign = "Scorpio"; sign_id = "549";}
   else if (month == 11 && day <=21) {sign = "Scorpio"; sign_id = "549";}
   else if (month == 11 && day >=22) {sign = "Sagittarius"; sign_id = "615";}
   else if (month == 12 && day <=20) {sign = "Sagittarius"; sign_id = "615";}
   else if (month == 12 && day >=21) {sign = "Capricorn"; sign_id = "539";}
   
   $('#field_zodiac_w').val(sign_id);
   
   return;
}

function generate_hints()
{
    $("input,select,textarea").focus( function(){
		$(this).siblings("span").css('display','inline');
	});
	
	$("input,select,textarea").blur( function(){
		$(this).siblings("span").css('display','none');
	});
	
	$("textarea").keydown( function(){
		$(this).siblings("span").css('display','none');
	});

	

}