function trim (str, charlist) {
	var whitespace, l = 0, i = 0;
    str += '';
    
    if (!charlist) {
        // default list
        whitespace = " \n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000";
    } else {
        // preg_quote custom list
        charlist += '';
        whitespace = charlist.replace(/([\[\]\(\)\.\?\/\*\{\}\+\$\^\:])/g, '$1');
    }
    
    l = str.length;
    for (i = 0; i < l; i++) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(i);
            break;
        }
    }
    
    l = str.length;
    for (i = l - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) === -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    
    return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}

function menu_toggle(id) {
	$('#menu' + id).toggle();
}

/* ----------------------------------------------------------------- */
/* UI Popups */
/* ----------------------------------------------------------------- */

function popup_autoresize() {
	$("#TB_ajaxContent").css({width: "auto", height: "auto"});
}

function popup_remove_friend(friend_id) {
	tb_show('Remove user from friends', '/popup/friend_remove?id='+friend_id+'&width=450&height=100', null);
	popup_autoresize();
}

function popup_friend_request(friend_id) {
	tb_show('Send friend request', '/popup/friend_add?id='+friend_id+'&width=450&height=200', null);
	popup_autoresize();
}

function popup_photo_delete(photo_id) {
	tb_show('Delete photo', '/popup/photo_delete?id='+photo_id+'&width=450&height=140', null);
	popup_autoresize();
}

function popup_video_delete(id) {
	tb_show('Delete video', '/popup/video_delete?id='+id+'&width=450&height=120', null);
	popup_autoresize();
}

function popup_product_review(id) {
	tb_show('Rate the product', '/popup/product_review?id='+id+'&width=550&height=330', null);
	popup_autoresize();
}

function popup_send_wishlist() {
	email = $('#email').val();
	comment = $('#comment').val();
	$('#processing').show();
	$('#send').attr('disabled', 'true');
	$('#email').attr('disabled', 'true');
	$('#comment').attr('disabled', 'true');
	$('#error').hide();
	$('#result').hide();
		
	$.post('/ajax/send_wishlist', {email: email, comment: comment}, function(resp) { 
		if (resp) {
			$('#processing').hide();
			
			if (resp.error == 0) {
				$('#send').attr('disabled', 'true');
				$('#result').show();
			}
			else {
				$('#send').attr('disabled', '');
				$('#email').attr('disabled', '');
				$('#comment').attr('disabled', '');
				$('#error').html(resp.error);
				$('#error').show();
			}
		}
	}, 'json');
}

function popup_post_comment() {
	video_id = $('#video_id').val();
	message = $('#video_comment_body').val();

	$('#processing').show();
	$('#send').attr('disabled', 'true');
	$('#video_comment_body').attr('disabled', 'true');
	$('#error').hide();
	$('#result').hide();
		
	$.post('/ajax/create_comment', {id: video_id, comment: message}, function(resp) { 
		if (resp) {
			$('#processing').hide();
			
			if (resp.error == 0) {
				$('#send').attr('disabled', 'true');
				$('#result').show();
				window.location.reload();
			}
			else {
				$('#send').attr('disabled', '');
				$('#video_comment_body').attr('disabled', '');
				$('#error').html(resp.error);
				$('#error').show();
			}
		}
	}, 'json');
}

function video_favorites_add(video_id) {
	$.get('/ajax/video_favorite_add', {video_id: video_id}, function(resp) {
		if (resp) {
			if (resp.error == 0) {
				alert('Added video to favorites!');
				$('#cmd_video_to_favorites').html('<a href="#">Added to favorites!</a>');
			}
			else alert(resp.error);
		}
	}, 'json');
}


/* ----------------------------------------------------------------- */

function role_delete(user_id, group_id) {
	$.post('/ajax/group_edit_role', {user: user_id, group: group_id, action: 'delete'}, function(resp) {
		//window.location.reload();
	}, 'json');
}

