/////////////////////////////////////////////////////
// Copyright (c) 2009 ChaseCreations LLC.
// All Rights Reserved.
/////////////////////////////////////////////////////

var emailre = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$/i; // email validator


function htmlentities(text)
{
	var newtext = text.replace(/&/,"&amp;");
	newtext = newtext.replace(/</,"&lt;");
	newtext = newtext.replace(/>/,"&gt;");
	return(newtext);
}


////////////////////////////////////////////////////////////
// Modal Functions
////////////////////////////////////////////////////////////

function modal_show(w,h,callback)
{
	modaltop =  Math.floor( $(window).scrollTop()+( ( ($(window).height()-h)/5)*2 ) );
	modalleft = Math.floor( ($(window).width()-w) / 2 );
	
	$("#mask").css({display:'block', opacity: 0.0});
	$("#mask").animate({opacity: 0.8}, 300, function(){
		$("#modal").css({ 'width': w, 'min-height': h, 'top': modaltop, 'left': modalleft }); // TODO: min-height may not be needed. Performance is better, though...
		//$("#modal").css({ 'top': modaltop, 'left': modalleft }); // TODO: min-height may not be needed. Performance is better, though...
		$("#modal_content").css({ 'width': w, 'min-height': h });
		
			//$("#modal_content").css({ 'width': w, 'height': h });
			//$("#modal").css({ 'width': w, 'top': modaltop, 'left': modalleft }); // maybe...
			//$("#modal_content").css({ 'width': w });
		
		$("#modal").fadeIn('slow',callback);
	}); // new; also added optional callback for final animation
}

function modal_hide()
{
	$("#modal").fadeOut('fast', function(){
		$("#mask").fadeOut(200);
		$("#modal_content").html('');
	});
}

function field_focus(field, defaultval) // FIXME: for new rows
{
	if (field.value == defaultval)
		field.value = '';
	field.focus();
}

function get_obj_top(obj) /// FIXME: use JQuery instead
{
	curtop = 0;
	oid = obj.id;
	
	if (obj) {
		do {
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent) ;
	}
	return curtop;
}

function limit_text(field, maxlen, counter)
{
	if (field.value.length > maxlen) {
		field.value = field.value.substring(0, maxlen);
	}
	else if (counter) {
		counter.value = (maxlen-field.value.length);
	}
}

function show_profile(userkey)
{
	$.get('/account/mod/profile_view.php', {'uid': userkey}, function(data){
		$('#modal_content').html(data);
		modal_show(600,500);
	}, 'html');
}

function set_inbox_flag(force)
{
	var tab = $('#tab-inbox');
	force = force ? 1 : 0;
	if (tab == null)
		return;
	$.get('/inbox/mod/get_message_count.php', {'force':force}, function(data){
		if (data.unreadmsgs && data.unreadmsgs>0)
			$('#tab-inbox').prepend('<span id="unread">'+data.unreadmsgs+'</span>');
	},'json');
}

// Google Translate
function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    gaTrack: true,
    gaId: 'UA-5452139-1'
  }, 'gte');
}