function friend_request() {
	$('#processing').show(); $('#buttons').hide(); $('#result').hide(); $('#result').html('');
	
	$.post('/ajax/friend_request', {user_id: $('#user').val(), message: $('#message').val()}, function(resp) {
		if (resp) {
			$('#processing').hide();
			
			if (resp.error == 0) {
				$('#result').html('Your request has been sent. <a href="javascript:void(0);" onclick="tb_remove();">Close</a>');
				$('#result').show();
			}
			else {
				$('#result').html(resp.error);
				$('#result').show();
			}
		}
	}, 'json');
}

function friend_request_approve(id) {
	$.post('/ajax/friend_approve', {id: id}, function(resp) {
		if (resp) {
			if (resp.error == 0) window.location.reload();
		}
	}, 'json');
}

function friend_request_deny(from) {
	$.post('/ajax/friend_request_deny', {from: from}, function(resp) {
		if (resp && resp.error == 0) {
			// todo, some actions
		}
	}, 'json');
}

function friend_remove(id) {
	$.post('/ajax/friend_remove', {id: id}, function(resp) {
		if (resp) {
			if (resp.error == 0) window.location.reload();
		}
	}, 'json');
}

function photo_delete(id) {
	$('#processing').show(); $('#buttons').hide(); $('#result').hide(); $('#result').html('');
	$.get('/ajax/photo_delete', {id: id}, function(resp) {	
		if (resp) {
			$('#processing').hide();
			
			if (resp.error == 0) {
				$('#result').html('Photo deleted. <a href="javascript:void(0);" onclick="tb_remove();">Close</a>');
				$('#result').show();
			}
			else {
				$('#result').html(resp.error);
				$('#result').show();
			}
		}
	}, 'json');
}

function review_create(id) {
	$('#processing').show(); $('#buttons').hide(); $('#result').hide(); $('#result').html('');
	$.post('/ajax/product_create_review', {id: id, value: $('#value').val(), comment: $('#comment').val() }, function(resp) {	
		if (resp) {
			$('#processing').hide();
			
			if (resp.error == 0) {
				$('#result').html('Review has been posted. <a href="javascript:void(0);" onclick="tb_remove();">Close</a>');
				$('#result').show();
			}
			else {
				$('#result').html(resp.error);
				$('#result').show();
			}
		}
	}, 'json');
}

/* ----------------------------------------------------------------- */

function cart_country_change() {
	c = $('#cart_quote_country').val();
	if (c == 230) $('#cart_quote_state').show();
	else $('#cart_quote_state').hide();
}

/* ----------------------------------------------------------------- */

var is_editing_forum_post = false;

function f_edit_cancel(id) {
	$('#edit' + id).remove();
	$('#post_controls' + id).show();
	$('#post_text' + id).show();
}

function forum_post_edit(id) {
	$('#post_controls' + id).hide();
	
	$.get('/ajax/forum_post_get', {id: id}, function(resp) {
		if (resp) {
			if (resp.error == 0) {
				ctls = '<input type="button" class="f-button editpost-save" value="Save changes" />';
				ctls += ' or <a href="javascript:void(0);" onclick="f_edit_cancel(' + id + ');">Cancel</a>';
				$('<div id="edit' + id + '"><textarea class="editpost">' + resp.post.body + '</textarea><br>' + ctls + "</div>").appendTo('#post_block' + id);
				$('#post_text' + id).hide();
			}
			else {
				$('#post_text' + id).html('Error loading post.');
			}
		}
	}, 'json');
}

function forum_post_delete(id) {
	if (confirm('Are you sure you want to delete forum post?')) {
		$('#post_controls' + id).html('<span style="font-size: 11px; color: #555;">Deleting...</span>');
		$.get('/ajax/forum_delete_post', {id: id}, function(resp) {
			if (resp) {
				if (resp.error == 0) {
					$('#post_div' + id).fadeOut();
				}
				else $('#post_controls' + id + ' span').html(resp.error);
			}
			else { 
				window.location.reload();
			}
		}, 'json');
	}
}

